test_confparse.c 30 KB

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