Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minimum log buffer size calculation #2372

Merged
merged 15 commits into from
Oct 28, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Bugs Fixed

[[#2372](https://github.com/Azure/azure-sdk-for-c/pull/2372)] Incorrect minimum buffer size calculation when logging an HTTP request.

### Other Changes

## 1.4.0-beta.1 (2022-08-09)
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/azure/core/az_http_policy_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static az_result _az_http_policy_logging_append_http_request_msg(
}
else
{
required_length = az_span_size(request->_internal.method) + request->_internal.url_length + 1;
required_length += az_span_size(request->_internal.method) + request->_internal.url_length + 1;
}

_az_RETURN_IF_NOT_ENOUGH_SIZE(*ref_log_msg, required_length);
Expand Down
121 changes: 121 additions & 0 deletions sdk/tests/core/test_az_logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,126 @@ static void test_az_log_everything_on_null(void** state)
}
}

#define _az_TEST_LOG_URL_PREFIX "HTTP Request : GET "
#define _az_TEST_LOG_URL_PROTOCOL "https://"
#define _az_TEST_LOG_URL_HOST ".microsoft.com"

#define _az_TEST_LOG_MAX_URL_SIZE \
(AZ_LOG_MESSAGE_BUFFER_SIZE - (sizeof(_az_TEST_LOG_URL_PREFIX) - 1))

static void _test_az_log_http_request_max_size_url_init(az_span url)
{
int32_t url_size = az_span_size(url);

az_span protocol = AZ_SPAN_FROM_STR(_az_TEST_LOG_URL_PROTOCOL);
az_span host = AZ_SPAN_FROM_STR(_az_TEST_LOG_URL_HOST);

url = az_span_copy(url, protocol);

for (int i = 0; i < (url_size - (az_span_size(protocol) + az_span_size(host))); ++i)
{
url = az_span_copy_u8(url, (uint8_t)'w');
}

az_span_copy(url, host);
}

static void _max_buf_size_log_listener(az_log_classification classification, az_span message)
{
uint8_t expected_msg_buf[AZ_LOG_MESSAGE_BUFFER_SIZE] = { 0 };
az_span expected_msg = AZ_SPAN_FROM_BUFFER(expected_msg_buf);

_test_az_log_http_request_max_size_url_init(
az_span_copy(expected_msg, AZ_SPAN_FROM_STR(_az_TEST_LOG_URL_PREFIX)));

switch (classification)
{
case AZ_LOG_HTTP_REQUEST:
_log_invoked_for_http_request = true;
assert_true(az_span_is_content_equal(message, expected_msg));

break;

default:
assert_true(false);
break;
}
}

static void _toobig_buf_size_log_listener(az_log_classification classification, az_span message)
{
uint8_t expected_msg_buf[AZ_LOG_MESSAGE_BUFFER_SIZE] = { 0 };
az_span expected_msg = AZ_SPAN_FROM_BUFFER(expected_msg_buf);

switch (classification)
{
case AZ_LOG_HTTP_REQUEST:
_log_invoked_for_http_request = true;
assert_true(az_span_is_content_equal(message, expected_msg));

break;

default:
assert_true(false);
break;
}
}

static void test_az_log_http_request_buffer_size(void** state)
{
(void)state;

_reset_log_invocation_status();
az_log_set_message_callback(_max_buf_size_log_listener);
{
uint8_t max_url_buf[_az_TEST_LOG_MAX_URL_SIZE] = { 0 };
az_span max_url = AZ_SPAN_FROM_BUFFER(max_url_buf);
_test_az_log_http_request_max_size_url_init(max_url);

az_http_request request = { 0 };
TEST_EXPECT_SUCCESS(az_http_request_init(
&request,
&az_context_application,
az_http_method_get(),
max_url,
az_span_size(max_url),
AZ_SPAN_FROM_STR(""),
AZ_SPAN_EMPTY));

_az_http_policy_logging_log_http_request(&request);
assert_true(_log_invoked_for_http_request == _az_BUILT_WITH_LOGGING(true, false));
}

_reset_log_invocation_status();
az_log_set_message_callback(_toobig_buf_size_log_listener);
{
uint8_t toobig_url_buf[_az_TEST_LOG_MAX_URL_SIZE + 1] = { 0 };
az_span toobig_url = AZ_SPAN_FROM_BUFFER(toobig_url_buf);
_test_az_log_http_request_max_size_url_init(toobig_url);

az_http_request request = { 0 };
TEST_EXPECT_SUCCESS(az_http_request_init(
&request,
&az_context_application,
az_http_method_get(),
toobig_url,
az_span_size(toobig_url),
AZ_SPAN_FROM_STR(""),
AZ_SPAN_EMPTY));

_az_http_policy_logging_log_http_request(&request);
assert_true(_log_invoked_for_http_request == _az_BUILT_WITH_LOGGING(true, false));
}

_reset_log_invocation_status();
az_log_set_message_callback(NULL);
}

#undef _az_TEST_LOG_URL_PREFIX
#undef _az_TEST_LOG_URL_PROTOCOL
#undef _az_TEST_LOG_URL_HOST
#undef _az_TEST_LOG_MAX_URL_SIZE

int test_az_logging()
{
const struct CMUnitTest tests[] = {
Expand All @@ -407,6 +527,7 @@ int test_az_logging()
cmocka_unit_test(test_az_log_incorrect_list_fails_gracefully),
cmocka_unit_test(test_az_log_everything_valid),
cmocka_unit_test(test_az_log_everything_on_null),
cmocka_unit_test(test_az_log_http_request_buffer_size),
};
return cmocka_run_group_tests_name("az_core_logging", tests, NULL, NULL);
}