Search in sources :

Example 6 with IotHubSasToken

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

the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFails.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [If an MQTT connection is unable to be established for any reason,
// the function shall throw an IOException.]
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFails() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            new IotHubSasToken(mockConfig, anyLong);
            result = mockToken;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            result = new IOException(anyString);
        }
    };
    try {
        MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
        connection.open();
    } catch (Exception e) {
        new Verifications() {

            {
            }
        };
        throw e;
    }
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with IotHubSasToken

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

the class HttpsIotHubConnection method receiveMessage.

/**
     * Receives a message, if one exists.
     *
     * @return a message, or null if none exists.
     *
     * @throws IOException if the IoT Hub could not be reached.
     */
public Message receiveMessage() throws IOException {
    synchronized (HTTPS_CONNECTION_LOCK) {
        String iotHubHostname = this.config.getIotHubHostname();
        String deviceId = this.config.getDeviceId();
        int readTimeoutMillis = this.config.getReadTimeoutMillis();
        int messageLockTimeoutSecs = this.config.getMessageLockTimeoutSecs();
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_013: [The function shall send a request to the URL 'https://[iotHubHostname]/devices/[deviceId]/messages/devicebound?api-version=2016-02-03'.]
        IotHubMessageUri messageUri = new IotHubMessageUri(iotHubHostname, deviceId);
        URL messageUrl = new URL("https://" + messageUri.toString());
        IotHubSasToken sasToken = new IotHubSasToken(this.config, System.currentTimeMillis() / 1000L + this.config.getTokenValidSecs() + 1L);
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_014: [The function shall send a GET request.]
        HttpsRequest request = new HttpsRequest(messageUrl, HttpsMethod.GET, new byte[0]).setReadTimeoutMillis(readTimeoutMillis).setHeaderField("authorization", sasToken.toString()).setHeaderField("iothub-to", messageUri.getPath()).setHeaderField("iothub-messagelocktimeout", Integer.toString(messageLockTimeoutSecs));
        //Codes_SRS_HTTPSIOTHUBCONNECTION_25_041: [The function shall set the IotHub SSL context by calling setSSLContext on the request.]
        request.setSSLContext(this.config.getIotHubSSLContext());
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_023: [If the IoT Hub could not be reached, the function shall throw an IOException.]
        HttpsResponse response = request.send();
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_021: [If a response with IoT Hub status code OK is not received, the function shall return null.]
        Message msg = null;
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_019: [If a response with IoT Hub status code OK is received, the function shall return the IoT Hub message included in the response.]
        IotHubStatusCode messageStatus = IotHubStatusCode.getIotHubStatusCode(response.getStatus());
        if (messageStatus == IotHubStatusCode.OK) {
            // Codes_SRS_HTTPSIOTHUBCONNECTION_11_020: [If a response with IoT Hub status code OK is received, the function shall save the response header field 'etag'.]
            this.messageEtag = sanitizeEtag(response.getHeaderField("etag"));
            HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(response);
            msg = httpsMsg.toMessage();
        }
        return msg;
    }
}
Also used : IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) URL(java.net.URL)

Example 8 with IotHubSasToken

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

the class IotHubSasTokenTest method expiryTimeSetCorrectly.

// Tests_SRS_IOTHUBSASTOKEN_11_002: [The expiry time shall be the given expiry time, where it is a UNIX timestamp and indicates the time after which the token becomes invalid.]
@Test
public void expiryTimeSetCorrectly() 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 = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), expiryTime);
    String tokenStr = token.toString();
    // extract the value assigned to se.
    int expiryTimeKeyIdx = tokenStr.indexOf("se=");
    int expiryTimeStartIdx = expiryTimeKeyIdx + 3;
    int expiryTimeEndIdx = tokenStr.indexOf("&", expiryTimeStartIdx);
    if (expiryTimeEndIdx == -1) {
        expiryTimeEndIdx = tokenStr.length();
    }
    String testExpiryTimeStr = tokenStr.substring(expiryTimeStartIdx, expiryTimeEndIdx);
    String expectedExpiryTimeStr = Long.toString(expiryTime);
    assertThat(testExpiryTimeStr, is(expectedExpiryTimeStr));
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) 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)

Example 9 with IotHubSasToken

use of com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken 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=S3%2flPidfBF48B7%2fOFAxMOYH8rpOneq68nu61D%2fBP6fo%3d&se=1469813873";
    ;
    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 = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
    String tokenStr = token.toString();
    assertTrue(tokenStr != sastoken);
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) 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 10 with IotHubSasToken

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

the class IotHubSasTokenTest method doesNotSetSASTokenWithoutSe.

// 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 doesNotSetSASTokenWithoutSe() throws URISyntaxException {
    String sastoken = "SharedAccessSignature sr=sample-iothub-hostname.net%2fdevices%2fsample-device-ID&sig=S3%2flPidfBF48B7%2fOFAxMOYH8rpOneq68nu61D%2fBP6fo%3d";
    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 = new IotHubSasToken(new DeviceClientConfig(iotHubConnectionString), 0);
    String tokenStr = token.toString();
    assertTrue(tokenStr != sastoken);
}
Also used : DeviceClientConfig(com.microsoft.azure.sdk.iot.device.DeviceClientConfig) 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)

Aggregations

IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)19 Test (org.junit.Test)15 DeviceClientConfig (com.microsoft.azure.sdk.iot.device.DeviceClientConfig)9 IotHubConnectionString (com.microsoft.azure.sdk.iot.device.IotHubConnectionString)9 NonStrictExpectations (mockit.NonStrictExpectations)6 IOException (java.io.IOException)5 AmqpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection)3 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)3 URL (java.net.URL)3 Verifications (mockit.Verifications)3 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)2 IotHubReactor (com.microsoft.azure.sdk.iot.device.transport.amqps.IotHubReactor)1 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)1