test_confparse.c 26 KB

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