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