From 96f64f553376646c146996e764e55e5d9684e043 Mon Sep 17 00:00:00 2001 From: Dane Walton Date: Fri, 8 Nov 2019 15:19:15 -0800 Subject: [PATCH] fix #1288: correlation id in body for upload to blob --- .../src/iothub_client_ll_uploadtoblob.c | 25 +++++++++++++------ .../iothub_client_ll_u2b_ut.c | 7 ++++-- .../iothubclient_uploadtoblob_e2e.c | 4 +-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/iothub_client/src/iothub_client_ll_uploadtoblob.c b/iothub_client/src/iothub_client_ll_uploadtoblob.c index 7589a93840..25e1515666 100644 --- a/iothub_client/src/iothub_client_ll_uploadtoblob.c +++ b/iothub_client/src/iothub_client_ll_uploadtoblob.c @@ -42,8 +42,8 @@ int snprintf(char * s, size_t n, const char * format, ...) #endif /*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadToBlob shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/ -#define FILE_UPLOAD_FAILED_BODY "{ \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"client not able to connect with the server\" }" -#define FILE_UPLOAD_ABORTED_BODY "{ \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"file upload aborted\" }" +#define FILE_UPLOAD_FAILED_BODY "{ \"correlationId\":\"%s\", \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"client not able to connect with the server\" }" +#define FILE_UPLOAD_ABORTED_BODY "{ \"correlationId\":\"%s\", \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"file upload aborted\" }" #define INDEFINITE_TIME ((time_t)-1) static const char* const EMPTY_STRING = ""; @@ -546,7 +546,8 @@ static int IoTHubClient_LL_UploadToBlob_step3(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HAND /*this POST "tries" to happen*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadMultipleBlocksToBlob(Ex) shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/ - STRING_HANDLE relativePathNotification = STRING_construct_sprintf("/devices/%s/files/notifications/%s%s", upload_data->deviceId, STRING_c_str(correlationId), API_VERSION); + STRING_HANDLE relativePathNotification = STRING_construct_sprintf("/devices/%s/files/notifications/%s", upload_data->deviceId, API_VERSION); + (void)correlationId; if (relativePathNotification == NULL) { result = MU_FAILURE; @@ -788,7 +789,10 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE /*Codes_SRS_IOTHUBCLIENT_LL_99_008: [ If step 2 is aborted by the client, then the HTTP message body shall look like: ]*/ LogInfo("Blob_UploadFromSasUri aborted file upload"); - if (BUFFER_build(responseToIoTHub, (const unsigned char*)FILE_UPLOAD_ABORTED_BODY, sizeof(FILE_UPLOAD_ABORTED_BODY) / sizeof(FILE_UPLOAD_ABORTED_BODY[0])) == 0) + STRING_HANDLE aborted_response; + aborted_response = STRING_construct_sprintf(FILE_UPLOAD_ABORTED_BODY, STRING_c_str(correlationId)); + size_t response_length = STRING_length(aborted_response); + if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(aborted_response), response_length) == 0) { if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0) { @@ -806,6 +810,7 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE LogError("Unable to BUFFER_build, can't perform IoTHubClient_LL_UploadToBlob_step3"); result = IOTHUB_CLIENT_ERROR; } + STRING_delete(aborted_response); } else if (uploadMultipleBlocksResult != BLOB_OK) { @@ -814,13 +819,17 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE /*do step 3*/ /*try*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_091: [ If step 2 fails without establishing an HTTP dialogue, then the HTTP message body shall look like: ]*/ - if (BUFFER_build(responseToIoTHub, (const unsigned char*)FILE_UPLOAD_FAILED_BODY, sizeof(FILE_UPLOAD_FAILED_BODY) / sizeof(FILE_UPLOAD_FAILED_BODY[0])) == 0) + STRING_HANDLE failed_response; + failed_response = STRING_construct_sprintf(FILE_UPLOAD_FAILED_BODY, STRING_c_str(correlationId)); + size_t response_length = STRING_length(failed_response); + if (BUFFER_build(responseToIoTHub, (const unsigned char*)STRING_c_str(failed_response), response_length) == 0) { if (IoTHubClient_LL_UploadToBlob_step3(upload_data, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0) { LogError("IoTHubClient_LL_UploadToBlob_step3 failed"); } } + STRING_delete(failed_response); result = IOTHUB_CLIENT_ERROR; } else @@ -830,11 +839,13 @@ IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadMultipleBlocksToBlob_Impl(IOTHUB_CLIE STRING_HANDLE req_string; if(response == NULL) { - req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":""}", ((httpResponse < 300) ? "true" : "false"), httpResponse); + req_string = STRING_construct_sprintf("{\"correlationId\":\"%s\", \"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":""}", + STRING_c_str(correlationId), ((httpResponse < 300) ? "true" : "false"), httpResponse); } else { - req_string = STRING_construct_sprintf("{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", ((httpResponse < 300) ? "true" : "false"), httpResponse, response); + req_string = STRING_construct_sprintf("{\"correlationId\":\"%s\", \"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", + STRING_c_str(correlationId), ((httpResponse < 300) ? "true" : "false"), httpResponse, response); } if (req_string == NULL) { diff --git a/iothub_client/tests/iothubclient_ll_u2b_ut/iothub_client_ll_u2b_ut.c b/iothub_client/tests/iothubclient_ll_u2b_ut/iothub_client_ll_u2b_ut.c index 7e510d942d..39b77a058f 100644 --- a/iothub_client/tests/iothubclient_ll_u2b_ut/iothub_client_ll_u2b_ut.c +++ b/iothub_client/tests/iothubclient_ll_u2b_ut/iothub_client_ll_u2b_ut.c @@ -702,7 +702,6 @@ static void setup_steps_3(IOTHUB_CREDENTIAL_TYPE cred_type) if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY) { - STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(IoTHubClient_Auth_Get_DeviceKey(TEST_AUTH_HANDLE)).CallCannotFail(); @@ -714,7 +713,6 @@ static void setup_steps_3(IOTHUB_CREDENTIAL_TYPE cred_type) } else { - STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(HTTPAPIEX_ExecuteRequest(IGNORED_PTR_ARG, HTTPAPI_REQUEST_POST, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, NULL, IGNORED_PTR_ARG)) .CopyOutArgumentBuffer_statusCode(&status_code, sizeof(status_code)); @@ -734,9 +732,13 @@ static void setup_Blob_UploadMultipleBlocksFromSasUri_mocks(IOTHUB_CREDENTIAL_TY STRICT_EXPECTED_CALL(Blob_UploadMultipleBlocksFromSasUri(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)) .CopyOutArgumentBuffer_httpStatus(&status_code, sizeof(status_code)) .SetReturn(blob_result); + STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); + STRICT_EXPECTED_CALL(STRING_length(IGNORED_PTR_ARG)); + STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(BUFFER_build(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG)); setup_steps_3(cred_type); + STRICT_EXPECTED_CALL(STRING_delete(IGNORED_PTR_ARG)); } else { @@ -752,6 +754,7 @@ static void setup_Blob_UploadMultipleBlocksFromSasUri_mocks(IOTHUB_CREDENTIAL_TY { STRICT_EXPECTED_CALL(BUFFER_u_char(IGNORED_PTR_ARG)).CallCannotFail(); } + STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(STRING_length(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(STRING_c_str(IGNORED_PTR_ARG)).CallCannotFail(); STRICT_EXPECTED_CALL(BUFFER_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG)); diff --git a/iothub_client/tests/iothubclient_uploadtoblob_e2e/iothubclient_uploadtoblob_e2e.c b/iothub_client/tests/iothubclient_uploadtoblob_e2e/iothubclient_uploadtoblob_e2e.c index 81c04f9d3e..f52ae3286b 100644 --- a/iothub_client/tests/iothubclient_uploadtoblob_e2e/iothubclient_uploadtoblob_e2e.c +++ b/iothub_client/tests/iothubclient_uploadtoblob_e2e/iothubclient_uploadtoblob_e2e.c @@ -240,13 +240,13 @@ IOTHUB_CLIENT_FILE_UPLOAD_GET_DATA_RESULT uploadToBlobGetDataEx(IOTHUB_CLIENT_FI UPLOADTOBLOB_CALLBACK_STATUS* callbackStatus = (UPLOADTOBLOB_CALLBACK_STATUS*)context; ASSERT_ARE_EQUAL(UPLOADTOBLOB_CALLBACK_STATUS, *callbackStatus, UPLOADTOBLOB_CALLBACK_PENDING); - ASSERT_ARE_EQUAL(int, (int)result, (int)FILE_UPLOAD_OK); + ASSERT_ARE_EQUAL(int, (int)FILE_UPLOAD_OK, (int)result); static char* uploadData0 = "AAA-"; static char* uploadData1 = "BBBBB-"; static char* uploadData2 = "CCCCCC-"; - ASSERT_ARE_EQUAL(int, (int)Lock(updateBlobTestLock), (int)LOCK_OK); + ASSERT_ARE_EQUAL(int, (int)LOCK_OK, (int)Lock(updateBlobTestLock)); if (data == NULL) {