test_confparse.c 30 KB

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