Search in sources :

Example 6 with ProvisioningConnectionString

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

the class ProvisioningConnectionStringBuilderTest method createConnectionStringWithPolicyTokenSucceeded.

/* Tests_SRS_PROVISIONINGCONNECTIONSTRING_BUILDER_21_005: [The function shall create a new ProvisioningConnectionString object using the given hostname and authenticationMethod.] */
@Test
public void createConnectionStringWithPolicyTokenSucceeded() throws Exception {
    // arrange
    AuthenticationMethod auth = Deencapsulation.newInstance("com.microsoft.azure.sdk.iot.provisioning.service.auth.ServiceAuthenticationWithSharedAccessPolicyToken", "myPolicy", "<token>");
    // act
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString("hostname", auth);
    // assert
    String connString = provisioningConnectionString.toString();
    assertEquals("Connection string mismatch!", "HostName=hostname;SharedAccessKeyName=myPolicy;SharedAccessKey=null;SharedAccessSignature=<token>", connString);
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) AuthenticationMethod(com.microsoft.azure.sdk.iot.provisioning.service.auth.AuthenticationMethod) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Example 7 with ProvisioningConnectionString

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

the class ProvisioningConnectionStringBuilderTest method validateFormatIfSpecifiedSucceeded.

/* Tests_SRS_PROVISIONINGCONNECTIONSTRING_BUILDER_21_020: [The function shall validate the property value against the given regex if the value is not null or empty.] */
@Test
public void validateFormatIfSpecifiedSucceeded() throws Exception {
    // arrange
    String regex = "[a-zA-Z0-9_\\-\\.]+$";
    String deviceProvisioningServiceName = "b.c.d";
    String hostName = "HOSTNAME." + deviceProvisioningServiceName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    // assert
    new Expectations() {

        {
            Deencapsulation.invoke(provisioningConnectionString, "validateFormat", anyString, HOST_NAME_PROPERTY_NAME, anyString);
        }
    };
    // act
    Deencapsulation.invoke(provisioningConnectionString, "validateFormatIfSpecified", hostName, "hostName", regex);
}
Also used : Expectations(mockit.Expectations) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Example 8 with ProvisioningConnectionString

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

the class ProvisioningConnectionStringBuilderTest method setAuthenticationMethodWithKeySucceeded.

/* Tests_SRS_PROVISIONINGCONNECTIONSTRING_BUILDER_21_023: [The function shall populate and set the authenticationMethod on the given target provisioningConnectionString object.] */
@Test
public void setAuthenticationMethodWithKeySucceeded() throws Exception {
    // arrange
    String regex = "[a-zA-Z0-9_\\-\\.]+$";
    String deviceProvisioningServiceName = "b.c.d";
    String hostName = "HOSTNAME." + deviceProvisioningServiceName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    String newPolicyName = "XXX";
    String newPolicyKey = "YYY";
    AuthenticationMethod auth = Deencapsulation.newInstance("com.microsoft.azure.sdk.iot.provisioning.service.auth.ServiceAuthenticationWithSharedAccessPolicyKey", newPolicyName, newPolicyKey);
    new Expectations() {

        {
            Deencapsulation.invoke(provisioningConnectionString, "validateFormat", hostName, HOST_NAME_PROPERTY_NAME, regex);
        }
    };
    // act
    Deencapsulation.invoke(provisioningConnectionString, "setAuthenticationMethod", auth, provisioningConnectionString);
    // assert
    assertEquals(newPolicyName, provisioningConnectionString.getSharedAccessKeyName());
    assertEquals(newPolicyKey, provisioningConnectionString.getSharedAccessKey());
    assertNull(provisioningConnectionString.getSharedAccessSignature());
}
Also used : Expectations(mockit.Expectations) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) AuthenticationMethod(com.microsoft.azure.sdk.iot.provisioning.service.auth.AuthenticationMethod) Test(org.junit.Test)

Example 9 with ProvisioningConnectionString

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

the class ProvisioningConnectionStringBuilderTest method parserDeviceProvisioningServiceNameWithOneSeparatorSucceeded.

/* Tests_SRS_PROVISIONINGCONNECTIONSTRING_BUILDER_21_013: [The function shall return the substring of the host name until the first `.` character.] */
@Test
public void parserDeviceProvisioningServiceNameWithOneSeparatorSucceeded() throws Exception {
    // arrange
    String deviceProvisioningServiceName = "PROVISIONINGNAME";
    String iotHubHostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + deviceProvisioningServiceName + "." + iotHubHostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String userString = sharedAccessKeyName + "@SAS.root." + deviceProvisioningServiceName;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    // act
    String actual = Deencapsulation.invoke(provisioningConnectionString, "parseDeviceProvisioningServiceName", provisioningConnectionString);
    // assert
    assertEquals("Parser error: DeviceProvisioningServiceName mismatch!", deviceProvisioningServiceName, actual);
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Example 10 with ProvisioningConnectionString

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

the class ProvisioningConnectionStringBuilderTest method parserThrowsOnNullProvisioningConnectionString.

/* Tests_SRS_PROVISIONINGCONNECTIONSTRING_BUILDER_21_007: [The function shall throw IllegalArgumentException if the input target itoHubConnectionString is null.] */
@Test(expected = IllegalArgumentException.class)
public void parserThrowsOnNullProvisioningConnectionString() throws Exception {
    // arrange
    String deviceProvisioningServiceName = "PROVISIONINGNAME";
    String iotHubHostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + deviceProvisioningServiceName + "." + iotHubHostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
    // act
    Deencapsulation.invoke(provisioningConnectionString, "parse", connectionString, ProvisioningConnectionString.class);
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Aggregations

ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)48 Test (org.junit.Test)47 Expectations (mockit.Expectations)7 AuthenticationMethod (com.microsoft.azure.sdk.iot.provisioning.service.auth.AuthenticationMethod)5 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)4 Verifications (mockit.Verifications)4 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)3 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)1 URLEncoder (java.net.URLEncoder)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1