Search in sources :

Example 31 with IotHubConnectionString

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

the class IotHubServiceSasTokenTest method constructor_buil_token_failed.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_007: [The constructor shall throw Exception if building the token failed]
// Assert
@Test(expected = Exception.class)
public void constructor_buil_token_failed() throws Exception {
    // Arrange
    String iotHubName = "b.c.d";
    String hostName = "HOSTNAME." + iotHubName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String expectedToken = "SharedAccessSignature sr=hostname.b.c.d&sig=M%2FT5oCM8WWs%2B%2FMv7okAVmfrzVM%2FGUyA7EIp%2FfKo8BeQ%3D&se=1474065852&skn=ACCESSKEYNAME";
    // Act
    IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
    Deencapsulation.setField(iotHubConnectionString, "hostName", null);
    IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 32 with IotHubConnectionString

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

the class IotHubServiceSasTokenTest method constructor_good_case_format_check.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string (one year)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_006: [The constructor shall concatenate the target uri, the signature, the expiry time and the key name using the format: "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s"]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_007: [The function shall return with the generated token]
@Test
public void constructor_good_case_format_check() throws Exception {
    // Arrange
    String iotHubName = "b.c.d";
    String hostName = "HOSTNAME." + iotHubName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String expectedToken = "SharedAccessSignature sr=hostname.b.c.d&sig=M%2FT5oCM8WWs%2B%2FMv7okAVmfrzVM%2FGUyA7EIp%2FfKo8BeQ%3D&se=1474065852&skn=ACCESSKEYNAME";
    IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
    // Act
    IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
    String token = iotHubServiceSasToken.toString();
    // Assert
    assertEquals(token.contains("SharedAccessSignature sr=hostname.b.c.d&sig="), true);
    assertEquals(token.contains("&se="), true);
    assertEquals(token.contains("&skn=ACCESSKEYNAME"), true);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 33 with IotHubConnectionString

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

the class IotHubServiceSasTokenTest method constructor_input_null.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_001: [The constructor shall throw IllegalArgumentException if the input object is null]
// Assert
@Test(expected = IllegalArgumentException.class)
public void constructor_input_null() {
    // Arrange
    IotHubConnectionString iotHubConnectionString = null;
    // Act
    IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 34 with IotHubConnectionString

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

the class IotHubServiceSasTokenTest method constructor_good_case_flow_check.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_006: [The constructor shall concatenate the target uri, the signature, the expiry time and the key name using the format: "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s"]
@Test
public void constructor_good_case_flow_check() throws Exception {
    // Arrange
    String cryptoProvider = "HmacSHA256";
    String charset = "UTF-8";
    String iotHubName = "b.c.d";
    String hostName = "HOSTNAME." + iotHubName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String expectedToken = "SharedAccessSignature sr=hostname.b.c.d&sig=M%2FT5oCM8WWs%2B%2FMv7okAVmfrzVM%2FGUyA7EIp%2FfKo8BeQ%3D&se=1474065852&skn=ACCESSKEYNAME";
    IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
    // Assert
    new Expectations() {

        URLEncoder urlEncoder;

        Base64 base64;

        System system;

        SecretKeySpec secretKeySpec;

        Mac mac;

        {
            urlEncoder.encode(hostName.toLowerCase(), String.valueOf(StandardCharsets.UTF_8));
            system.currentTimeMillis();
            Base64.decodeBase64(sharedAccessKey.getBytes(charset));
            byte[] body = { 1 };
            secretKeySpec = new SecretKeySpec(body, cryptoProvider);
            mac.getInstance(cryptoProvider);
        }
    };
    // Act
    IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
}
Also used : Expectations(mockit.Expectations) Base64(org.apache.commons.codec.binary.Base64) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URLEncoder(java.net.URLEncoder) Mac(javax.crypto.Mac) Test(org.junit.Test)

Example 35 with IotHubConnectionString

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

the class DeviceOperationsTest method request_nullUrl_failed.

/* Tests_SRS_DEVICE_OPERATIONS_21_002: [The request shall throw IllegalArgumentException if the provided `url` is null.] */
@Test(expected = IllegalArgumentException.class)
public void request_nullUrl_failed() throws Exception {
    //arrange
    final IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(STANDARD_CONNECTIONSTRING);
    //act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, null, HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
//assert
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Aggregations

IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)76 Test (org.junit.Test)75 Expectations (mockit.Expectations)17 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)12 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)12 ServiceAuthenticationWithSharedAccessPolicyKey (com.microsoft.azure.sdk.iot.service.ServiceAuthenticationWithSharedAccessPolicyKey)6 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)6 URL (java.net.URL)6 ServiceAuthenticationWithSharedAccessPolicyToken (com.microsoft.azure.sdk.iot.service.ServiceAuthenticationWithSharedAccessPolicyToken)4 AuthenticationMethod (com.microsoft.azure.sdk.iot.service.AuthenticationMethod)3 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)3 IOException (java.io.IOException)2 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)1 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)1 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)1 URLEncoder (java.net.URLEncoder)1