Search in sources :

Example 11 with IotHubSSLContext

use of com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext in project azure-iot-sdk-java by Azure.

the class AmqpSendHandlerTest method onConnectionBound_call_flow_and_init_ok_amqp_ws.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_009: [The event handler shall set the SASL PLAIN authentication on the Transport using the given user name and sas token]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_010: [The event handler shall set VERIFY_PEER authentication mode on the domain of the Transport]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_024: [The event handler shall initialize WebSocket if the protocol is AMQP_WS]
@Test
public void onConnectionBound_call_flow_and_init_ok_amqp_ws() throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS_WS;
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    // Assert
    new Expectations() {

        {
            event.getConnection();
            result = connection;
            connection.getTransport();
            result = transportInternal;
            new WebSocketImpl(anyInt);
            result = webSocket;
            webSocket.configure(anyString, anyString, anyString, 443, anyString, null, null);
            transportInternal.addTransportLayer(webSocket);
            Proton.sslDomain();
            result = sslDomain;
            sslDomain.init(SslDomain.Mode.CLIENT);
            sslDomain.setPeerAuthentication(SslDomain.VerifyMode.VERIFY_PEER);
            transportInternal.ssl(sslDomain);
            new IotHubSSLContext();
            result = mockedIotHubSSLContext;
        }
    };
    // Act
    amqpSendHandler.onConnectionBound(event);
}
Also used : Expectations(mockit.Expectations) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) WebSocketImpl(com.microsoft.azure.proton.transport.ws.impl.WebSocketImpl) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 12 with IotHubSSLContext

use of com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext in project azure-iot-sdk-java by Azure.

the class IotHubX509HardwareIotHubAuthenticationProviderTest method getSSLContextSuccess.

// Tests_SRS_IOTHUBX509HARDWAREAUTHENTICATION_34_003: [If this object's ssl context has not been generated yet, this function shall generate it from the saved security provider.]
// Tests_SRS_IOTHUBX509HARDWAREAUTHENTICATION_34_005: [This function shall return the saved IotHubSSLContext.]
@Test
public void getSSLContextSuccess() throws SecurityProviderException, IOException, TransportException {
    // arrange
    IotHubAuthenticationProvider authentication = new IotHubX509HardwareAuthenticationProvider(hostname, gatewayHostname, deviceId, moduleId, mockSecurityProviderX509);
    new NonStrictExpectations() {

        {
            Deencapsulation.invoke(mockIotHubSSLContext, "getSSLContext");
            result = mockSSLContext;
            mockSecurityProviderX509.getSSLContext();
            result = mockSSLContext;
            Deencapsulation.newInstance(IotHubSSLContext.class, new Class[] { SSLContext.class }, mockSSLContext);
            result = mockIotHubSSLContext;
            Deencapsulation.invoke(mockIotHubSSLContext, "getSSLContext");
            result = mockSSLContext;
        }
    };
    Deencapsulation.setField(authentication, "iotHubSSLContext", null);
    // act
    SSLContext actualSSLContext = authentication.getSSLContext();
    assertEquals(mockSSLContext, actualSSLContext);
}
Also used : IotHubAuthenticationProvider(com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider) IotHubX509HardwareAuthenticationProvider(com.microsoft.azure.sdk.iot.device.auth.IotHubX509HardwareAuthenticationProvider) SSLContext(javax.net.ssl.SSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 13 with IotHubSSLContext

use of com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext in project azure-iot-sdk-java by Azure.

the class IotHubX509SoftwareIotHubAuthenticationProviderTest method getSSLContextSuccess.

// Tests_SRS_IOTHUBX509SOFTWAREAUTHENTICATION_34_003: [If this object's ssl context has not been generated yet, this function shall generate it from the saved security provider.]
// Tests_SRS_IOTHUBX509SOFTWAREAUTHENTICATION_34_005: [This function shall return the saved IotHubSSLContext.]
@Test
public void getSSLContextSuccess() throws IOException, TransportException {
    // arrange
    IotHubAuthenticationProvider authentication = new IotHubX509SoftwareAuthenticationProvider(hostname, gatewayHostname, deviceId, moduleId, publicKeyCertificate, false, privateKey, false);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(IotHubSSLContext.class, new Class[] { String.class, String.class });
            result = mockIotHubSSLContext;
            Deencapsulation.invoke(mockIotHubSSLContext, "getSSLContext");
            result = mockSSLContext;
        }
    };
    Deencapsulation.setField(authentication, "iotHubSSLContext", null);
    // act
    SSLContext actualSSLContext = authentication.getSSLContext();
    assertEquals(mockSSLContext, actualSSLContext);
}
Also used : SSLContext(javax.net.ssl.SSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Test(org.junit.Test)

Example 14 with IotHubSSLContext

use of com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext in project azure-iot-sdk-java by Azure.

the class AmqpConnectionHandler method makeDomain.

/**
 * Create Proton SslDomain object from Address using the given Ssl mode
 * @return The created Ssl domain
 */
private SslDomain makeDomain() {
    SslDomain domain = Proton.sslDomain();
    try {
        if (this.sslContext == null) {
            // Need the base trusted certs for IotHub in our ssl context. IotHubSSLContext handles that
            domain.setSslContext(new IotHubSSLContext().getSSLContext());
        } else {
            // Custom SSLContext set by user from service client options
            domain.setSslContext(this.sslContext);
        }
    } catch (Exception e) {
        this.savedException = e;
    }
    domain.init(SslDomain.Mode.CLIENT);
    return domain;
}
Also used : SslDomain(org.apache.qpid.proton.engine.SslDomain) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 15 with IotHubSSLContext

use of com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext in project azure-iot-sdk-java by Azure.

the class IotHubSSLContextTest method constructorCreatesSSLContext.

// Tests_SRS_IOTHUBSSLCONTEXT_25_001: [**The constructor shall create a default certificate to be used with IotHub.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_002: [**The constructor shall create default SSL context for TLSv1.2.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_003: [**The constructor shall create default TrustManagerFactory with the default algorithm.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_004: [**The constructor shall create default KeyStore instance with the default type and initialize it.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_005: [**The constructor shall set the above created certificate into a keystore.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_006: [**The constructor shall initialize TrustManagerFactory with the above initialized keystore.**]**
// Tests_SRS_IOTHUBSSLCONTEXT_25_007: [**The constructor shall initialize SSL context with the above initialized TrustManagerFactory and a new secure random.**]**
@Test
public void constructorCreatesSSLContext() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException, CertificateException {
    // arrange
    testCollection.add(mockedX509Certificate);
    generateSSLContextExpectations();
    // act
    IotHubSSLContext testContext = Deencapsulation.newInstance(IotHubSSLContext.class);
    // assert
    generateSSLContextVerifications();
    assertNotNull(Deencapsulation.invoke(testContext, "getSSLContext"));
    testCollection.remove(mockedX509Certificate);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Test(org.junit.Test)

Aggregations

IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)18 Test (org.junit.Test)14 SSLContext (javax.net.ssl.SSLContext)7 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)3 X509Certificate (java.security.cert.X509Certificate)3 ArrayList (java.util.ArrayList)3 Device (com.microsoft.azure.sdk.iot.service.Device)2 IOException (java.io.IOException)2 WebSocketImpl (com.microsoft.azure.proton.transport.ws.impl.WebSocketImpl)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 IotHubX509HardwareAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubX509HardwareAuthenticationProvider)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)1 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)1 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)1 ServiceClientOptions (com.microsoft.azure.sdk.iot.service.ServiceClientOptions)1 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1