test_routerlist.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. download_want_authority_t want_authority)
  14. {
  15. (void)dir_purpose;
  16. (void)router_purpose;
  17. (void)pds_flags;
  18. (void)want_authority;
  19. tt_assert(resource);
  20. strlcpy(output, resource, sizeof(output));
  21. done:
  22. ;
  23. }
  24. static void
  25. test_routerlist_initiate_descriptor_downloads(void *arg)
  26. {
  27. const char *prose = "unhurried and wise, we perceive.";
  28. smartlist_t *digests = smartlist_new();
  29. (void)arg;
  30. for (int i = 0; i < 20; i++) {
  31. smartlist_add(digests, (char*)prose);
  32. }
  33. MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
  34. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  35. digests, 3, 7, 0);
  36. UNMOCK(directory_get_from_dirserver);
  37. tt_str_op(output, OP_EQ, "d/"
  38. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  39. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  40. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  41. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
  42. ".z");
  43. done:
  44. smartlist_free(digests);
  45. }
  46. static int count = 0;
  47. static void
  48. mock_initiate_descriptor_downloads(const routerstatus_t *source,
  49. int purpose, smartlist_t *digests,
  50. int lo, int hi, int pds_flags)
  51. {
  52. (void)source;
  53. (void)purpose;
  54. (void)digests;
  55. (void)pds_flags;
  56. (void)hi;
  57. (void)lo;
  58. count += 1;
  59. }
  60. static void
  61. test_routerlist_launch_descriptor_downloads(void *arg)
  62. {
  63. smartlist_t *downloadable = smartlist_new();
  64. time_t now = time(NULL);
  65. char *cp;
  66. (void)arg;
  67. for (int i = 0; i < 100; i++) {
  68. cp = tor_malloc(DIGEST256_LEN);
  69. tt_assert(cp);
  70. crypto_rand(cp, DIGEST256_LEN);
  71. smartlist_add(downloadable, cp);
  72. }
  73. MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
  74. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
  75. NULL, now);
  76. tt_int_op(3, ==, count);
  77. UNMOCK(initiate_descriptor_downloads);
  78. done:
  79. SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
  80. smartlist_free(downloadable);
  81. }
  82. #define NODE(name, flags) \
  83. { #name, test_routerlist_##name, (flags), NULL, NULL }
  84. struct testcase_t routerlist_tests[] = {
  85. NODE(initiate_descriptor_downloads, 0),
  86. NODE(launch_descriptor_downloads, 0),
  87. END_OF_TESTCASES
  88. };