test_confparse.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. -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_badcsvi1 =
  432. { "csv_interval 10 wl\n", "malformed" };
  433. static const badval_test_t bv_badcsvi2 =
  434. { "csv_interval cl,10\n", "malformed" };
  435. static const badval_test_t bv_nonoption = { "fnord 10\n", "Unknown option" };
  436. static const badval_test_t bv_badmem = { "mem 3 trits\n", "malformed" };
  437. static const badval_test_t bv_badbool = { "boolean 7\n", "Unrecognized value"};
  438. static const badval_test_t bv_badabool =
  439. { "autobool 7\n", "Unrecognized value" };
  440. static const badval_test_t bv_badtime = { "time lunchtime\n", "Invalid time" };
  441. static const badval_test_t bv_virt = { "MixedLines 7\n", "virtual option" };
  442. static const badval_test_t bv_rs = { "Routerset 2.2.2.2.2\n", "Invalid" };
  443. static const badval_test_t bv_big_interval =
  444. { "interval 1000 months", "too large" };
  445. /* Try config_dump(), and make sure it behaves correctly */
  446. static void
  447. test_confparse_dump(void *arg)
  448. {
  449. (void)arg;
  450. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  451. config_mgr_freeze(mgr);
  452. test_struct_t *tst = get_simple_config(mgr);
  453. char *dumped = NULL;
  454. /* Minimal version. */
  455. dumped = config_dump(mgr, NULL, tst, 1, 0);
  456. tt_str_op(dumped, OP_EQ,
  457. "autobool 0\n"
  458. "boolean 1\n"
  459. "csv configuration,parsing,system\n"
  460. "csv_interval 10\n"
  461. "dbl 6.060842\n"
  462. "fn /simple/test of the\n"
  463. "i 3\n"
  464. "interval 300\n"
  465. "lines hello\n"
  466. "lines world\n"
  467. "mem 10\n"
  468. "VisibleLineB ABC\n"
  469. "LineTypeA i d\n"
  470. "LineTypeB i c\n"
  471. "msec_interval 300000\n"
  472. "pos 77\n"
  473. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  474. "s this is a\n"
  475. "time 2019-06-14 13:58:51\n"
  476. "u64 1000000000000\n");
  477. tor_free(dumped);
  478. dumped = config_dump(mgr, NULL, tst, 0, 0);
  479. tt_str_op(dumped, OP_EQ,
  480. "autobool 0\n"
  481. "boolean 1\n"
  482. "csv configuration,parsing,system\n"
  483. "csv_interval 10\n"
  484. "dbl 6.060842\n"
  485. "deprecated_int 3\n"
  486. "fn /simple/test of the\n"
  487. "i 3\n"
  488. "interval 300\n"
  489. "lines hello\n"
  490. "lines world\n"
  491. "mem 10\n"
  492. "VisibleLineB ABC\n"
  493. "LineTypeA i d\n"
  494. "LineTypeB i c\n"
  495. "msec_interval 300000\n"
  496. "pos 77\n"
  497. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  498. "s this is a\n"
  499. "time 2019-06-14 13:58:51\n"
  500. "u64 1000000000000\n");
  501. /* commented */
  502. tor_free(dumped);
  503. dumped = config_dump(mgr, NULL, tst, 0, 1);
  504. tt_str_op(dumped, OP_EQ,
  505. "autobool 0\n"
  506. "boolean 1\n"
  507. "csv configuration,parsing,system\n"
  508. "csv_interval 10\n"
  509. "dbl 6.060842\n"
  510. "# deprecated_int 3\n"
  511. "fn /simple/test of the\n"
  512. "i 3\n"
  513. "interval 300\n"
  514. "lines hello\n"
  515. "lines world\n"
  516. "mem 10\n"
  517. "VisibleLineB ABC\n"
  518. "LineTypeA i d\n"
  519. "LineTypeB i c\n"
  520. "msec_interval 300000\n"
  521. "pos 77\n"
  522. "routerset $FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n"
  523. "s this is a\n"
  524. "time 2019-06-14 13:58:51\n"
  525. "u64 1000000000000\n");
  526. done:
  527. config_free(mgr, tst);
  528. tor_free(dumped);
  529. config_mgr_free(mgr);
  530. }
  531. /* Try confparse_reset_line(), and make sure it behaves correctly */
  532. static void
  533. test_confparse_reset(void *arg)
  534. {
  535. (void)arg;
  536. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  537. config_mgr_freeze(mgr);
  538. test_struct_t *tst = get_simple_config(mgr);
  539. config_reset_line(mgr, tst, "interval", 0);
  540. tt_int_op(tst->interval, OP_EQ, 0);
  541. config_reset_line(mgr, tst, "interval", 1);
  542. tt_int_op(tst->interval, OP_EQ, 10);
  543. done:
  544. config_free(mgr, tst);
  545. config_mgr_free(mgr);
  546. }
  547. /* Try setting options a second time on a config object, and make sure
  548. * it behaves correctly. */
  549. static void
  550. test_confparse_reassign(void *arg)
  551. {
  552. (void)arg;
  553. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  554. config_mgr_freeze(mgr);
  555. test_struct_t *tst = get_simple_config(mgr);
  556. config_line_t *lines = NULL;
  557. char *msg = NULL, *rs = NULL;
  558. int r = config_get_lines(
  559. "s eleven\n"
  560. "i 12\n"
  561. "lines 13\n"
  562. "csv 14,15\n"
  563. "routerset 127.0.0.1\n",
  564. &lines, 0);
  565. r = config_assign(mgr, tst,lines, 0, &msg);
  566. tt_int_op(r, OP_EQ, 0);
  567. tt_ptr_op(msg, OP_EQ, NULL);
  568. tt_str_op(tst->s, OP_EQ, "eleven");
  569. tt_str_op(tst->fn, OP_EQ, "/simple/test of the"); // unchanged
  570. tt_int_op(tst->pos, OP_EQ, 77); // unchanged
  571. tt_int_op(tst->i, OP_EQ, 12);
  572. tt_ptr_op(tst->lines, OP_NE, NULL);
  573. tt_str_op(tst->lines->key, OP_EQ, "lines");
  574. tt_str_op(tst->lines->value, OP_EQ, "13");
  575. tt_ptr_op(tst->lines->next, OP_EQ, NULL);
  576. tt_int_op(smartlist_len(tst->csv), OP_EQ, 2);
  577. tt_str_op(smartlist_get(tst->csv, 0), OP_EQ, "14");
  578. tt_str_op(smartlist_get(tst->csv, 1), OP_EQ, "15");
  579. rs = routerset_to_string(tst->routerset);
  580. tt_str_op(rs, OP_EQ, "127.0.0.1");
  581. // Try again with the CLEAR_FIRST and USE_DEFAULTS flags
  582. r = config_assign(mgr, tst, lines,
  583. CAL_CLEAR_FIRST|CAL_USE_DEFAULTS, &msg);
  584. tt_int_op(r, OP_EQ, 0);
  585. tt_ptr_op(msg, OP_EQ, NULL);
  586. tt_str_op(tst->s, OP_EQ, "eleven");
  587. // tt_ptr_op(tst->fn, OP_EQ, NULL); //XXXX why is this not cleared?
  588. // tt_int_op(tst->pos, OP_EQ, 0); //XXXX why is this not cleared?
  589. tt_int_op(tst->i, OP_EQ, 12);
  590. done:
  591. config_free(mgr, tst);
  592. config_free_lines(lines);
  593. tor_free(msg);
  594. tor_free(rs);
  595. config_mgr_free(mgr);
  596. }
  597. /* Try setting options a second time on a config object, using the +foo
  598. * linelist-extending syntax. */
  599. static void
  600. test_confparse_reassign_extend(void *arg)
  601. {
  602. (void)arg;
  603. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  604. config_mgr_freeze(mgr);
  605. test_struct_t *tst = get_simple_config(mgr);
  606. config_line_t *lines = NULL;
  607. char *msg = NULL;
  608. int r = config_get_lines(
  609. "+lines 13\n",
  610. &lines, 1); // allow extended format.
  611. tt_int_op(r, OP_EQ, 0);
  612. r = config_assign(mgr, 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);
  616. tt_str_op(tst->lines->key, OP_EQ, "lines");
  617. tt_str_op(tst->lines->value, OP_EQ, "hello");
  618. tt_assert(tst->lines->next);
  619. tt_str_op(tst->lines->next->key, OP_EQ, "lines");
  620. tt_str_op(tst->lines->next->value, OP_EQ, "world");
  621. tt_assert(tst->lines->next->next);
  622. tt_str_op(tst->lines->next->next->key, OP_EQ, "lines");
  623. tt_str_op(tst->lines->next->next->value, OP_EQ, "13");
  624. tt_assert(tst->lines->next->next->next == NULL);
  625. config_free_lines(lines);
  626. r = config_get_lines(
  627. "/lines\n",
  628. &lines, 1); // allow extended format.
  629. tt_int_op(r, OP_EQ, 0);
  630. r = config_assign(mgr, tst, lines, 0, &msg);
  631. tt_int_op(r, OP_EQ, 0);
  632. tt_ptr_op(msg, OP_EQ, NULL);
  633. tt_assert(tst->lines == NULL);
  634. config_free_lines(lines);
  635. config_free(mgr, tst);
  636. tst = get_simple_config(mgr);
  637. r = config_get_lines(
  638. "/lines away!\n",
  639. &lines, 1); // allow extended format.
  640. tt_int_op(r, OP_EQ, 0);
  641. r = config_assign(mgr, tst, lines, 0, &msg);
  642. tt_int_op(r, OP_EQ, 0);
  643. tt_ptr_op(msg, OP_EQ, NULL);
  644. tt_assert(tst->lines == NULL);
  645. done:
  646. config_free(mgr, tst);
  647. config_free_lines(lines);
  648. tor_free(msg);
  649. config_mgr_free(mgr);
  650. }
  651. /* Test out confparse_get_assigned(). */
  652. static void
  653. test_confparse_get_assigned(void *arg)
  654. {
  655. (void)arg;
  656. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  657. config_mgr_freeze(mgr);
  658. test_struct_t *tst = get_simple_config(mgr);
  659. config_line_t *lines = NULL;
  660. lines = config_get_assigned_option(mgr, tst, "I", 1);
  661. tt_assert(lines);
  662. tt_str_op(lines->key, OP_EQ, "i");
  663. tt_str_op(lines->value, OP_EQ, "3");
  664. tt_assert(lines->next == NULL);
  665. config_free_lines(lines);
  666. lines = config_get_assigned_option(mgr, tst, "s", 1);
  667. tt_assert(lines);
  668. tt_str_op(lines->key, OP_EQ, "s");
  669. tt_str_op(lines->value, OP_EQ, "this is a");
  670. tt_assert(lines->next == NULL);
  671. config_free_lines(lines);
  672. lines = config_get_assigned_option(mgr, tst, "obsolete", 1);
  673. tt_assert(!lines);
  674. lines = config_get_assigned_option(mgr, tst, "nonesuch", 1);
  675. tt_assert(!lines);
  676. lines = config_get_assigned_option(mgr, tst, "mixedlines", 1);
  677. tt_assert(lines);
  678. tt_str_op(lines->key, OP_EQ, "LineTypeA");
  679. tt_str_op(lines->value, OP_EQ, "i d");
  680. tt_assert(lines->next);
  681. tt_str_op(lines->next->key, OP_EQ, "LineTypeB");
  682. tt_str_op(lines->next->value, OP_EQ, "i c");
  683. tt_assert(lines->next->next == NULL);
  684. config_free_lines(lines);
  685. lines = config_get_assigned_option(mgr, tst, "linetypeb", 1);
  686. tt_assert(lines);
  687. tt_str_op(lines->key, OP_EQ, "LineTypeB");
  688. tt_str_op(lines->value, OP_EQ, "i c");
  689. tt_assert(lines->next == NULL);
  690. config_free_lines(lines);
  691. tor_free(tst->s);
  692. tst->s = tor_strdup("Hello\nWorld");
  693. lines = config_get_assigned_option(mgr, tst, "s", 1);
  694. tt_assert(lines);
  695. tt_str_op(lines->key, OP_EQ, "s");
  696. tt_str_op(lines->value, OP_EQ, "\"Hello\\nWorld\"");
  697. tt_assert(lines->next == NULL);
  698. config_free_lines(lines);
  699. done:
  700. config_free(mgr, tst);
  701. config_free_lines(lines);
  702. config_mgr_free(mgr);
  703. }
  704. /* Another variant, which accepts and stores unrecognized lines.*/
  705. #define ETEST_MAGIC 13371337
  706. static struct_member_t extra = {
  707. .name = "__extra",
  708. .type = CONFIG_TYPE_LINELIST,
  709. .offset = offsetof(test_struct_t, extra_lines),
  710. };
  711. static config_format_t etest_fmt = {
  712. sizeof(test_struct_t),
  713. {
  714. "test_struct_t (with extra lines)",
  715. ETEST_MAGIC,
  716. offsetof(test_struct_t, magic),
  717. },
  718. test_abbrevs,
  719. test_deprecation_notes,
  720. test_vars,
  721. test_validate_cb,
  722. NULL,
  723. &extra,
  724. -1,
  725. };
  726. /* Try out the feature where we can store unrecognized lines and dump them
  727. * again. (State files use this.) */
  728. static void
  729. test_confparse_extra_lines(void *arg)
  730. {
  731. (void)arg;
  732. config_mgr_t *mgr = config_mgr_new(&etest_fmt);
  733. config_mgr_freeze(mgr);
  734. test_struct_t *tst = config_new(mgr);
  735. config_line_t *lines = NULL;
  736. char *msg = NULL, *dump = NULL;
  737. config_init(mgr, tst);
  738. int r = config_get_lines(
  739. "unknotty addita\n"
  740. "pos 99\n"
  741. "wombat knish\n", &lines, 0);
  742. tt_int_op(r, OP_EQ, 0);
  743. r = config_assign(mgr, tst, lines, 0, &msg);
  744. tt_int_op(r, OP_EQ, 0);
  745. tt_ptr_op(msg, OP_EQ, NULL);
  746. tt_assert(tst->extra_lines);
  747. dump = config_dump(mgr, NULL, tst, 1, 0);
  748. tt_str_op(dump, OP_EQ,
  749. "pos 99\n"
  750. "unknotty addita\n"
  751. "wombat knish\n");
  752. done:
  753. tor_free(msg);
  754. tor_free(dump);
  755. config_free_lines(lines);
  756. config_free(mgr, tst);
  757. config_mgr_free(mgr);
  758. }
  759. static void
  760. test_confparse_unitparse(void *args)
  761. {
  762. (void)args;
  763. /* spot-check a few memunit values. */
  764. int ok = 3;
  765. tt_u64_op(config_parse_memunit("100 MB", &ok), OP_EQ, 100<<20);
  766. tt_assert(ok);
  767. tt_u64_op(config_parse_memunit("100 TB", &ok), OP_EQ, UINT64_C(100)<<40);
  768. tt_assert(ok);
  769. // This is a floating-point value, but note that 1.5 can be represented
  770. // precisely.
  771. tt_u64_op(config_parse_memunit("1.5 MB", &ok), OP_EQ, 3<<19);
  772. tt_assert(ok);
  773. /* Try some good intervals and msec intervals */
  774. tt_int_op(config_parse_interval("2 days", &ok), OP_EQ, 48*3600);
  775. tt_assert(ok);
  776. tt_int_op(config_parse_interval("1.5 hour", &ok), OP_EQ, 5400);
  777. tt_assert(ok);
  778. tt_u64_op(config_parse_interval("1 minute", &ok), OP_EQ, 60);
  779. tt_assert(ok);
  780. tt_int_op(config_parse_msec_interval("2 days", &ok), OP_EQ, 48*3600*1000);
  781. tt_assert(ok);
  782. tt_int_op(config_parse_msec_interval("10 msec", &ok), OP_EQ, 10);
  783. tt_assert(ok);
  784. /* Try a couple of unitless values. */
  785. tt_int_op(config_parse_interval("10", &ok), OP_EQ, 10);
  786. tt_assert(ok);
  787. tt_u64_op(config_parse_interval("15.0", &ok), OP_EQ, 15);
  788. tt_assert(ok);
  789. /* u64 overflow */
  790. /* XXXX our implementation does not currently detect this. See bug 30920. */
  791. /*
  792. tt_u64_op(config_parse_memunit("20000000 TB", &ok), OP_EQ, 0);
  793. tt_assert(!ok);
  794. */
  795. /* i32 overflow */
  796. tt_int_op(config_parse_interval("1000 months", &ok), OP_EQ, -1);
  797. tt_assert(!ok);
  798. tt_int_op(config_parse_msec_interval("4 weeks", &ok), OP_EQ, -1);
  799. tt_assert(!ok);
  800. /* bad units */
  801. tt_u64_op(config_parse_memunit("7 nybbles", &ok), OP_EQ, 0);
  802. tt_assert(!ok);
  803. // XXXX these next two should return -1 according to the documentation.
  804. tt_int_op(config_parse_interval("7 cowznofski", &ok), OP_EQ, 0);
  805. tt_assert(!ok);
  806. tt_int_op(config_parse_msec_interval("1 kalpa", &ok), OP_EQ, 0);
  807. tt_assert(!ok);
  808. done:
  809. ;
  810. }
  811. static void
  812. test_confparse_check_ok_fail(void *arg)
  813. {
  814. (void)arg;
  815. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  816. config_mgr_freeze(mgr);
  817. test_struct_t *tst = config_new(mgr);
  818. tst->pos = -10;
  819. tt_assert(! config_check_ok(mgr, tst, LOG_INFO));
  820. done:
  821. config_free(mgr, tst);
  822. config_mgr_free(mgr);
  823. }
  824. static void
  825. test_confparse_list_vars(void *arg)
  826. {
  827. (void)arg;
  828. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  829. smartlist_t *vars = config_mgr_list_vars(mgr);
  830. smartlist_t *varnames = smartlist_new();
  831. char *joined = NULL;
  832. tt_assert(vars);
  833. SMARTLIST_FOREACH(vars, config_var_t *, cv,
  834. smartlist_add(varnames, (void*)cv->member.name));
  835. smartlist_sort_strings(varnames);
  836. joined = smartlist_join_strings(varnames, "::", 0, NULL);
  837. tt_str_op(joined, OP_EQ,
  838. "LineTypeA::"
  839. "LineTypeB::"
  840. "MixedHiddenLines::"
  841. "MixedLines::"
  842. "VisibleLineB::"
  843. "__HiddenInt::"
  844. "__HiddenLineA::"
  845. "autobool::"
  846. "boolean::"
  847. "csv::"
  848. "csv_interval::"
  849. "dbl::"
  850. "deprecated_int::"
  851. "fn::"
  852. "i::"
  853. "interval::"
  854. "lines::"
  855. "mem::"
  856. "msec_interval::"
  857. "obsolete::"
  858. "pos::"
  859. "routerset::"
  860. "s::"
  861. "time::"
  862. "u64");
  863. done:
  864. tor_free(joined);
  865. smartlist_free(varnames);
  866. smartlist_free(vars);
  867. config_mgr_free(mgr);
  868. }
  869. static void
  870. test_confparse_list_deprecated(void *arg)
  871. {
  872. (void)arg;
  873. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  874. smartlist_t *vars = config_mgr_list_deprecated_vars(mgr);
  875. char *joined = NULL;
  876. tt_assert(vars);
  877. smartlist_sort_strings(vars);
  878. joined = smartlist_join_strings(vars, "::", 0, NULL);
  879. tt_str_op(joined, OP_EQ, "deprecated_int");
  880. done:
  881. tor_free(joined);
  882. smartlist_free(vars);
  883. config_mgr_free(mgr);
  884. }
  885. static void
  886. test_confparse_find_option_name(void *arg)
  887. {
  888. (void)arg;
  889. config_mgr_t *mgr = config_mgr_new(&test_fmt);
  890. // exact match
  891. tt_str_op(config_find_option_name(mgr, "u64"), OP_EQ, "u64");
  892. // case-insensitive match
  893. tt_str_op(config_find_option_name(mgr, "S"), OP_EQ, "s");
  894. tt_str_op(config_find_option_name(mgr, "linetypea"), OP_EQ, "LineTypeA");
  895. // prefix match
  896. tt_str_op(config_find_option_name(mgr, "deprec"), OP_EQ, "deprecated_int");
  897. // explicit abbreviation
  898. tt_str_op(config_find_option_name(mgr, "uint"), OP_EQ, "pos");
  899. tt_str_op(config_find_option_name(mgr, "UINT"), OP_EQ, "pos");
  900. // no match
  901. tt_ptr_op(config_find_option_name(mgr, "absent"), OP_EQ, NULL);
  902. done:
  903. config_mgr_free(mgr);
  904. }
  905. #define CONFPARSE_TEST(name, flags) \
  906. { #name, test_confparse_ ## name, flags, NULL, NULL }
  907. #define BADVAL_TEST(name) \
  908. { "badval_" #name, test_confparse_assign_badval, 0, \
  909. &passthrough_setup, (void*)&bv_ ## name }
  910. struct testcase_t confparse_tests[] = {
  911. CONFPARSE_TEST(init, 0),
  912. CONFPARSE_TEST(assign_simple, 0),
  913. CONFPARSE_TEST(assign_obsolete, 0),
  914. CONFPARSE_TEST(assign_deprecated, 0),
  915. CONFPARSE_TEST(assign_replaced, 0),
  916. CONFPARSE_TEST(assign_emptystring, 0),
  917. CONFPARSE_TEST(assign_twice, 0),
  918. BADVAL_TEST(notint),
  919. BADVAL_TEST(negint),
  920. BADVAL_TEST(badu64),
  921. BADVAL_TEST(badcsvi1),
  922. BADVAL_TEST(badcsvi2),
  923. BADVAL_TEST(nonoption),
  924. BADVAL_TEST(badmem),
  925. BADVAL_TEST(badbool),
  926. BADVAL_TEST(badabool),
  927. BADVAL_TEST(badtime),
  928. BADVAL_TEST(virt),
  929. BADVAL_TEST(rs),
  930. BADVAL_TEST(big_interval),
  931. CONFPARSE_TEST(dump, 0),
  932. CONFPARSE_TEST(reset, 0),
  933. CONFPARSE_TEST(reassign, 0),
  934. CONFPARSE_TEST(reassign_extend, 0),
  935. CONFPARSE_TEST(get_assigned, 0),
  936. CONFPARSE_TEST(extra_lines, 0),
  937. CONFPARSE_TEST(unitparse, 0),
  938. CONFPARSE_TEST(check_ok_fail, 0),
  939. CONFPARSE_TEST(list_vars, 0),
  940. CONFPARSE_TEST(list_deprecated, 0),
  941. CONFPARSE_TEST(find_option_name, 0),
  942. END_OF_TESTCASES
  943. };