Java Code Examples for org.apache.qpid.proton.Proton#transport()

The following examples show how to use org.apache.qpid.proton.Proton#transport() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameWriterBenchmark.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
public void initProton() {
    byteBuf = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
    this.decoder = new DecoderImpl();
    this.encoder = new EncoderImpl(decoder);
    AMQPDefinedTypes.registerAllTypes(decoder, encoder);

    transport = (TransportImpl) Proton.transport();
    frameWriter = new FrameWriter(encoder, 16 * 1024, (byte) 0, transport);

    transfer = new Transfer();
    transfer.setDeliveryId(UnsignedInteger.ONE);
    transfer.setHandle(UnsignedInteger.valueOf(16));
    transfer.setDeliveryTag(new Binary(new byte[] { 0, 1}));
    transfer.setMessageFormat(UnsignedInteger.ZERO);

    payload = ReadableBuffer.ByteBufferReader.wrap(PAYLOAD_BYTES);
}
 
Example 2
Source File: SimpleTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void test()
{
    Connection connection1 = Proton.connection();
    Connection connection2 = Proton.connection();;
    Transport transport1 = Proton.transport();
    transport1.bind(connection1);

    Transport transport2 = Proton.transport();
    transport2.bind(connection2);

    assertEquals(EndpointState.UNINITIALIZED, connection1.getLocalState());
    assertEquals(EndpointState.UNINITIALIZED, connection1.getRemoteState());

    connection1.open();
    connection2.open();
}
 
Example 3
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testChannelMaxDefault() throws Exception
{
    Transport transport = Proton.transport();

    assertEquals("Unesxpected value for channel-max", 65535, transport.getChannelMax());
}
 
Example 4
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetGetChannelMax() throws Exception
{
    Transport transport = Proton.transport();

    int channelMax = 456;
    transport.setChannelMax(channelMax);
    assertEquals("Unesxpected value for channel-max", channelMax, transport.getChannelMax());
}
 
Example 5
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
/** 5.3.2 SASL Negotiation. ...challenge/response step can occur zero or more times*/
@Test
public void testOptionalChallengeResponseStepOmitted() throws Exception
{
    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();
    serverSasl.setMechanisms(TESTMECH1);
    assertEquals("Server should not yet know the remote's chosen mechanism.",
                 0,
                 serverSasl.getRemoteMechanisms().length);

    pumpClientToServer();
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    clientSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();

    serverSasl.done(SaslOutcome.PN_SASL_OK);
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome());
}
 
Example 6
Source File: IOHandler.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void handleOpen(Reactor reactor, Event event) {
    Connection connection = event.getConnection();
    if (connection.getRemoteState() != EndpointState.UNINITIALIZED) {
        return;
    }
    // Outgoing Reactor connections set the virtual host automatically using the
    // following rules:
    String vhost = connection.getHostname();
    if (vhost == null) {
        // setHostname never called, use the host from the connection's
        // socket address as the default virtual host:
        String conAddr = reactor.getConnectionAddress(connection);
        if (conAddr != null) {
            Address addr = new Address(conAddr);
            connection.setHostname(addr.getHost());
        }
    } else if (vhost.isEmpty()) {
        // setHostname called explictly with a null string. This allows
        // the application to completely avoid sending a virtual host
        // name
        connection.setHostname(null);
    } else {
        // setHostname set by application - use it.
    }
    Transport transport = Proton.transport();

    int maxFrameSizeOption = reactor.getOptions().getMaxFrameSize();
    if (maxFrameSizeOption != 0) {
        transport.setMaxFrameSize(maxFrameSizeOption);
    }

    if (reactor.getOptions().isEnableSaslByDefault()) {
        Sasl sasl = transport.sasl();
        sasl.client();
        sasl.setMechanisms("ANONYMOUS");
    }

    transport.bind(connection);
}
 
Example 7
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
/**
 *  5.3.3.6 Connection authentication failed due to an unspecified problem with the supplied credentials.
 */
@Test
public void testAuthenticationFails() throws Exception
{
    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();
    serverSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    clientSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();

    serverSasl.done(SaslOutcome.PN_SASL_AUTH);
    pumpServerToClient();
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_AUTH, clientSasl.getOutcome());

}
 
Example 8
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 *  5.3.3.5 The additional-data field carries additional data on successful authentication outcome as specified
 *  by the SASL specification [RFC4422].
 */
@Test
public void testOutcomeAdditionalData() throws Exception
{
    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();
    serverSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    clientSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();

    serverSasl.send(CHALLENGE_BYTES, 0, CHALLENGE_BYTES.length);

    pumpServerToClient();

    byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()];
    clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length);

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    clientSasl.send(RESPONSE_BYTES, 0, RESPONSE_BYTES.length);

    pumpClientToServer();

    byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()];
    serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length);

    serverSasl.send(ADDITIONAL_DATA_BYTES, 0, ADDITIONAL_DATA_BYTES.length);
    serverSasl.done(SaslOutcome.PN_SASL_OK);
    pumpServerToClient();

    byte[] clientReceivedAdditionalDataBytes = new byte[clientSasl.pending()];
    clientSasl.recv(clientReceivedAdditionalDataBytes, 0, clientReceivedAdditionalDataBytes.length);

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome());
    assertArrayEquals("Client should now know the serrver's additional-data",
                      ADDITIONAL_DATA_BYTES,
                      clientReceivedAdditionalDataBytes);
}
 
Example 9
Source File: SslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void doMultiplePemTrustCertificatesTestImpl(String serverKeystore) throws Exception {
    Transport clientTransport = Proton.transport();
    Transport serverTransport = Proton.transport();

    TransportPumper pumper = new TransportPumper(clientTransport, serverTransport);

    Connection clientConnection = Proton.connection();
    Connection serverConnection = Proton.connection();

    SslDomain clientSslDomain = SslDomain.Factory.create();
    clientSslDomain.init(Mode.CLIENT);
    clientSslDomain.setPeerAuthentication(VerifyMode.VERIFY_PEER);
    clientSslDomain.setTrustedCaDb(CA_CERTS);
    clientTransport.ssl(clientSslDomain);

    SslDomain serverSslDomain = SslDomain.Factory.create();
    serverSslDomain.init(Mode.SERVER);
    SSLContext serverSslContext = createSslContext(serverKeystore, PASSWORD, SERVER_JKS_TRUSTSTORE, PASSWORD);
    serverSslDomain.setSslContext(serverSslContext);
    serverTransport.ssl(serverSslDomain);

    clientConnection.setContainer(CLIENT_CONTAINER);
    serverConnection.setContainer(SERVER_CONTAINER);

    clientTransport.bind(clientConnection);
    serverTransport.bind(serverConnection);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    clientConnection.open();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, UNINITIALIZED, UNINITIALIZED);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    pumper.pumpAll();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, UNINITIALIZED, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    serverConnection.open();

    assertEndpointState(clientConnection, ACTIVE, UNINITIALIZED);
    assertEndpointState(serverConnection, ACTIVE, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);

    pumper.pumpAll();

    assertEndpointState(clientConnection, ACTIVE, ACTIVE);
    assertEndpointState(serverConnection, ACTIVE, ACTIVE);

    assertConditions(clientTransport);
    assertConditions(serverTransport);
}
 
Example 10
Source File: SessionTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() throws Exception
{
    final Symbol clientPropName = Symbol.valueOf("ClientPropName");
    final Integer clientPropValue = 1234;
    final Symbol serverPropName = Symbol.valueOf("ServerPropName");
    final Integer serverPropValue = 5678;

    Map<Symbol, Object> clientProps = new HashMap<>();
    clientProps.put(clientPropName, clientPropValue);

    Map<Symbol, Object> serverProps = new HashMap<>();
    serverProps.put(serverPropName, serverPropValue);

    LOGGER.fine(bold("======== About to create transports"));

    getClient().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX);

    getServer().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getServer().transport, "            " + TestLoggingHelper.SERVER_PREFIX);

    doOutputInputCycle();

    getClient().connection = Proton.connection();
    getClient().transport.bind(getClient().connection);

    getServer().connection = Proton.connection();
    getServer().transport.bind(getServer().connection);

    LOGGER.fine(bold("======== About to open connections"));
    getClient().connection.open();
    getServer().connection.open();

    doOutputInputCycle();

    LOGGER.fine(bold("======== About to open sessions"));
    getClient().session = getClient().connection.session();

    // Set the client session properties
    getClient().session.setProperties(clientProps);

    getClient().session.open();

    pumpClientToServer();

    getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
    assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE);

    // Set the server session properties
    getServer().session.setProperties(serverProps);

    getServer().session.open();

    assertEndpointState(getServer().session, ACTIVE, ACTIVE);

    pumpServerToClient();

    assertEndpointState(getClient().session, ACTIVE, ACTIVE);

    // Verify server side got the clients session properties as expected
    Map<Symbol, Object> serverRemoteProperties = getServer().session.getRemoteProperties();
    assertNotNull("Server had no remote properties", serverRemoteProperties);
    assertEquals("Server remote properties not expected size", 1, serverRemoteProperties.size());
    assertTrue("Server remote properties lack expected key: " + clientPropName, serverRemoteProperties.containsKey(clientPropName));
    assertEquals("Server remote properties contain unexpected value for key: " + clientPropName, clientPropValue, serverRemoteProperties.get(clientPropName));

    // Verify the client side got the servers session properties as expected
    Map<Symbol, Object> clientRemoteProperties = getClient().session.getRemoteProperties();
    assertNotNull("Client had no remote properties", clientRemoteProperties);
    assertEquals("Client remote properties not expected size", 1, clientRemoteProperties.size());
    assertTrue("Client remote properties lack expected key: " + serverPropName, clientRemoteProperties.containsKey(serverPropName));
    assertEquals("Client remote properties contain unexpected value for key: " + serverPropName, serverPropValue, clientRemoteProperties.get(serverPropName));
}
 
Example 11
Source File: SessionTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCapabilities() throws Exception
{
    final Symbol clientOfferedCap = Symbol.valueOf("clientOfferedCapability");
    final Symbol clientDesiredCap = Symbol.valueOf("clientDesiredCapability");
    final Symbol serverOfferedCap = Symbol.valueOf("serverOfferedCapability");
    final Symbol serverDesiredCap = Symbol.valueOf("serverDesiredCapability");

    Symbol[] clientOfferedCapabilities = new Symbol[] { clientOfferedCap };
    Symbol[] clientDesiredCapabilities = new Symbol[] { clientDesiredCap };

    Symbol[] serverOfferedCapabilities = new Symbol[] { serverOfferedCap };
    Symbol[] serverDesiredCapabilities = new Symbol[] { serverDesiredCap };

    LOGGER.fine(bold("======== About to create transports"));

    getClient().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX);

    getServer().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getServer().transport, "            " + TestLoggingHelper.SERVER_PREFIX);

    doOutputInputCycle();

    getClient().connection = Proton.connection();
    getClient().transport.bind(getClient().connection);

    getServer().connection = Proton.connection();
    getServer().transport.bind(getServer().connection);

    LOGGER.fine(bold("======== About to open connections"));
    getClient().connection.open();
    getServer().connection.open();

    doOutputInputCycle();

    LOGGER.fine(bold("======== About to open sessions"));
    getClient().session = getClient().connection.session();

    // Set the client session capabilities
    getClient().session.setOfferedCapabilities(clientOfferedCapabilities);
    getClient().session.setDesiredCapabilities(clientDesiredCapabilities);

    getClient().session.open();

    pumpClientToServer();

    getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
    assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE);

    // Set the server session capabilities
    getServer().session.setOfferedCapabilities(serverOfferedCapabilities);
    getServer().session.setDesiredCapabilities(serverDesiredCapabilities);

    getServer().session.open();
    assertEndpointState(getServer().session, ACTIVE, ACTIVE);

    pumpServerToClient();

    // Verify server side got the clients session capabilities as expected
    Symbol[] serverRemoteOfferedCapabilities = getServer().session.getRemoteOfferedCapabilities();
    assertNotNull("Server had no remote offered capabilities", serverRemoteOfferedCapabilities);
    assertEquals("Server remote offered capabilities not expected size", 1, serverRemoteOfferedCapabilities.length);
    assertTrue("Server remote offered capabilities lack expected value: " + clientOfferedCap, Arrays.asList(serverRemoteOfferedCapabilities).contains(clientOfferedCap));

    Symbol[] serverRemoteDesiredCapabilities = getServer().session.getRemoteDesiredCapabilities();
    assertNotNull("Server had no remote desired capabilities", serverRemoteDesiredCapabilities);
    assertEquals("Server remote desired capabilities not expected size", 1, serverRemoteDesiredCapabilities.length);
    assertTrue("Server remote desired capabilities lack expected value: " + clientDesiredCap, Arrays.asList(serverRemoteDesiredCapabilities).contains(clientDesiredCap));

    // Verify the client side got the servers session capabilities as expected
    Symbol[]  clientRemoteOfferedCapabilities = getClient().session.getRemoteOfferedCapabilities();
    assertNotNull("Client had no remote offered capabilities", clientRemoteOfferedCapabilities);
    assertEquals("Client remote offered capabilities not expected size", 1, clientRemoteOfferedCapabilities.length);
    assertTrue("Client remote offered capabilities lack expected value: " + serverOfferedCap, Arrays.asList(clientRemoteOfferedCapabilities).contains(serverOfferedCap));

    Symbol[]  clientRemoteDesiredCapabilities = getClient().session.getRemoteDesiredCapabilities();
    assertNotNull("Client had no remote desired capabilities", clientRemoteDesiredCapabilities);
    assertEquals("Client remote desired capabilities not expected size", 1, clientRemoteDesiredCapabilities.length);
    assertTrue("Client remote desired capabilities lack expected value: " + serverDesiredCap, Arrays.asList(clientRemoteDesiredCapabilities).contains(serverDesiredCap));
}
 
Example 12
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSaslNegotiationUsingListener() throws Exception
{
    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    AtomicBoolean mechanismsReceived = new AtomicBoolean();
    AtomicBoolean challengeReceived = new AtomicBoolean();
    AtomicBoolean outcomeReceived = new AtomicBoolean();

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();
    clientSasl.setListener(new ClientSaslHandling(mechanismsReceived, challengeReceived, outcomeReceived));

    AtomicBoolean initReceived = new AtomicBoolean();
    AtomicBoolean responseReceived = new AtomicBoolean();

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();
    serverSasl.setMechanisms(TESTMECH1, TESTMECH2);
    serverSasl.setListener(new ServerSaslHandling(initReceived, responseReceived));

    pumpClientToServer();
    pumpServerToClient();

    assertTrue("mechanisms were not received by client", mechanismsReceived.get());
    assertFalse("init was received by server", initReceived.get());

    pumpClientToServer();

    assertTrue("init was not received by server", initReceived.get());
    assertFalse("challenge was received by client", challengeReceived.get());

    pumpServerToClient();

    assertTrue("challenge was not received by client", challengeReceived.get());
    assertFalse("response was received by server", responseReceived.get());

    pumpClientToServer();

    assertTrue("response was received by server", responseReceived.get());
    assertFalse("outcome was received by client", outcomeReceived.get());

    pumpServerToClient();

    assertTrue("outcome was received by client", outcomeReceived.get());

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome());
}
 
Example 13
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSaslNegotiationWithConfiguredLargerFrameSize() throws Exception
{
    final byte[] largeInitialResponseBytesOrig = fillBytes("initialResponse", 1431);
    final byte[] largeChallengeBytesOrig = fillBytes("challenge", 1375);
    final byte[] largeResponseBytesOrig = fillBytes("response", 1282);
    final byte[] largeAdditionalBytesOrig = fillBytes("additionalData", 1529);

    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    // Configure transports to allow for larger initial frame sizes
    getClient().transport.setInitialRemoteMaxFrameSize(2048);
    getServer().transport.setInitialRemoteMaxFrameSize(2048);

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();

    // Negotiate the mech
    serverSasl.setMechanisms(TESTMECH1, TESTMECH2);

    pumpClientToServer();
    pumpServerToClient();

    assertArrayEquals("Client should now know the server's mechanisms.", new String[] { TESTMECH1, TESTMECH2 },
            clientSasl.getRemoteMechanisms());
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());

    // Select a mech, send large initial response along with it in sasl-init, verify server receives it
    clientSasl.setMechanisms(TESTMECH1);
    byte[] initialResponseBytes = Arrays.copyOf(largeInitialResponseBytesOrig, largeInitialResponseBytesOrig.length);
    clientSasl.send(initialResponseBytes, 0, initialResponseBytes.length);

    pumpClientToServer();

    assertArrayEquals("Server should now know the client's chosen mechanism.", new String[] { TESTMECH1 },
            serverSasl.getRemoteMechanisms());

    byte[] serverReceivedInitialResponseBytes = new byte[serverSasl.pending()];
    serverSasl.recv(serverReceivedInitialResponseBytes, 0, serverReceivedInitialResponseBytes.length);

    assertArrayEquals("Server should now know the clients initial response", largeInitialResponseBytesOrig,
            serverReceivedInitialResponseBytes);

    // Send a large challenge in a sasl-challenge, verify client receives it
    byte[] challengeBytes = Arrays.copyOf(largeChallengeBytesOrig, largeChallengeBytesOrig.length);
    serverSasl.send(challengeBytes, 0, challengeBytes.length);

    pumpServerToClient();

    byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()];
    clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length);

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    assertArrayEquals("Client should now know the server's challenge", largeChallengeBytesOrig,
            clientReceivedChallengeBytes);

    // Send a large response in a sasl-response, verify server receives it
    byte[] responseBytes = Arrays.copyOf(largeResponseBytesOrig, largeResponseBytesOrig.length);
    clientSasl.send(responseBytes, 0, responseBytes.length);

    pumpClientToServer();

    byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()];
    serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length);

    assertArrayEquals("Server should now know the client's response", largeResponseBytesOrig, serverReceivedResponseBytes);

    // Send an outcome with large additional data in a sasl-outcome, verify client receives it
    byte[] additionalBytes = Arrays.copyOf(largeAdditionalBytesOrig, largeAdditionalBytesOrig.length);
    serverSasl.send(additionalBytes, 0, additionalBytes.length);
    serverSasl.done(SaslOutcome.PN_SASL_OK);
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome());

    byte[] clientReceivedAdditionalBytes = new byte[clientSasl.pending()];
    clientSasl.recv(clientReceivedAdditionalBytes, 0, clientReceivedAdditionalBytes.length);

    assertArrayEquals("Client should now know the server's outcome additional data", largeAdditionalBytesOrig,
            clientReceivedAdditionalBytes);
}
 
Example 14
Source File: SaslTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/** 5.3.2 SASL Negotiation. */
@Test
public void testSaslNegotiation() throws Exception
{
    getClient().transport = Proton.transport();
    getServer().transport = Proton.transport();

    Sasl clientSasl = getClient().transport.sasl();
    clientSasl.client();
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());

    Sasl serverSasl = getServer().transport.sasl();
    serverSasl.server();
    serverSasl.setMechanisms(TESTMECH1, TESTMECH2);
    assertEquals("Server should not yet know the remote's chosen mechanism.",
                 0,
                 serverSasl.getRemoteMechanisms().length);

    pumpClientToServer();
    pumpServerToClient();

    assertArrayEquals("Client should now know the server's mechanisms.",
                      new String[]{TESTMECH1, TESTMECH2},
                      clientSasl.getRemoteMechanisms());
    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    clientSasl.setMechanisms(TESTMECH1);

    pumpClientToServer();

    assertArrayEquals("Server should now know the client's chosen mechanism.",
                      new String[]{TESTMECH1},
                      serverSasl.getRemoteMechanisms());

    serverSasl.send(CHALLENGE_BYTES, 0, CHALLENGE_BYTES.length);

    pumpServerToClient();

    byte[] clientReceivedChallengeBytes = new byte[clientSasl.pending()];
    clientSasl.recv(clientReceivedChallengeBytes, 0, clientReceivedChallengeBytes.length);

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_NONE, clientSasl.getOutcome());
    assertArrayEquals("Client should now know the server's challenge",
                      CHALLENGE_BYTES,
                      clientReceivedChallengeBytes);

    clientSasl.send(RESPONSE_BYTES, 0, RESPONSE_BYTES.length);

    pumpClientToServer();

    byte[] serverReceivedResponseBytes = new byte[serverSasl.pending()];
    serverSasl.recv(serverReceivedResponseBytes, 0, serverReceivedResponseBytes.length);

    assertArrayEquals("Server should now know the client's response",
                      RESPONSE_BYTES,
                      serverReceivedResponseBytes);

    serverSasl.done(SaslOutcome.PN_SASL_OK);
    pumpServerToClient();

    assertEquals("Unexpected SASL outcome at client", SaslOutcome.PN_SASL_OK, clientSasl.getOutcome());
}
 
Example 15
Source File: LinkTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMaxMessageSizeValue() throws Exception
{
    LOGGER.fine(bold("======== About to create transports"));

    Transport clientTransport = Proton.transport();
    getClient().setTransport(clientTransport);
    ProtocolTracerEnabler.setProtocolTracer(clientTransport, TestLoggingHelper.CLIENT_PREFIX);

    Transport serverTransport = Proton.transport();
    getServer().setTransport(serverTransport);
    ProtocolTracerEnabler.setProtocolTracer(serverTransport, "            " + TestLoggingHelper.SERVER_PREFIX);

    doOutputInputCycle();

    Connection clientConnection = Proton.connection();
    getClient().setConnection(clientConnection);
    clientTransport.bind(clientConnection);

    Connection serverConnection = Proton.connection();
    getServer().setConnection(serverConnection);
    serverTransport.bind(serverConnection);

    LOGGER.fine(bold("======== About to open connections"));
    clientConnection.open();
    serverConnection.open();

    doOutputInputCycle();

    LOGGER.fine(bold("======== About to open sessions"));
    Session clientSession = clientConnection.session();
    getClient().setSession(clientSession);
    clientSession.open();

    pumpClientToServer();

    Session serverSession = serverConnection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
    getServer().setSession(serverSession);

    serverSession.open();

    pumpServerToClient();

    LOGGER.fine(bold("======== About to create receiver"));

    Source clientSource = new Source();
    getClient().setSource(clientSource);
    clientSource.setAddress(_sourceAddress);

    Target clientTarget = new Target();
    getClient().setTarget(clientTarget);
    clientTarget.setAddress(null);

    Receiver clientReceiver = clientSession.receiver("link1");
    getClient().setReceiver(clientReceiver);
    clientReceiver.setTarget(clientTarget);
    clientReceiver.setSource(clientSource);

    clientReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
    clientReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);

    // Set the local link max-message-size
    assertNull("Expected no value to be set", clientReceiver.getMaxMessageSize());
    clientReceiver.setMaxMessageSize(CLIENT_MAX_MSG_SIZE);
    assertEquals("Expected value to be set", CLIENT_MAX_MSG_SIZE, clientReceiver.getMaxMessageSize());

    clientReceiver.open();
    pumpClientToServer();

    LOGGER.fine(bold("======== About to set up implicitly created sender"));

    Sender serverSender = (Sender) getServer().getConnection().linkHead(of(UNINITIALIZED), of(ACTIVE));
    getServer().setSender(serverSender);

    serverSender.setReceiverSettleMode(serverSender.getRemoteReceiverSettleMode());
    serverSender.setSenderSettleMode(serverSender.getRemoteSenderSettleMode());

    org.apache.qpid.proton.amqp.transport.Source serverRemoteSource = serverSender.getRemoteSource();
    serverSender.setSource(serverRemoteSource);

    assertEquals("Expected value to be set", CLIENT_MAX_MSG_SIZE, serverSender.getRemoteMaxMessageSize());

    // Set the local link max-message-size
    assertNull("Expected no value to be set", serverSender.getMaxMessageSize());
    serverSender.setMaxMessageSize(SERVER_MAX_MSG_SIZE);
    assertEquals("Expected value to be set", SERVER_MAX_MSG_SIZE, serverSender.getMaxMessageSize());

    serverSender.open();

    assertNull("Expected no value to be present yet", clientReceiver.getRemoteMaxMessageSize());

    pumpServerToClient();

    assertEquals("Expected value to be set", SERVER_MAX_MSG_SIZE, clientReceiver.getRemoteMaxMessageSize());
}
 
Example 16
Source File: AcceptorImpl.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Selectable selectable) {
    Reactor reactor = selectable.getReactor();
    try {
        SocketChannel socketChannel = ((ServerSocketChannel)selectable.getChannel()).accept();
        if (socketChannel == null) {
            throw new ReactorInternalException("Selectable readable, but no socket to accept");
        }
        Handler handler = BaseHandler.getHandler(AcceptorImpl.this);
        if (handler == null) {
            handler = reactor.getHandler();
        }
        Connection conn = reactor.connection(handler);
        Record conn_recs = conn.attachments();
        conn_recs.set(CONNECTION_ACCEPTOR_KEY, Acceptor.class, AcceptorImpl.this);
        InetSocketAddress peerAddr = (InetSocketAddress)socketChannel.getRemoteAddress();
        if (peerAddr != null) {
            Address addr = new Address();
            addr.setHost(peerAddr.getHostString());
            addr.setPort(Integer.toString(peerAddr.getPort()));
            conn_recs.set(ReactorImpl.CONNECTION_PEER_ADDRESS_KEY, Address.class, addr);
        }
        Transport trans = Proton.transport();

        int maxFrameSizeOption = reactor.getOptions().getMaxFrameSize();
        if (maxFrameSizeOption != 0) {
            trans.setMaxFrameSize(maxFrameSizeOption);
        }

        if(reactor.getOptions().isEnableSaslByDefault()) {
            Sasl sasl = trans.sasl();
            sasl.server();
            sasl.setMechanisms("ANONYMOUS");
            sasl.done(SaslOutcome.PN_SASL_OK);
        }
        trans.bind(conn);
        IOHandler.selectableTransport(reactor, socketChannel.socket(), trans);
    } catch(IOException ioException) {
        sel.error();
    }
}
 
Example 17
Source File: ProtonEngineExampleTest.java    From qpid-proton-j with Apache License 2.0 2 votes vote down vote up
@Ignore("This test does not have a fix yet")
@Test
public void testPROTON_1017() throws Exception
{
    LOGGER.fine(bold("======== About to create transports"));

    getClient().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX);

    getServer().transport = Proton.transport();
    ProtocolTracerEnabler.setProtocolTracer(getServer().transport, "            " + TestLoggingHelper.SERVER_PREFIX);

    doOutputInputCycle();

    getClient().connection = Proton.connection();
    getClient().transport.bind(getClient().connection);

    getServer().connection = Proton.connection();
    getServer().transport.bind(getServer().connection);

    LOGGER.fine(bold("======== About to open connections"));
    getClient().connection.open();
    getServer().connection.open();

    doOutputInputCycle();

    LOGGER.fine(bold("======== About to open and close client session"));
    getClient().session = getClient().connection.session();
    getClient().session.open();
    getClient().session.close();
    pumpClientToServer();

    getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(CLOSED));
    assertEndpointState(getServer().session, UNINITIALIZED, CLOSED);

    getServer().session.open();
    assertEndpointState(getServer().session, ACTIVE, CLOSED);

    getServer().session.close();
    assertEndpointState(getServer().session, CLOSED, CLOSED);

    pumpServerToClient();
    assertEndpointState(getClient().session, CLOSED, CLOSED);

    LOGGER.fine(bold("======== About to close client's connection"));

    getClient().connection.close();

    pumpClientToServer();

    LOGGER.fine(bold("======== Server about to process client's connection closure"));

    assertEquals(CLOSED, getServer().connection.getRemoteState());
    getServer().connection.close();

    pumpServerToClient();

    LOGGER.fine(bold("======== Checking client has nothing more to pump"));

    assertClientHasNothingToOutput();

    LOGGER.fine(bold("======== Done!"));
}