Search in sources :

Example 16 with IotHubConnectionString

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

the class IotHubSasTokenTest method signatureSetCorrectly.

// Tests_SRS_IOTHUBSASTOKEN_11_005: [The signature shall be correctly computed and set.]
// Tests_SRS_IOTHUBSASTOKEN_11_006: [The function shall return the string representation of the SAS token.]
@Test
public void signatureSetCorrectly() throws URISyntaxException {
    final long expiryTime = 100;
    final String signature = "sample-sig";
    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);
    new NonStrictExpectations() {

        {
            mockSig.toString();
            result = signature;
        }
    };
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // extract the value assigned to sig.
    int signatureKeyIdx = tokenStr.indexOf("sig=");
    int signatureStartIdx = signatureKeyIdx + 4;
    int signatureEndIdx = tokenStr.indexOf("&", signatureStartIdx);
    if (signatureEndIdx == -1) {
        signatureEndIdx = tokenStr.length();
    }
    String testSignature = tokenStr.substring(signatureStartIdx, signatureEndIdx);
    final String expectedSignature = signature;
    assertThat(testSignature, is(expectedSignature));
}
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) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 17 with IotHubConnectionString

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

the class IotHubSasTokenTest method sasTokenHasCorrectFormat.

// 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
public void sasTokenHasCorrectFormat() throws URISyntaxException {
    final long expiryTime = 100;
    final String signature = "sample-sig";
    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);
    new NonStrictExpectations() {

        {
            mockSig.toString();
            result = signature;
        }
    };
    IotHubSasToken token = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // assert that sig, se and sr exist in the token in any order.
    assertThat(tokenStr.indexOf("SharedAccessSignature "), is(not(-1)));
    assertThat(tokenStr.indexOf("sig="), is(not(-1)));
    assertThat(tokenStr.indexOf("se="), is(not(-1)));
    assertThat(tokenStr.indexOf("sr="), is(not(-1)));
}
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) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 18 with IotHubConnectionString

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

the class DeviceClientConfigTest method constructorWithConnectionStringTokenSucceed.

// Tests_SRS_DEVICECLIENTCONFIG_21_033: [The constructor shall save the IoT Hub hostname, hubname,
// device ID, device key, and device token, provided in the `iotHubConnectionString`.]
@Test
public void constructorWithConnectionStringTokenSucceed() throws URISyntaxException {
    // arrange
    final String iotHubname = "iotHubName";
    final String iotHubHostname = iotHubname + ".iothubhostname";
    final String deviceId = "test-deviceid";
    final String sharedAccessToken = "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 }, "HostName=" + iotHubHostname + ";CredentialType=SharedAccessKey;CredentialScope=Device;" + "DeviceId=" + deviceId + ";SharedAccessSignature=" + sharedAccessToken + ";");
    // act
    DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
    // assert
    assertNotNull(config);
    IotHubConnectionString actualConnectionString = Deencapsulation.getField(config, "iotHubConnectionString");
    assertEquals(iotHubHostname, actualConnectionString.getHostName());
    assertEquals(iotHubname, actualConnectionString.getHubName());
    assertEquals(deviceId, actualConnectionString.getDeviceId());
    assertEquals(sharedAccessToken, actualConnectionString.getSharedAccessToken());
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 19 with IotHubConnectionString

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

the class DeviceClientConfigTest method getandsetMessageValidSecsReturnsConstant.

// Tests_SRS_DEVICECLIENTCONFIG_25_008: [The function shall set the value of tokenValidSecs.]
@Test
public void getandsetMessageValidSecsReturnsConstant() throws URISyntaxException {
    final String iotHubHostname = "test.iothubhostname";
    final String deviceId = "test-deviceid";
    final String deviceKey = "test-devicekey";
    final String sharedAccessToken = null;
    final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, iotHubHostname, deviceId, deviceKey, sharedAccessToken);
    DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
    long testsetMessageValidSecs = 60;
    config.setTokenValidSecs(testsetMessageValidSecs);
    long testgetMessageValidSecs = config.getTokenValidSecs();
    final long expectedMessageValidSecs = testsetMessageValidSecs;
    assertThat(testgetMessageValidSecs, is(expectedMessageValidSecs));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Example 20 with IotHubConnectionString

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

the class DeviceClientConfigTest method getIotHubSSLContextGets.

//Tests_SRS_DEVICECLIENTCONFIG_25_032: [**The function shall return the IotHubSSLContext.**] **
@Test
public void getIotHubSSLContextGets(@Mocked final IotHubSSLContext mockedContext) throws URISyntaxException {
    final String iotHubHostname = "test.iothubhostname";
    final String deviceId = "test-deviceid";
    final String deviceKey = "test-devicekey";
    final String sharedAccessToken = null;
    final String cert = "ValidCertString";
    final IotHubConnectionString iotHubConnectionString = Deencapsulation.newInstance(IotHubConnectionString.class, new Class[] { String.class, String.class, String.class, String.class }, iotHubHostname, deviceId, deviceKey, sharedAccessToken);
    DeviceClientConfig config = new DeviceClientConfig(iotHubConnectionString);
    config.setIotHubSSLContext(mockedContext);
    IotHubSSLContext testContext = config.getIotHubSSLContext();
    assertNotNull(testContext);
    assertEquals(testContext, mockedContext);
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.device.IotHubSSLContext) DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.device.IotHubConnectionString) Test(org.junit.Test)

Aggregations

DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)31 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)31 Test (org.junit.Test)31 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)9 NonStrictExpectations (mockit.NonStrictExpectations)3 IotHubSSLContext (com.microsoft.azure.sdk.iot.device.IotHubSSLContext)2 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)1