Search in sources :

Example 56 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.device.IotHubConnectionString 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=srValue&se=" + Long.MAX_VALUE;
    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 = Deencapsulation.newInstance(IotHubSasToken.class, new Class[] { String.class, String.class, String.class, String.class, long.class }, iotHubConnectionString.getHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getSharedAccessKey(), iotHubConnectionString.getSharedAccessToken(), 0);
}
Also used : 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 57 with IotHubConnectionString

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

the class IotHubSasTokenTest method expiredTokenCausesSecurityException.

// Tests_SRS_IOTHUBSASTOKEN_34_009: [**The SAS Token shall be checked to see if it has expired and a SecurityException will be thrown if it is expired.**]**
@Test(expected = SecurityException.class)
public void expiredTokenCausesSecurityException(@Mocked final IotHubConnectionString mockIotHubConnectionString) throws SecurityException {
    // This expiryTime does not expire for a few billion years
    Long expiredExpiryTime = 0L;
    final String sasTokenExpired = "SharedAccessSignature sr=srValue&sig=sigValue&se=" + expiredExpiryTime;
    new NonStrictExpectations() {

        {
            mockIotHubConnectionString.getSharedAccessToken();
            result = sasTokenExpired;
        }
    };
    IotHubSasToken token = Deencapsulation.newInstance(IotHubSasToken.class, new Class[] { String.class, String.class, String.class, String.class, String.class, long.class }, mockIotHubConnectionString.getHostName(), mockIotHubConnectionString.getDeviceId(), mockIotHubConnectionString.getSharedAccessKey(), mockIotHubConnectionString.getSharedAccessToken(), mockIotHubConnectionString.getModuleId(), 0);
}
Also used : IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 58 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.device.IotHubConnectionString 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=blah&sig=blah&se=" + Long.MAX_VALUE;
    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 = Deencapsulation.newInstance(IotHubSasToken.class, new Class[] { String.class, String.class, String.class, String.class, String.class, long.class }, iotHubConnectionString.getHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getSharedAccessKey(), iotHubConnectionString.getSharedAccessToken(), iotHubConnectionString.getModuleId(), 0);
    String tokenStr = token.toString();
    assertEquals(tokenStr, sastoken);
}
Also used : 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 59 with IotHubConnectionString

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

the class IotHubSasTokenTest method emptyExpiryTimeFieldValueThrowsIllegalArgumentException.

@Test(expected = IllegalArgumentException.class)
public void emptyExpiryTimeFieldValueThrowsIllegalArgumentException() {
    String expiryTime = "";
    String sastoken = "SharedAccessSignature sr=srValue&sig=sigValue&se=" + expiryTime;
    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 = Deencapsulation.newInstance(IotHubSasToken.class, new Class[] { String.class, String.class, String.class, String.class, long.class }, iotHubConnectionString.getHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getSharedAccessKey(), iotHubConnectionString.getSharedAccessToken(), 0);
}
Also used : 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 60 with IotHubConnectionString

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 = Deencapsulation.newInstance(IotHubSasToken.class, new Class[] { String.class, String.class, String.class, String.class, String.class, long.class }, iotHubConnectionString.getHostName(), iotHubConnectionString.getDeviceId(), iotHubConnectionString.getSharedAccessKey(), iotHubConnectionString.getSharedAccessToken(), iotHubConnectionString.getModuleId(), 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 : 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

IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)64 Test (org.junit.Test)63 DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)31 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)27 NonStrictExpectations (mockit.NonStrictExpectations)9 IotHubSSLContext (com.microsoft.azure.sdk.iot.device.IotHubSSLContext)2 Message (com.microsoft.azure.sdk.iot.device.Message)1 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)1 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)1 Date (java.util.Date)1