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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations