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
}
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
}
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);
}
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"));
}
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);
}
Aggregations