test_options.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define CONFIG_PRIVATE
  6. #include "or.h"
  7. #include "confparse.h"
  8. #include "config.h"
  9. #include "test.h"
  10. typedef struct {
  11. int severity;
  12. uint32_t domain;
  13. char *msg;
  14. } logmsg_t;
  15. static smartlist_t *messages = NULL;
  16. static void
  17. log_cback(int severity, uint32_t domain, const char *msg)
  18. {
  19. logmsg_t *x = tor_malloc(sizeof(*x));
  20. x->severity = severity;
  21. x->domain = domain;
  22. x->msg = tor_strdup(msg);
  23. if (!messages)
  24. messages = smartlist_new();
  25. smartlist_add(messages, x);
  26. }
  27. static void
  28. setup_log_callback(void)
  29. {
  30. log_severity_list_t lst;
  31. memset(&lst, 0, sizeof(lst));
  32. lst.masks[LOG_ERR - LOG_ERR] = ~0;
  33. lst.masks[LOG_WARN - LOG_ERR] = ~0;
  34. lst.masks[LOG_NOTICE - LOG_ERR] = ~0;
  35. add_callback_log(&lst, log_cback);
  36. }
  37. static char *
  38. dump_logs(void)
  39. {
  40. smartlist_t *msgs;
  41. char *out;
  42. if (! messages)
  43. return tor_strdup("");
  44. msgs = smartlist_new();
  45. SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, x) {
  46. smartlist_add_asprintf(msgs, "[%s] %s",
  47. log_level_to_string(x->severity), x->msg);
  48. } SMARTLIST_FOREACH_END(x);
  49. out = smartlist_join_strings(msgs, "", 0, NULL);
  50. SMARTLIST_FOREACH(msgs, char *, cp, tor_free(cp));
  51. smartlist_free(msgs);
  52. return out;
  53. }
  54. static void
  55. clear_log_messages(void)
  56. {
  57. if (!messages)
  58. return;
  59. SMARTLIST_FOREACH(messages, logmsg_t *, m,
  60. { tor_free(m->msg); tor_free(m); });
  61. smartlist_free(messages);
  62. messages = NULL;
  63. }
  64. static void
  65. test_options_validate_impl(const char *configuration,
  66. const char *expect_errmsg,
  67. int expect_log_severity,
  68. const char *expect_log)
  69. {
  70. or_options_t *opt = options_new();
  71. or_options_t *dflt;
  72. config_line_t *cl=NULL;
  73. char *msg=NULL;
  74. int r;
  75. opt->command = CMD_RUN_TOR;
  76. options_init(opt);
  77. dflt = config_dup(&options_format, opt);
  78. clear_log_messages();
  79. r = config_get_lines(configuration, &cl, 1);
  80. tt_int_op(r, ==, 0);
  81. r = config_assign(&options_format, opt, cl, 0, 0, &msg);
  82. tt_int_op(r, ==, 0);
  83. r = options_validate(NULL, opt, dflt, 0, &msg);
  84. if (expect_errmsg && !msg) {
  85. TT_DIE(("Expected error message <%s> from <%s>, but got none.",
  86. expect_errmsg, configuration));
  87. } else if (expect_errmsg && !strstr(msg, expect_errmsg)) {
  88. TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
  89. expect_errmsg, configuration, msg));
  90. } else if (!expect_errmsg && msg) {
  91. TT_DIE(("Expected no error message from <%s> but got <%s>.",
  92. configuration, msg));
  93. }
  94. tt_int_op((r == 0), ==, (msg == NULL));
  95. if (expect_log) {
  96. int found = 0;
  97. if (messages) {
  98. SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, m) {
  99. if (m->severity == expect_log_severity &&
  100. strstr(m->msg, expect_log)) {
  101. found = 1;
  102. break;
  103. }
  104. } SMARTLIST_FOREACH_END(m);
  105. }
  106. if (!found) {
  107. tor_free(msg);
  108. msg = dump_logs();
  109. TT_DIE(("Expected log message [%s] %s from <%s>, but got <%s>.",
  110. log_level_to_string(expect_log_severity), expect_log,
  111. configuration, msg));
  112. }
  113. }
  114. done:
  115. config_free_lines(cl);
  116. or_options_free(opt);
  117. or_options_free(dflt);
  118. tor_free(msg);
  119. clear_log_messages();
  120. }
  121. #define WANT_ERR(config, msg) \
  122. test_options_validate_impl((config), (msg), 0, NULL)
  123. #define WANT_LOG(config, severity, msg) \
  124. test_options_validate_impl((config), NULL, (severity), (msg))
  125. #define WANT_ERR_LOG(config, msg, severity, logmsg) \
  126. test_options_validate_impl((config), (msg), (severity), (logmsg))
  127. #define OK(config) \
  128. test_options_validate_impl((config), NULL, 0, NULL)
  129. static void
  130. test_options_validate(void *arg)
  131. {
  132. (void)arg;
  133. setup_log_callback();
  134. WANT_ERR("ExtORPort 500000", "Invalid ExtORPort");
  135. WANT_ERR_LOG("ServerTransportOptions trebuchet",
  136. "ServerTransportOptions did not parse",
  137. LOG_WARN, "Too few arguments");
  138. OK("ServerTransportOptions trebuchet sling=snappy");
  139. OK("ServerTransportOptions trebuchet sling=");
  140. WANT_ERR_LOG("ServerTransportOptions trebuchet slingsnappy",
  141. "ServerTransportOptions did not parse",
  142. LOG_WARN, "\"slingsnappy\" is not a k=v");
  143. clear_log_messages();
  144. return;
  145. }
  146. struct testcase_t options_tests[] = {
  147. { "validate", test_options_validate, TT_FORK, NULL, NULL },
  148. END_OF_TESTCASES
  149. };