| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/mqtt.h> |
| 7 |
#include <aws/mqtt/private/v5/mqtt5_topic_alias.h> |
| 8 |
|
| 9 |
#include <aws/common/byte_buf.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
|
| 12 |
#include <aws/testing/aws_test_harness.h> |
| 13 |
|
| 14 |
AWS_STATIC_STRING_FROM_LITERAL(s_topic1, "hello/world"); |
| 15 |
AWS_STATIC_STRING_FROM_LITERAL(s_topic2, "this/is/a/longer/topic"); |
| 16 |
|
| 17 |
static int s_mqtt5_inbound_topic_alias_register_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 18 |
(void)ctx; |
| 19 |
|
| 20 |
struct aws_mqtt5_inbound_topic_alias_resolver resolver; |
| 21 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_init(&resolver, allocator)); |
| 22 |
|
| 23 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&resolver, 10)); |
| 24 |
|
| 25 |
ASSERT_FAILS( |
| 26 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 0, aws_byte_cursor_from_string(s_topic1))); |
| 27 |
ASSERT_FAILS( |
| 28 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 11, aws_byte_cursor_from_string(s_topic1))); |
| 29 |
|
| 30 |
aws_mqtt5_inbound_topic_alias_resolver_clean_up(&resolver); |
| 31 |
|
| 32 |
return AWS_OP_SUCCESS; |
| 33 |
} |
| 34 |
|
| 35 |
AWS_TEST_CASE(mqtt5_inbound_topic_alias_register_failure, s_mqtt5_inbound_topic_alias_register_failure_fn) |
| 36 |
|
| 37 |
static int s_mqtt5_inbound_topic_alias_resolve_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 38 |
(void)ctx; |
| 39 |
|
| 40 |
struct aws_mqtt5_inbound_topic_alias_resolver resolver; |
| 41 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_init(&resolver, allocator)); |
| 42 |
|
| 43 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&resolver, 10)); |
| 44 |
|
| 45 |
ASSERT_SUCCESS( |
| 46 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 1, aws_byte_cursor_from_string(s_topic1))); |
| 47 |
ASSERT_SUCCESS( |
| 48 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 10, aws_byte_cursor_from_string(s_topic2))); |
| 49 |
|
| 50 |
struct aws_byte_cursor topic1; |
| 51 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 1, &topic1)); |
| 52 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, topic1.ptr, topic1.len); |
| 53 |
|
| 54 |
struct aws_byte_cursor topic2; |
| 55 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 10, &topic2)); |
| 56 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic2->bytes, s_topic2->len, topic2.ptr, topic2.len); |
| 57 |
|
| 58 |
|
| 59 |
ASSERT_SUCCESS( |
| 60 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 10, aws_byte_cursor_from_string(s_topic1))); |
| 61 |
|
| 62 |
struct aws_byte_cursor topic3; |
| 63 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 10, &topic3)); |
| 64 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, topic3.ptr, topic3.len); |
| 65 |
|
| 66 |
aws_mqtt5_inbound_topic_alias_resolver_clean_up(&resolver); |
| 67 |
|
| 68 |
return AWS_OP_SUCCESS; |
| 69 |
} |
| 70 |
|
| 71 |
AWS_TEST_CASE(mqtt5_inbound_topic_alias_resolve_success, s_mqtt5_inbound_topic_alias_resolve_success_fn) |
| 72 |
|
| 73 |
static int s_mqtt5_inbound_topic_alias_resolve_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 74 |
(void)ctx; |
| 75 |
|
| 76 |
struct aws_mqtt5_inbound_topic_alias_resolver resolver; |
| 77 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_init(&resolver, allocator)); |
| 78 |
|
| 79 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&resolver, 10)); |
| 80 |
|
| 81 |
struct aws_byte_cursor topic; |
| 82 |
ASSERT_FAILS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 0, &topic)); |
| 83 |
ASSERT_FAILS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 11, &topic)); |
| 84 |
ASSERT_FAILS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 10, &topic)); |
| 85 |
|
| 86 |
aws_mqtt5_inbound_topic_alias_resolver_clean_up(&resolver); |
| 87 |
|
| 88 |
return AWS_OP_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
AWS_TEST_CASE(mqtt5_inbound_topic_alias_resolve_failure, s_mqtt5_inbound_topic_alias_resolve_failure_fn) |
| 92 |
|
| 93 |
static int s_mqtt5_inbound_topic_alias_reset_fn(struct aws_allocator *allocator, void *ctx) { |
| 94 |
(void)ctx; |
| 95 |
|
| 96 |
struct aws_mqtt5_inbound_topic_alias_resolver resolver; |
| 97 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_init(&resolver, allocator)); |
| 98 |
|
| 99 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&resolver, 10)); |
| 100 |
|
| 101 |
ASSERT_SUCCESS( |
| 102 |
aws_mqtt5_inbound_topic_alias_resolver_register_alias(&resolver, 1, aws_byte_cursor_from_string(s_topic1))); |
| 103 |
|
| 104 |
struct aws_byte_cursor topic; |
| 105 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 1, &topic)); |
| 106 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, topic.ptr, topic.len); |
| 107 |
|
| 108 |
ASSERT_SUCCESS(aws_mqtt5_inbound_topic_alias_resolver_reset(&resolver, 10)); |
| 109 |
|
| 110 |
ASSERT_FAILS(aws_mqtt5_inbound_topic_alias_resolver_resolve_alias(&resolver, 1, &topic)); |
| 111 |
|
| 112 |
aws_mqtt5_inbound_topic_alias_resolver_clean_up(&resolver); |
| 113 |
|
| 114 |
return AWS_OP_SUCCESS; |
| 115 |
} |
| 116 |
|
| 117 |
AWS_TEST_CASE(mqtt5_inbound_topic_alias_reset, s_mqtt5_inbound_topic_alias_reset_fn) |
| 118 |
|
| 119 |
static int s_mqtt5_outbound_topic_alias_disabled_resolve_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 120 |
(void)ctx; |
| 121 |
|
| 122 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 123 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_DISABLED); |
| 124 |
ASSERT_NOT_NULL(resolver); |
| 125 |
|
| 126 |
struct aws_mqtt5_packet_publish_view publish_view = {.topic = aws_byte_cursor_from_string(s_topic1)}; |
| 127 |
|
| 128 |
uint16_t outbound_alias_id = 0; |
| 129 |
struct aws_byte_cursor outbound_topic; |
| 130 |
AWS_ZERO_STRUCT(outbound_topic); |
| 131 |
|
| 132 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 133 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 134 |
|
| 135 |
ASSERT_INT_EQUALS(0, outbound_alias_id); |
| 136 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, outbound_topic.ptr, outbound_topic.len); |
| 137 |
|
| 138 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 0)); |
| 139 |
|
| 140 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 141 |
|
| 142 |
return AWS_OP_SUCCESS; |
| 143 |
} |
| 144 |
|
| 145 |
AWS_TEST_CASE( |
| 146 |
mqtt5_outbound_topic_alias_disabled_resolve_success, |
| 147 |
s_mqtt5_outbound_topic_alias_disabled_resolve_success_fn) |
| 148 |
|
| 149 |
static int s_mqtt5_outbound_topic_alias_disabled_resolve_failure_fn(struct aws_allocator *allocator, void *ctx) { |
| 150 |
(void)ctx; |
| 151 |
|
| 152 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 153 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_DISABLED); |
| 154 |
ASSERT_NOT_NULL(resolver); |
| 155 |
|
| 156 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 157 |
.topic = |
| 158 |
{ |
| 159 |
.ptr = NULL, |
| 160 |
.len = 0, |
| 161 |
}, |
| 162 |
}; |
| 163 |
|
| 164 |
uint16_t outbound_alias_id = 0; |
| 165 |
struct aws_byte_cursor outbound_topic; |
| 166 |
AWS_ZERO_STRUCT(outbound_topic); |
| 167 |
|
| 168 |
ASSERT_FAILS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 169 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 170 |
|
| 171 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 172 |
|
| 173 |
return AWS_OP_SUCCESS; |
| 174 |
} |
| 175 |
|
| 176 |
AWS_TEST_CASE( |
| 177 |
mqtt5_outbound_topic_alias_disabled_resolve_failure, |
| 178 |
s_mqtt5_outbound_topic_alias_disabled_resolve_failure_fn) |
| 179 |
|
| 180 |
static int s_mqtt5_outbound_topic_alias_manual_resolve_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 181 |
(void)ctx; |
| 182 |
|
| 183 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 184 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_MANUAL); |
| 185 |
ASSERT_NOT_NULL(resolver); |
| 186 |
|
| 187 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 5); |
| 188 |
|
| 189 |
struct aws_mqtt5_packet_publish_view publish_view1 = { |
| 190 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 191 |
}; |
| 192 |
|
| 193 |
uint16_t outbound_alias_id = 0; |
| 194 |
struct aws_byte_cursor outbound_topic; |
| 195 |
AWS_ZERO_STRUCT(outbound_topic); |
| 196 |
|
| 197 |
|
| 198 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 199 |
resolver, &publish_view1, &outbound_alias_id, &outbound_topic)); |
| 200 |
|
| 201 |
ASSERT_INT_EQUALS(0, outbound_alias_id); |
| 202 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, outbound_topic.ptr, outbound_topic.len); |
| 203 |
|
| 204 |
uint16_t alias = 1; |
| 205 |
struct aws_mqtt5_packet_publish_view publish_view2 = { |
| 206 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 207 |
.topic_alias = &alias, |
| 208 |
}; |
| 209 |
|
| 210 |
outbound_alias_id = 0; |
| 211 |
AWS_ZERO_STRUCT(outbound_topic); |
| 212 |
|
| 213 |
|
| 214 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 215 |
resolver, &publish_view2, &outbound_alias_id, &outbound_topic)); |
| 216 |
|
| 217 |
ASSERT_INT_EQUALS(1, outbound_alias_id); |
| 218 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, outbound_topic.ptr, outbound_topic.len); |
| 219 |
|
| 220 |
struct aws_mqtt5_packet_publish_view publish_view3 = { |
| 221 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 222 |
.topic_alias = &alias, |
| 223 |
}; |
| 224 |
|
| 225 |
outbound_alias_id = 0; |
| 226 |
AWS_ZERO_STRUCT(outbound_topic); |
| 227 |
|
| 228 |
|
| 229 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 230 |
resolver, &publish_view3, &outbound_alias_id, &outbound_topic)); |
| 231 |
|
| 232 |
ASSERT_INT_EQUALS(1, outbound_alias_id); |
| 233 |
ASSERT_INT_EQUALS(0, outbound_topic.len); |
| 234 |
|
| 235 |
|
| 236 |
struct aws_mqtt5_packet_publish_view publish_view4 = { |
| 237 |
.topic = aws_byte_cursor_from_string(s_topic2), |
| 238 |
.topic_alias = &alias, |
| 239 |
}; |
| 240 |
|
| 241 |
outbound_alias_id = 0; |
| 242 |
AWS_ZERO_STRUCT(outbound_topic); |
| 243 |
|
| 244 |
|
| 245 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 246 |
resolver, &publish_view4, &outbound_alias_id, &outbound_topic)); |
| 247 |
|
| 248 |
ASSERT_INT_EQUALS(1, outbound_alias_id); |
| 249 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic2->bytes, s_topic2->len, outbound_topic.ptr, outbound_topic.len); |
| 250 |
|
| 251 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 252 |
|
| 253 |
return AWS_OP_SUCCESS; |
| 254 |
} |
| 255 |
|
| 256 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_manual_resolve_success, s_mqtt5_outbound_topic_alias_manual_resolve_success_fn) |
| 257 |
|
| 258 |
static int s_mqtt5_outbound_topic_alias_manual_resolve_failure_zero_alias_fn( |
| 259 |
struct aws_allocator *allocator, |
| 260 |
void *ctx) { |
| 261 |
(void)ctx; |
| 262 |
|
| 263 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 264 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_MANUAL); |
| 265 |
ASSERT_NOT_NULL(resolver); |
| 266 |
|
| 267 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 5); |
| 268 |
|
| 269 |
uint16_t alias = 0; |
| 270 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 271 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 272 |
.topic_alias = &alias, |
| 273 |
}; |
| 274 |
|
| 275 |
uint16_t outbound_alias_id = 0; |
| 276 |
struct aws_byte_cursor outbound_topic; |
| 277 |
AWS_ZERO_STRUCT(outbound_topic); |
| 278 |
|
| 279 |
ASSERT_FAILS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 280 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 281 |
|
| 282 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 283 |
|
| 284 |
return AWS_OP_SUCCESS; |
| 285 |
} |
| 286 |
|
| 287 |
AWS_TEST_CASE( |
| 288 |
mqtt5_outbound_topic_alias_manual_resolve_failure_zero_alias, |
| 289 |
s_mqtt5_outbound_topic_alias_manual_resolve_failure_zero_alias_fn) |
| 290 |
|
| 291 |
static int s_mqtt5_outbound_topic_alias_manual_resolve_failure_too_big_alias_fn( |
| 292 |
struct aws_allocator *allocator, |
| 293 |
void *ctx) { |
| 294 |
(void)ctx; |
| 295 |
|
| 296 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 297 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_MANUAL); |
| 298 |
ASSERT_NOT_NULL(resolver); |
| 299 |
|
| 300 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 5); |
| 301 |
|
| 302 |
uint16_t alias = 6; |
| 303 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 304 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 305 |
.topic_alias = &alias, |
| 306 |
}; |
| 307 |
|
| 308 |
uint16_t outbound_alias_id = 0; |
| 309 |
struct aws_byte_cursor outbound_topic; |
| 310 |
AWS_ZERO_STRUCT(outbound_topic); |
| 311 |
|
| 312 |
ASSERT_FAILS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 313 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 314 |
|
| 315 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 316 |
|
| 317 |
return AWS_OP_SUCCESS; |
| 318 |
} |
| 319 |
|
| 320 |
AWS_TEST_CASE( |
| 321 |
mqtt5_outbound_topic_alias_manual_resolve_failure_too_big_alias, |
| 322 |
s_mqtt5_outbound_topic_alias_manual_resolve_failure_too_big_alias_fn) |
| 323 |
|
| 324 |
static int s_mqtt5_outbound_topic_alias_manual_reset_fn(struct aws_allocator *allocator, void *ctx) { |
| 325 |
(void)ctx; |
| 326 |
|
| 327 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 328 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_MANUAL); |
| 329 |
ASSERT_NOT_NULL(resolver); |
| 330 |
|
| 331 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 5); |
| 332 |
|
| 333 |
uint16_t alias = 2; |
| 334 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 335 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 336 |
.topic_alias = &alias, |
| 337 |
}; |
| 338 |
|
| 339 |
uint16_t outbound_alias_id = 0; |
| 340 |
struct aws_byte_cursor outbound_topic; |
| 341 |
AWS_ZERO_STRUCT(outbound_topic); |
| 342 |
|
| 343 |
|
| 344 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 345 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 346 |
|
| 347 |
ASSERT_INT_EQUALS(2, outbound_alias_id); |
| 348 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, outbound_topic.ptr, outbound_topic.len); |
| 349 |
|
| 350 |
|
| 351 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 352 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 353 |
|
| 354 |
ASSERT_INT_EQUALS(2, outbound_alias_id); |
| 355 |
ASSERT_INT_EQUALS(0, outbound_topic.len); |
| 356 |
|
| 357 |
|
| 358 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 5); |
| 359 |
|
| 360 |
|
| 361 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 362 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 363 |
|
| 364 |
ASSERT_INT_EQUALS(2, outbound_alias_id); |
| 365 |
ASSERT_BIN_ARRAYS_EQUALS(s_topic1->bytes, s_topic1->len, outbound_topic.ptr, outbound_topic.len); |
| 366 |
|
| 367 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 368 |
|
| 369 |
return AWS_OP_SUCCESS; |
| 370 |
} |
| 371 |
|
| 372 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_manual_reset, s_mqtt5_outbound_topic_alias_manual_reset_fn) |
| 373 |
|
| 374 |
static int s_mqtt5_outbound_topic_alias_lru_zero_size_fn(struct aws_allocator *allocator, void *ctx) { |
| 375 |
(void)ctx; |
| 376 |
|
| 377 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 378 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_LRU); |
| 379 |
ASSERT_NOT_NULL(resolver); |
| 380 |
|
| 381 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, 0); |
| 382 |
|
| 383 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 384 |
.topic = aws_byte_cursor_from_string(s_topic1), |
| 385 |
}; |
| 386 |
|
| 387 |
uint16_t outbound_alias_id = 0; |
| 388 |
struct aws_byte_cursor outbound_topic; |
| 389 |
AWS_ZERO_STRUCT(outbound_topic); |
| 390 |
|
| 391 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 392 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 393 |
|
| 394 |
ASSERT_INT_EQUALS(0, outbound_alias_id); |
| 395 |
ASSERT_BIN_ARRAYS_EQUALS(publish_view.topic.ptr, publish_view.topic.len, outbound_topic.ptr, outbound_topic.len); |
| 396 |
|
| 397 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 398 |
|
| 399 |
return AWS_OP_SUCCESS; |
| 400 |
} |
| 401 |
|
| 402 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_zero_size, s_mqtt5_outbound_topic_alias_lru_zero_size_fn) |
| 403 |
|
| 404 |
#define LRU_SEQUENCE_TEST_CACHE_SIZE 2 |
| 405 |
|
| 406 |
struct lru_test_operation { |
| 407 |
struct aws_byte_cursor topic; |
| 408 |
size_t expected_alias_id; |
| 409 |
bool expected_reuse; |
| 410 |
}; |
| 411 |
|
| 412 |
#define DEFINE_LRU_TEST_OPERATION(topic_type, alias_index, reused) \ |
| 413 |
{ \ |
| 414 |
.topic = aws_byte_cursor_from_string(s_topic_##topic_type), \ |
| 415 |
.expected_alias_id = alias_index, \ |
| 416 |
.expected_reuse = reused, \ |
| 417 |
} |
| 418 |
|
| 419 |
static int s_perform_lru_test_operation( |
| 420 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver, |
| 421 |
struct lru_test_operation *operation) { |
| 422 |
|
| 423 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 424 |
.topic = operation->topic, |
| 425 |
}; |
| 426 |
|
| 427 |
uint16_t outbound_alias_id = 0; |
| 428 |
struct aws_byte_cursor outbound_topic; |
| 429 |
AWS_ZERO_STRUCT(outbound_topic); |
| 430 |
|
| 431 |
ASSERT_SUCCESS(aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 432 |
resolver, &publish_view, &outbound_alias_id, &outbound_topic)); |
| 433 |
|
| 434 |
ASSERT_INT_EQUALS(operation->expected_alias_id, outbound_alias_id); |
| 435 |
if (operation->expected_reuse) { |
| 436 |
ASSERT_INT_EQUALS(0, outbound_topic.len); |
| 437 |
} else { |
| 438 |
ASSERT_BIN_ARRAYS_EQUALS(operation->topic.ptr, operation->topic.len, outbound_topic.ptr, outbound_topic.len); |
| 439 |
} |
| 440 |
|
| 441 |
return AWS_OP_SUCCESS; |
| 442 |
} |
| 443 |
|
| 444 |
static int s_check_lru_sequence( |
| 445 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver, |
| 446 |
struct lru_test_operation *operations, |
| 447 |
size_t operation_count) { |
| 448 |
for (size_t i = 0; i < operation_count; ++i) { |
| 449 |
struct lru_test_operation *operation = &operations[i]; |
| 450 |
ASSERT_SUCCESS(s_perform_lru_test_operation(resolver, operation)); |
| 451 |
} |
| 452 |
|
| 453 |
return AWS_OP_SUCCESS; |
| 454 |
} |
| 455 |
|
| 456 |
static int s_perform_lru_sequence_test( |
| 457 |
struct aws_allocator *allocator, |
| 458 |
struct lru_test_operation *operations, |
| 459 |
size_t operation_count) { |
| 460 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 461 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_LRU); |
| 462 |
ASSERT_NOT_NULL(resolver); |
| 463 |
|
| 464 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, LRU_SEQUENCE_TEST_CACHE_SIZE); |
| 465 |
|
| 466 |
ASSERT_SUCCESS(s_check_lru_sequence(resolver, operations, operation_count)); |
| 467 |
|
| 468 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 469 |
|
| 470 |
return AWS_OP_SUCCESS; |
| 471 |
} |
| 472 |
|
| 473 |
AWS_STATIC_STRING_FROM_LITERAL(s_topic_a, "topic/a"); |
| 474 |
AWS_STATIC_STRING_FROM_LITERAL(s_topic_b, "b/topic"); |
| 475 |
AWS_STATIC_STRING_FROM_LITERAL(s_topic_c, "topic/c"); |
| 476 |
|
| 477 |
static int s_mqtt5_outbound_topic_alias_lru_a_ar_fn(struct aws_allocator *allocator, void *ctx) { |
| 478 |
(void)ctx; |
| 479 |
|
| 480 |
struct lru_test_operation test_operations[] = { |
| 481 |
DEFINE_LRU_TEST_OPERATION(a, 1, false), |
| 482 |
DEFINE_LRU_TEST_OPERATION(a, 1, true), |
| 483 |
}; |
| 484 |
|
| 485 |
ASSERT_SUCCESS(s_perform_lru_sequence_test(allocator, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 486 |
|
| 487 |
return AWS_OP_SUCCESS; |
| 488 |
} |
| 489 |
|
| 490 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_a_ar, s_mqtt5_outbound_topic_alias_lru_a_ar_fn) |
| 491 |
|
| 492 |
static int s_mqtt5_outbound_topic_alias_lru_b_a_br_fn(struct aws_allocator *allocator, void *ctx) { |
| 493 |
(void)ctx; |
| 494 |
|
| 495 |
struct lru_test_operation test_operations[] = { |
| 496 |
DEFINE_LRU_TEST_OPERATION(b, 1, false), |
| 497 |
DEFINE_LRU_TEST_OPERATION(a, 2, false), |
| 498 |
DEFINE_LRU_TEST_OPERATION(b, 1, true), |
| 499 |
}; |
| 500 |
|
| 501 |
ASSERT_SUCCESS(s_perform_lru_sequence_test(allocator, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 502 |
|
| 503 |
return AWS_OP_SUCCESS; |
| 504 |
} |
| 505 |
|
| 506 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_b_a_br, s_mqtt5_outbound_topic_alias_lru_b_a_br_fn) |
| 507 |
|
| 508 |
static int s_mqtt5_outbound_topic_alias_lru_a_b_ar_br_fn(struct aws_allocator *allocator, void *ctx) { |
| 509 |
(void)ctx; |
| 510 |
|
| 511 |
struct lru_test_operation test_operations[] = { |
| 512 |
DEFINE_LRU_TEST_OPERATION(a, 1, false), |
| 513 |
DEFINE_LRU_TEST_OPERATION(b, 2, false), |
| 514 |
DEFINE_LRU_TEST_OPERATION(a, 1, true), |
| 515 |
DEFINE_LRU_TEST_OPERATION(b, 2, true), |
| 516 |
}; |
| 517 |
|
| 518 |
ASSERT_SUCCESS(s_perform_lru_sequence_test(allocator, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 519 |
|
| 520 |
return AWS_OP_SUCCESS; |
| 521 |
} |
| 522 |
|
| 523 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_a_b_ar_br, s_mqtt5_outbound_topic_alias_lru_a_b_ar_br_fn) |
| 524 |
|
| 525 |
static int s_mqtt5_outbound_topic_alias_lru_a_b_c_br_cr_br_cr_a_fn(struct aws_allocator *allocator, void *ctx) { |
| 526 |
(void)ctx; |
| 527 |
|
| 528 |
struct lru_test_operation test_operations[] = { |
| 529 |
DEFINE_LRU_TEST_OPERATION(a, 1, false), |
| 530 |
DEFINE_LRU_TEST_OPERATION(b, 2, false), |
| 531 |
DEFINE_LRU_TEST_OPERATION(c, 1, false), |
| 532 |
DEFINE_LRU_TEST_OPERATION(b, 2, true), |
| 533 |
DEFINE_LRU_TEST_OPERATION(c, 1, true), |
| 534 |
DEFINE_LRU_TEST_OPERATION(b, 2, true), |
| 535 |
DEFINE_LRU_TEST_OPERATION(c, 1, true), |
| 536 |
DEFINE_LRU_TEST_OPERATION(a, 2, false), |
| 537 |
}; |
| 538 |
|
| 539 |
ASSERT_SUCCESS(s_perform_lru_sequence_test(allocator, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 540 |
|
| 541 |
return AWS_OP_SUCCESS; |
| 542 |
} |
| 543 |
|
| 544 |
AWS_TEST_CASE( |
| 545 |
mqtt5_outbound_topic_alias_lru_a_b_c_br_cr_br_cr_a, |
| 546 |
s_mqtt5_outbound_topic_alias_lru_a_b_c_br_cr_br_cr_a_fn) |
| 547 |
|
| 548 |
static int s_mqtt5_outbound_topic_alias_lru_a_b_c_a_cr_b_fn(struct aws_allocator *allocator, void *ctx) { |
| 549 |
(void)ctx; |
| 550 |
|
| 551 |
struct lru_test_operation test_operations[] = { |
| 552 |
DEFINE_LRU_TEST_OPERATION(a, 1, false), |
| 553 |
DEFINE_LRU_TEST_OPERATION(b, 2, false), |
| 554 |
DEFINE_LRU_TEST_OPERATION(c, 1, false), |
| 555 |
DEFINE_LRU_TEST_OPERATION(a, 2, false), |
| 556 |
DEFINE_LRU_TEST_OPERATION(c, 1, true), |
| 557 |
DEFINE_LRU_TEST_OPERATION(b, 2, false), |
| 558 |
}; |
| 559 |
|
| 560 |
ASSERT_SUCCESS(s_perform_lru_sequence_test(allocator, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 561 |
|
| 562 |
return AWS_OP_SUCCESS; |
| 563 |
} |
| 564 |
|
| 565 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_a_b_c_a_cr_b, s_mqtt5_outbound_topic_alias_lru_a_b_c_a_cr_b_fn) |
| 566 |
|
| 567 |
static int s_mqtt5_outbound_topic_alias_lru_a_b_reset_a_b_fn(struct aws_allocator *allocator, void *ctx) { |
| 568 |
(void)ctx; |
| 569 |
|
| 570 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver = |
| 571 |
aws_mqtt5_outbound_topic_alias_resolver_new(allocator, AWS_MQTT5_COTABT_LRU); |
| 572 |
ASSERT_NOT_NULL(resolver); |
| 573 |
|
| 574 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, LRU_SEQUENCE_TEST_CACHE_SIZE); |
| 575 |
|
| 576 |
struct lru_test_operation test_operations[] = { |
| 577 |
DEFINE_LRU_TEST_OPERATION(a, 1, false), |
| 578 |
DEFINE_LRU_TEST_OPERATION(b, 2, false), |
| 579 |
}; |
| 580 |
|
| 581 |
ASSERT_SUCCESS(s_check_lru_sequence(resolver, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 582 |
|
| 583 |
aws_mqtt5_outbound_topic_alias_resolver_reset(resolver, LRU_SEQUENCE_TEST_CACHE_SIZE); |
| 584 |
|
| 585 |
ASSERT_SUCCESS(s_check_lru_sequence(resolver, test_operations, AWS_ARRAY_SIZE(test_operations))); |
| 586 |
|
| 587 |
aws_mqtt5_outbound_topic_alias_resolver_destroy(resolver); |
| 588 |
|
| 589 |
return AWS_OP_SUCCESS; |
| 590 |
} |
| 591 |
|
| 592 |
AWS_TEST_CASE(mqtt5_outbound_topic_alias_lru_a_b_reset_a_b, s_mqtt5_outbound_topic_alias_lru_a_b_reset_a_b_fn) |