use of com.microsoft.azure.sdk.iot.device.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class IotHubSasTokenTest method constructorSetsExpiryTimeCorrectly.
// Tests_SRS_IOTHUBSASTOKEN_11_013: [**The token generated from DeviceClientConfig shall use correct expiry time (seconds rather than milliseconds)]
@Test
public void constructorSetsExpiryTimeCorrectly() throws URISyntaxException {
final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, "iothub.sample-iothub-hostname.net", "sample-device-ID", "sample-device-key", null);
long token_valid_secs = 100;
long expiryTimeTestErrorRange = 1;
long expiryTimeBaseInSecs = System.currentTimeMillis() / 1000L + token_valid_secs + 1L;
IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTimeBaseInSecs);
String tokenStr = token.toString();
// extract the value assigned to se.
int expiryTimeKeyIdx = tokenStr.indexOf("se=");
int expiryTimeStartIdx = expiryTimeKeyIdx + 3;
int expiryTimeEndIdx = tokenStr.indexOf("&", expiryTimeStartIdx);
if (expiryTimeEndIdx == -1) {
expiryTimeEndIdx = tokenStr.length();
}
String testExpiryTimeStr = tokenStr.substring(expiryTimeStartIdx, expiryTimeEndIdx);
long expiryTimeInSecs = Long.valueOf(testExpiryTimeStr);
assertTrue(expiryTimeBaseInSecs <= expiryTimeInSecs && expiryTimeInSecs <= (expiryTimeBaseInSecs + expiryTimeTestErrorRange));
}
Aggregations