tests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* $OpenBSD: tests.c,v 1.1 2018/03/03 03:16:17 djm Exp $ */
  2. /*
  3. * Regress test for keys options functions.
  4. *
  5. * Placed in the public domain
  6. */
  7. #include <sys/types.h>
  8. #include <sys/param.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "test_helper.h"
  14. #include "sshkey.h"
  15. #include "authfile.h"
  16. #include "auth-options.h"
  17. #include "misc.h"
  18. #include "log.h"
  19. static struct sshkey *
  20. load_key(const char *name)
  21. {
  22. struct sshkey *ret;
  23. int r;
  24. r = sshkey_load_public(test_data_file(name), &ret, NULL);
  25. ASSERT_INT_EQ(r, 0);
  26. ASSERT_PTR_NE(ret, NULL);
  27. return ret;
  28. }
  29. static struct sshauthopt *
  30. default_authkey_opts(void)
  31. {
  32. struct sshauthopt *ret = sshauthopt_new();
  33. ASSERT_PTR_NE(ret, NULL);
  34. ret->permit_port_forwarding_flag = 1;
  35. ret->permit_agent_forwarding_flag = 1;
  36. ret->permit_x11_forwarding_flag = 1;
  37. ret->permit_pty_flag = 1;
  38. ret->permit_user_rc = 1;
  39. return ret;
  40. }
  41. static struct sshauthopt *
  42. default_authkey_restrict_opts(void)
  43. {
  44. struct sshauthopt *ret = sshauthopt_new();
  45. ASSERT_PTR_NE(ret, NULL);
  46. ret->permit_port_forwarding_flag = 0;
  47. ret->permit_agent_forwarding_flag = 0;
  48. ret->permit_x11_forwarding_flag = 0;
  49. ret->permit_pty_flag = 0;
  50. ret->permit_user_rc = 0;
  51. ret->restricted = 1;
  52. return ret;
  53. }
  54. static char **
  55. commasplit(const char *s, size_t *np)
  56. {
  57. char *ocp, *cp, *cp2, **ret = NULL;
  58. size_t n;
  59. ocp = cp = strdup(s);
  60. ASSERT_PTR_NE(cp, NULL);
  61. for (n = 0; (cp2 = strsep(&cp, ",")) != NULL;) {
  62. ret = recallocarray(ret, n, n + 1, sizeof(*ret));
  63. ASSERT_PTR_NE(ret, NULL);
  64. cp2 = strdup(cp2);
  65. ASSERT_PTR_NE(cp2, NULL);
  66. ret[n++] = cp2;
  67. }
  68. free(ocp);
  69. *np = n;
  70. return ret;
  71. }
  72. static void
  73. compare_opts(const struct sshauthopt *opts,
  74. const struct sshauthopt *expected)
  75. {
  76. size_t i;
  77. ASSERT_PTR_NE(opts, NULL);
  78. ASSERT_PTR_NE(expected, NULL);
  79. ASSERT_PTR_NE(expected, opts); /* bozo :) */
  80. #define FLAG_EQ(x) ASSERT_INT_EQ(opts->x, expected->x)
  81. FLAG_EQ(permit_port_forwarding_flag);
  82. FLAG_EQ(permit_agent_forwarding_flag);
  83. FLAG_EQ(permit_x11_forwarding_flag);
  84. FLAG_EQ(permit_pty_flag);
  85. FLAG_EQ(permit_user_rc);
  86. FLAG_EQ(restricted);
  87. FLAG_EQ(cert_authority);
  88. #undef FLAG_EQ
  89. #define STR_EQ(x) \
  90. do { \
  91. if (expected->x == NULL) \
  92. ASSERT_PTR_EQ(opts->x, expected->x); \
  93. else \
  94. ASSERT_STRING_EQ(opts->x, expected->x); \
  95. } while (0)
  96. STR_EQ(cert_principals);
  97. STR_EQ(force_command);
  98. STR_EQ(required_from_host_cert);
  99. STR_EQ(required_from_host_keys);
  100. #undef STR_EQ
  101. #define ARRAY_EQ(nx, x) \
  102. do { \
  103. ASSERT_SIZE_T_EQ(opts->nx, expected->nx); \
  104. if (expected->nx == 0) \
  105. break; \
  106. for (i = 0; i < expected->nx; i++) \
  107. ASSERT_STRING_EQ(opts->x[i], expected->x[i]); \
  108. } while (0)
  109. ARRAY_EQ(nenv, env);
  110. ARRAY_EQ(npermitopen, permitopen);
  111. #undef ARRAY_EQ
  112. }
  113. static void
  114. test_authkeys_parse(void)
  115. {
  116. struct sshauthopt *opts, *expected;
  117. const char *errstr;
  118. #define FAIL_TEST(label, keywords) \
  119. do { \
  120. TEST_START("sshauthopt_parse invalid " label); \
  121. opts = sshauthopt_parse(keywords, &errstr); \
  122. ASSERT_PTR_EQ(opts, NULL); \
  123. ASSERT_PTR_NE(errstr, NULL); \
  124. TEST_DONE(); \
  125. } while (0)
  126. #define CHECK_SUCCESS_AND_CLEANUP() \
  127. do { \
  128. if (errstr != NULL) \
  129. ASSERT_STRING_EQ(errstr, ""); \
  130. compare_opts(opts, expected); \
  131. sshauthopt_free(expected); \
  132. sshauthopt_free(opts); \
  133. } while (0)
  134. /* Basic tests */
  135. TEST_START("sshauthopt_parse empty");
  136. expected = default_authkey_opts();
  137. opts = sshauthopt_parse("", &errstr);
  138. CHECK_SUCCESS_AND_CLEANUP();
  139. TEST_DONE();
  140. TEST_START("sshauthopt_parse trailing whitespace");
  141. expected = default_authkey_opts();
  142. opts = sshauthopt_parse(" ", &errstr);
  143. CHECK_SUCCESS_AND_CLEANUP();
  144. TEST_DONE();
  145. TEST_START("sshauthopt_parse restrict");
  146. expected = default_authkey_restrict_opts();
  147. opts = sshauthopt_parse("restrict", &errstr);
  148. CHECK_SUCCESS_AND_CLEANUP();
  149. TEST_DONE();
  150. /* Invalid syntax */
  151. FAIL_TEST("trailing comma", "restrict,");
  152. FAIL_TEST("bare comma", ",");
  153. FAIL_TEST("unknown option", "BLAH");
  154. FAIL_TEST("unknown option with trailing comma", "BLAH,");
  155. FAIL_TEST("unknown option with trailing whitespace", "BLAH ");
  156. /* force_tun_device */
  157. TEST_START("sshauthopt_parse tunnel explicit");
  158. expected = default_authkey_opts();
  159. expected->force_tun_device = 1;
  160. opts = sshauthopt_parse("tunnel=\"1\"", &errstr);
  161. CHECK_SUCCESS_AND_CLEANUP();
  162. TEST_DONE();
  163. TEST_START("sshauthopt_parse tunnel any");
  164. expected = default_authkey_opts();
  165. expected->force_tun_device = SSH_TUNID_ANY;
  166. opts = sshauthopt_parse("tunnel=\"any\"", &errstr);
  167. CHECK_SUCCESS_AND_CLEANUP();
  168. TEST_DONE();
  169. FAIL_TEST("tunnel", "tunnel=\"blah\"");
  170. /* Flag options */
  171. #define FLAG_TEST(keyword, var, val) \
  172. do { \
  173. TEST_START("sshauthopt_parse " keyword); \
  174. expected = default_authkey_opts(); \
  175. expected->var = val; \
  176. opts = sshauthopt_parse(keyword, &errstr); \
  177. CHECK_SUCCESS_AND_CLEANUP(); \
  178. expected = default_authkey_restrict_opts(); \
  179. expected->var = val; \
  180. opts = sshauthopt_parse("restrict,"keyword, &errstr); \
  181. CHECK_SUCCESS_AND_CLEANUP(); \
  182. TEST_DONE(); \
  183. } while (0)
  184. /* Positive flags */
  185. FLAG_TEST("cert-authority", cert_authority, 1);
  186. FLAG_TEST("port-forwarding", permit_port_forwarding_flag, 1);
  187. FLAG_TEST("agent-forwarding", permit_agent_forwarding_flag, 1);
  188. FLAG_TEST("x11-forwarding", permit_x11_forwarding_flag, 1);
  189. FLAG_TEST("pty", permit_pty_flag, 1);
  190. FLAG_TEST("user-rc", permit_user_rc, 1);
  191. /* Negative flags */
  192. FLAG_TEST("no-port-forwarding", permit_port_forwarding_flag, 0);
  193. FLAG_TEST("no-agent-forwarding", permit_agent_forwarding_flag, 0);
  194. FLAG_TEST("no-x11-forwarding", permit_x11_forwarding_flag, 0);
  195. FLAG_TEST("no-pty", permit_pty_flag, 0);
  196. FLAG_TEST("no-user-rc", permit_user_rc, 0);
  197. #undef FLAG_TEST
  198. FAIL_TEST("no-cert-authority", "no-cert-authority");
  199. /* String options */
  200. #define STRING_TEST(keyword, var, val) \
  201. do { \
  202. TEST_START("sshauthopt_parse " keyword); \
  203. expected = default_authkey_opts(); \
  204. expected->var = strdup(val); \
  205. ASSERT_PTR_NE(expected->var, NULL); \
  206. opts = sshauthopt_parse(keyword "=" #val, &errstr); \
  207. CHECK_SUCCESS_AND_CLEANUP(); \
  208. expected = default_authkey_restrict_opts(); \
  209. expected->var = strdup(val); \
  210. ASSERT_PTR_NE(expected->var, NULL); \
  211. opts = sshauthopt_parse( \
  212. "restrict," keyword "=" #val ",restrict", &errstr); \
  213. CHECK_SUCCESS_AND_CLEANUP(); \
  214. TEST_DONE(); \
  215. } while (0)
  216. STRING_TEST("command", force_command, "/bin/true");
  217. STRING_TEST("principals", cert_principals, "gregor,josef,K");
  218. STRING_TEST("from", required_from_host_keys, "127.0.0.0/8");
  219. #undef STRING_TEST
  220. FAIL_TEST("unquoted command", "command=oops");
  221. FAIL_TEST("unquoted principals", "principals=estragon");
  222. FAIL_TEST("unquoted from", "from=127.0.0.1");
  223. /* String array option tests */
  224. #define ARRAY_TEST(label, keywords, var, nvar, val) \
  225. do { \
  226. TEST_START("sshauthopt_parse " label); \
  227. expected = default_authkey_opts(); \
  228. expected->var = commasplit(val, &expected->nvar); \
  229. ASSERT_PTR_NE(expected->var, NULL); \
  230. opts = sshauthopt_parse(keywords, &errstr); \
  231. CHECK_SUCCESS_AND_CLEANUP(); \
  232. expected = default_authkey_restrict_opts(); \
  233. expected->var = commasplit(val, &expected->nvar); \
  234. ASSERT_PTR_NE(expected->var, NULL); \
  235. opts = sshauthopt_parse( \
  236. "restrict," keywords ",restrict", &errstr); \
  237. CHECK_SUCCESS_AND_CLEANUP(); \
  238. TEST_DONE(); \
  239. } while (0)
  240. ARRAY_TEST("environment", "environment=\"foo=1\",environment=\"bar=2\"",
  241. env, nenv, "foo=1,bar=2");
  242. ARRAY_TEST("permitopen", "permitopen=\"foo:123\",permitopen=\"bar:*\"",
  243. permitopen, npermitopen, "foo:123,bar:*");
  244. #undef ARRAY_TEST
  245. FAIL_TEST("environment", "environment=\",=bah\"");
  246. FAIL_TEST("permitopen port", "foo:bar");
  247. FAIL_TEST("permitopen missing port", "foo:");
  248. FAIL_TEST("permitopen missing port specification", "foo");
  249. FAIL_TEST("permitopen invalid host", "[:");
  250. #undef CHECK_SUCCESS_AND_CLEANUP
  251. #undef FAIL_TEST
  252. }
  253. static void
  254. test_cert_parse(void)
  255. {
  256. struct sshkey *cert;
  257. struct sshauthopt *opts, *expected;
  258. #define CHECK_SUCCESS_AND_CLEANUP() \
  259. do { \
  260. compare_opts(opts, expected); \
  261. sshauthopt_free(expected); \
  262. sshauthopt_free(opts); \
  263. sshkey_free(cert); \
  264. } while (0)
  265. #define FLAG_TEST(keybase, var) \
  266. do { \
  267. TEST_START("sshauthopt_from_cert no_" keybase); \
  268. cert = load_key("no_" keybase ".cert"); \
  269. expected = default_authkey_opts(); \
  270. expected->var = 0; \
  271. opts = sshauthopt_from_cert(cert); \
  272. CHECK_SUCCESS_AND_CLEANUP(); \
  273. TEST_DONE(); \
  274. TEST_START("sshauthopt_from_cert only_" keybase); \
  275. cert = load_key("only_" keybase ".cert"); \
  276. expected = sshauthopt_new(); \
  277. ASSERT_PTR_NE(expected, NULL); \
  278. expected->var = 1; \
  279. opts = sshauthopt_from_cert(cert); \
  280. CHECK_SUCCESS_AND_CLEANUP(); \
  281. TEST_DONE(); \
  282. } while (0)
  283. FLAG_TEST("agentfwd", permit_agent_forwarding_flag);
  284. FLAG_TEST("portfwd", permit_port_forwarding_flag);
  285. FLAG_TEST("pty", permit_pty_flag);
  286. FLAG_TEST("user_rc", permit_user_rc);
  287. FLAG_TEST("x11fwd", permit_x11_forwarding_flag);
  288. #undef FLAG_TEST
  289. TEST_START("sshauthopt_from_cert all permitted");
  290. cert = load_key("all_permit.cert");
  291. expected = default_authkey_opts();
  292. opts = sshauthopt_from_cert(cert);
  293. CHECK_SUCCESS_AND_CLEANUP();
  294. TEST_DONE();
  295. TEST_START("sshauthopt_from_cert nothing permitted");
  296. cert = load_key("no_permit.cert");
  297. expected = sshauthopt_new();
  298. ASSERT_PTR_NE(expected, NULL);
  299. opts = sshauthopt_from_cert(cert);
  300. CHECK_SUCCESS_AND_CLEANUP();
  301. TEST_DONE();
  302. TEST_START("sshauthopt_from_cert force-command");
  303. cert = load_key("force_command.cert");
  304. expected = default_authkey_opts();
  305. expected->force_command = strdup("foo");
  306. ASSERT_PTR_NE(expected->force_command, NULL);
  307. opts = sshauthopt_from_cert(cert);
  308. CHECK_SUCCESS_AND_CLEANUP();
  309. TEST_DONE();
  310. TEST_START("sshauthopt_from_cert source-address");
  311. cert = load_key("sourceaddr.cert");
  312. expected = default_authkey_opts();
  313. expected->required_from_host_cert = strdup("127.0.0.1/32,::1/128");
  314. ASSERT_PTR_NE(expected->required_from_host_cert, NULL);
  315. opts = sshauthopt_from_cert(cert);
  316. CHECK_SUCCESS_AND_CLEANUP();
  317. TEST_DONE();
  318. #undef CHECK_SUCCESS_AND_CLEANUP
  319. #define FAIL_TEST(keybase) \
  320. do { \
  321. TEST_START("sshauthopt_from_cert " keybase); \
  322. cert = load_key(keybase ".cert"); \
  323. opts = sshauthopt_from_cert(cert); \
  324. ASSERT_PTR_EQ(opts, NULL); \
  325. sshkey_free(cert); \
  326. TEST_DONE(); \
  327. } while (0)
  328. FAIL_TEST("host");
  329. FAIL_TEST("bad_sourceaddr");
  330. FAIL_TEST("unknown_critical");
  331. #undef FAIL_TEST
  332. }
  333. static void
  334. test_merge(void)
  335. {
  336. struct sshkey *cert;
  337. struct sshauthopt *key_opts, *cert_opts, *merge_opts, *expected;
  338. const char *errstr;
  339. /*
  340. * Prepare for a test by making some key and cert options and
  341. * attempting to merge them.
  342. */
  343. #define PREPARE(label, keyname, keywords) \
  344. do { \
  345. expected = NULL; \
  346. TEST_START("sshauthopt_merge " label); \
  347. cert = load_key(keyname ".cert"); \
  348. cert_opts = sshauthopt_from_cert(cert); \
  349. ASSERT_PTR_NE(cert_opts, NULL); \
  350. key_opts = sshauthopt_parse(keywords, &errstr); \
  351. if (errstr != NULL) \
  352. ASSERT_STRING_EQ(errstr, ""); \
  353. ASSERT_PTR_NE(key_opts, NULL); \
  354. merge_opts = sshauthopt_merge(key_opts, \
  355. cert_opts, &errstr); \
  356. } while (0)
  357. /* Cleanup stuff allocated by PREPARE() */
  358. #define CLEANUP() \
  359. do { \
  360. sshauthopt_free(expected); \
  361. sshauthopt_free(merge_opts); \
  362. sshauthopt_free(key_opts); \
  363. sshauthopt_free(cert_opts); \
  364. sshkey_free(cert); \
  365. } while (0)
  366. /* Check the results of PREPARE() against expectation; calls CLEANUP */
  367. #define CHECK_SUCCESS_AND_CLEANUP() \
  368. do { \
  369. if (errstr != NULL) \
  370. ASSERT_STRING_EQ(errstr, ""); \
  371. compare_opts(merge_opts, expected); \
  372. CLEANUP(); \
  373. } while (0)
  374. /* Check a single case of merging of flag options */
  375. #define FLAG_CASE(keybase, label, keyname, keywords, mostly_off, var, val) \
  376. do { \
  377. PREPARE(keybase " " label, keyname, keywords); \
  378. expected = mostly_off ? \
  379. sshauthopt_new() : default_authkey_opts(); \
  380. expected->var = val; \
  381. ASSERT_PTR_NE(expected, NULL); \
  382. CHECK_SUCCESS_AND_CLEANUP(); \
  383. TEST_DONE(); \
  384. } while (0)
  385. /*
  386. * Fairly exhaustive exercise of a flag option. Tests
  387. * option both set and clear in certificate, set and clear in
  388. * authorized_keys and set and cleared via restrict keyword.
  389. */
  390. #define FLAG_TEST(keybase, keyword, var) \
  391. do { \
  392. FLAG_CASE(keybase, "keys:default,yes cert:default,no", \
  393. "no_" keybase, keyword, 0, var, 0); \
  394. FLAG_CASE(keybase,"keys:-*,yes cert:default,no", \
  395. "no_" keybase, "restrict," keyword, 1, var, 0); \
  396. FLAG_CASE(keybase, "keys:default,no cert:default,no", \
  397. "no_" keybase, "no-" keyword, 0, var, 0); \
  398. FLAG_CASE(keybase, "keys:-*,no cert:default,no", \
  399. "no_" keybase, "restrict,no-" keyword, 1, var, 0); \
  400. \
  401. FLAG_CASE(keybase, "keys:default,yes cert:-*,yes", \
  402. "only_" keybase, keyword, 1, var, 1); \
  403. FLAG_CASE(keybase,"keys:-*,yes cert:-*,yes", \
  404. "only_" keybase, "restrict," keyword, 1, var, 1); \
  405. FLAG_CASE(keybase, "keys:default,no cert:-*,yes", \
  406. "only_" keybase, "no-" keyword, 1, var, 0); \
  407. FLAG_CASE(keybase, "keys:-*,no cert:-*,yes", \
  408. "only_" keybase, "restrict,no-" keyword, 1, var, 0); \
  409. \
  410. FLAG_CASE(keybase, "keys:default,yes cert:-*", \
  411. "no_permit", keyword, 1, var, 0); \
  412. FLAG_CASE(keybase,"keys:-*,yes cert:-*", \
  413. "no_permit", "restrict," keyword, 1, var, 0); \
  414. FLAG_CASE(keybase, "keys:default,no cert:-*", \
  415. "no_permit", "no-" keyword, 1, var, 0); \
  416. FLAG_CASE(keybase, "keys:-*,no cert:-*", \
  417. "no_permit", "restrict,no-" keyword, 1, var, 0); \
  418. \
  419. FLAG_CASE(keybase, "keys:default,yes cert:*", \
  420. "all_permit", keyword, 0, var, 1); \
  421. FLAG_CASE(keybase,"keys:-*,yes cert:*", \
  422. "all_permit", "restrict," keyword, 1, var, 1); \
  423. FLAG_CASE(keybase, "keys:default,no cert:*", \
  424. "all_permit", "no-" keyword, 0, var, 0); \
  425. FLAG_CASE(keybase, "keys:-*,no cert:*", \
  426. "all_permit", "restrict,no-" keyword, 1, var, 0); \
  427. \
  428. } while (0)
  429. FLAG_TEST("portfwd", "port-forwarding", permit_port_forwarding_flag);
  430. FLAG_TEST("agentfwd", "agent-forwarding", permit_agent_forwarding_flag);
  431. FLAG_TEST("pty", "pty", permit_pty_flag);
  432. FLAG_TEST("user_rc", "user-rc", permit_user_rc);
  433. FLAG_TEST("x11fwd", "x11-forwarding", permit_x11_forwarding_flag);
  434. #undef FLAG_TEST
  435. PREPARE("source-address both", "sourceaddr", "from=\"127.0.0.1\"");
  436. expected = default_authkey_opts();
  437. expected->required_from_host_cert = strdup("127.0.0.1/32,::1/128");
  438. ASSERT_PTR_NE(expected->required_from_host_cert, NULL);
  439. expected->required_from_host_keys = strdup("127.0.0.1");
  440. ASSERT_PTR_NE(expected->required_from_host_keys, NULL);
  441. CHECK_SUCCESS_AND_CLEANUP();
  442. TEST_DONE();
  443. PREPARE("source-address none", "all_permit", "");
  444. expected = default_authkey_opts();
  445. CHECK_SUCCESS_AND_CLEANUP();
  446. TEST_DONE();
  447. PREPARE("source-address keys", "all_permit", "from=\"127.0.0.1\"");
  448. expected = default_authkey_opts();
  449. expected->required_from_host_keys = strdup("127.0.0.1");
  450. ASSERT_PTR_NE(expected->required_from_host_keys, NULL);
  451. CHECK_SUCCESS_AND_CLEANUP();
  452. TEST_DONE();
  453. PREPARE("source-address cert", "sourceaddr", "");
  454. expected = default_authkey_opts();
  455. expected->required_from_host_cert = strdup("127.0.0.1/32,::1/128");
  456. ASSERT_PTR_NE(expected->required_from_host_cert, NULL);
  457. CHECK_SUCCESS_AND_CLEANUP();
  458. TEST_DONE();
  459. PREPARE("force-command both", "force_command", "command=\"foo\"");
  460. expected = default_authkey_opts();
  461. expected->force_command = strdup("foo");
  462. ASSERT_PTR_NE(expected->force_command, NULL);
  463. CHECK_SUCCESS_AND_CLEANUP();
  464. TEST_DONE();
  465. PREPARE("force-command none", "all_permit", "");
  466. expected = default_authkey_opts();
  467. CHECK_SUCCESS_AND_CLEANUP();
  468. TEST_DONE();
  469. PREPARE("force-command keys", "all_permit", "command=\"bar\"");
  470. expected = default_authkey_opts();
  471. expected->force_command = strdup("bar");
  472. ASSERT_PTR_NE(expected->force_command, NULL);
  473. CHECK_SUCCESS_AND_CLEANUP();
  474. TEST_DONE();
  475. PREPARE("force-command cert", "force_command", "");
  476. expected = default_authkey_opts();
  477. expected->force_command = strdup("foo");
  478. ASSERT_PTR_NE(expected->force_command, NULL);
  479. CHECK_SUCCESS_AND_CLEANUP();
  480. TEST_DONE();
  481. PREPARE("force-command mismatch", "force_command", "command=\"bar\"");
  482. ASSERT_PTR_EQ(merge_opts, NULL);
  483. CLEANUP();
  484. TEST_DONE();
  485. PREPARE("tunnel", "all_permit", "tunnel=\"6\"");
  486. expected = default_authkey_opts();
  487. expected->force_tun_device = 6;
  488. CHECK_SUCCESS_AND_CLEANUP();
  489. TEST_DONE();
  490. PREPARE("permitopen", "all_permit",
  491. "permitopen=\"127.0.0.1:*\",permitopen=\"127.0.0.1:123\"");
  492. expected = default_authkey_opts();
  493. expected->permitopen = commasplit("127.0.0.1:*,127.0.0.1:123",
  494. &expected->npermitopen);
  495. CHECK_SUCCESS_AND_CLEANUP();
  496. TEST_DONE();
  497. PREPARE("environment", "all_permit",
  498. "environment=\"foo=a\",environment=\"bar=b\"");
  499. expected = default_authkey_opts();
  500. expected->env = commasplit("foo=a,bar=b", &expected->nenv);
  501. CHECK_SUCCESS_AND_CLEANUP();
  502. TEST_DONE();
  503. }
  504. void
  505. tests(void)
  506. {
  507. extern char *__progname;
  508. LogLevel ll = test_is_verbose() ?
  509. SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_QUIET;
  510. /* test_cert_parse() are a bit spammy to error() by default... */
  511. log_init(__progname, ll, SYSLOG_FACILITY_USER, 1);
  512. test_authkeys_parse();
  513. test_cert_parse();
  514. test_merge();
  515. }