test_parsecommon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. #include "core/or/or.h"
  6. #include "test/test.h"
  7. #include "lib/memarea/memarea.h"
  8. #include "lib/encoding/binascii.h"
  9. #include "feature/dirparse/parsecommon.h"
  10. #include "test/log_test_helpers.h"
  11. static void
  12. test_parsecommon_tokenize_string_null(void *arg)
  13. {
  14. memarea_t *area = memarea_new();
  15. smartlist_t *tokens = smartlist_new();
  16. (void)arg;
  17. const char *str_with_null = "a\0bccccccccc";
  18. int retval =
  19. tokenize_string(area, str_with_null,
  20. str_with_null + 3,
  21. tokens, NULL, 0);
  22. tt_int_op(retval, OP_EQ, -1);
  23. done:
  24. memarea_drop_all(area);
  25. smartlist_free(tokens);
  26. return;
  27. }
  28. static void
  29. test_parsecommon_tokenize_string_multiple_lines(void *arg)
  30. {
  31. memarea_t *area = memarea_new();
  32. smartlist_t *tokens = smartlist_new();
  33. (void)arg;
  34. token_rule_t table[] = {
  35. T01("uptime", K_UPTIME, GE(1), NO_OBJ),
  36. T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ),
  37. T1( "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ),
  38. END_OF_TABLE,
  39. };
  40. char *str = tor_strdup(
  41. "hibernating 0\nuptime 1024\n"
  42. "published 2018-10-15 10:00:00\n");
  43. int retval =
  44. tokenize_string(area, str, NULL,
  45. tokens, table, 0);
  46. tt_int_op(smartlist_len(tokens), OP_EQ, 3);
  47. directory_token_t *token = smartlist_get(tokens, 0);
  48. tt_int_op(token->tp, OP_EQ, K_HIBERNATING);
  49. token = smartlist_get(tokens, 1);
  50. tt_int_op(token->tp, OP_EQ, K_UPTIME);
  51. token = smartlist_get(tokens, 2);
  52. tt_int_op(token->tp, OP_EQ, K_PUBLISHED);
  53. tt_int_op(retval, OP_EQ, 0);
  54. done:
  55. tor_free(str);
  56. memarea_drop_all(area);
  57. smartlist_free(tokens);
  58. return;
  59. }
  60. static void
  61. test_parsecommon_tokenize_string_min_cnt(void *arg)
  62. {
  63. memarea_t *area = memarea_new();
  64. smartlist_t *tokens = smartlist_new();
  65. (void)arg;
  66. token_rule_t table[] = {
  67. T01("uptime", K_UPTIME, EQ(2), NO_OBJ),
  68. T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ),
  69. END_OF_TABLE,
  70. };
  71. // Missing "uptime"
  72. char *str = tor_strdup("uptime 1024\nhibernating 0\n");
  73. int retval =
  74. tokenize_string(area, str, NULL,
  75. tokens, table, 0);
  76. tt_int_op(retval, OP_EQ, -1);
  77. done:
  78. tor_free(str);
  79. memarea_drop_all(area);
  80. smartlist_free(tokens);
  81. return;
  82. }
  83. static void
  84. test_parsecommon_tokenize_string_max_cnt(void *arg)
  85. {
  86. memarea_t *area = memarea_new();
  87. smartlist_t *tokens = smartlist_new();
  88. (void)arg;
  89. token_rule_t table[] = {
  90. T01("uptime", K_UPTIME, EQ(1), NO_OBJ),
  91. T01("hibernating", K_HIBERNATING, GE(1), NO_OBJ),
  92. END_OF_TABLE,
  93. };
  94. // "uptime" expected once, but occurs twice in input.
  95. char *str = tor_strdup(
  96. "uptime 1024\nuptime 2048\nhibernating 0\n");
  97. int retval =
  98. tokenize_string(area, str, NULL,
  99. tokens, table, 0);
  100. tt_int_op(retval, OP_EQ, -1);
  101. done:
  102. tor_free(str);
  103. memarea_drop_all(area);
  104. smartlist_free(tokens);
  105. return;
  106. }
  107. static void
  108. test_parsecommon_tokenize_string_at_start(void *arg)
  109. {
  110. memarea_t *area = memarea_new();
  111. smartlist_t *tokens = smartlist_new();
  112. (void)arg;
  113. token_rule_t table[] = {
  114. T1_START("client-name", C_CLIENT_NAME, CONCAT_ARGS, NO_OBJ),
  115. T01("uptime", K_UPTIME, EQ(1), NO_OBJ),
  116. END_OF_TABLE,
  117. };
  118. // "client-name" is not the first line.
  119. char *str = tor_strdup(
  120. "uptime 1024\nclient-name Alice\n");
  121. int retval =
  122. tokenize_string(area, str, NULL, tokens, table, 0);
  123. tt_int_op(retval, OP_EQ, -1);
  124. done:
  125. tor_free(str);
  126. memarea_drop_all(area);
  127. smartlist_free(tokens);
  128. return;
  129. }
  130. static void
  131. test_parsecommon_tokenize_string_at_end(void *arg)
  132. {
  133. memarea_t *area = memarea_new();
  134. smartlist_t *tokens = smartlist_new();
  135. (void)arg;
  136. token_rule_t table[] = {
  137. T1_END("client-name", C_CLIENT_NAME, CONCAT_ARGS, NO_OBJ),
  138. T01("uptime", K_UPTIME, EQ(1), NO_OBJ),
  139. END_OF_TABLE,
  140. };
  141. // "client-name" is not the last line.
  142. char *str = tor_strdup(
  143. "client-name Alice\nuptime 1024\n");
  144. int retval =
  145. tokenize_string(area, str, NULL, tokens, table, 0);
  146. tt_int_op(retval, OP_EQ, -1);
  147. done:
  148. tor_free(str);
  149. memarea_drop_all(area);
  150. smartlist_free(tokens);
  151. return;
  152. }
  153. static void
  154. test_parsecommon_tokenize_string_no_annotations(void *arg)
  155. {
  156. memarea_t *area = memarea_new();
  157. smartlist_t *tokens = smartlist_new();
  158. (void)arg;
  159. token_rule_t table[] = {
  160. A01("@last-listed", A_LAST_LISTED, CONCAT_ARGS, NO_OBJ),
  161. END_OF_TABLE,
  162. };
  163. char *str = tor_strdup("@last-listed 2018-09-21 15:30:03\n");
  164. int retval =
  165. tokenize_string(area, str, NULL, tokens, table, 0);
  166. tt_int_op(retval, OP_EQ, -1);
  167. done:
  168. tor_free(str);
  169. memarea_drop_all(area);
  170. smartlist_free(tokens);
  171. return;
  172. }
  173. static void
  174. test_parsecommon_get_next_token_success(void *arg)
  175. {
  176. memarea_t *area = memarea_new();
  177. const char *str = "uptime 1024";
  178. const char *end = str + strlen(str);
  179. const char **s = &str;
  180. token_rule_t table = T01("uptime", K_UPTIME, GE(1), NO_OBJ);
  181. (void)arg;
  182. directory_token_t *token = get_next_token(area, s, end, &table);
  183. tt_int_op(token->tp, OP_EQ, K_UPTIME);
  184. tt_int_op(token->n_args, OP_EQ, 1);
  185. tt_str_op(*(token->args), OP_EQ, "1024");
  186. tt_assert(!token->object_type);
  187. tt_int_op(token->object_size, OP_EQ, 0);
  188. tt_assert(!token->object_body);
  189. tt_ptr_op(*s, OP_EQ, end);
  190. done:
  191. memarea_drop_all(area);
  192. return;
  193. }
  194. static void
  195. test_parsecommon_get_next_token_concat_args(void *arg)
  196. {
  197. memarea_t *area = memarea_new();
  198. const char *str = "proto A=1 B=2";
  199. const char *end = str + strlen(str);
  200. const char **s = &str;
  201. token_rule_t rule = T01("proto", K_PROTO, CONCAT_ARGS, NO_OBJ);
  202. (void)arg;
  203. directory_token_t *token = get_next_token(area, s, end, &rule);
  204. tt_int_op(token->tp, OP_EQ, K_PROTO);
  205. tt_int_op(token->n_args, OP_EQ, 1);
  206. tt_str_op(*(token->args), OP_EQ, "A=1 B=2");
  207. done:
  208. memarea_drop_all(area);
  209. }
  210. static void
  211. test_parsecommon_get_next_token_parse_keys(void *arg)
  212. {
  213. (void)arg;
  214. memarea_t *area = memarea_new();
  215. const char *str =
  216. "onion-key\n"
  217. "-----BEGIN RSA PUBLIC KEY-----\n"
  218. "MIGJAoGBAMDdIya33BfNlHOkzoTKSTT8EjD64waMfUr372syVHiFjHhObwKwGA5u\n"
  219. "sHaMIe9r+Ij/4C1dKyuXkcz3DOl6gWNhTD7dZ89I+Okoh1jWe30jxCiAcywC22p5\n"
  220. "XLhrDkX1A63Z7XCH9ltwU2WMqWsVM98N2GR6MTujP7wtqdLExYN1AgMBAAE=\n"
  221. "-----END RSA PUBLIC KEY-----\n";
  222. const char *end = str + strlen(str);
  223. const char **s = (const char **)&str;
  224. directory_token_t *token = NULL;
  225. directory_token_t *token2 = NULL;
  226. token_rule_t rule = T1("onion-key", R_IPO_ONION_KEY, NO_ARGS, NEED_KEY_1024);
  227. token = get_next_token(area, s, end, &rule);
  228. tt_assert(token);
  229. tt_int_op(token->tp, OP_EQ, R_IPO_ONION_KEY);
  230. tt_int_op(token->n_args, OP_EQ, 0);
  231. tt_str_op(token->object_type, OP_EQ, "RSA PUBLIC KEY");
  232. tt_int_op(token->object_size, OP_EQ, 140);
  233. tt_assert(token->object_body);
  234. tt_assert(token->key);
  235. tt_assert(!token->error);
  236. const char *str2 =
  237. "client-key\n"
  238. "-----BEGIN RSA PRIVATE KEY-----\n"
  239. "MIICXAIBAAKBgQCwS810a2auH2PQchOBz9smNgjlDu31aq0IYlUohSYbhcv5AJ+d\n"
  240. "DY0nfZWzS+mZPwzL3UiEnTt6PVv7AgoZ5V9ZJWJTKIURjJpkK0mstfJKHKIZhf84\n"
  241. "pmFfRej9GQViB6NLtp1obOXJgJixSlMfw9doDI4NoAnEISCyH/tD77Qs2wIDAQAB\n"
  242. "AoGAbDg8CKkdQOnX9c7xFpCnsE8fKqz9eddgHHNwXw1NFTwOt+2gDWKSMZmv2X5S\n"
  243. "CVZg3owZxf5W0nT0D6Ny2+6nliak7foYAvkD0BsCiBhgftwC0zAo6k5rIbUKB3PJ\n"
  244. "QLFXgpJhqWuXkODyt/hS/GTernR437WVSEGp1bnALqiFabECQQDaqHOxzoWY/nvH\n"
  245. "KrfUi8EhqCnqERlRHwrW0MQZ1RPvF16OPPma+xa+ht/amfh3vYN5tZY82Zm43gGl\n"
  246. "XWL5cZhNAkEAzmdSootYVnqLLLRMfHKXnO1XbaEcA/08MDNKGlSclBJixFenE8jX\n"
  247. "iQsUbHwMJuGONvzWpRGPBP2f8xBd28ZtxwJARY+LZshtpfNniz/ixYJESaHG28je\n"
  248. "xfjbKOW3TQSFV+2WTifFvHEeljQwKMoMyoMGvYRwLCGJjs9JtMLVxsdFjQJBAKwD\n"
  249. "3BBvBQ39TuPQ1zWX4tb7zjMlY83HTFP3Sriq71tP/1QWoL2SUl56B2lp8E6vB/C3\n"
  250. "wsMK4SCNprHRYAd7VZ0CQDKn6Zhd11P94PLs0msybFEh1VXr6CEW/BrxBgbL4ls6\n"
  251. "dbX5XO0z4Ra8gYXgObgimhyMDYO98Idt5+Z3HIdyrSc=\n"
  252. "-----END RSA PRIVATE KEY-----\n";
  253. const char *end2 = str2 + strlen(str2);
  254. const char **s2 = (const char **)&str2;
  255. token_rule_t rule2 = T01("client-key", C_CLIENT_KEY, NO_ARGS,
  256. NEED_SKEY_1024);
  257. token2 = get_next_token(area, s2, end2, &rule2);
  258. tt_assert(token2);
  259. tt_int_op(token2->tp, OP_EQ, C_CLIENT_KEY);
  260. tt_int_op(token2->n_args, OP_EQ, 0);
  261. tt_str_op(token2->object_type, OP_EQ, "RSA PRIVATE KEY");
  262. tt_int_op(token2->object_size, OP_EQ, 608);
  263. tt_assert(token2->object_body);
  264. tt_assert(token2->key);
  265. tt_assert(!token->error);
  266. done:
  267. if (token) token_clear(token);
  268. if (token2) token_clear(token2);
  269. memarea_drop_all(area);
  270. }
  271. static void
  272. test_parsecommon_get_next_token_object(void *arg)
  273. {
  274. memarea_t *area = memarea_new();
  275. const char *str =
  276. "directory-signature 0232AF901C31A04EE9848595AF9BB7620D4C5B2E "
  277. "CD1FD971855430880D3C31E0331C5C55800C2F79\n"
  278. "-----BEGIN SIGNATURE-----\n"
  279. "dLTbc1Lad/OWKBJhA/dERzDHumswTAzBFAWAz2vnQhLsebs1SOm0W/vceEsiEkiF\n"
  280. "A+JJSzIyfywJc6Mnk7aKMEIFjOO/MaxuAp4zv+q+JonJkF0ExjMqvKR0D6pSFmfN\n"
  281. "cnemnxGHxNuPDnKl0imbWKmWDsHtwgi4zWeTq3MekfMOXKi6gIh+bDFzCs9/Vquh\n"
  282. "uNKJI1jW/A2DEKeaSAODEv9VoCsYSvbVVEuHCBWjeNAurd5aL26BrAolW6m7pkD6\n"
  283. "I+cQ8dQG6Wa/Zt6gLXtBbOP2o/iDI7ahDP9diNkBI/rm4nfp9j4piTwsqpi7xz9J\n"
  284. "Ua9DEZB9KbJHVX1rGShrLA==\n"
  285. "-----END SIGNATURE-----\n";
  286. const char *end = str + strlen(str);
  287. const char **s = &str;
  288. token_rule_t rule = T("directory-signature", K_DIRECTORY_SIGNATURE,
  289. GE(2), NEED_OBJ);
  290. (void)arg;
  291. directory_token_t *token = get_next_token(area, s, end, &rule);
  292. tt_int_op(token->tp, OP_EQ, K_DIRECTORY_SIGNATURE);
  293. tt_int_op(token->n_args, OP_EQ, 2);
  294. tt_str_op(token->args[0], OP_EQ,
  295. "0232AF901C31A04EE9848595AF9BB7620D4C5B2E");
  296. tt_str_op(token->args[1], OP_EQ,
  297. "CD1FD971855430880D3C31E0331C5C55800C2F79");
  298. tt_assert(!token->error);
  299. char decoded[256];
  300. const char *signature =
  301. "dLTbc1Lad/OWKBJhA/dERzDHumswTAzBFAWAz2vnQhLsebs1SOm0W/vceEsiEkiF\n"
  302. "A+JJSzIyfywJc6Mnk7aKMEIFjOO/MaxuAp4zv+q+JonJkF0ExjMqvKR0D6pSFmfN\n"
  303. "cnemnxGHxNuPDnKl0imbWKmWDsHtwgi4zWeTq3MekfMOXKi6gIh+bDFzCs9/Vquh\n"
  304. "uNKJI1jW/A2DEKeaSAODEv9VoCsYSvbVVEuHCBWjeNAurd5aL26BrAolW6m7pkD6\n"
  305. "I+cQ8dQG6Wa/Zt6gLXtBbOP2o/iDI7ahDP9diNkBI/rm4nfp9j4piTwsqpi7xz9J\n"
  306. "Ua9DEZB9KbJHVX1rGShrLA==\n";
  307. tt_assert(signature);
  308. size_t signature_len = strlen(signature);
  309. base64_decode(decoded, sizeof(decoded), signature, signature_len);
  310. tt_str_op(token->object_type, OP_EQ, "SIGNATURE");
  311. tt_int_op(token->object_size, OP_EQ, 256);
  312. tt_mem_op(token->object_body, OP_EQ, decoded, 256);
  313. tt_assert(!token->key);
  314. done:
  315. memarea_drop_all(area);
  316. }
  317. static void
  318. test_parsecommon_get_next_token_err_too_many_args(void *arg)
  319. {
  320. memarea_t *area = memarea_new();
  321. const char *str = "uptime 1024 1024 1024";
  322. const char *end = str + strlen(str);
  323. const char **s = &str;
  324. token_rule_t table = T01("uptime", K_UPTIME, EQ(1), NO_OBJ);
  325. (void)arg;
  326. directory_token_t *token = get_next_token(area, s, end, &table);
  327. tt_int_op(token->tp, OP_EQ, ERR_);
  328. tt_str_op(token->error, OP_EQ, "Too many arguments to uptime");
  329. done:
  330. memarea_drop_all(area);
  331. return;
  332. }
  333. static void
  334. test_parsecommon_get_next_token_err_too_few_args(void *arg)
  335. {
  336. memarea_t *area = memarea_new();
  337. const char *str = "uptime";
  338. const char *end = str + strlen(str);
  339. const char **s = &str;
  340. token_rule_t table = T01("uptime", K_UPTIME, EQ(1), NO_OBJ);
  341. (void)arg;
  342. directory_token_t *token = get_next_token(area, s, end, &table);
  343. tt_int_op(token->tp, OP_EQ, ERR_);
  344. tt_str_op(token->error, OP_EQ, "Too few arguments to uptime");
  345. done:
  346. memarea_drop_all(area);
  347. return;
  348. }
  349. static void
  350. test_parsecommon_get_next_token_err_obj_missing_endline(void *arg)
  351. {
  352. memarea_t *area = memarea_new();
  353. const char *str =
  354. "directory-signature 0232AF901C31A04EE9848595AF9BB7620D4C5B2E "
  355. "CD1FD971855430880D3C31E0331C5C55800C2F79\n"
  356. "-----BEGIN SIGNATURE-----\n"
  357. "dLTbc1Lad/OWKBJhA/dERzDHumswTAzBFAWAz2vnQhLsebs1SOm0W/vceEsiEkiF\n"
  358. "A+JJSzIyfywJc6Mnk7aKMEIFjOO/MaxuAp4zv+q+JonJkF0ExjMqvKR0D6pSFmfN\n"
  359. "cnemnxGHxNuPDnKl0imbWKmWDsHtwgi4zWeTq3MekfMOXKi6gIh+bDFzCs9/Vquh\n"
  360. "uNKJI1jW/A2DEKeaSAODEv9VoCsYSvbVVEuHCBWjeNAurd5aL26BrAolW6m7pkD6\n"
  361. "I+cQ8dQG6Wa/Zt6gLXtBbOP2o/iDI7ahDP9diNkBI/rm4nfp9j4piTwsqpi7xz9J\n"
  362. "Ua9DEZB9KbJHVX1rGShrLA==\n";
  363. const char *end = str + strlen(str);
  364. const char **s = &str;
  365. token_rule_t rule = T("directory-signature", K_DIRECTORY_SIGNATURE,
  366. GE(2), NEED_OBJ);
  367. (void)arg;
  368. directory_token_t *token = get_next_token(area, s, end, &rule);
  369. tt_int_op(token->tp, OP_EQ, ERR_);
  370. tt_str_op(token->error, OP_EQ, "Malformed object: missing object end line");
  371. done:
  372. memarea_drop_all(area);
  373. return;
  374. }
  375. static void
  376. test_parsecommon_get_next_token_err_bad_beginline(void *arg)
  377. {
  378. memarea_t *area = memarea_new();
  379. const char *str =
  380. "directory-signature 0232AF901C31A04EE9848595AF9BB7620D4C5B2E "
  381. "CD1FD971855430880D3C31E0331C5C55800C2F79\n"
  382. "-----BEGIN SIGNATURE-Z---\n"
  383. "dLTbc1Lad/OWKBJhA/dERzDHumswTAzBFAWAz2vnQhLsebs1SOm0W/vceEsiEkiF\n"
  384. "A+JJSzIyfywJc6Mnk7aKMEIFjOO/MaxuAp4zv+q+JonJkF0ExjMqvKR0D6pSFmfN\n"
  385. "cnemnxGHxNuPDnKl0imbWKmWDsHtwgi4zWeTq3MekfMOXKi6gIh+bDFzCs9/Vquh\n"
  386. "uNKJI1jW/A2DEKeaSAODEv9VoCsYSvbVVEuHCBWjeNAurd5aL26BrAolW6m7pkD6\n"
  387. "I+cQ8dQG6Wa/Zt6gLXtBbOP2o/iDI7ahDP9diNkBI/rm4nfp9j4piTwsqpi7xz9J\n"
  388. "Ua9DEZB9KbJHVX1rGShrLA==\n"
  389. "-----END SIGNATURE-----\n";
  390. const char *end = str + strlen(str);
  391. const char **s = &str;
  392. token_rule_t rule = T("directory-signature", K_DIRECTORY_SIGNATURE,
  393. GE(2), NEED_OBJ);
  394. (void)arg;
  395. directory_token_t *token = get_next_token(area, s, end, &rule);
  396. tt_int_op(token->tp, OP_EQ, ERR_);
  397. tt_str_op(token->error, OP_EQ, "Malformed object: bad begin line");
  398. done:
  399. memarea_drop_all(area);
  400. return;
  401. }
  402. static void
  403. test_parsecommon_get_next_token_err_tag_mismatch(void *arg)
  404. {
  405. memarea_t *area = memarea_new();
  406. const char *str =
  407. "directory-signature 0232AF901C31A04EE9848595AF9BB7620D4C5B2E "
  408. "CD1FD971855430880D3C31E0331C5C55800C2F79\n"
  409. "-----BEGIN SIGNATURE-----\n"
  410. "dLTbc1Lad/OWKBJhA/dERzDHumswTAzBFAWAz2vnQhLsebs1SOm0W/vceEsiEkiF\n"
  411. "A+JJSzIyfywJc6Mnk7aKMEIFjOO/MaxuAp4zv+q+JonJkF0ExjMqvKR0D6pSFmfN\n"
  412. "cnemnxGHxNuPDnKl0imbWKmWDsHtwgi4zWeTq3MekfMOXKi6gIh+bDFzCs9/Vquh\n"
  413. "uNKJI1jW/A2DEKeaSAODEv9VoCsYSvbVVEuHCBWjeNAurd5aL26BrAolW6m7pkD6\n"
  414. "I+cQ8dQG6Wa/Zt6gLXtBbOP2o/iDI7ahDP9diNkBI/rm4nfp9j4piTwsqpi7xz9J\n"
  415. "Ua9DEZB9KbJHVX1rGShrLA==\n"
  416. "-----END SOMETHINGELSE-----\n";
  417. const char *end = str + strlen(str);
  418. const char **s = &str;
  419. token_rule_t rule = T("directory-signature", K_DIRECTORY_SIGNATURE,
  420. GE(2), NEED_OBJ);
  421. (void)arg;
  422. directory_token_t *token = get_next_token(area, s, end, &rule);
  423. tt_int_op(token->tp, OP_EQ, ERR_);
  424. tt_str_op(token->error, OP_EQ,
  425. "Malformed object: mismatched end tag SIGNATURE");
  426. done:
  427. memarea_drop_all(area);
  428. return;
  429. }
  430. static void
  431. test_parsecommon_get_next_token_err_bad_base64(void *arg)
  432. {
  433. memarea_t *area = memarea_new();
  434. const char *str =
  435. "directory-signature 0232AF901C31A04EE9848595AF9BB7620D4C5B2E "
  436. "CD1FD971855430880D3C31E0331C5C55800C2F79\n"
  437. "-----BEGIN SIGNATURE-----\n"
  438. "%%@%%%%%%%!!!'\n"
  439. "-----END SIGNATURE-----\n";
  440. const char *end = str + strlen(str);
  441. const char **s = &str;
  442. token_rule_t rule = T("directory-signature", K_DIRECTORY_SIGNATURE,
  443. GE(2), NEED_OBJ);
  444. (void)arg;
  445. directory_token_t *token = get_next_token(area, s, end, &rule);
  446. tt_int_op(token->tp, OP_EQ, ERR_);
  447. tt_str_op(token->error, OP_EQ, "Malformed object: bad base64-encoded data");
  448. done:
  449. memarea_drop_all(area);
  450. return;
  451. }
  452. #define PARSECOMMON_TEST(name) \
  453. { #name, test_parsecommon_ ## name, 0, NULL, NULL }
  454. struct testcase_t parsecommon_tests[] = {
  455. PARSECOMMON_TEST(tokenize_string_null),
  456. PARSECOMMON_TEST(tokenize_string_multiple_lines),
  457. PARSECOMMON_TEST(tokenize_string_min_cnt),
  458. PARSECOMMON_TEST(tokenize_string_max_cnt),
  459. PARSECOMMON_TEST(tokenize_string_at_start),
  460. PARSECOMMON_TEST(tokenize_string_at_end),
  461. PARSECOMMON_TEST(tokenize_string_no_annotations),
  462. PARSECOMMON_TEST(get_next_token_success),
  463. PARSECOMMON_TEST(get_next_token_concat_args),
  464. PARSECOMMON_TEST(get_next_token_parse_keys),
  465. PARSECOMMON_TEST(get_next_token_object),
  466. PARSECOMMON_TEST(get_next_token_err_too_many_args),
  467. PARSECOMMON_TEST(get_next_token_err_too_few_args),
  468. PARSECOMMON_TEST(get_next_token_err_obj_missing_endline),
  469. PARSECOMMON_TEST(get_next_token_err_bad_beginline),
  470. PARSECOMMON_TEST(get_next_token_err_tag_mismatch),
  471. PARSECOMMON_TEST(get_next_token_err_bad_base64),
  472. END_OF_TESTCASES
  473. };