Search in sources :

Example 16 with IotHubSSLContext

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

the class IotHubSSLContextTest method constructorWithCertAndKeySuccess.

// Tests_SRS_IOTHUBSSLCONTEXT_34_018: [This constructor shall generate a temporary password to protect the created keystore holding the private key.]
// Tests_SRS_IOTHUBSSLCONTEXT_34_019: [The constructor shall create default SSL context for TLSv1.2.]
// Tests_SRS_IOTHUBSSLCONTEXT_34_020: [The constructor shall create a keystore containing the public key certificate and the private key.]
// Tests_SRS_IOTHUBSSLCONTEXT_34_021: [The constructor shall initialize a default trust manager factory that accepts communications from Iot Hub.]
// Tests_SRS_IOTHUBSSLCONTEXT_34_024: [The constructor shall initialize SSL context with its initialized keystore, its initialized TrustManagerFactory and a new secure random.]
@Test
public void constructorWithCertAndKeySuccess() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException, CertificateException, UnrecoverableKeyException {
    // arrange
    final String publicKeyCert = "someCert";
    final String privateKey = "someKey";
    final Collection<X509Certificate> testCertChain = new ArrayList<>();
    testCertChain.add(mockedX509Certificate);
    new MockUp<IotHubSSLContext>() {

        @Mock
        Key parsePrivateKey(String privateKeyString) throws CertificateException {
            return mockedPrivateKey;
        }

        @Mock
        Collection<X509Certificate> parsePublicKeyCertificate(String publicKeyCertificateString) throws CertificateException {
            return testCertChain;
        }
    };
    new Expectations() {

        {
            new SecureRandom();
            result = mockedSecureRandom;
            mockedSecureRandom.nextInt(anyInt);
            result = 'a';
            Deencapsulation.newInstance(IotHubCertificateManager.class);
            result = mockedCertificateManager;
            mockKeyManagerFactory.getKeyManagers();
            result = mockKeyManagers;
            TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            result = mockedTrustManagerFactory;
            mockedTrustManagerFactory.getTrustManagers();
            result = mockedTrustManager;
        }
    };
    final IotHubSSLContext iotHubSSLContext = Deencapsulation.newInstance(IotHubSSLContext.class, new Class[] { String.class, String.class }, publicKeyCert, privateKey);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) ArrayList(java.util.ArrayList) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 17 with IotHubSSLContext

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

the class IotHubSSLContextTest method getterGetsContext.

// Tests_SRS_IOTHUBSSLCONTEXT_25_017: [*This method shall return the value of sslContext.**]**
@Test
public void getterGetsContext() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException, CertificateException {
    // arrange
    testCollection.add(mockedX509Certificate);
    generateSSLContextExpectations();
    IotHubSSLContext testContext = Deencapsulation.newInstance(IotHubSSLContext.class, new Class[] {});
    // act
    SSLContext testSSLContext = Deencapsulation.invoke(testContext, "getSSLContext");
    // assert
    generateSSLContextVerifications();
    assertNotNull(testSSLContext);
    testCollection.remove(mockedX509Certificate);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Test(org.junit.Test)

Example 18 with IotHubSSLContext

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

the class IotHubSSLContextTest method constructorWithSSLContextSavesSSLContext.

// Tests_SRS_IOTHUBSSLCONTEXT_34_027: [This constructor shall save the provided ssl context.]
@Test
public void constructorWithSSLContextSavesSSLContext() {
    // act
    IotHubSSLContext iotHubSSLContext = new IotHubSSLContext(mockedSSLContext);
    // assert
    SSLContext savedSSLContext = Deencapsulation.getField(iotHubSSLContext, "sslContext");
    assertEquals(mockedSSLContext, savedSSLContext);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) 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