Search in sources :

Example 16 with IotHubSasToken

use of com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken in project azure-iot-sdk-java by Azure.

the class IotHubSasTokenTest method doesNotSetInvalidSASToken.

// Tests_SRS_IOTHUBSASTOKEN_25_008: [**The required format for the SAS Token shall be verified and IllegalArgumentException is thrown if unmatched.**]**
//Tests_SRS_IOTHUBSASTOKEN_11_001: [**The SAS token shall have the format `SharedAccessSignature sig=<signature >&se=<expiryTime>&sr=<resourceURI>`. The params can be in any order.**]**
@Test(expected = IllegalArgumentException.class)
public void doesNotSetInvalidSASToken() throws URISyntaxException {
    String sastoken = "SharedAccessSignature sr =sample-iothub-hostname.net%2fdevices%2fsample-device-ID&sig =S3%2flPidfBF48B7%2fOFAxMOYH8rpOneq68nu61D%2fBP6fo%3d&se =1469813873";
    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", null, sastoken);
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
    String tokenStr = token.toString();
    assertTrue(tokenStr != sastoken);
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 17 with IotHubSasToken

use of com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken in project azure-iot-sdk-java by Azure.

the class IotHubSasTokenTest method setValidSASTokenCorrectly.

// Tests_SRS_IOTHUBSASTOKEN_25_007: [**If device key is not provided in config then the SASToken from config shall be used.**]**
@Test
public void setValidSASTokenCorrectly() throws URISyntaxException {
    String sastoken = "SharedAccessSignature sr=sample-iothub-hostname.net%2fdevices%2fsample-device-ID&sig=S3%2flPidfBF48B7%2fOFAxMOYH8rpOneq68nu61D%2fBP6fo%3d&se=1469813873";
    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", null, sastoken);
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
    String tokenStr = token.toString();
    assertTrue(tokenStr == sastoken);
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 18 with IotHubSasToken

use of com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken in project azure-iot-sdk-java by Azure.

the class IotHubSasTokenTest method doesNotSetSASTokenWithoutSig.

// Tests_SRS_IOTHUBSASTOKEN_25_008: [**The required format for the SAS Token shall be verified and IllegalArgumentException is thrown if unmatched.**]**
//Tests_SRS_IOTHUBSASTOKEN_11_001: [**The SAS token shall have the format `SharedAccessSignature sig=<signature >&se=<expiryTime>&sr=<resourceURI>`. The params can be in any order.**]**
@Test(expected = IllegalArgumentException.class)
public void doesNotSetSASTokenWithoutSig() throws URISyntaxException {
    String sastoken = "SharedAccessSignature sr=sample-iothub-hostname.net%2fdevices%2fsample-device-ID&se=1469813873";
    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", null, sastoken);
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
    String tokenStr = token.toString();
    assertTrue(tokenStr == sastoken);
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 19 with IotHubSasToken

use of com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken 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));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Aggregations

IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)19 Test (org.junit.Test)15 DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)9 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)9 NonStrictExpectations (mockit.NonStrictExpectations)6 IOException (java.io.IOException)5 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)3 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 URL (java.net.URL)3 Verifications (mockit.Verifications)3 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)2 IotHubReactor (com.microsoft.azure.sdk.iot.device.transport.amqps.IotHubReactor)1 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)1