test_confparse.c 27 KB

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