Search in sources :

Example 1 with LogonResult

use of org.teiid.client.security.LogonResult in project teiid by teiid.

the class TestPreparedStatement method testBatchedUpdateExecution.

/**
 * Verify that the <code>executeBatch()</code> method of <code>
 * MMPreparedStatement</code> is resulting in the correct command,
 * parameter values for each command of the batch, and the request type
 * are being set in the request message that would normally be sent to the
 * server.
 *
 * @throws Exception
 */
@Test
public void testBatchedUpdateExecution() throws Exception {
    // Build up a fake connection instance for use with the prepared statement
    ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
    Mockito.stub(conn.getConnectionProps()).toReturn(new Properties());
    DQP dqp = Mockito.mock(DQP.class);
    ServerConnection serverConn = Mockito.mock(ServerConnection.class);
    LogonResult logonResult = Mockito.mock(LogonResult.class);
    // stub methods
    Mockito.stub(conn.getServerConnection()).toReturn(serverConn);
    Mockito.stub(serverConn.getLogonResult()).toReturn(logonResult);
    Mockito.stub(logonResult.getTimeZone()).toReturn(TimeZone.getDefault());
    // a dummy result message that is specific to this test case
    final ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>();
    final int[] count = new int[1];
    final ResultsMessage rm = new ResultsMessage();
    Mockito.stub(dqp.executeRequest(Matchers.anyLong(), (RequestMessage) Matchers.anyObject())).toAnswer(new Answer<ResultsFuture<ResultsMessage>>() {

        @Override
        public ResultsFuture<ResultsMessage> answer(InvocationOnMock invocation) throws Throwable {
            RequestMessage requestMessage = (RequestMessage) invocation.getArguments()[1];
            count[0] += requestMessage.getParameterValues().size();
            if (count[0] == 100000) {
                rm.setException(new TeiidException());
                rm.setResults(new List<?>[] { Arrays.asList(Statement.EXECUTE_FAILED) });
            } else {
                List<?>[] vals = new List<?>[requestMessage.getParameterValues().size()];
                Arrays.fill(vals, Arrays.asList(0));
                rm.setResults(Arrays.asList(vals));
            }
            return results;
        }
    });
    rm.setUpdateResult(true);
    results.getResultsReceiver().receiveResults(rm);
    Mockito.stub(conn.getDQP()).toReturn(dqp);
    // some update SQL
    // $NON-NLS-1$
    String sqlCommand = "delete from table where col=?";
    TestableMMPreparedStatement statement = (TestableMMPreparedStatement) getMMPreparedStatement(conn, sqlCommand);
    ArrayList<ArrayList<Object>> expectedParameterValues = new ArrayList<ArrayList<Object>>(3);
    // Add some batches and their parameter values
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(1) })));
    statement.setInt(1, new Integer(1));
    statement.addBatch();
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(2) })));
    statement.setInt(1, new Integer(2));
    statement.addBatch();
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(3) })));
    statement.setInt(1, new Integer(3));
    statement.addBatch();
    // execute the batch and verify that it matches our dummy results
    // message set earlier
    assertTrue(Arrays.equals(new int[] { 0, 0, 0 }, statement.executeBatch()));
    // Now verify the statement's RequestMessage is what we expect
    // $NON-NLS-1$
    assertEquals("Command does not match", sqlCommand, statement.requestMessage.getCommandString());
    // $NON-NLS-1$
    assertEquals("Parameter values do not match", expectedParameterValues, statement.requestMessage.getParameterValues());
    // $NON-NLS-1$
    assertTrue("RequestMessage.isBatchedUpdate should be true", statement.requestMessage.isBatchedUpdate());
    // $NON-NLS-1$
    assertFalse("RequestMessage.isCallableStatement should be false", statement.requestMessage.isCallableStatement());
    // $NON-NLS-1$
    assertTrue("RequestMessage.isPreparedStatement should be true", statement.requestMessage.isPreparedStatement());
    count[0] = 0;
    // large batch handling - should split into 5
    for (int i = 0; i < 100000; i++) {
        statement.setInt(1, new Integer(1));
        statement.addBatch();
    }
    try {
        statement.executeBatch();
        fail();
    } catch (BatchUpdateException e) {
        assertEquals(100000, count[0]);
        assertEquals(95309, e.getUpdateCounts().length);
        assertEquals(Statement.EXECUTE_FAILED, e.getUpdateCounts()[95308]);
    }
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) LogonResult(org.teiid.client.security.LogonResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) RequestMessage(org.teiid.client.RequestMessage) ArrayList(java.util.ArrayList) List(java.util.List) BatchUpdateException(java.sql.BatchUpdateException) DQP(org.teiid.client.DQP) ServerConnection(org.teiid.net.ServerConnection) TeiidException(org.teiid.core.TeiidException) ResultsFuture(org.teiid.client.util.ResultsFuture) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 2 with LogonResult

use of org.teiid.client.security.LogonResult in project teiid by teiid.

the class TestPreparedStatement method getMMPreparedStatement.

/**
 * A helper method to get an <code>MMPreparedStatement</code> that can be
 * used for simple test cases.
 * <p>
 * The returned value is an instance of <code>TestableMMPreparedStatement</code>
 * <p>
 * This method invokes <code>getMMPreparedStatement(final MMConnection conn,
 * final String sql)</code> with a fake connection object constructed by
 * <code>Mockito</code>.
 *
 * @param sql the query for the prepared statement
 * @return an instance of TestableMMPreparedStatement
 * @throws SQLException
 */
protected PreparedStatementImpl getMMPreparedStatement(final String sql) throws SQLException {
    ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
    ServerConnection serverConn = Mockito.mock(ServerConnection.class);
    LogonResult logonResult = Mockito.mock(LogonResult.class);
    Mockito.stub(conn.getServerConnection()).toReturn(serverConn);
    Mockito.stub(serverConn.getLogonResult()).toReturn(logonResult);
    Mockito.stub(logonResult.getTimeZone()).toReturn(TimeZone.getDefault());
    return getMMPreparedStatement(conn, sql);
}
Also used : LogonResult(org.teiid.client.security.LogonResult) ServerConnection(org.teiid.net.ServerConnection)

Example 3 with LogonResult

use of org.teiid.client.security.LogonResult in project teiid by teiid.

the class TestCallableStatement method getCallableStatement.

private CallableStatementImpl getCallableStatement() throws SQLException {
    ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
    ServerConnection sc = Mockito.mock(ServerConnection.class);
    Mockito.stub(sc.getLogonResult()).toReturn(new LogonResult());
    Mockito.stub(conn.getServerConnection()).toReturn(sc);
    CallableStatementImpl mmcs = new CallableStatementImpl(conn, "{?=call x(?)}", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    return mmcs;
}
Also used : LogonResult(org.teiid.client.security.LogonResult) ServerConnection(org.teiid.net.ServerConnection)

Example 4 with LogonResult

use of org.teiid.client.security.LogonResult in project teiid by teiid.

the class TestSocketRemoting method testMethodInvocation.

@Test
public void testMethodInvocation() throws Exception {
    ClientServiceRegistryImpl csr = new ClientServiceRegistryImpl() {

        @Override
        public ClassLoader getCallerClassloader() {
            return getClass().getClassLoader();
        }
    };
    csr.registerClientService(ILogon.class, new ILogon() {

        public ResultsFuture<?> logoff() throws InvalidSessionException {
            ResultsFuture<?> result = new ResultsFuture<Void>();
            // $NON-NLS-1$
            result.getResultsReceiver().exceptionOccurred(new TeiidComponentException("some exception"));
            return result;
        }

        public LogonResult logon(Properties connectionProperties) throws LogonException, TeiidComponentException {
            return new LogonResult();
        }

        // tests asynch where we don't care about the result
        public ResultsFuture<?> ping() throws InvalidSessionException, TeiidComponentException {
            return null;
        }

        @Override
        public ResultsFuture<?> ping(Collection<String> sessions) throws TeiidComponentException, CommunicationException {
            return null;
        }

        @Override
        public void assertIdentity(SessionToken sessionId) throws InvalidSessionException, TeiidComponentException {
        }

        @Override
        public LogonResult neogitiateGssLogin(Properties connectionProperties, byte[] serviceToken, boolean createSession) throws LogonException {
            return null;
        }
    }, // $NON-NLS-1$
    "foo");
    // $NON-NLS-1$
    csr.registerClientService(FakeService.class, new FakeServiceImpl(), "foo");
    final FakeClientServerInstance serverInstance = new FakeClientServerInstance(csr);
    SocketServerConnection connection = createFakeConnection(serverInstance);
    ILogon logon = connection.getService(ILogon.class);
    Future<?> result = logon.ping();
    assertNull(result.get(0, TimeUnit.MILLISECONDS));
    result = logon.logoff();
    try {
        result.get(0, TimeUnit.MICROSECONDS);
        // $NON-NLS-1$
        fail("exception expected");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof TeiidComponentException);
    }
    FakeService service = connection.getService(FakeService.class);
    Future<Integer> asynchInteger = service.asynchResult();
    assertEquals(new Integer(5), asynchInteger.get(0, TimeUnit.MILLISECONDS));
    try {
        service.exceptionMethod();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (TeiidProcessingException e) {
    }
    DQP dqp = connection.getService(DQP.class);
    try {
        ResultsFuture<?> future = dqp.begin();
        future.get();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (Exception e) {
        // $NON-NLS-1$
        assertTrue(e.getMessage().indexOf("Component not found:") != -1);
    }
}
Also used : LogonResult(org.teiid.client.security.LogonResult) Properties(java.util.Properties) TeiidProcessingException(org.teiid.core.TeiidProcessingException) LogonException(org.teiid.client.security.LogonException) ExecutionException(java.util.concurrent.ExecutionException) InvalidSessionException(org.teiid.client.security.InvalidSessionException) DQP(org.teiid.client.DQP) CommunicationException(org.teiid.net.CommunicationException) SessionToken(org.teiid.client.security.SessionToken) ILogon(org.teiid.client.security.ILogon) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) LogonException(org.teiid.client.security.LogonException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ConnectionException(org.teiid.net.ConnectionException) ResultsFuture(org.teiid.client.util.ResultsFuture) TeiidComponentException(org.teiid.core.TeiidComponentException) SocketServerConnection(org.teiid.net.socket.SocketServerConnection) Test(org.junit.Test)

Example 5 with LogonResult

use of org.teiid.client.security.LogonResult in project teiid by teiid.

the class GssAction method run.

public Object run() {
    byte[] outToken = null;
    try {
        org.ietf.jgss.Oid[] desiredMechs = new org.ietf.jgss.Oid[1];
        // $NON-NLS-1$
        desiredMechs[0] = new org.ietf.jgss.Oid("1.2.840.113554.1.2.2");
        GSSManager manager = GSSManager.getInstance();
        // http://docs.oracle.com/cd/E21455_01/common/tutorials/kerberos_principal.html
        // $NON-NLS-1$
        org.ietf.jgss.Oid KERBEROS_V5_PRINCIPAL_NAME = new org.ietf.jgss.Oid("1.2.840.113554.1.2.2.1");
        // null on second param means the serverName is already in the native format.
        // GSSName serverName = manager.createName(this.kerberosPrincipalName, null);
        GSSName serverName = manager.createName(this.kerberosPrincipalName, KERBEROS_V5_PRINCIPAL_NAME);
        GSSCredential clientCreds = null;
        if (this.gssCredential != null) {
            clientCreds = this.gssCredential;
        }
        GSSContext secContext = manager.createContext(serverName, desiredMechs[0], clientCreds, GSSContext.DEFAULT_LIFETIME);
        secContext.requestMutualAuth(true);
        // Will use confidentiality later
        secContext.requestConf(true);
        // Will use integrity later
        secContext.requestInteg(true);
        // will use credential delegation
        secContext.requestCredDeleg(true);
        byte[] inToken = new byte[0];
        boolean established = false;
        LogonResult result = null;
        while (!established) {
            outToken = secContext.initSecContext(inToken, 0, inToken.length);
            if (outToken != null) {
                if (logger.isLoggable(Level.FINE)) {
                    // $NON-NLS-1$
                    logger.fine("Sending Service Token to Server (GSS Authentication Token)");
                }
                result = logon.neogitiateGssLogin(this.props, outToken, true);
                inToken = (byte[]) result.getProperty(ILogon.KRB5TOKEN);
            }
            if (!secContext.isEstablished()) {
                if (logger.isLoggable(Level.FINE)) {
                    // $NON-NLS-1$
                    logger.fine("Authentication GSS Continue");
                }
            } else {
                established = true;
                if (logger.isLoggable(Level.FINE)) {
                    // $NON-NLS-1$
                    logger.fine("Authentication GSS Established");
                }
            }
        }
        return result;
    } catch (GSSException gsse) {
        return TeiidSQLException.create(gsse, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20005));
    } catch (Exception e) {
        return e;
    }
}
Also used : LogonResult(org.teiid.client.security.LogonResult) org.ietf.jgss(org.ietf.jgss) TeiidComponentException(org.teiid.core.TeiidComponentException) CommunicationException(org.teiid.net.CommunicationException) LogonException(org.teiid.client.security.LogonException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException)

Aggregations

LogonResult (org.teiid.client.security.LogonResult)15 LogonException (org.teiid.client.security.LogonException)9 Properties (java.util.Properties)7 Test (org.junit.Test)5 SessionToken (org.teiid.client.security.SessionToken)5 TeiidComponentException (org.teiid.core.TeiidComponentException)4 ServerConnection (org.teiid.net.ServerConnection)4 Subject (javax.security.auth.Subject)3 DQP (org.teiid.client.DQP)3 ResultsFuture (org.teiid.client.util.ResultsFuture)3 DQPWorkContext (org.teiid.dqp.internal.process.DQPWorkContext)3 CommunicationException (org.teiid.net.CommunicationException)3 GSSCredential (org.ietf.jgss.GSSCredential)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 SessionMetadata (org.teiid.adminapi.impl.SessionMetadata)2 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)2 InvalidSessionException (org.teiid.client.security.InvalidSessionException)2 VDBRepository (org.teiid.deployers.VDBRepository)2 SessionService (org.teiid.dqp.service.SessionService)2 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)2