test_routerlist.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define ROUTERLIST_PRIVATE
  4. #include "or.h"
  5. #include "routerlist.h"
  6. #include "directory.h"
  7. #include "test.h"
  8. /* 4 digests + 3 sep + pre + post + NULL */
  9. static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
  10. static void
  11. mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  12. const char *resource, int pds_flags)
  13. {
  14. (void)dir_purpose;
  15. (void)router_purpose;
  16. (void)pds_flags;
  17. tt_assert(resource);
  18. strlcpy(output, resource, sizeof(output));
  19. done:
  20. ;
  21. }
  22. static void
  23. test_routerlist_initiate_descriptor_downloads(void *arg)
  24. {
  25. const char *prose = "unhurried and wise, we perceive.";
  26. smartlist_t *digests = smartlist_new();
  27. (void)arg;
  28. for (int i = 0; i < 20; i++) {
  29. smartlist_add(digests, (char*)prose);
  30. }
  31. MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
  32. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  33. digests, 3, 7, 0);
  34. UNMOCK(directory_get_from_dirserver);
  35. tt_str_op(output, OP_EQ, "d/"
  36. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  37. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  38. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  39. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
  40. ".z");
  41. done:
  42. smartlist_free(digests);
  43. }
  44. static int count = 0;
  45. static void
  46. mock_initiate_descriptor_downloads(const routerstatus_t *source,
  47. int purpose, smartlist_t *digests,
  48. int lo, int hi, int pds_flags)
  49. {
  50. (void)source;
  51. (void)purpose;
  52. (void)digests;
  53. (void)pds_flags;
  54. (void)hi;
  55. (void)lo;
  56. count += 1;
  57. }
  58. static void
  59. test_routerlist_launch_descriptor_downloads(void *arg)
  60. {
  61. smartlist_t *downloadable = smartlist_new();
  62. time_t now = time(NULL);
  63. char *cp;
  64. (void)arg;
  65. for (int i = 0; i < 100; i++) {
  66. cp = tor_malloc(DIGEST256_LEN);
  67. tt_assert(cp);
  68. crypto_rand(cp, DIGEST256_LEN);
  69. smartlist_add(downloadable, cp);
  70. }
  71. MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
  72. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
  73. NULL, now);
  74. tt_int_op(3, ==, count);
  75. UNMOCK(initiate_descriptor_downloads);
  76. done:
  77. SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
  78. smartlist_free(downloadable);
  79. }
  80. #define NODE(name, flags) \
  81. { #name, test_routerlist_##name, (flags), NULL, NULL }
  82. struct testcase_t routerlist_tests[] = {
  83. NODE(initiate_descriptor_downloads, 0),
  84. NODE(launch_descriptor_downloads, 0),
  85. END_OF_TESTCASES
  86. };