test_confparse.c 24 KB

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