test_confparse.c 27 KB

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