test_namemap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "test/test.h"
  4. #include "lib/cc/torint.h"
  5. #include "lib/container/namemap.h"
  6. #include "lib/container/namemap_st.h"
  7. #include "lib/malloc/malloc.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. static void
  11. test_namemap_empty(void *arg)
  12. {
  13. (void)arg;
  14. namemap_t m;
  15. namemap_init(&m);
  16. namemap_t m2 = NAMEMAP_INIT();
  17. tt_uint_op(0, OP_EQ, namemap_get_size(&m));
  18. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello"));
  19. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello"));
  20. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello128"));
  21. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, ""));
  22. tt_uint_op(0, OP_EQ, namemap_get_size(&m));
  23. tt_uint_op(0, OP_EQ, namemap_get_size(&m2));
  24. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
  25. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
  26. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello128"));
  27. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, ""));
  28. tt_uint_op(0, OP_EQ, namemap_get_size(&m));
  29. done:
  30. namemap_clear(&m);
  31. namemap_clear(&m2);
  32. }
  33. static void
  34. test_namemap_toolong(void *arg)
  35. {
  36. (void)arg;
  37. namemap_t m;
  38. char *ok = NULL;
  39. char *toolong = NULL;
  40. namemap_init(&m);
  41. ok = tor_malloc_zero(MAX_NAMEMAP_NAME_LEN+1);
  42. memset(ok, 'x', MAX_NAMEMAP_NAME_LEN);
  43. toolong = tor_malloc_zero(MAX_NAMEMAP_NAME_LEN+2);
  44. memset(toolong, 'x', MAX_NAMEMAP_NAME_LEN+1);
  45. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, ok));
  46. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, toolong));
  47. unsigned u1 = namemap_get_or_create_id(&m, toolong);
  48. unsigned u2 = namemap_get_or_create_id(&m, ok);
  49. tt_uint_op(u1, OP_EQ, NAMEMAP_ERR);
  50. tt_uint_op(u2, OP_NE, NAMEMAP_ERR);
  51. tt_uint_op(u2, OP_EQ, namemap_get_id(&m, ok));
  52. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, toolong));
  53. tt_str_op(ok, OP_EQ, namemap_get_name(&m, u2));
  54. tt_ptr_op(NULL, OP_EQ, namemap_get_name(&m, u1));
  55. done:
  56. tor_free(ok);
  57. tor_free(toolong);
  58. namemap_clear(&m);
  59. }
  60. static void
  61. test_namemap_blackbox(void *arg)
  62. {
  63. (void)arg;
  64. namemap_t m1, m2;
  65. namemap_init(&m1);
  66. namemap_init(&m2);
  67. unsigned u1 = namemap_get_or_create_id(&m1, "hello");
  68. unsigned u2 = namemap_get_or_create_id(&m1, "world");
  69. tt_uint_op(u1, OP_NE, NAMEMAP_ERR);
  70. tt_uint_op(u2, OP_NE, NAMEMAP_ERR);
  71. tt_uint_op(u1, OP_NE, u2);
  72. tt_uint_op(u1, OP_EQ, namemap_get_id(&m1, "hello"));
  73. tt_uint_op(u1, OP_EQ, namemap_get_or_create_id(&m1, "hello"));
  74. tt_uint_op(u2, OP_EQ, namemap_get_id(&m1, "world"));
  75. tt_uint_op(u2, OP_EQ, namemap_get_or_create_id(&m1, "world"));
  76. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m1, "HELLO"));
  77. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
  78. unsigned u3 = namemap_get_or_create_id(&m2, "hola");
  79. tt_uint_op(u3, OP_NE, NAMEMAP_ERR);
  80. tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m1, "hola"));
  81. tt_uint_op(u3, OP_EQ, namemap_get_or_create_id(&m2, "hola"));
  82. tt_uint_op(u3, OP_EQ, namemap_get_id(&m2, "hola"));
  83. unsigned int u4 = namemap_get_or_create_id(&m1, "hola");
  84. tt_uint_op(u4, OP_NE, NAMEMAP_ERR);
  85. tt_uint_op(u4, OP_EQ, namemap_get_id(&m1, "hola"));
  86. tt_uint_op(u3, OP_EQ, namemap_get_id(&m2, "hola"));
  87. tt_str_op("hello", OP_EQ, namemap_get_name(&m1, u1));
  88. tt_str_op("world", OP_EQ, namemap_get_name(&m1, u2));
  89. tt_str_op("hola", OP_EQ, namemap_get_name(&m2, u3));
  90. tt_str_op("hola", OP_EQ, namemap_get_name(&m1, u4));
  91. tt_ptr_op(NULL, OP_EQ, namemap_get_name(&m2, u3 + 10));
  92. done:
  93. namemap_clear(&m1);
  94. namemap_clear(&m2);
  95. }
  96. static void
  97. test_namemap_internals(void *arg)
  98. {
  99. (void)arg;
  100. // This test actually assumes know something about the identity layout.
  101. namemap_t m;
  102. namemap_init(&m);
  103. tt_uint_op(0, OP_EQ, namemap_get_or_create_id(&m, "that"));
  104. tt_uint_op(0, OP_EQ, namemap_get_or_create_id(&m, "that"));
  105. tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
  106. tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
  107. tt_uint_op(0, OP_EQ, namemap_get_id(&m, "that"));
  108. tt_uint_op(0, OP_EQ, namemap_get_id(&m, "that"));
  109. tt_uint_op(1, OP_EQ, namemap_get_id(&m, "is"));
  110. tt_uint_op(2, OP_EQ, namemap_get_or_create_id(&m, "not"));
  111. tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
  112. tt_uint_op(2, OP_EQ, namemap_get_or_create_id(&m, "not"));
  113. done:
  114. namemap_clear(&m);
  115. }
  116. static void
  117. test_namemap_fmt(void *arg)
  118. {
  119. (void)arg;
  120. namemap_t m = NAMEMAP_INIT();
  121. unsigned a = namemap_get_or_create_id(&m, "greetings");
  122. unsigned b = namemap_get_or_create_id(&m, "earthlings");
  123. tt_str_op(namemap_fmt_name(&m, a), OP_EQ, "greetings");
  124. tt_str_op(namemap_fmt_name(&m, b), OP_EQ, "earthlings");
  125. tt_int_op(a, OP_NE, 100);
  126. tt_int_op(b, OP_NE, 100);
  127. tt_str_op(namemap_fmt_name(&m, 100), OP_EQ, "{100}");
  128. done:
  129. namemap_clear(&m);
  130. }
  131. #define T(name) \
  132. { #name, test_namemap_ ## name , 0, NULL, NULL }
  133. struct testcase_t namemap_tests[] = {
  134. T(empty),
  135. T(toolong),
  136. T(blackbox),
  137. T(internals),
  138. T(fmt),
  139. END_OF_TESTCASES
  140. };