Search in sources :

Example 1 with TestDeviceIdentity

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method cloudToDeviceTelemetry.

public void cloudToDeviceTelemetry(boolean withProxy, boolean withPayload, boolean withLargestPayload, boolean withCustomSSLContext, boolean withAzureSasCredential) throws Exception {
    // We remove and recreate the device for a clean start
    RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
    Device device = testDeviceIdentity.getDevice();
    Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
    // Create service client
    ProxyOptions proxyOptions = null;
    if (withProxy) {
        Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        proxyOptions = new ProxyOptions(testProxy);
    }
    SSLContext sslContext = null;
    if (withCustomSSLContext) {
        sslContext = new IotHubSSLContext().getSSLContext();
    }
    ServiceClientOptions serviceClientOptions = ServiceClientOptions.builder().proxyOptions(proxyOptions).sslContext(sslContext).build();
    ServiceClient serviceClient;
    if (withAzureSasCredential) {
        serviceClient = buildServiceClientWithAzureSasCredential(testInstance.protocol, serviceClientOptions);
    } else {
        serviceClient = new ServiceClient(iotHubConnectionString, testInstance.protocol, serviceClientOptions);
    }
    serviceClient.open();
    Message message;
    if (withPayload) {
        if (withLargestPayload) {
            message = new Message(LARGEST_PAYLOAD);
        } else {
            message = new Message(SMALL_PAYLOAD);
        }
    } else {
        message = new Message();
    }
    serviceClient.send(device.getDeviceId(), message);
    Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
    serviceClient.close();
    Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), deviceGetBefore.getDeviceId(), deviceGetAfter.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), 0, deviceGetBefore.getCloudToDeviceMessageCount());
    assertEquals(buildExceptionMessage("", hostName), 1, deviceGetAfter.getCloudToDeviceMessageCount());
    registryManager.close();
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) InetSocketAddress(java.net.InetSocketAddress) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) SSLContext(javax.net.ssl.SSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)

Example 2 with TestDeviceIdentity

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity in project azure-iot-sdk-java by Azure.

the class SendMessagesTests method tokenExpiredAfterOpenButBeforeSendHttp.

@Test
@ContinuousIntegrationTest
public void tokenExpiredAfterOpenButBeforeSendHttp() throws Exception {
    final long SECONDS_FOR_SAS_TOKEN_TO_LIVE = 3;
    final long MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE = 5000;
    if (testInstance.protocol != HTTPS || testInstance.authenticationType != SAS || testInstance.useHttpProxy) {
        // as long as token did not expire before open. X509 doesn't apply either
        return;
    }
    this.testInstance.setup();
    String soonToBeExpiredSASToken = generateSasTokenForIotDevice(hostName, testInstance.identity.getDeviceId(), ((TestDeviceIdentity) testInstance.identity).getDevice().getPrimaryKey(), SECONDS_FOR_SAS_TOKEN_TO_LIVE);
    DeviceClient client = new DeviceClient(soonToBeExpiredSASToken, testInstance.protocol);
    client.open();
    // Force the SAS token to expire before sending messages
    Thread.sleep(MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE);
    IotHubServicesCommon.sendMessagesExpectingSASTokenExpiration(client, testInstance.protocol.toString(), 1, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS, testInstance.authenticationType);
    client.closeNow();
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 3 with TestDeviceIdentity

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method failedRegistrationDoesNotAffectSubsequentRegistrations.

@ContinuousIntegrationTest
@Test
public void failedRegistrationDoesNotAffectSubsequentRegistrations() throws Exception {
    testInstance.setup(0);
    testInstance.multiplexingClient.open();
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, this.testInstance.protocol, AuthenticationType.SAS, false);
    String deviceConnectionString = registryManager.getDeviceConnectionString(testDeviceIdentity.getDevice());
    String deviceNotFoundConnectionString = deviceConnectionString.replace(testDeviceIdentity.getDeviceId(), testDeviceIdentity.getDeviceId().toUpperCase());
    DeviceClient validDeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol);
    DeviceClient invalidDeviceClient = new DeviceClient(deviceNotFoundConnectionString, testInstance.protocol);
    try {
        testInstance.multiplexingClient.registerDeviceClient(invalidDeviceClient);
        fail("Expected multiplexingClient to throw since it registered a device that did not exist.");
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
    // expected throw since the deviceId in the connection string does not exist, ignore
    }
    testInstance.multiplexingClient.registerDeviceClient(validDeviceClient);
    testSendingMessageFromDeviceClient(validDeviceClient);
    testInstance.multiplexingClient.close();
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 4 with TestDeviceIdentity

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method serviceClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
    Device device = testDeviceIdentity.getDevice();
    ServiceClient serviceClient;
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    serviceClient = new ServiceClient(iotHubConnectionStringObj.getHostName(), sasCredential, testInstance.protocol);
    serviceClient.open();
    Message message = new Message(SMALL_PAYLOAD);
    serviceClient.send(device.getDeviceId(), message);
    // deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        serviceClient.send(device.getDeviceId(), message);
        fail("Expected sending cloud to device message to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IOException e) {
        // For service client, the unauthorized exception is wrapped by an IOException, so we need to unwrap it here
        if (e.getCause() instanceof IotHubUnathorizedException) {
            log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
        } else {
            throw e;
        }
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // The final c2d send should succeed since the shared access signature has been renewed
    serviceClient.send(device.getDeviceId(), message);
    serviceClient.close();
    registryManager.close();
    Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Aggregations

TestDeviceIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)4 Test (org.junit.Test)3 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)2 Device (com.microsoft.azure.sdk.iot.service.Device)2 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)2 Message (com.microsoft.azure.sdk.iot.service.Message)2 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)2 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)2 CorrelationDetailsLoggingAssert.buildExceptionMessage (tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage)2 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)2 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)2 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)2 AzureSasCredential (com.azure.core.credential.AzureSasCredential)1 IotHubSSLContext (com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext)1 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)1 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)1 ServiceClientOptions (com.microsoft.azure.sdk.iot.service.ServiceClientOptions)1 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)1 IOException (java.io.IOException)1