test_voting_schedule.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "or.h"
  5. #include "voting_schedule.h"
  6. #include "test.h"
  7. static void
  8. test_voting_schedule_interval_start(void *arg)
  9. {
  10. #define next_interval voting_schedule_get_start_of_next_interval
  11. (void)arg;
  12. char buf[ISO_TIME_LEN+1];
  13. // Midnight UTC tonight (as I am writing this test)
  14. const time_t midnight = 1525651200;
  15. format_iso_time(buf, midnight);
  16. tt_str_op(buf, OP_EQ, "2018-05-07 00:00:00");
  17. /* Some simple tests with a 50-minute voting interval */
  18. tt_i64_op(next_interval(midnight, 3000, 0), OP_EQ,
  19. midnight+3000);
  20. tt_i64_op(next_interval(midnight+100, 3000, 0), OP_EQ,
  21. midnight+3000);
  22. tt_i64_op(next_interval(midnight+3000, 3000, 0), OP_EQ,
  23. midnight+6000);
  24. tt_i64_op(next_interval(midnight+3001, 3000, 0), OP_EQ,
  25. midnight+6000);
  26. /* Make sure that we roll around properly at midnight */
  27. tt_i64_op(next_interval(midnight+83000, 3000, 0), OP_EQ,
  28. midnight+84000);
  29. /* We start fresh at midnight UTC, even if there are leftover seconds. */
  30. tt_i64_op(next_interval(midnight+84005, 3000, 0), OP_EQ,
  31. midnight+86400);
  32. /* Now try with offsets. (These are only used for test networks.) */
  33. tt_i64_op(next_interval(midnight, 3000, 99), OP_EQ,
  34. midnight+99);
  35. tt_i64_op(next_interval(midnight+100, 3000, 99), OP_EQ,
  36. midnight+3099);
  37. done:
  38. ;
  39. #undef next_interval
  40. }
  41. #define VS(name,flags) \
  42. { #name, test_voting_schedule_##name, (flags), NULL, NULL }
  43. struct testcase_t voting_schedule_tests[] = {
  44. VS(interval_start, 0),
  45. END_OF_TESTCASES
  46. };