test_routes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #define SG_EXTERN
  27. #include "sg_assert.h"
  28. #include <stdlib.h>
  29. #ifdef PCRE2_JIT_SUPPORT
  30. #include <stdint.h>
  31. #endif /* PCRE2_JIT_SUPPORT */
  32. #include <errno.h>
  33. #include "sg_macros.h"
  34. #include "pcre2.h"
  35. #include "sg_routes.c"
  36. #include <sagui.h>
  37. static void route_cb(__SG_UNUSED void *cls,
  38. __SG_UNUSED struct sg_route *route) {
  39. }
  40. static int route_segments_empty_iter_cb(__SG_UNUSED void *cls,
  41. __SG_UNUSED unsigned int index,
  42. __SG_UNUSED const char *segment) {
  43. return 0;
  44. }
  45. static int route_segments_123_iter_cb(__SG_UNUSED void *cls,
  46. __SG_UNUSED unsigned int index,
  47. __SG_UNUSED const char *segment) {
  48. return 123;
  49. }
  50. static int route_segments_concat_iter_cb(void *cls,
  51. __SG_UNUSED unsigned int index,
  52. const char *segment) {
  53. const char *str = cls;
  54. sprintf(cls, "%s%d%s", str, index, segment);
  55. return 0;
  56. }
  57. static int route_vars_empty_iter_cb(__SG_UNUSED void *cls,
  58. __SG_UNUSED const char *name,
  59. __SG_UNUSED const char *val) {
  60. return 0;
  61. }
  62. static int route_vars_123_iter_cb(__SG_UNUSED void *cls,
  63. __SG_UNUSED const char *name,
  64. __SG_UNUSED const char *val) {
  65. return 123;
  66. }
  67. static int route_vars_concat_iter_cb(void *cls, const char *name,
  68. const char *val) {
  69. strcat(cls, name);
  70. strcat(cls, val);
  71. return 0;
  72. }
  73. static int routes_iter_empty_cb(__SG_UNUSED void *cls,
  74. __SG_UNUSED struct sg_route *route) {
  75. return 0;
  76. }
  77. static int routes_iter_123_cb(__SG_UNUSED void *cls,
  78. __SG_UNUSED struct sg_route *route) {
  79. return 123;
  80. }
  81. static int routes_iter_concat_cb(void *cls, struct sg_route *route) {
  82. strcat(cls, sg_route_rawpattern(route));
  83. return 0;
  84. }
  85. static void test__route_new(void) {
  86. struct sg_route *route;
  87. char err[SG_ERR_SIZE], errmsg[SG_ERR_SIZE];
  88. #ifdef PCRE2_JIT_SUPPORT
  89. uint32_t opt;
  90. #endif /* PCRE2_JIT_SUPPORT */
  91. int errnum;
  92. ASSERT(!sg__route_new("some\\Kpattern", err, sizeof(err), &errnum, route_cb,
  93. "foo"));
  94. ASSERT(errnum == EINVAL);
  95. ASSERT(strcmp(err, _("\\K is not not allowed.\n")) == 0);
  96. ASSERT(!sg__route_new("foo[bar", err, sizeof(err), &errnum, route_cb, "foo"));
  97. ASSERT(errnum == EINVAL);
  98. snprintf(errmsg, sizeof(errmsg),
  99. _("Pattern compilation failed at offset %d: %s.\n"), 9,
  100. "missing terminating ] for character class");
  101. ASSERT(strcmp(err, errmsg) == 0);
  102. ASSERT((route = sg__route_new("(*NOTEMPTY)pattern", err, sizeof(err), &errnum,
  103. route_cb, "foo")));
  104. ASSERT(errnum == 0);
  105. ASSERT(strcmp("(*NOTEMPTY)pattern", route->pattern) == 0);
  106. ASSERT(route->cb == route_cb);
  107. ASSERT(strcmp("foo", route->cls) == 0);
  108. sg__route_free(route);
  109. /* TODO: test internal pcre2_jit_compile() and pcre2_match_data_create_from_pattern() errors. */
  110. #ifdef PCRE2_JIT_SUPPORT
  111. ASSERT(pcre2_config(PCRE2_CONFIG_JIT, &opt) == 0);
  112. ASSERT(opt == 1);
  113. #endif /* PCRE2_JIT_SUPPORT */
  114. ASSERT((route = sg__route_new("pattern", err, sizeof(err), &errnum, route_cb,
  115. "foo")));
  116. ASSERT(errnum == 0);
  117. ASSERT(strcmp("^pattern$", route->pattern) == 0);
  118. sg__route_free(route);
  119. }
  120. static void test_route_handle(void) {
  121. struct sg_route route;
  122. int dummy = 123;
  123. errno = 0;
  124. ASSERT(!sg_route_handle(NULL));
  125. ASSERT(errno == EINVAL);
  126. route.re = (pcre2_code *) &dummy;
  127. errno = 0;
  128. ASSERT(sg_route_handle(&route) == (pcre2_code *) &dummy);
  129. ASSERT(errno == 0);
  130. }
  131. static void test_route_match(void) {
  132. struct sg_route route;
  133. int dummy = 123;
  134. errno = 0;
  135. ASSERT(!sg_route_match(NULL));
  136. ASSERT(errno == EINVAL);
  137. route.match = (pcre2_match_data *) &dummy;
  138. errno = 0;
  139. ASSERT(sg_route_match(&route) == (pcre2_match_data *) &dummy);
  140. ASSERT(errno == 0);
  141. }
  142. static void test_route_rawpattern(void) {
  143. struct sg_route route;
  144. errno = 0;
  145. ASSERT(!sg_route_rawpattern(NULL));
  146. ASSERT(errno == EINVAL);
  147. route.pattern = NULL;
  148. errno = 0;
  149. ASSERT(!sg_route_rawpattern(&route));
  150. ASSERT(errno == 0);
  151. route.pattern = "^foo$";
  152. errno = 0;
  153. ASSERT(strcmp(sg_route_rawpattern(&route), "^foo$") == 0);
  154. ASSERT(errno == 0);
  155. }
  156. static void test_route_pattern(void) {
  157. struct sg_route route;
  158. char *pattern;
  159. errno = 0;
  160. ASSERT(!sg_route_pattern(NULL));
  161. ASSERT(errno == EINVAL);
  162. route.pattern = NULL;
  163. errno = 0;
  164. ASSERT(!sg_route_pattern(&route));
  165. ASSERT(errno == 0);
  166. route.pattern = "^foo$";
  167. errno = 0;
  168. ASSERT((pattern = sg_route_pattern(&route)));
  169. ASSERT(errno == 0);
  170. ASSERT(strcmp(pattern, "foo") == 0);
  171. sg_free(pattern);
  172. }
  173. static void test_route_path(void) {
  174. struct sg_route route;
  175. errno = 0;
  176. ASSERT(!sg_route_path(NULL));
  177. ASSERT(errno == EINVAL);
  178. route.path = NULL;
  179. errno = 0;
  180. ASSERT(!sg_route_path(&route));
  181. ASSERT(errno == 0);
  182. route.path = "/foo";
  183. errno = 0;
  184. ASSERT(strcmp(sg_route_path(&route), "/foo") == 0);
  185. ASSERT(errno == 0);
  186. }
  187. static void test_route_segments_iter(void) {
  188. struct sg_route *route;
  189. char err[SG_ERR_SIZE];
  190. char str[100];
  191. int errnum;
  192. ASSERT(sg_route_segments_iter(NULL, route_segments_empty_iter_cb, "foo") ==
  193. EINVAL);
  194. route =
  195. sg__route_new("/(foo)/(bar)", err, sizeof(err), &errnum, route_cb, "foo");
  196. ASSERT(sg_route_segments_iter(route, NULL, "foo") == EINVAL);
  197. route->path = "/foo/bar";
  198. route->rc = pcre2_match(route->re, (PCRE2_SPTR) route->path,
  199. strlen(route->path), 0, 0, route->match, NULL);
  200. ASSERT(sg_route_segments_iter(route, route_segments_123_iter_cb, NULL) ==
  201. 123);
  202. memset(str, 0, sizeof(str));
  203. ASSERT(strcmp(str, "") == 0);
  204. ASSERT(sg_route_segments_iter(route, route_segments_concat_iter_cb, str) ==
  205. 0);
  206. ASSERT(strcmp(str, "0foo1bar") == 0);
  207. sg__route_free(route);
  208. }
  209. static void test_route_vars_iter(void) {
  210. struct sg_route *route;
  211. char err[SG_ERR_SIZE];
  212. char str[100];
  213. int errnum;
  214. ASSERT(sg_route_vars_iter(NULL, route_vars_empty_iter_cb, "foo") == EINVAL);
  215. route = sg__route_new("/(?<var1>[a-z]+)/(?<var2>[0-9]+)", err, sizeof(err),
  216. &errnum, route_cb, "foo");
  217. ASSERT(sg_route_vars_iter(route, NULL, "foo") == EINVAL);
  218. route->path = "/abc/123";
  219. route->rc = pcre2_match(route->re, (PCRE2_SPTR) route->path,
  220. strlen(route->path), 0, 0, route->match, NULL);
  221. ASSERT(sg_route_vars_iter(route, route_vars_123_iter_cb, NULL) == 123);
  222. memset(str, 0, sizeof(str));
  223. ASSERT(strcmp(str, "") == 0);
  224. ASSERT(sg_route_vars_iter(route, route_vars_concat_iter_cb, str) == 0);
  225. ASSERT(strcmp(str, "var1abcvar2123") == 0);
  226. sg__route_free(route);
  227. }
  228. static void test_route_user_data(void) {
  229. struct sg_route route;
  230. errno = 0;
  231. ASSERT(!sg_route_user_data(NULL));
  232. ASSERT(errno == EINVAL);
  233. route.user_data = NULL;
  234. errno = 0;
  235. ASSERT(!sg_route_user_data(&route));
  236. ASSERT(errno == 0);
  237. route.user_data = "foo";
  238. errno = 0;
  239. ASSERT(strcmp(sg_route_user_data(&route), "foo") == 0);
  240. ASSERT(errno == 0);
  241. }
  242. static void test_routes_add2(void) {
  243. struct sg_route *routes = NULL;
  244. struct sg_route *route;
  245. char err[SG_ERR_SIZE];
  246. ASSERT(sg_routes_add2(NULL, &route, "/foo", err, sizeof(err), route_cb,
  247. "foo") == EINVAL);
  248. ASSERT(sg_routes_add2(&routes, NULL, "/foo", err, sizeof(err), route_cb,
  249. "foo") == EINVAL);
  250. ASSERT(sg_routes_add2(&routes, &route, NULL, err, sizeof(err), route_cb,
  251. "foo") == EINVAL);
  252. ASSERT(sg_routes_add2(&routes, &route, "/foo", NULL, sizeof(err), route_cb,
  253. "foo") == EINVAL);
  254. ASSERT(sg_routes_add2(&routes, &route, "/foo", err, 0, route_cb, "foo") ==
  255. EINVAL);
  256. ASSERT(sg_routes_add2(&routes, &route, "/foo", err, sizeof(err), NULL,
  257. "foo") == EINVAL);
  258. ASSERT(sg_routes_add2(&routes, &route, "/foo\\K/bar", err, sizeof(err),
  259. route_cb, "foo") == EINVAL);
  260. ASSERT(sg_routes_add2(&routes, &route, "foo", err, sizeof(err), route_cb,
  261. "foo") == 0);
  262. ASSERT(sg_routes_add2(&routes, &route, "foo", err, sizeof(err), route_cb,
  263. "foo") == EALREADY);
  264. sg_routes_cleanup(&routes);
  265. ASSERT(sg_routes_add2(&routes, &route, "foo", err, sizeof(err), route_cb,
  266. "foo") == 0);
  267. ASSERT(route->cb == route_cb);
  268. ASSERT(strcmp(route->cls, "foo") == 0);
  269. ASSERT(sg_routes_add2(&routes, &route, "bar", err, sizeof(err), route_cb,
  270. "foo") == 0);
  271. ASSERT(sg_routes_add2(&routes, &route, "foobar", err, sizeof(err), route_cb,
  272. "foobar") == 0);
  273. route = routes;
  274. ASSERT(strcmp(sg_route_rawpattern(route), "^foo$") == 0);
  275. sg_routes_next(&route);
  276. ASSERT(strcmp(sg_route_rawpattern(route), "^bar$") == 0);
  277. sg_routes_next(&route);
  278. ASSERT(strcmp(sg_route_rawpattern(route), "^foobar$") == 0);
  279. sg_routes_cleanup(&routes);
  280. }
  281. static void test_routes_add(void) {
  282. struct sg_route *routes = NULL;
  283. struct sg_route *route;
  284. errno = 0;
  285. ASSERT(!sg_routes_add(NULL, "/foo", route_cb, "foo"));
  286. ASSERT(errno == EINVAL);
  287. errno = 0;
  288. ASSERT(!sg_routes_add(&routes, NULL, route_cb, "foo"));
  289. ASSERT(errno == EINVAL);
  290. errno = 0;
  291. ASSERT(!sg_routes_add(&routes, "/foo", NULL, "foo"));
  292. ASSERT(errno == EINVAL);
  293. errno = 0;
  294. ASSERT(!sg_routes_add(&routes, "/foo\\K/bar", route_cb, "foo"));
  295. ASSERT(errno == EINVAL);
  296. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  297. errno = 0;
  298. ASSERT(!sg_routes_add(&routes, "foo", route_cb, "foo"));
  299. ASSERT(errno == EALREADY);
  300. sg_routes_cleanup(&routes);
  301. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  302. ASSERT(routes->cb == route_cb);
  303. ASSERT(strcmp(routes->cls, "foo") == 0);
  304. ASSERT(sg_routes_add(&routes, "bar", route_cb, "foo"));
  305. ASSERT(sg_routes_add(&routes, "foobar", route_cb, "foobar"));
  306. route = routes;
  307. ASSERT(strcmp(sg_route_rawpattern(route), "^foo$") == 0);
  308. sg_routes_next(&route);
  309. ASSERT(strcmp(sg_route_rawpattern(route), "^bar$") == 0);
  310. sg_routes_next(&route);
  311. ASSERT(strcmp(sg_route_rawpattern(route), "^foobar$") == 0);
  312. sg_routes_cleanup(&routes);
  313. }
  314. static void test_routes_rm(void) {
  315. struct sg_route *routes = NULL;
  316. struct sg_route *route;
  317. ASSERT(sg_routes_rm(NULL, "foo") == EINVAL);
  318. ASSERT(sg_routes_rm(&routes, NULL) == EINVAL);
  319. ASSERT(sg_routes_rm(&routes, "foo") == ENOENT);
  320. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  321. ASSERT(sg_routes_add(&routes, "bar", route_cb, "bar"));
  322. ASSERT(sg_routes_count(routes) == 2);
  323. route = routes;
  324. ASSERT(strcmp(sg_route_rawpattern(route), "^foo$") == 0);
  325. sg_routes_next(&route);
  326. ASSERT(strcmp(sg_route_rawpattern(route), "^bar$") == 0);
  327. ASSERT(sg_routes_rm(&routes, "foo") == 0);
  328. ASSERT(sg_routes_count(routes) == 1);
  329. ASSERT(strcmp(sg_route_rawpattern(route), "^bar$") == 0);
  330. ASSERT(sg_routes_rm(&routes, "bar") == 0);
  331. ASSERT(sg_routes_count(routes) == 0);
  332. ASSERT(sg_routes_rm(&routes, "bar") == ENOENT);
  333. sg_routes_cleanup(&routes);
  334. }
  335. static void test_routes_iter(void) {
  336. struct sg_route *routes = NULL;
  337. char str[100];
  338. ASSERT(sg_routes_iter(NULL, routes_iter_empty_cb, "foo") == 0);
  339. ASSERT(sg_routes_iter(routes, NULL, "foo") == EINVAL);
  340. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  341. ASSERT(sg_routes_add(&routes, "bar", route_cb, "bar"));
  342. ASSERT(sg_routes_count(routes) == 2);
  343. ASSERT(sg_routes_iter(routes, routes_iter_123_cb, "foo") == 123);
  344. memset(str, 0, sizeof(str));
  345. ASSERT(sg_routes_iter(routes, routes_iter_concat_cb, str) == 0);
  346. ASSERT(strcmp(str, "^foo$^bar$") == 0);
  347. sg_routes_cleanup(&routes);
  348. }
  349. static void test_routes_next(void) {
  350. struct sg_route *routes = NULL;
  351. struct sg_route *route;
  352. ASSERT(sg_routes_next(NULL) == EINVAL);
  353. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  354. ASSERT(sg_routes_add(&routes, "bar", route_cb, "bar"));
  355. ASSERT(sg_routes_count(routes) == 2);
  356. route = routes;
  357. ASSERT(strcmp(sg_route_rawpattern(route), "^foo$") == 0);
  358. ASSERT(sg_routes_next(&route) == 0);
  359. ASSERT(strcmp(sg_route_rawpattern(route), "^bar$") == 0);
  360. ASSERT(sg_routes_next(&route) == 0);
  361. ASSERT(!route);
  362. sg_routes_cleanup(&routes);
  363. }
  364. static void test_routes_count(void) {
  365. struct sg_route *routes = NULL;
  366. ASSERT(sg_routes_count(NULL) == 0);
  367. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  368. ASSERT(sg_routes_add(&routes, "bar", route_cb, "bar"));
  369. ASSERT(sg_routes_count(routes) == 2);
  370. sg_routes_cleanup(&routes);
  371. }
  372. static void test_routes_cleanup(void) {
  373. struct sg_route *routes = NULL;
  374. ASSERT(sg_routes_cleanup(NULL) == EINVAL);
  375. ASSERT(sg_routes_cleanup(&routes) == 0);
  376. ASSERT(!routes);
  377. ASSERT(sg_routes_add(&routes, "foo", route_cb, "foo"));
  378. ASSERT(sg_routes_add(&routes, "bar", route_cb, "bar"));
  379. ASSERT(sg_routes_count(routes) == 2);
  380. ASSERT(sg_routes_cleanup(&routes) == 0);
  381. ASSERT(!routes);
  382. ASSERT(sg_routes_cleanup(&routes) == 0);
  383. }
  384. int main(void) {
  385. test__route_new();
  386. test_route_handle();
  387. test_route_match();
  388. test_route_rawpattern();
  389. test_route_pattern();
  390. test_route_path();
  391. test_route_segments_iter();
  392. test_route_vars_iter();
  393. test_route_user_data();
  394. test_routes_add2();
  395. test_routes_add();
  396. test_routes_rm();
  397. test_routes_iter();
  398. test_routes_next();
  399. test_routes_count();
  400. test_routes_cleanup();
  401. return EXIT_SUCCESS;
  402. }