test_confparse.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /*
  6. * Tests for confparse.c module that we use to parse various
  7. * configuration/state file types.
  8. */
  9. #define CONFPARSE_PRIVATE
  10. #include "orconfig.h"
  11. #include "core/or/or.h"
  12. #include "lib/encoding/confline.h"
  13. #include "feature/nodelist/routerset.h"
  14. #include "app/config/confparse.h"
  15. #include "test/test.h"
  16. #include "test/log_test_helpers.h"
  17. #include "lib/confmgt/unitparse.h"
  18. typedef struct test_struct_t {
  19. uint32_t magic;
  20. char *s;
  21. char *fn;
  22. int pos;
  23. int i;
  24. int deprecated_int;
  25. uint64_t u64;
  26. int interval;
  27. int msec_interval;
  28. uint64_t mem;
  29. double dbl;
  30. int boolean;
  31. int autobool;
  32. time_t time;
  33. smartlist_t *csv;
  34. int csv_interval;
  35. config_line_t *lines;
  36. config_line_t *mixed_lines;
  37. routerset_t *routerset;
  38. int hidden_int;
  39. config_line_t *mixed_hidden_lines;
  40. config_line_t *extra_lines;
  41. } test_struct_t;
  42. static test_struct_t test_struct_t_dummy;
  43. #define VAR(varname,conftype,member,initvalue) \
  44. { { .name = varname, \
  45. .type = CONFIG_TYPE_##conftype, \
  46. .offset = offsetof(test_struct_t, member), }, \
  47. initvalue CONF_TEST_MEMBERS(test_struct_t, conftype, member) }
  48. #define V(name,conftype,initvalue) \
  49. VAR( #name, conftype, name, initvalue )
  50. #define OBSOLETE(varname) \
  51. { { .name=varname, .type=CONFIG_TYPE_OBSOLETE }, NULL, {.INT=NULL} }
  52. static config_var_t test_vars[] = {
  53. V(s, STRING, "hello"),
  54. V(fn, FILENAME, NULL),
  55. V(pos, POSINT, NULL),
  56. V(i, INT, "-10"),
  57. V(deprecated_int, INT, "3"),
  58. V(u64, UINT64, NULL),
  59. V(interval, INTERVAL, "10 seconds"),
  60. V(msec_interval, MSEC_INTERVAL, "150 msec"),
  61. V(mem, MEMUNIT, "10 MB"),
  62. V(dbl, DOUBLE, NULL),
  63. V(boolean, BOOL, "0"),
  64. V(autobool, AUTOBOOL, "auto"),
  65. V(time, ISOTIME, NULL),
  66. V(csv, CSV, NULL),
  67. V(csv_interval, CSV_INTERVAL, "5 seconds"),
  68. V(lines, LINELIST, NULL),
  69. VAR("MixedLines", LINELIST_V, mixed_lines, NULL),
  70. VAR("LineTypeA", LINELIST_S, mixed_lines, NULL),
  71. VAR("LineTypeB", LINELIST_S, mixed_lines, NULL),
  72. OBSOLETE("obsolete"),
  73. {
  74. { .name = "routerset",
  75. .type = CONFIG_TYPE_ROUTERSET,
  76. .type_def = &ROUTERSET_type_defn,
  77. .offset = offsetof(test_struct_t, routerset),
  78. },
  79. NULL, {.INT=NULL}
  80. },
  81. VAR("__HiddenInt", POSINT, hidden_int, "0"),
  82. VAR("MixedHiddenLines", LINELIST_V, mixed_hidden_lines, NULL),
  83. VAR("__HiddenLineA", LINELIST_S, mixed_hidden_lines, NULL),
  84. VAR("VisibleLineB", LINELIST_S, mixed_hidden_lines, NULL),
  85. END_OF_CONFIG_VARS,
  86. };
  87. static config_abbrev_t test_abbrevs[] = {
  88. { "uint", "pos", 0, 0 },
  89. { "float", "dbl", 0, 1 },
  90. { NULL, NULL, 0, 0 }
  91. };
  92. static config_deprecation_t test_deprecation_notes[] = {
  93. { "deprecated_int", "This integer is deprecated." },
  94. { NULL, NULL }
  95. };
  96. static int
  97. test_validate_cb(void *old_options, void *options, void *default_options,
  98. int from_setconf, char **msg)
  99. {
  100. (void)old_options;
  101. (void)default_options;
  102. (void)from_setconf;
  103. (void)msg;
  104. test_struct_t *ts = options;
  105. if (ts->i == 0xbad) {
  106. *msg = tor_strdup("bad value for i");
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static void test_free_cb(void *options);
  112. #define TEST_MAGIC 0x1337
  113. static config_format_t test_fmt = {
  114. sizeof(test_struct_t),
  115. {
  116. "test_struct_t",
  117. TEST_MAGIC,
  118. offsetof(test_struct_t, magic),
  119. },
  120. test_abbrevs,
  121. test_deprecation_notes,
  122. test_vars,
  123. test_validate_cb,
  124. test_free_cb,
  125. NULL,
  126. };
  127. static void
  128. test_free_cb(void *options)
  129. {
  130. if (!options)
  131. return;
  132. config_free(&test_fmt, options);
  133. }
  134. /* Make sure that config_init sets everything to the right defaults. */
  135. static void
  136. test_confparse_init(void *arg)
  137. {
  138. (void)arg;
  139. test_struct_t *tst = config_new(&test_fmt);
  140. config_init(&test_fmt, tst);
  141. // Make sure that options are initialized right. */
  142. tt_uint_op(tst->magic, OP_EQ, TEST_MAGIC);
  143. tt_str_op(tst->s, OP_EQ, "hello");
  144. tt_ptr_op(tst->fn, OP_EQ, NULL);
  145. tt_int_op(tst->pos, OP_EQ, 0);
  146. tt_int_op(tst->i, OP_EQ, -10);
  147. tt_int_op(tst->deprecated_int, OP_EQ, 3);
  148. tt_u64_op(tst->u64, OP_EQ, 0);
  149. tt_int_op(tst->interval, OP_EQ, 10);
  150. tt_int_op(tst->msec_interval, OP_EQ, 150);
  151. tt_u64_op(tst->mem, OP_EQ, 10 * 1024 * 1024);
  152. tt_double_op(tst->dbl, OP_LT, .0000000001);
  153. tt_double_op(tst->dbl, OP_GT, -0.0000000001);
  154. tt_int_op(tst->boolean, OP_EQ, 0);
  155. tt_int_op(tst->autobool, OP_EQ, -1);
  156. tt_i64_op(tst->time, OP_EQ, 0);
  157. tt_ptr_op(tst->csv, OP_EQ, NULL);
  158. tt_int_op(tst->csv_interval, OP_EQ, 5);
  159. tt_ptr_op(tst->lines, OP_EQ, NULL);
  160. tt_ptr_op(tst->mixed_lines, OP_EQ, NULL);
  161. tt_int_op(tst->hidden_int, OP_EQ, 0);
  162. done:
  163. config_free(&test_fmt, tst);
  164. }
  165. static const char simple_settings[] =
  166. "s this is a \n"
  167. "fn /simple/test of the\n"
  168. "uint 77\n" // this is an abbrev
  169. "i 3\n"
  170. "u64 1000000000000 \n"
  171. "interval 5 minutes \n"
  172. "msec_interval 5 minutes \n"
  173. "mem 10\n"
  174. "dbl 6.060842\n"
  175. "BOOLEAN 1\n"
  176. "aUtObOOl 0\n"
  177. "time 2019-06-14 13:58:51\n"
  178. "csv configuration, parsing , system \n"
  179. "csv_interval 10 seconds, 5 seconds, 10 hours\n"
  180. "lines hello\n"
  181. "LINES world\n"
  182. "linetypea i d\n"
  183. "linetypeb i c\n"
  184. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  185. "__hiddenint 11\n"
  186. "__hiddenlineA XYZ\n"
  187. "visiblelineB ABC\n";
  188. /* Return a configuration object set up from simple_settings above. */
  189. static test_struct_t *
  190. get_simple_config(void)
  191. {
  192. test_struct_t *result = NULL;
  193. test_struct_t *tst = config_new(&test_fmt);
  194. config_line_t *lines = NULL;
  195. char *msg = NULL;
  196. config_init(&test_fmt, tst);
  197. int r = config_get_lines(simple_settings, &lines, 0);
  198. tt_int_op(r, OP_EQ, 0);
  199. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  200. tt_int_op(r, OP_EQ, 0);
  201. tt_ptr_op(msg, OP_EQ, NULL);
  202. result = tst;
  203. tst = NULL; // prevent free
  204. done:
  205. tor_free(msg);
  206. config_free_lines(lines);
  207. config_free(&test_fmt, tst);
  208. return result;
  209. }
  210. /* Make sure that config_assign can parse things. */
  211. static void
  212. test_confparse_assign_simple(void *arg)
  213. {
  214. (void)arg;
  215. test_struct_t *tst = get_simple_config();
  216. tt_str_op(tst->s, OP_EQ, "this is a");
  217. tt_str_op(tst->fn, OP_EQ, "/simple/test of the");
  218. tt_int_op(tst->pos, OP_EQ, 77);
  219. tt_int_op(tst->i, OP_EQ, 3);
  220. tt_int_op(tst->deprecated_int, OP_EQ, 3);
  221. tt_u64_op(tst->u64, OP_EQ, UINT64_C(1000000000000));
  222. tt_int_op(tst->interval, OP_EQ, 5 * 60);
  223. tt_int_op(tst->msec_interval, OP_EQ, 5 * 60 * 1000);
  224. tt_u64_op(tst->mem, OP_EQ, 10);
  225. tt_double_op(tst->dbl, OP_LT, 6.060843);
  226. tt_double_op(tst->dbl, OP_GT, 6.060841);
  227. tt_int_op(tst->boolean, OP_EQ, 1);
  228. tt_int_op(tst->autobool, OP_EQ, 0);
  229. tt_i64_op(tst->time, OP_EQ, 1560520731);
  230. tt_ptr_op(tst->csv, OP_NE, NULL);
  231. tt_int_op(smartlist_len(tst->csv), OP_EQ, 3);
  232. tt_str_op(smartlist_get(tst->csv, 0), OP_EQ, "configuration");
  233. tt_str_op(smartlist_get(tst->csv, 1), OP_EQ, "parsing");
  234. tt_str_op(smartlist_get(tst->csv, 2), OP_EQ, "system");
  235. tt_int_op(tst->csv_interval, OP_EQ, 10);
  236. tt_int_op(tst->hidden_int, OP_EQ, 11);
  237. tt_assert(tst->lines);
  238. tt_str_op(tst->lines->key, OP_EQ, "lines");
  239. tt_str_op(tst->lines->value, OP_EQ, "hello");
  240. tt_assert(tst->lines->next);
  241. tt_str_op(tst->lines->next->key, OP_EQ, "lines");
  242. tt_str_op(tst->lines->next->value, OP_EQ, "world");
  243. tt_assert(!tst->lines->next->next);
  244. tt_assert(tst->mixed_lines);
  245. tt_str_op(tst->mixed_lines->key, OP_EQ, "LineTypeA");
  246. tt_str_op(tst->mixed_lines->value, OP_EQ, "i d");
  247. tt_assert(tst->mixed_lines->next);
  248. tt_str_op(tst->mixed_lines->next->key, OP_EQ, "LineTypeB");
  249. tt_str_op(tst->mixed_lines->next->value, OP_EQ, "i c");
  250. tt_assert(!tst->mixed_lines->next->next);
  251. tt_assert(tst->mixed_hidden_lines);
  252. tt_str_op(tst->mixed_hidden_lines->key, OP_EQ, "__HiddenLineA");
  253. tt_str_op(tst->mixed_hidden_lines->value, OP_EQ, "XYZ");
  254. tt_assert(tst->mixed_hidden_lines->next);
  255. tt_str_op(tst->mixed_hidden_lines->next->key, OP_EQ, "VisibleLineB");
  256. tt_str_op(tst->mixed_hidden_lines->next->value, OP_EQ, "ABC");
  257. tt_assert(!tst->mixed_hidden_lines->next->next);
  258. tt_assert(config_check_ok(&test_fmt, tst, LOG_ERR));
  259. done:
  260. config_free(&test_fmt, tst);
  261. }
  262. /* Try to assign to an obsolete option, and make sure we get a warning. */
  263. static void
  264. test_confparse_assign_obsolete(void *arg)
  265. {
  266. (void)arg;
  267. test_struct_t *tst = config_new(&test_fmt);
  268. config_line_t *lines = NULL;
  269. char *msg = NULL;
  270. config_init(&test_fmt, tst);
  271. int r = config_get_lines("obsolete option here",
  272. &lines, 0);
  273. tt_int_op(r, OP_EQ, 0);
  274. setup_capture_of_logs(LOG_WARN);
  275. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  276. tt_int_op(r, OP_EQ, 0);
  277. tt_ptr_op(msg, OP_EQ, NULL);
  278. expect_single_log_msg_containing("Skipping obsolete configuration option");
  279. done:
  280. teardown_capture_of_logs();
  281. config_free(&test_fmt, tst);
  282. config_free_lines(lines);
  283. tor_free(msg);
  284. }
  285. /* Try to assign to an deprecated option, and make sure we get a warning
  286. * but the assignment works anyway. */
  287. static void
  288. test_confparse_assign_deprecated(void *arg)
  289. {
  290. (void)arg;
  291. test_struct_t *tst = config_new(&test_fmt);
  292. config_line_t *lines = NULL;
  293. char *msg = NULL;
  294. config_init(&test_fmt, tst);
  295. int r = config_get_lines("deprecated_int 7",
  296. &lines, 0);
  297. tt_int_op(r, OP_EQ, 0);
  298. setup_capture_of_logs(LOG_WARN);
  299. r = config_assign(&test_fmt, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
  300. tt_int_op(r, OP_EQ, 0);
  301. tt_ptr_op(msg, OP_EQ, NULL);
  302. expect_single_log_msg_containing("This integer is deprecated.");
  303. tt_int_op(tst->deprecated_int, OP_EQ, 7);
  304. tt_assert(config_check_ok(&test_fmt, tst, LOG_ERR));
  305. done:
  306. teardown_capture_of_logs();
  307. config_free(&test_fmt, tst);
  308. config_free_lines(lines);
  309. tor_free(msg);
  310. }
  311. /* Try to re-assign an option name that has been depreacted in favor of
  312. * another. */
  313. static void
  314. test_confparse_assign_replaced(void *arg)
  315. {
  316. (void)arg;
  317. test_struct_t *tst = config_new(&test_fmt);
  318. config_line_t *lines = NULL;
  319. char *msg = NULL;
  320. config_init(&test_fmt, tst);
  321. int r = config_get_lines("float 1000\n", &lines, 0);
  322. tt_int_op(r, OP_EQ, 0);
  323. setup_capture_of_logs(LOG_WARN);
  324. r = config_assign(&test_fmt, tst, lines, CAL_WARN_DEPRECATIONS, &msg);
  325. tt_int_op(r, OP_EQ, 0);
  326. tt_ptr_op(msg, OP_EQ, NULL);
  327. expect_single_log_msg_containing("use 'dbl' instead.");
  328. tt_double_op(tst->dbl, OP_GT, 999.999);
  329. tt_double_op(tst->dbl, OP_LT, 1000.001);
  330. done:
  331. teardown_capture_of_logs();
  332. config_free(&test_fmt, tst);
  333. config_free_lines(lines);
  334. tor_free(msg);
  335. }
  336. /* Try to set a linelist value with no option. */
  337. static void
  338. test_confparse_assign_emptystring(void *arg)
  339. {
  340. (void)arg;
  341. test_struct_t *tst = config_new(&test_fmt);
  342. config_line_t *lines = NULL;
  343. char *msg = NULL;
  344. config_init(&test_fmt, tst);
  345. int r = config_get_lines("lines\n", &lines, 0);
  346. tt_int_op(r, OP_EQ, 0);
  347. setup_capture_of_logs(LOG_WARN);
  348. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  349. tt_int_op(r, OP_EQ, 0);
  350. tt_ptr_op(msg, OP_EQ, NULL);
  351. expect_single_log_msg_containing("has no value");
  352. done:
  353. teardown_capture_of_logs();
  354. config_free(&test_fmt, tst);
  355. config_free_lines(lines);
  356. tor_free(msg);
  357. }
  358. /* Try to set a the same option twice; make sure we get a warning. */
  359. static void
  360. test_confparse_assign_twice(void *arg)
  361. {
  362. (void)arg;
  363. test_struct_t *tst = config_new(&test_fmt);
  364. config_line_t *lines = NULL;
  365. char *msg = NULL;
  366. config_init(&test_fmt, tst);
  367. int r = config_get_lines("pos 10\n"
  368. "pos 99\n", &lines, 0);
  369. tt_int_op(r, OP_EQ, 0);
  370. setup_capture_of_logs(LOG_WARN);
  371. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  372. tt_int_op(r, OP_EQ, 0);
  373. tt_ptr_op(msg, OP_EQ, NULL);
  374. expect_single_log_msg_containing("used more than once");
  375. done:
  376. teardown_capture_of_logs();
  377. config_free(&test_fmt, tst);
  378. config_free_lines(lines);
  379. tor_free(msg);
  380. }
  381. typedef struct badval_test_t {
  382. const char *cfg;
  383. const char *expect_msg;
  384. } badval_test_t;
  385. /* Try to set an option and make sure that we get a failure and an expected
  386. * warning. */
  387. static void
  388. test_confparse_assign_badval(void *arg)
  389. {
  390. const badval_test_t *bt = arg;
  391. test_struct_t *tst = config_new(&test_fmt);
  392. config_line_t *lines = NULL;
  393. char *msg = NULL;
  394. config_init(&test_fmt, tst);
  395. int r = config_get_lines(bt->cfg, &lines, 0);
  396. tt_int_op(r, OP_EQ, 0);
  397. setup_capture_of_logs(LOG_WARN);
  398. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  399. tt_int_op(r, OP_LT, 0);
  400. tt_ptr_op(msg, OP_NE, NULL);
  401. if (! strstr(msg, bt->expect_msg)) {
  402. TT_DIE(("'%s' did not contain '%s'" , msg, bt->expect_msg));
  403. }
  404. done:
  405. teardown_capture_of_logs();
  406. config_free(&test_fmt, tst);
  407. config_free_lines(lines);
  408. tor_free(msg);
  409. }
  410. /* Various arguments for badval test.
  411. *
  412. * Note that the expected warnings here are _very_ truncated, since we
  413. * are writing these tests before a refactoring that we expect will
  414. * change them.
  415. */
  416. static const badval_test_t bv_notint = { "pos X\n", "malformed" };
  417. static const badval_test_t bv_negint = { "pos -10\n", "out of bounds" };
  418. static const badval_test_t bv_badu64 = { "u64 u64\n", "malformed" };
  419. static const badval_test_t bv_badcsvi1 =
  420. { "csv_interval 10 wl\n", "malformed" };
  421. static const badval_test_t bv_badcsvi2 =
  422. { "csv_interval cl,10\n", "malformed" };
  423. static const badval_test_t bv_nonoption = { "fnord 10\n", "Unknown option" };
  424. static const badval_test_t bv_badmem = { "mem 3 trits\n", "malformed" };
  425. static const badval_test_t bv_badbool = { "boolean 7\n", "Unrecognized value"};
  426. static const badval_test_t bv_badabool =
  427. { "autobool 7\n", "Unrecognized value" };
  428. static const badval_test_t bv_badtime = { "time lunchtime\n", "Invalid time" };
  429. static const badval_test_t bv_virt = { "MixedLines 7\n", "virtual option" };
  430. static const badval_test_t bv_rs = { "Routerset 2.2.2.2.2\n", "Invalid" };
  431. static const badval_test_t bv_big_interval =
  432. { "interval 1000 months", "too large" };
  433. /* Try config_dump(), and make sure it behaves correctly */
  434. static void
  435. test_confparse_dump(void *arg)
  436. {
  437. (void)arg;
  438. test_struct_t *tst = get_simple_config();
  439. char *dumped = NULL;
  440. /* Minimal version. */
  441. dumped = config_dump(&test_fmt, NULL, tst, 1, 0);
  442. tt_str_op(dumped, OP_EQ,
  443. "s this is a\n"
  444. "fn /simple/test of the\n"
  445. "pos 77\n"
  446. "i 3\n"
  447. "u64 1000000000000\n"
  448. "interval 300\n"
  449. "msec_interval 300000\n"
  450. "mem 10\n"
  451. "dbl 6.060842\n"
  452. "boolean 1\n"
  453. "autobool 0\n"
  454. "time 2019-06-14 13:58:51\n"
  455. "csv configuration,parsing,system\n"
  456. "csv_interval 10\n"
  457. "lines hello\n"
  458. "lines world\n"
  459. "LineTypeA i d\n"
  460. "LineTypeB i c\n"
  461. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  462. "VisibleLineB ABC\n");
  463. /* Maximal */
  464. tor_free(dumped);
  465. dumped = config_dump(&test_fmt, NULL, tst, 0, 0);
  466. tt_str_op(dumped, OP_EQ,
  467. "s this is a\n"
  468. "fn /simple/test of the\n"
  469. "pos 77\n"
  470. "i 3\n"
  471. "deprecated_int 3\n"
  472. "u64 1000000000000\n"
  473. "interval 300\n"
  474. "msec_interval 300000\n"
  475. "mem 10\n"
  476. "dbl 6.060842\n"
  477. "boolean 1\n"
  478. "autobool 0\n"
  479. "time 2019-06-14 13:58:51\n"
  480. "csv configuration,parsing,system\n"
  481. "csv_interval 10\n"
  482. "lines hello\n"
  483. "lines world\n"
  484. "LineTypeA i d\n"
  485. "LineTypeB i c\n"
  486. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  487. "VisibleLineB ABC\n");
  488. /* commented */
  489. tor_free(dumped);
  490. dumped = config_dump(&test_fmt, NULL, tst, 0, 1);
  491. tt_str_op(dumped, OP_EQ,
  492. "s this is a\n"
  493. "fn /simple/test of the\n"
  494. "pos 77\n"
  495. "i 3\n"
  496. "# deprecated_int 3\n"
  497. "u64 1000000000000\n"
  498. "interval 300\n"
  499. "msec_interval 300000\n"
  500. "mem 10\n"
  501. "dbl 6.060842\n"
  502. "boolean 1\n"
  503. "autobool 0\n"
  504. "time 2019-06-14 13:58:51\n"
  505. "csv configuration,parsing,system\n"
  506. "csv_interval 10\n"
  507. "lines hello\n"
  508. "lines world\n"
  509. "LineTypeA i d\n"
  510. "LineTypeB i c\n"
  511. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  512. "VisibleLineB ABC\n");
  513. done:
  514. config_free(&test_fmt, tst);
  515. tor_free(dumped);
  516. }
  517. /* Try confparse_reset_line(), and make sure it behaves correctly */
  518. static void
  519. test_confparse_reset(void *arg)
  520. {
  521. (void)arg;
  522. test_struct_t *tst = get_simple_config();
  523. config_reset_line(&test_fmt, tst, "interval", 0);
  524. tt_int_op(tst->interval, OP_EQ, 0);
  525. config_reset_line(&test_fmt, tst, "interval", 1);
  526. tt_int_op(tst->interval, OP_EQ, 10);
  527. done:
  528. config_free(&test_fmt, tst);
  529. }
  530. /* Try setting options a second time on a config object, and make sure
  531. * it behaves correctly. */
  532. static void
  533. test_confparse_reassign(void *arg)
  534. {
  535. (void)arg;
  536. test_struct_t *tst = get_simple_config();
  537. config_line_t *lines = NULL;
  538. char *msg = NULL, *rs = NULL;
  539. int r = config_get_lines(
  540. "s eleven\n"
  541. "i 12\n"
  542. "lines 13\n"
  543. "csv 14,15\n"
  544. "routerset 127.0.0.1\n",
  545. &lines, 0);
  546. r = config_assign(&test_fmt, tst,lines, 0, &msg);
  547. tt_int_op(r, OP_EQ, 0);
  548. tt_ptr_op(msg, OP_EQ, NULL);
  549. tt_str_op(tst->s, OP_EQ, "eleven");
  550. tt_str_op(tst->fn, OP_EQ, "/simple/test of the"); // unchanged
  551. tt_int_op(tst->pos, OP_EQ, 77); // unchanged
  552. tt_int_op(tst->i, OP_EQ, 12);
  553. tt_ptr_op(tst->lines, OP_NE, NULL);
  554. tt_str_op(tst->lines->key, OP_EQ, "lines");
  555. tt_str_op(tst->lines->value, OP_EQ, "13");
  556. tt_ptr_op(tst->lines->next, OP_EQ, NULL);
  557. tt_int_op(smartlist_len(tst->csv), OP_EQ, 2);
  558. tt_str_op(smartlist_get(tst->csv, 0), OP_EQ, "14");
  559. tt_str_op(smartlist_get(tst->csv, 1), OP_EQ, "15");
  560. rs = routerset_to_string(tst->routerset);
  561. tt_str_op(rs, OP_EQ, "127.0.0.1");
  562. // Try again with the CLEAR_FIRST and USE_DEFAULTS flags
  563. r = config_assign(&test_fmt, tst, lines,
  564. CAL_CLEAR_FIRST|CAL_USE_DEFAULTS, &msg);
  565. tt_int_op(r, OP_EQ, 0);
  566. tt_ptr_op(msg, OP_EQ, NULL);
  567. tt_str_op(tst->s, OP_EQ, "eleven");
  568. // tt_ptr_op(tst->fn, OP_EQ, NULL); //XXXX why is this not cleared?
  569. // tt_int_op(tst->pos, OP_EQ, 0); //XXXX why is this not cleared?
  570. tt_int_op(tst->i, OP_EQ, 12);
  571. done:
  572. config_free(&test_fmt, tst);
  573. config_free_lines(lines);
  574. tor_free(msg);
  575. tor_free(rs);
  576. }
  577. /* Try setting options a second time on a config object, using the +foo
  578. * linelist-extending syntax. */
  579. static void
  580. test_confparse_reassign_extend(void *arg)
  581. {
  582. (void)arg;
  583. test_struct_t *tst = get_simple_config();
  584. config_line_t *lines = NULL;
  585. char *msg = NULL;
  586. int r = config_get_lines(
  587. "+lines 13\n",
  588. &lines, 1); // allow extended format.
  589. tt_int_op(r, OP_EQ, 0);
  590. r = config_assign(&test_fmt, tst,lines, 0, &msg);
  591. tt_int_op(r, OP_EQ, 0);
  592. tt_ptr_op(msg, OP_EQ, NULL);
  593. tt_assert(tst->lines);
  594. tt_str_op(tst->lines->key, OP_EQ, "lines");
  595. tt_str_op(tst->lines->value, OP_EQ, "hello");
  596. tt_assert(tst->lines->next);
  597. tt_str_op(tst->lines->next->key, OP_EQ, "lines");
  598. tt_str_op(tst->lines->next->value, OP_EQ, "world");
  599. tt_assert(tst->lines->next->next);
  600. tt_str_op(tst->lines->next->next->key, OP_EQ, "lines");
  601. tt_str_op(tst->lines->next->next->value, OP_EQ, "13");
  602. tt_assert(tst->lines->next->next->next == NULL);
  603. config_free_lines(lines);
  604. r = config_get_lines(
  605. "/lines\n",
  606. &lines, 1); // allow extended format.
  607. tt_int_op(r, OP_EQ, 0);
  608. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  609. tt_int_op(r, OP_EQ, 0);
  610. tt_ptr_op(msg, OP_EQ, NULL);
  611. tt_assert(tst->lines == NULL);
  612. config_free_lines(lines);
  613. config_free(&test_fmt, tst);
  614. tst = get_simple_config();
  615. r = config_get_lines(
  616. "/lines away!\n",
  617. &lines, 1); // allow extended format.
  618. tt_int_op(r, OP_EQ, 0);
  619. r = config_assign(&test_fmt, tst, lines, 0, &msg);
  620. tt_int_op(r, OP_EQ, 0);
  621. tt_ptr_op(msg, OP_EQ, NULL);
  622. tt_assert(tst->lines == NULL);
  623. done:
  624. config_free(&test_fmt, tst);
  625. config_free_lines(lines);
  626. tor_free(msg);
  627. }
  628. /* Test out confparse_get_assigned(). */
  629. static void
  630. test_confparse_get_assigned(void *arg)
  631. {
  632. (void)arg;
  633. test_struct_t *tst = get_simple_config();
  634. config_line_t *lines = NULL;
  635. lines = config_get_assigned_option(&test_fmt, tst, "I", 1);
  636. tt_assert(lines);
  637. tt_str_op(lines->key, OP_EQ, "i");
  638. tt_str_op(lines->value, OP_EQ, "3");
  639. tt_assert(lines->next == NULL);
  640. config_free_lines(lines);
  641. lines = config_get_assigned_option(&test_fmt, tst, "s", 1);
  642. tt_assert(lines);
  643. tt_str_op(lines->key, OP_EQ, "s");
  644. tt_str_op(lines->value, OP_EQ, "this is a");
  645. tt_assert(lines->next == NULL);
  646. config_free_lines(lines);
  647. lines = config_get_assigned_option(&test_fmt, tst, "obsolete", 1);
  648. tt_assert(!lines);
  649. lines = config_get_assigned_option(&test_fmt, tst, "nonesuch", 1);
  650. tt_assert(!lines);
  651. lines = config_get_assigned_option(&test_fmt, tst, "mixedlines", 1);
  652. tt_assert(lines);
  653. tt_str_op(lines->key, OP_EQ, "LineTypeA");
  654. tt_str_op(lines->value, OP_EQ, "i d");
  655. tt_assert(lines->next);
  656. tt_str_op(lines->next->key, OP_EQ, "LineTypeB");
  657. tt_str_op(lines->next->value, OP_EQ, "i c");
  658. tt_assert(lines->next->next == NULL);
  659. config_free_lines(lines);
  660. lines = config_get_assigned_option(&test_fmt, tst, "linetypeb", 1);
  661. tt_assert(lines);
  662. tt_str_op(lines->key, OP_EQ, "LineTypeB");
  663. tt_str_op(lines->value, OP_EQ, "i c");
  664. tt_assert(lines->next == NULL);
  665. config_free_lines(lines);
  666. tor_free(tst->s);
  667. tst->s = tor_strdup("Hello\nWorld");
  668. lines = config_get_assigned_option(&test_fmt, tst, "s", 1);
  669. tt_assert(lines);
  670. tt_str_op(lines->key, OP_EQ, "s");
  671. tt_str_op(lines->value, OP_EQ, "\"Hello\\nWorld\"");
  672. tt_assert(lines->next == NULL);
  673. config_free_lines(lines);
  674. done:
  675. config_free(&test_fmt, tst);
  676. config_free_lines(lines);
  677. }
  678. /* Another variant, which accepts and stores unrecognized lines.*/
  679. #define ETEST_MAGIC 13371337
  680. static struct_member_t extra = {
  681. .name = "__extra",
  682. .type = CONFIG_TYPE_LINELIST,
  683. .offset = offsetof(test_struct_t, extra_lines),
  684. };
  685. static config_format_t etest_fmt = {
  686. sizeof(test_struct_t),
  687. {
  688. "test_struct_t (with extra lines)",
  689. ETEST_MAGIC,
  690. offsetof(test_struct_t, magic),
  691. },
  692. test_abbrevs,
  693. test_deprecation_notes,
  694. test_vars,
  695. test_validate_cb,
  696. test_free_cb,
  697. &extra,
  698. };
  699. /* Try out the feature where we can store unrecognized lines and dump them
  700. * again. (State files use this.) */
  701. static void
  702. test_confparse_extra_lines(void *arg)
  703. {
  704. (void)arg;
  705. test_struct_t *tst = config_new(&etest_fmt);
  706. config_line_t *lines = NULL;
  707. char *msg = NULL, *dump = NULL;
  708. config_init(&etest_fmt, tst);
  709. int r = config_get_lines(
  710. "unknotty addita\n"
  711. "pos 99\n"
  712. "wombat knish\n", &lines, 0);
  713. tt_int_op(r, OP_EQ, 0);
  714. r = config_assign(&etest_fmt, tst, lines, 0, &msg);
  715. tt_int_op(r, OP_EQ, 0);
  716. tt_ptr_op(msg, OP_EQ, NULL);
  717. tt_assert(tst->extra_lines);
  718. dump = config_dump(&etest_fmt, NULL, tst, 1, 0);
  719. tt_str_op(dump, OP_EQ,
  720. "pos 99\n"
  721. "unknotty addita\n"
  722. "wombat knish\n");
  723. done:
  724. tor_free(msg);
  725. tor_free(dump);
  726. config_free_lines(lines);
  727. config_free(&etest_fmt, tst);
  728. }
  729. static void
  730. test_confparse_unitparse(void *args)
  731. {
  732. (void)args;
  733. /* spot-check a few memunit values. */
  734. int ok = 3;
  735. tt_u64_op(config_parse_memunit("100 MB", &ok), OP_EQ, 100<<20);
  736. tt_assert(ok);
  737. tt_u64_op(config_parse_memunit("100 TB", &ok), OP_EQ, UINT64_C(100)<<40);
  738. tt_assert(ok);
  739. // This is a floating-point value, but note that 1.5 can be represented
  740. // precisely.
  741. tt_u64_op(config_parse_memunit("1.5 MB", &ok), OP_EQ, 3<<19);
  742. tt_assert(ok);
  743. /* Try some good intervals and msec intervals */
  744. tt_int_op(config_parse_interval("2 days", &ok), OP_EQ, 48*3600);
  745. tt_assert(ok);
  746. tt_int_op(config_parse_interval("1.5 hour", &ok), OP_EQ, 5400);
  747. tt_assert(ok);
  748. tt_u64_op(config_parse_interval("1 minute", &ok), OP_EQ, 60);
  749. tt_assert(ok);
  750. tt_int_op(config_parse_msec_interval("2 days", &ok), OP_EQ, 48*3600*1000);
  751. tt_assert(ok);
  752. tt_int_op(config_parse_msec_interval("10 msec", &ok), OP_EQ, 10);
  753. tt_assert(ok);
  754. /* Try a couple of unitless values. */
  755. tt_int_op(config_parse_interval("10", &ok), OP_EQ, 10);
  756. tt_assert(ok);
  757. tt_u64_op(config_parse_interval("15.0", &ok), OP_EQ, 15);
  758. tt_assert(ok);
  759. /* u64 overflow */
  760. /* XXXX our implementation does not currently detect this. See bug 30920. */
  761. /*
  762. tt_u64_op(config_parse_memunit("20000000 TB", &ok), OP_EQ, 0);
  763. tt_assert(!ok);
  764. */
  765. /* i32 overflow */
  766. tt_int_op(config_parse_interval("1000 months", &ok), OP_EQ, -1);
  767. tt_assert(!ok);
  768. tt_int_op(config_parse_msec_interval("4 weeks", &ok), OP_EQ, -1);
  769. tt_assert(!ok);
  770. /* bad units */
  771. tt_u64_op(config_parse_memunit("7 nybbles", &ok), OP_EQ, 0);
  772. tt_assert(!ok);
  773. // XXXX these next two should return -1 according to the documentation.
  774. tt_int_op(config_parse_interval("7 cowznofski", &ok), OP_EQ, 0);
  775. tt_assert(!ok);
  776. tt_int_op(config_parse_msec_interval("1 kalpa", &ok), OP_EQ, 0);
  777. tt_assert(!ok);
  778. done:
  779. ;
  780. }
  781. static void
  782. test_confparse_check_ok_fail(void *arg)
  783. {
  784. (void)arg;
  785. test_struct_t *tst = config_new(&test_fmt);
  786. tst->pos = -10;
  787. tt_assert(! config_check_ok(&test_fmt, tst, LOG_INFO));
  788. done:
  789. config_free(&test_fmt, tst);
  790. }
  791. #define CONFPARSE_TEST(name, flags) \
  792. { #name, test_confparse_ ## name, flags, NULL, NULL }
  793. #define BADVAL_TEST(name) \
  794. { "badval_" #name, test_confparse_assign_badval, 0, \
  795. &passthrough_setup, (void*)&bv_ ## name }
  796. struct testcase_t confparse_tests[] = {
  797. CONFPARSE_TEST(init, 0),
  798. CONFPARSE_TEST(assign_simple, 0),
  799. CONFPARSE_TEST(assign_obsolete, 0),
  800. CONFPARSE_TEST(assign_deprecated, 0),
  801. CONFPARSE_TEST(assign_replaced, 0),
  802. CONFPARSE_TEST(assign_emptystring, 0),
  803. CONFPARSE_TEST(assign_twice, 0),
  804. BADVAL_TEST(notint),
  805. BADVAL_TEST(negint),
  806. BADVAL_TEST(badu64),
  807. BADVAL_TEST(badcsvi1),
  808. BADVAL_TEST(badcsvi2),
  809. BADVAL_TEST(nonoption),
  810. BADVAL_TEST(badmem),
  811. BADVAL_TEST(badbool),
  812. BADVAL_TEST(badabool),
  813. BADVAL_TEST(badtime),
  814. BADVAL_TEST(virt),
  815. BADVAL_TEST(rs),
  816. BADVAL_TEST(big_interval),
  817. CONFPARSE_TEST(dump, 0),
  818. CONFPARSE_TEST(reset, 0),
  819. CONFPARSE_TEST(reassign, 0),
  820. CONFPARSE_TEST(reassign_extend, 0),
  821. CONFPARSE_TEST(get_assigned, 0),
  822. CONFPARSE_TEST(extra_lines, 0),
  823. CONFPARSE_TEST(unitparse, 0),
  824. CONFPARSE_TEST(check_ok_fail, 0),
  825. END_OF_TESTCASES
  826. };