Search in sources :

Example 1 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestThrowsOnSendHttpRequestFailed.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_015: [If the HttpRequest failed send the message, the request shall throw ProvisioningServiceClientTransportException, threw by the callee.*/
@Test(expected = ProvisioningServiceClientException.class)
public void requestThrowsOnSendHttpRequestFailed() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = mockedURL;
            new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
            result = mockedHttpRequest;
            mockedHttpRequest.send();
            result = new ProvisioningServiceClientException();
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) ProvisioningServiceClientException(com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException) Test(org.junit.Test)

Example 2 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken in project azure-iot-sdk-java by Azure.

the class ContractApiHttpTest method requestThrowsOnWrongPath.

/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_009: [If the provided path contains not valid characters, the request shall throw IllegalArgumentException.*/
@Test(expected = IllegalArgumentException.class)
public void requestThrowsOnWrongPath() throws ProvisioningServiceClientException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new ProvisioningSasToken(mockedProvisioningConnectionString);
            result = mockedProvisioningSasToken;
            mockedProvisioningSasToken.toString();
            result = VALID_SASTOKEN;
            mockedProvisioningConnectionString.getHostName();
            result = VALID_HOST_NAME;
            new URL((String) any);
            result = new MalformedURLException();
        }
    };
    ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
    // act
    contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
// assert
}
Also used : MalformedURLException(java.net.MalformedURLException) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) ContractApiHttp(com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp) URL(java.net.URL) Test(org.junit.Test)

Example 3 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken in project azure-iot-sdk-java by Azure.

the class ProvisioningServiceSasTokenTest method constructorThrowsOnNullConnectionString.

// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_001: [The constructor shall throw IllegalArgumentException if the input object is null]
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnNullConnectionString() throws IllegalArgumentException {
    // Arrange
    ProvisioningConnectionString provisioningConnectionString = null;
    // Act
    ProvisioningSasToken provisioningServiceSasToken = new ProvisioningSasToken(provisioningConnectionString);
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) Test(org.junit.Test)

Example 4 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken in project azure-iot-sdk-java by Azure.

the class ProvisioningServiceSasTokenTest method constructorCheckFormatSucceeded.

// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string (one year)]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_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_PROVISIONING_SERVICE_SASTOKEN_12_008: [The function shall return with the generated token]
@Test
public void constructorCheckFormatSucceeded() throws Exception {
    // Arrange
    String deviceProvisioningServiceName = "b.c.d";
    String hostName = "HOSTNAME." + deviceProvisioningServiceName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = encodeBase64String("key".getBytes(StandardCharsets.UTF_8));
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    // Act
    ProvisioningSasToken provisioningServiceSasToken = new ProvisioningSasToken(provisioningConnectionString);
    String token = provisioningServiceSasToken.toString();
    // Assert
    assertTrue(token.contains("SharedAccessSignature sr=hostname.b.c.d&sig="));
    assertTrue(token.contains("&se="));
    assertTrue(token.contains("&skn=ACCESSKEYNAME"));
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Example 5 with ProvisioningSasToken

use of com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken in project azure-iot-sdk-java by Azure.

the class ProvisioningServiceSasTokenTest method constructorSucceeded.

// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_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 constructorSucceeded() throws Exception {
    // Arrange
    String cryptoProvider = "HmacSHA256";
    String charset = "UTF-8";
    String deviceProvisioningServiceName = "b.c.d";
    String hostName = "HOSTNAME." + deviceProvisioningServiceName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = encodeBase64String("key".getBytes(StandardCharsets.UTF_8));
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    // Assert
    new Expectations() {

        URLEncoder urlEncoder;

        System system;

        final SecretKeySpec secretKeySpec;

        Mac mac;

        {
            URLEncoder.encode(hostName.toLowerCase(), String.valueOf(StandardCharsets.UTF_8));
            System.currentTimeMillis();
            decodeBase64(sharedAccessKey.getBytes(charset));
            // Semmle flags this as sensitive call, but it is a false positive since it is for test purposes
            // lgtm
            byte[] body = { 1 };
            secretKeySpec = new SecretKeySpec(body, cryptoProvider);
            Mac.getInstance(cryptoProvider);
        }
    };
    // Act
    ProvisioningSasToken provisioningServiceSasToken = new ProvisioningSasToken(provisioningConnectionString);
}
Also used : Expectations(mockit.Expectations) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) URLEncoder(java.net.URLEncoder) Mac(javax.crypto.Mac) Test(org.junit.Test)

Aggregations

ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)14 Test (org.junit.Test)13 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)9 URL (java.net.URL)7 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)6 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)5 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)3 IOException (java.io.IOException)2 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)1 ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)1 ProvisioningServiceClientTransportException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException)1 MalformedURLException (java.net.MalformedURLException)1 URLEncoder (java.net.URLEncoder)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 Expectations (mockit.Expectations)1