test_protover.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define PROTOVER_PRIVATE
  4. #include "orconfig.h"
  5. #include "test.h"
  6. #include "protover.h"
  7. static void
  8. test_protover_parse(void *arg)
  9. {
  10. (void) arg;
  11. char *re_encoded = NULL;
  12. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  13. smartlist_t *elts = parse_protocol_list(orig);
  14. tt_assert(elts);
  15. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  16. const proto_entry_t *e;
  17. const proto_range_t *r;
  18. e = smartlist_get(elts, 0);
  19. tt_str_op(e->name, OP_EQ, "Foo");
  20. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  21. {
  22. r = smartlist_get(e->ranges, 0);
  23. tt_int_op(r->low, OP_EQ, 1);
  24. tt_int_op(r->high, OP_EQ, 1);
  25. r = smartlist_get(e->ranges, 1);
  26. tt_int_op(r->low, OP_EQ, 3);
  27. tt_int_op(r->high, OP_EQ, 3);
  28. }
  29. e = smartlist_get(elts, 1);
  30. tt_str_op(e->name, OP_EQ, "Bar");
  31. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  32. {
  33. r = smartlist_get(e->ranges, 0);
  34. tt_int_op(r->low, OP_EQ, 3);
  35. tt_int_op(r->high, OP_EQ, 3);
  36. }
  37. e = smartlist_get(elts, 2);
  38. tt_str_op(e->name, OP_EQ, "Baz");
  39. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  40. e = smartlist_get(elts, 3);
  41. tt_str_op(e->name, OP_EQ, "Quux");
  42. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  43. {
  44. r = smartlist_get(e->ranges, 0);
  45. tt_int_op(r->low, OP_EQ, 9);
  46. tt_int_op(r->high, OP_EQ, 12);
  47. r = smartlist_get(e->ranges, 1);
  48. tt_int_op(r->low, OP_EQ, 14);
  49. tt_int_op(r->high, OP_EQ, 14);
  50. r = smartlist_get(e->ranges, 2);
  51. tt_int_op(r->low, OP_EQ, 15);
  52. tt_int_op(r->high, OP_EQ, 16);
  53. r = smartlist_get(e->ranges, 3);
  54. tt_int_op(r->low, OP_EQ, 900);
  55. tt_int_op(r->high, OP_EQ, 900);
  56. }
  57. re_encoded = encode_protocol_list(elts);
  58. tt_assert(re_encoded);
  59. tt_str_op(re_encoded, OP_EQ, orig);
  60. done:
  61. if (elts)
  62. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  63. smartlist_free(elts);
  64. tor_free(re_encoded);
  65. }
  66. #define PV_TEST(name, flags) \
  67. { #name, test_protover_ ##name, (flags), NULL, NULL }
  68. struct testcase_t protover_tests[] = {
  69. PV_TEST(parse, 0),
  70. END_OF_TESTCASES
  71. };