Search in sources :

Example 46 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=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 47 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=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 48 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 = 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)

Example 49 with IotHubConnectionString

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

the class IotHubConnectionStringTest method sharedAccessTokenSetterWorks.

// Tests_SRS_IOTHUB_CONNECTIONSTRING_34_038: [This function shall set the value of this object's shared access token to the provided value.]
@Test
public void sharedAccessTokenSetterWorks() throws ClassNotFoundException {
    // arrange
    String expectedToken = "new token";
    final String connString = "HostName=" + VALID_HOSTNAME + ";CredentialType=SharedAccessKey;CredentialScope=Device;" + "DeviceId=" + VALID_DEVICEID + ";SharedAccessSignature=" + VALID_SHARED_ACCESS_TOKEN + ";";
    Object iotHubConnectionString = Deencapsulation.newInstance(Class.forName(IOTHUB_CONNECTION_STRING_CLASS), new Class[] { String.class }, connString);
    // act
    Deencapsulation.invoke(iotHubConnectionString, "setSharedAccessToken", expectedToken);
    // assert
    String actualToken = Deencapsulation.getField(iotHubConnectionString, "sharedAccessToken");
    assertEquals(expectedToken, actualToken);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 50 with IotHubConnectionString

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

the class IotHubConnectionStringTest method assertConnectionString.

// Since this is a helper method, the expected assertion param can be passed any value.
@SuppressWarnings("SameParameterValue")
private void assertConnectionString(Object iotHubConnectionString, String expectedHostName, String expectedDeviceId, String expectedSharedAccessKey, String expectedSharedAccessToken) {
    assertNotNull(iotHubConnectionString);
    String hostName = Deencapsulation.getField(iotHubConnectionString, "hostName");
    String hubName = Deencapsulation.getField(iotHubConnectionString, "hubName");
    String deviceId = Deencapsulation.getField(iotHubConnectionString, "deviceId");
    String sharedAccessKey = Deencapsulation.getField(iotHubConnectionString, "sharedAccessKey");
    String sharedAccessToken = Deencapsulation.getField(iotHubConnectionString, "sharedAccessToken");
    int iotHubNameEndIdx = expectedHostName.indexOf(".");
    String expectedHubName = expectedHostName.substring(0, iotHubNameEndIdx);
    assertEquals(expectedHostName, hostName);
    assertEquals(expectedHubName, hubName);
    assertEquals(expectedDeviceId, deviceId);
    assertEquals(expectedSharedAccessKey, sharedAccessKey);
    assertEquals(expectedSharedAccessToken, sharedAccessToken);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString)

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