Search in sources :

Example 1 with UnauthorizedException

use of com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).getDeviceId();
            assertTrue(registrationExceptions.containsKey(deviceId));
            assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        }
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) ArrayList(java.util.ArrayList) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 2 with UnauthorizedException

use of com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsAfterOpenThrows.

// Attempt to register a batch of devices, all with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsAfterOpenThrows() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    testInstance.multiplexingClient.open();
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).getDeviceId();
            assertTrue(registrationExceptions.containsKey(deviceId));
            assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        }
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) ArrayList(java.util.ArrayList) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 3 with UnauthorizedException

use of com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

// Before opening the multiplexed connection, register a single device with incorrect credentials. Opening the client
// should throw and the thrown exception should have details on why the open failed
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    // Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
    String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
    DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
    testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(1, registrationExceptions.size());
        String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
        assertTrue(registrationExceptions.containsKey(deviceId));
        assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 4 with UnauthorizedException

use of com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsAfterOpenThrows.

// Attempt to register a single device with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsAfterOpenThrows() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    testInstance.multiplexingClient.open();
    // Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
    String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
    DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(1, registrationExceptions.size());
        String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
        assertTrue(registrationExceptions.containsKey(deviceId));
        assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 5 with UnauthorizedException

use of com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException in project azure-iot-sdk-java by Azure.

the class FileUploadTaskTest method getFileUploadSasUriChecksHttpStatusCode.

@Test
public void getFileUploadSasUriChecksHttpStatusCode() throws IOException, IllegalArgumentException {
    // arrange
    FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
    new Expectations() {

        {
            mockHttpsTransportManager.getFileUploadSasUri((IotHubTransportMessage) any);
            result = mockResponseMessage;
            mockResponseMessage.getStatus();
            result = IotHubStatusCode.UNAUTHORIZED;
        }
    };
    // act
    try {
        fileUploadTask.getFileUploadSasUri(mockFileUploadSasUriRequest);
        fail("Test should have thrown an IOException with a nested UnauthorizedException");
    } catch (IOException e) {
        assertNotNull(e.getCause());
        assertTrue(e.getCause() instanceof UnauthorizedException);
    }
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)5 Test (org.junit.Test)5 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)4 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)4 ArrayList (java.util.ArrayList)2 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)1 IOException (java.io.IOException)1 Expectations (mockit.Expectations)1 NonStrictExpectations (mockit.NonStrictExpectations)1