Search in sources :

Example 61 with IotHubConnectionString

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

the class IotHubSasTokenTest method doesNotSetSASTokenWithoutSr.

// 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 doesNotSetSASTokenWithoutSr() throws URISyntaxException {
    String sastoken = "SharedAccessSignature 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, 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 62 with IotHubConnectionString

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

the class IotHubSasTokenTest method setValidSASTokenCorrectlyDespiteFurtherReorganizedSignature.

// 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 setValidSASTokenCorrectlyDespiteFurtherReorganizedSignature() throws URISyntaxException {
    // Typical order of fields in SAS token is sr=<>&sig=<>&se=<>, however these should be able to be reorganized freely
    // This test tries se=<>&sig=<>&sr=<>
    String sastoken = "SharedAccessSignature se=" + Long.MAX_VALUE + "&sig=sigValue&sr=srValue";
    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 63 with IotHubConnectionString

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

the class IotHubSasTokenTest method emptyResourceURIFieldValueThrowsIllegalArgumentException.

@Test(expected = IllegalArgumentException.class)
public void emptyResourceURIFieldValueThrowsIllegalArgumentException() {
    Long expiryTime = Long.MAX_VALUE;
    String sastoken = "SharedAccessSignature sr=&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 64 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 = 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(), 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 : 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)

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