| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/encoding.h> |
| 7 |
#include <aws/mqtt/mqtt.h> |
| 8 |
#include <aws/mqtt/private/mqtt_iot_metrics.h> |
| 9 |
#include <v3/mqtt311_testing_utils.h> |
| 10 |
|
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
struct utf8_example { |
| 14 |
const char *name; |
| 15 |
struct aws_byte_cursor text; |
| 16 |
}; |
| 17 |
|
| 18 |
static struct utf8_example s_valid_mqtt_utf8_examples[] = { |
| 19 |
{ |
| 20 |
.name = "1 letter", |
| 21 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("a"), |
| 22 |
}, |
| 23 |
{ |
| 24 |
.name = "Several ascii letters", |
| 25 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ascii word"), |
| 26 |
}, |
| 27 |
{ |
| 28 |
.name = "empty string", |
| 29 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""), |
| 30 |
}, |
| 31 |
{ |
| 32 |
.name = "2 byte codepoint", |
| 33 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\xA3"), |
| 34 |
}, |
| 35 |
{ |
| 36 |
.name = "3 byte codepoint", |
| 37 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE2\x82\xAC"), |
| 38 |
}, |
| 39 |
{ |
| 40 |
.name = "4 byte codepoint", |
| 41 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x90\x8D\x88"), |
| 42 |
}, |
| 43 |
{ |
| 44 |
.name = "A variety of different length codepoints", |
| 45 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 46 |
"\xF0\x90\x8D\x88\xE2\x82\xAC\xC2\xA3\x24\xC2\xA3\xE2\x82\xAC\xF0\x90\x8D\x88"), |
| 47 |
}, |
| 48 |
{ |
| 49 |
.name = "UTF8 BOM", |
| 50 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBB\xBF"), |
| 51 |
}, |
| 52 |
{ |
| 53 |
.name = "UTF8 BOM plus extra", |
| 54 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBB\xBF\x24\xC2\xA3"), |
| 55 |
}, |
| 56 |
{ |
| 57 |
.name = "First possible 3 byte codepoint", |
| 58 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE0\xA0\x80"), |
| 59 |
}, |
| 60 |
{ |
| 61 |
.name = "First possible 4 byte codepoint", |
| 62 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x90\x80\x80"), |
| 63 |
}, |
| 64 |
{ |
| 65 |
.name = "Last possible 2 byte codepoint", |
| 66 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xDF\xBF"), |
| 67 |
}, |
| 68 |
{ |
| 69 |
.name = "Last valid codepoint before prohibited range U+D800 - U+DFFF", |
| 70 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xED\x9F\xBF"), |
| 71 |
}, |
| 72 |
{ |
| 73 |
.name = "Next valid codepoint after prohibited range U+D800 - U+DFFF", |
| 74 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEE\x80\x80"), |
| 75 |
}, |
| 76 |
{ |
| 77 |
.name = "Boundary condition", |
| 78 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBF\xBD"), |
| 79 |
}, |
| 80 |
{ |
| 81 |
.name = "Boundary condition", |
| 82 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF4\x90\x80\x80"), |
| 83 |
}, |
| 84 |
}; |
| 85 |
|
| 86 |
static struct utf8_example s_illegal_mqtt_utf8_examples[] = { |
| 87 |
{ |
| 88 |
.name = "non character U+0000", |
| 89 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x00"), |
| 90 |
}, |
| 91 |
{ |
| 92 |
.name = "Codepoint in prohibited range U+0001 - U+001F (in the middle)", |
| 93 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x04"), |
| 94 |
}, |
| 95 |
{ |
| 96 |
.name = "Codepoint in prohibited range U+0001 - U+001F (boundary)", |
| 97 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x1F"), |
| 98 |
}, |
| 99 |
{ |
| 100 |
.name = "Codepoint in prohibited range U+007F - U+009F (min: U+7F)", |
| 101 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x7F"), |
| 102 |
}, |
| 103 |
{ |
| 104 |
.name = "Codepoint in prohibited range U+007F - U+009F (in the middle u+8F)", |
| 105 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\x8F"), |
| 106 |
}, |
| 107 |
{ |
| 108 |
.name = "Codepoint in prohibited range U+007F - U+009F (boundary U+9F)", |
| 109 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\x9F"), |
| 110 |
}, |
| 111 |
{ |
| 112 |
.name = "non character end with U+FFFF", |
| 113 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBF\xBF"), |
| 114 |
}, |
| 115 |
{ |
| 116 |
.name = "non character end with U+FFFE", |
| 117 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF7\xBF\xBF\xBE"), |
| 118 |
}, |
| 119 |
{ |
| 120 |
.name = "non character in U+FDD0 - U+FDEF (lower bound)", |
| 121 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xB7\x90"), |
| 122 |
}, |
| 123 |
{ |
| 124 |
.name = "non character in U+FDD0 - U+FDEF (in middle)", |
| 125 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xB7\xA1"), |
| 126 |
}, |
| 127 |
{ |
| 128 |
.name = "non character in U+FDD0 - U+FDEF (upper bound)", |
| 129 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xB7\xAF"), |
| 130 |
}}; |
| 131 |
|
| 132 |
static int s_mqtt_utf8_encoded_string_test(struct aws_allocator *allocator, void *ctx) { |
| 133 |
(void)ctx; |
| 134 |
|
| 135 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_mqtt_utf8_examples); ++i) { |
| 136 |
struct utf8_example example = s_valid_mqtt_utf8_examples[i]; |
| 137 |
printf("valid example [%zu]: %s\n", i, example.name); |
| 138 |
ASSERT_SUCCESS(aws_mqtt_validate_utf8_text(example.text)); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
struct aws_byte_buf all_good_text; |
| 143 |
aws_byte_buf_init(&all_good_text, allocator, 1024); |
| 144 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_mqtt_utf8_examples); ++i) { |
| 145 |
aws_byte_buf_append_dynamic(&all_good_text, &s_valid_mqtt_utf8_examples[i].text); |
| 146 |
} |
| 147 |
ASSERT_SUCCESS(aws_mqtt_validate_utf8_text(aws_byte_cursor_from_buf(&all_good_text))); |
| 148 |
aws_byte_buf_clean_up(&all_good_text); |
| 149 |
|
| 150 |
|
| 151 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_illegal_mqtt_utf8_examples); ++i) { |
| 152 |
struct utf8_example example = s_illegal_mqtt_utf8_examples[i]; |
| 153 |
printf("illegal example [%zu]: %s\n", i, example.name); |
| 154 |
ASSERT_FAILS(aws_mqtt_validate_utf8_text(example.text)); |
| 155 |
} |
| 156 |
|
| 157 |
return AWS_OP_SUCCESS; |
| 158 |
} |
| 159 |
|
| 160 |
AWS_TEST_CASE(mqtt_utf8_encoded_string_test, s_mqtt_utf8_encoded_string_test) |
| 161 |
|
| 162 |
static int s_test_mqtt_append_sdk_metrics_empty(struct aws_allocator *allocator, void *ctx) { |
| 163 |
(void)ctx; |
| 164 |
|
| 165 |
struct aws_mqtt_iot_metrics metrics = {{0}}; |
| 166 |
|
| 167 |
struct aws_byte_buf output_username; |
| 168 |
AWS_ZERO_STRUCT(output_username); |
| 169 |
|
| 170 |
ASSERT_SUCCESS(aws_mqtt_append_sdk_metrics_to_username(allocator, NULL, &metrics, &output_username, NULL)); |
| 171 |
|
| 172 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 173 |
|
| 174 |
|
| 175 |
struct aws_byte_buf expected_buf; |
| 176 |
AWS_ZERO_STRUCT(expected_buf); |
| 177 |
aws_test_mqtt_build_expected_metrics( |
| 178 |
allocator, NULL, aws_byte_cursor_from_c_str("IoTDeviceSDK/C"), NULL, NULL, 0, &expected_buf); |
| 179 |
|
| 180 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&output_cursor, &expected_buf)); |
| 181 |
|
| 182 |
aws_byte_buf_clean_up(&output_username); |
| 183 |
aws_byte_buf_clean_up(&expected_buf); |
| 184 |
|
| 185 |
return AWS_OP_SUCCESS; |
| 186 |
} |
| 187 |
|
| 188 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_empty, s_test_mqtt_append_sdk_metrics_empty) |
| 189 |
|
| 190 |
static int s_test_mqtt_append_sdk_metrics_basic(struct aws_allocator *allocator, void *ctx) { |
| 191 |
(void)ctx; |
| 192 |
|
| 193 |
struct aws_mqtt_iot_metrics metrics = { |
| 194 |
.library_name = aws_byte_cursor_from_c_str("TEST_SDK_STRING"), |
| 195 |
}; |
| 196 |
|
| 197 |
struct aws_byte_buf output_username; |
| 198 |
AWS_ZERO_STRUCT(output_username); |
| 199 |
|
| 200 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("TEST_USERNAME"); |
| 201 |
|
| 202 |
ASSERT_SUCCESS( |
| 203 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 204 |
|
| 205 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 206 |
|
| 207 |
|
| 208 |
struct aws_byte_buf expected_buf; |
| 209 |
AWS_ZERO_STRUCT(expected_buf); |
| 210 |
aws_test_mqtt_build_expected_metrics( |
| 211 |
allocator, &original_username, aws_byte_cursor_from_c_str("TEST_SDK_STRING"), NULL, NULL, 0, &expected_buf); |
| 212 |
|
| 213 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&output_cursor, &expected_buf)); |
| 214 |
|
| 215 |
aws_byte_buf_clean_up(&output_username); |
| 216 |
aws_byte_buf_clean_up(&expected_buf); |
| 217 |
|
| 218 |
return AWS_OP_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_basic, s_test_mqtt_append_sdk_metrics_basic) |
| 222 |
|
| 223 |
static int s_test_mqtt_append_sdk_metrics_existing_attributes(struct aws_allocator *allocator, void *ctx) { |
| 224 |
(void)ctx; |
| 225 |
|
| 226 |
struct aws_mqtt_iot_metrics metrics = { |
| 227 |
.library_name = aws_byte_cursor_from_c_str("NewSDK"), |
| 228 |
}; |
| 229 |
|
| 230 |
struct aws_byte_buf output_username; |
| 231 |
AWS_ZERO_STRUCT(output_username); |
| 232 |
|
| 233 |
struct aws_byte_cursor original_username = |
| 234 |
aws_byte_cursor_from_c_str("user?SDK=ExistingSDK&Platform=ExistingPlatform"); |
| 235 |
|
| 236 |
ASSERT_SUCCESS( |
| 237 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 238 |
|
| 239 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 240 |
|
| 241 |
ASSERT_TRUE(aws_byte_cursor_eq(&output_cursor, &original_username)); |
| 242 |
|
| 243 |
aws_byte_buf_clean_up(&output_username); |
| 244 |
|
| 245 |
return AWS_OP_SUCCESS; |
| 246 |
} |
| 247 |
|
| 248 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_existing_attributes, s_test_mqtt_append_sdk_metrics_existing_attributes) |
| 249 |
|
| 250 |
static int s_test_mqtt_append_sdk_metrics_special_chars(struct aws_allocator *allocator, void *ctx) { |
| 251 |
(void)ctx; |
| 252 |
|
| 253 |
struct aws_mqtt_iot_metrics metrics = {.library_name = aws_byte_cursor_from_c_str("SDK/Test-1.0")}; |
| 254 |
|
| 255 |
struct aws_byte_buf output_username; |
| 256 |
AWS_ZERO_STRUCT(output_username); |
| 257 |
|
| 258 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("[email protected]"); |
| 259 |
|
| 260 |
ASSERT_SUCCESS( |
| 261 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 262 |
|
| 263 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 264 |
|
| 265 |
|
| 266 |
struct aws_byte_buf expected_buf; |
| 267 |
AWS_ZERO_STRUCT(expected_buf); |
| 268 |
aws_test_mqtt_build_expected_metrics( |
| 269 |
allocator, &original_username, aws_byte_cursor_from_c_str("SDK/Test-1.0"), NULL, NULL, 0, &expected_buf); |
| 270 |
|
| 271 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&output_cursor, &expected_buf)); |
| 272 |
|
| 273 |
aws_byte_buf_clean_up(&output_username); |
| 274 |
aws_byte_buf_clean_up(&expected_buf); |
| 275 |
|
| 276 |
return AWS_OP_SUCCESS; |
| 277 |
} |
| 278 |
|
| 279 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_special_chars, s_test_mqtt_append_sdk_metrics_special_chars) |
| 280 |
|
| 281 |
static int s_test_mqtt_append_sdk_metrics_multiple_question_mark(struct aws_allocator *allocator, void *ctx) { |
| 282 |
(void)ctx; |
| 283 |
|
| 284 |
struct aws_mqtt_iot_metrics metrics = {.library_name = aws_byte_cursor_from_c_str("SDK/Test-1.0")}; |
| 285 |
|
| 286 |
struct aws_byte_buf output_username; |
| 287 |
AWS_ZERO_STRUCT(output_username); |
| 288 |
|
| 289 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("user?name?test?"); |
| 290 |
|
| 291 |
ASSERT_SUCCESS( |
| 292 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 293 |
|
| 294 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 295 |
|
| 296 |
|
| 297 |
struct aws_byte_cursor base_username = aws_byte_cursor_from_c_str("user?name?test"); |
| 298 |
struct aws_byte_buf expected_buf; |
| 299 |
AWS_ZERO_STRUCT(expected_buf); |
| 300 |
aws_test_mqtt_build_expected_metrics( |
| 301 |
allocator, &base_username, aws_byte_cursor_from_c_str("SDK/Test-1.0"), NULL, NULL, 0, &expected_buf); |
| 302 |
|
| 303 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&output_cursor, &expected_buf)); |
| 304 |
|
| 305 |
aws_byte_buf_clean_up(&output_username); |
| 306 |
aws_byte_buf_clean_up(&expected_buf); |
| 307 |
|
| 308 |
return AWS_OP_SUCCESS; |
| 309 |
} |
| 310 |
|
| 311 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_multiple_question_mark, s_test_mqtt_append_sdk_metrics_multiple_question_mark) |
| 312 |
|
| 313 |
static int s_test_mqtt_append_sdk_metrics_long_strings(struct aws_allocator *allocator, void *ctx) { |
| 314 |
(void)ctx; |
| 315 |
|
| 316 |
char long_username[1024]; |
| 317 |
char long_sdk_name[2 * 1024 * 128]; |
| 318 |
|
| 319 |
memset(long_username, 'A', sizeof(long_username) - 1); |
| 320 |
long_username[sizeof(long_username) - 1] = '\0'; |
| 321 |
|
| 322 |
memset(long_sdk_name, 'B', sizeof(long_sdk_name) - 1); |
| 323 |
long_sdk_name[sizeof(long_sdk_name) - 1] = '\0'; |
| 324 |
|
| 325 |
struct aws_mqtt_iot_metrics metrics = {.library_name = aws_byte_cursor_from_c_str(long_sdk_name)}; |
| 326 |
|
| 327 |
struct aws_mqtt_iot_metrics short_metrics = {.library_name = aws_byte_cursor_from_c_str("SHORT_SDK_NAME")}; |
| 328 |
|
| 329 |
struct aws_byte_buf output_username; |
| 330 |
AWS_ZERO_STRUCT(output_username); |
| 331 |
|
| 332 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str(long_username); |
| 333 |
|
| 334 |
|
| 335 |
ASSERT_SUCCESS( |
| 336 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &short_metrics, &output_username, NULL)); |
| 337 |
aws_byte_buf_clean_up(&output_username); |
| 338 |
|
| 339 |
|
| 340 |
ASSERT_FAILS( |
| 341 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 342 |
|
| 343 |
return AWS_OP_SUCCESS; |
| 344 |
} |
| 345 |
|
| 346 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_long_strings, s_test_mqtt_append_sdk_metrics_long_strings) |
| 347 |
|
| 348 |
static int s_test_mqtt_append_sdk_metrics_invalid_utf8(struct aws_allocator *allocator, void *ctx) { |
| 349 |
(void)allocator; |
| 350 |
(void)ctx; |
| 351 |
|
| 352 |
|
| 353 |
struct aws_byte_cursor invalid_utf8_library = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSDK\xFF\xFE"); |
| 354 |
|
| 355 |
struct aws_mqtt_iot_metrics metrics = {.library_name = invalid_utf8_library}; |
| 356 |
|
| 357 |
struct aws_byte_buf output_username; |
| 358 |
AWS_ZERO_STRUCT(output_username); |
| 359 |
|
| 360 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("testuser"); |
| 361 |
|
| 362 |
|
| 363 |
ASSERT_FAILS( |
| 364 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 365 |
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8); |
| 366 |
|
| 367 |
aws_byte_buf_clean_up(&output_username); |
| 368 |
|
| 369 |
return AWS_OP_SUCCESS; |
| 370 |
} |
| 371 |
|
| 372 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_invalid_utf8, s_test_mqtt_append_sdk_metrics_invalid_utf8) |
| 373 |
|
| 374 |
static int s_test_mqtt_append_sdk_metrics_with_metadata(struct aws_allocator *allocator, void *ctx) { |
| 375 |
(void)ctx; |
| 376 |
|
| 377 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 378 |
{ |
| 379 |
.key = aws_byte_cursor_from_c_str("CustomKey1"), |
| 380 |
.value = aws_byte_cursor_from_c_str("CustomValue1"), |
| 381 |
}, |
| 382 |
{ |
| 383 |
.key = aws_byte_cursor_from_c_str("CustomKey2"), |
| 384 |
.value = aws_byte_cursor_from_c_str("CustomValue2"), |
| 385 |
}, |
| 386 |
}; |
| 387 |
|
| 388 |
struct aws_mqtt_iot_metrics metrics = { |
| 389 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 390 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 391 |
.metadata_entries = metadata_entries, |
| 392 |
}; |
| 393 |
|
| 394 |
struct aws_byte_buf output_username; |
| 395 |
AWS_ZERO_STRUCT(output_username); |
| 396 |
|
| 397 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("testuser"); |
| 398 |
|
| 399 |
ASSERT_SUCCESS( |
| 400 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 401 |
|
| 402 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 403 |
|
| 404 |
|
| 405 |
struct aws_byte_cursor metadata_format = |
| 406 |
aws_byte_cursor_from_c_str("&Metadata=(CustomKey1=CustomValue1;CustomKey2=CustomValue2)"); |
| 407 |
struct aws_byte_cursor found; |
| 408 |
|
| 409 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&output_cursor, &metadata_format, &found)); |
| 410 |
|
| 411 |
aws_byte_buf_clean_up(&output_username); |
| 412 |
|
| 413 |
return AWS_OP_SUCCESS; |
| 414 |
} |
| 415 |
|
| 416 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_with_metadata, s_test_mqtt_append_sdk_metrics_with_metadata) |
| 417 |
|
| 418 |
static int s_test_mqtt_append_sdk_metrics_with_metadata_invalid_utf8(struct aws_allocator *allocator, void *ctx) { |
| 419 |
(void)ctx; |
| 420 |
|
| 421 |
|
| 422 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 423 |
{ |
| 424 |
.key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("InvalidKey\xFF\xFE"), |
| 425 |
.value = aws_byte_cursor_from_c_str("ValidValue"), |
| 426 |
}, |
| 427 |
}; |
| 428 |
|
| 429 |
struct aws_mqtt_iot_metrics metrics = { |
| 430 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 431 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 432 |
.metadata_entries = metadata_entries, |
| 433 |
}; |
| 434 |
|
| 435 |
struct aws_byte_buf output_username; |
| 436 |
AWS_ZERO_STRUCT(output_username); |
| 437 |
|
| 438 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("testuser"); |
| 439 |
|
| 440 |
|
| 441 |
ASSERT_FAILS( |
| 442 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 443 |
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8); |
| 444 |
|
| 445 |
aws_byte_buf_clean_up(&output_username); |
| 446 |
|
| 447 |
return AWS_OP_SUCCESS; |
| 448 |
} |
| 449 |
|
| 450 |
AWS_TEST_CASE( |
| 451 |
mqtt_append_sdk_metrics_with_metadata_invalid_utf8, |
| 452 |
s_test_mqtt_append_sdk_metrics_with_metadata_invalid_utf8) |
| 453 |
|
| 454 |
static int s_test_mqtt_append_sdk_metrics_with_metadata_invalid_utf8_value(struct aws_allocator *allocator, void *ctx) { |
| 455 |
(void)ctx; |
| 456 |
|
| 457 |
|
| 458 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 459 |
{ |
| 460 |
.key = aws_byte_cursor_from_c_str("ValidKey"), |
| 461 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("InvalidValue\xFF\xFE"), |
| 462 |
}, |
| 463 |
}; |
| 464 |
|
| 465 |
struct aws_mqtt_iot_metrics metrics = { |
| 466 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 467 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 468 |
.metadata_entries = metadata_entries, |
| 469 |
}; |
| 470 |
|
| 471 |
struct aws_byte_buf output_username; |
| 472 |
AWS_ZERO_STRUCT(output_username); |
| 473 |
|
| 474 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str("testuser"); |
| 475 |
|
| 476 |
|
| 477 |
ASSERT_FAILS( |
| 478 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 479 |
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_UTF8); |
| 480 |
|
| 481 |
aws_byte_buf_clean_up(&output_username); |
| 482 |
|
| 483 |
return AWS_OP_SUCCESS; |
| 484 |
} |
| 485 |
|
| 486 |
AWS_TEST_CASE( |
| 487 |
mqtt_append_sdk_metrics_with_metadata_invalid_utf8_value, |
| 488 |
s_test_mqtt_append_sdk_metrics_with_metadata_invalid_utf8_value) |
| 489 |
|
| 490 |
static int s_test_mqtt_iot_metrics_storage_with_metadata(struct aws_allocator *allocator, void *ctx) { |
| 491 |
(void)ctx; |
| 492 |
|
| 493 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 494 |
{ |
| 495 |
.key = aws_byte_cursor_from_c_str("Key1"), |
| 496 |
.value = aws_byte_cursor_from_c_str("Value1"), |
| 497 |
}, |
| 498 |
{ |
| 499 |
.key = aws_byte_cursor_from_c_str("Key2"), |
| 500 |
.value = aws_byte_cursor_from_c_str("Value2"), |
| 501 |
}, |
| 502 |
}; |
| 503 |
|
| 504 |
struct aws_mqtt_iot_metrics metrics = { |
| 505 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 506 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 507 |
.metadata_entries = metadata_entries, |
| 508 |
}; |
| 509 |
|
| 510 |
struct aws_mqtt_iot_metrics_storage *storage = aws_mqtt_iot_metrics_storage_new(allocator, &metrics); |
| 511 |
ASSERT_NOT_NULL(storage); |
| 512 |
|
| 513 |
|
| 514 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.library_name, "TestSDK/1.0")); |
| 515 |
ASSERT_INT_EQUALS(2, storage->storage_view.metadata_count); |
| 516 |
ASSERT_NOT_NULL(storage->storage_view.metadata_entries); |
| 517 |
|
| 518 |
|
| 519 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.metadata_entries[0].key, "Key1")); |
| 520 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.metadata_entries[0].value, "Value1")); |
| 521 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.metadata_entries[1].key, "Key2")); |
| 522 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.metadata_entries[1].value, "Value2")); |
| 523 |
|
| 524 |
aws_mqtt_iot_metrics_storage_destroy(storage); |
| 525 |
|
| 526 |
return AWS_OP_SUCCESS; |
| 527 |
} |
| 528 |
|
| 529 |
AWS_TEST_CASE(mqtt_iot_metrics_storage_with_metadata, s_test_mqtt_iot_metrics_storage_with_metadata) |
| 530 |
|
| 531 |
static int s_test_mqtt_iot_metrics_storage_empty_metadata(struct aws_allocator *allocator, void *ctx) { |
| 532 |
(void)ctx; |
| 533 |
|
| 534 |
struct aws_mqtt_iot_metrics metrics = { |
| 535 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 536 |
.metadata_count = 0, |
| 537 |
.metadata_entries = NULL, |
| 538 |
}; |
| 539 |
|
| 540 |
struct aws_mqtt_iot_metrics_storage *storage = aws_mqtt_iot_metrics_storage_new(allocator, &metrics); |
| 541 |
ASSERT_NOT_NULL(storage); |
| 542 |
|
| 543 |
|
| 544 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&storage->storage_view.library_name, "TestSDK/1.0")); |
| 545 |
ASSERT_INT_EQUALS(0, storage->storage_view.metadata_count); |
| 546 |
|
| 547 |
aws_mqtt_iot_metrics_storage_destroy(storage); |
| 548 |
|
| 549 |
return AWS_OP_SUCCESS; |
| 550 |
} |
| 551 |
|
| 552 |
AWS_TEST_CASE(mqtt_iot_metrics_storage_empty_metadata, s_test_mqtt_iot_metrics_storage_empty_metadata) |
| 553 |
|
| 554 |
static int s_test_mqtt_append_sdk_metrics_existing_metadata(struct aws_allocator *allocator, void *ctx) { |
| 555 |
(void)ctx; |
| 556 |
|
| 557 |
|
| 558 |
struct aws_mqtt_metadata_entry metadata_entries[] = { |
| 559 |
{ |
| 560 |
.key = aws_byte_cursor_from_c_str("NewKey"), |
| 561 |
.value = aws_byte_cursor_from_c_str("NewValue"), |
| 562 |
}, |
| 563 |
}; |
| 564 |
|
| 565 |
struct aws_mqtt_iot_metrics metrics = { |
| 566 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 567 |
.metadata_count = AWS_ARRAY_SIZE(metadata_entries), |
| 568 |
.metadata_entries = metadata_entries, |
| 569 |
}; |
| 570 |
|
| 571 |
struct aws_byte_buf output_username; |
| 572 |
AWS_ZERO_STRUCT(output_username); |
| 573 |
|
| 574 |
|
| 575 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str( |
| 576 |
"testuser?SDK=ExistingSDK&Platform=ExistingPlatform&Metadata=(ExistingKey1=ExistingValue1;ExistingKey2=" |
| 577 |
"ExistingValue2)"); |
| 578 |
|
| 579 |
ASSERT_SUCCESS( |
| 580 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 581 |
|
| 582 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
struct aws_byte_cursor merged_metadata = aws_byte_cursor_from_c_str( |
| 587 |
"Metadata=(ExistingKey1=ExistingValue1;ExistingKey2=ExistingValue2;NewKey=NewValue)"); |
| 588 |
struct aws_byte_cursor found; |
| 589 |
|
| 590 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&output_cursor, &merged_metadata, &found)); |
| 591 |
|
| 592 |
|
| 593 |
struct aws_byte_cursor sdk_check = aws_byte_cursor_from_c_str("SDK=ExistingSDK"); |
| 594 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&output_cursor, &sdk_check, &found)); |
| 595 |
|
| 596 |
struct aws_byte_cursor platform_check = aws_byte_cursor_from_c_str("Platform=ExistingPlatform"); |
| 597 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&output_cursor, &platform_check, &found)); |
| 598 |
|
| 599 |
aws_byte_buf_clean_up(&output_username); |
| 600 |
|
| 601 |
return AWS_OP_SUCCESS; |
| 602 |
} |
| 603 |
|
| 604 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_existing_metadata, s_test_mqtt_append_sdk_metrics_existing_metadata) |
| 605 |
|
| 606 |
static int s_test_mqtt_append_sdk_metrics_existing_metadata_no_new(struct aws_allocator *allocator, void *ctx) { |
| 607 |
(void)ctx; |
| 608 |
|
| 609 |
|
| 610 |
struct aws_mqtt_iot_metrics metrics = { |
| 611 |
.library_name = aws_byte_cursor_from_c_str("TestSDK/1.0"), |
| 612 |
.metadata_count = 0, |
| 613 |
.metadata_entries = NULL, |
| 614 |
}; |
| 615 |
|
| 616 |
struct aws_byte_buf output_username; |
| 617 |
AWS_ZERO_STRUCT(output_username); |
| 618 |
|
| 619 |
|
| 620 |
struct aws_byte_cursor original_username = aws_byte_cursor_from_c_str( |
| 621 |
"testuser?SDK=ExistingSDK&Platform=ExistingPlatform&Metadata=(ExistingKey=ExistingValue)"); |
| 622 |
|
| 623 |
ASSERT_SUCCESS( |
| 624 |
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, &metrics, &output_username, NULL)); |
| 625 |
|
| 626 |
struct aws_byte_cursor output_cursor = aws_byte_cursor_from_buf(&output_username); |
| 627 |
|
| 628 |
|
| 629 |
struct aws_byte_cursor existing_metadata = aws_byte_cursor_from_c_str("Metadata=(ExistingKey=ExistingValue)"); |
| 630 |
struct aws_byte_cursor found; |
| 631 |
|
| 632 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&output_cursor, &existing_metadata, &found)); |
| 633 |
|
| 634 |
aws_byte_buf_clean_up(&output_username); |
| 635 |
|
| 636 |
return AWS_OP_SUCCESS; |
| 637 |
} |
| 638 |
|
| 639 |
AWS_TEST_CASE(mqtt_append_sdk_metrics_existing_metadata_no_new, s_test_mqtt_append_sdk_metrics_existing_metadata_no_new) |