test_confparse.c 26 KB

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