test_confparse.c 26 KB

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