test_confparse.c 26 KB

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