• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2016 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "shrpx_router_test.h"
26 
27 #include "munitxx.h"
28 
29 #include "shrpx_router.h"
30 
31 namespace shrpx {
32 
33 namespace {
34 const MunitTest tests[]{
35   munit_void_test(test_shrpx_router_match),
36   munit_void_test(test_shrpx_router_match_wildcard),
37   munit_void_test(test_shrpx_router_match_prefix),
38   munit_test_end(),
39 };
40 } // namespace
41 
42 const MunitSuite router_suite{
43   "/router", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
44 };
45 
46 struct Pattern {
47   StringRef pattern;
48   size_t idx;
49   bool wildcard;
50 };
51 
test_shrpx_router_match(void)52 void test_shrpx_router_match(void) {
53   auto patterns = std::vector<Pattern>{
54     {"nghttp2.org/"_sr, 0},
55     {"nghttp2.org/alpha"_sr, 1},
56     {"nghttp2.org/alpha/"_sr, 2},
57     {"nghttp2.org/alpha/bravo/"_sr, 3},
58     {"www.nghttp2.org/alpha/"_sr, 4},
59     {"/alpha"_sr, 5},
60     {"example.com/alpha/"_sr, 6},
61     {"nghttp2.org/alpha/bravo2/"_sr, 7},
62     {"www2.nghttp2.org/alpha/"_sr, 8},
63     {"www2.nghttp2.org/alpha2/"_sr, 9},
64   };
65 
66   Router router;
67 
68   for (auto &p : patterns) {
69     router.add_route(p.pattern, p.idx);
70   }
71 
72   ssize_t idx;
73 
74   idx = router.match("nghttp2.org"_sr, "/"_sr);
75 
76   assert_ssize(0, ==, idx);
77 
78   idx = router.match("nghttp2.org"_sr, "/alpha"_sr);
79 
80   assert_ssize(1, ==, idx);
81 
82   idx = router.match("nghttp2.org"_sr, "/alpha/"_sr);
83 
84   assert_ssize(2, ==, idx);
85 
86   idx = router.match("nghttp2.org"_sr, "/alpha/charlie"_sr);
87 
88   assert_ssize(2, ==, idx);
89 
90   idx = router.match("nghttp2.org"_sr, "/alpha/bravo/"_sr);
91 
92   assert_ssize(3, ==, idx);
93 
94   // matches pattern when last '/' is missing in path
95   idx = router.match("nghttp2.org"_sr, "/alpha/bravo"_sr);
96 
97   assert_ssize(3, ==, idx);
98 
99   idx = router.match("www2.nghttp2.org"_sr, "/alpha"_sr);
100 
101   assert_ssize(8, ==, idx);
102 
103   idx = router.match(StringRef{}, "/alpha"_sr);
104 
105   assert_ssize(5, ==, idx);
106 }
107 
test_shrpx_router_match_wildcard(void)108 void test_shrpx_router_match_wildcard(void) {
109   constexpr auto patterns = std::to_array<Pattern>({
110     {"nghttp2.org/"_sr, 0},
111     {"nghttp2.org/"_sr, 1, true},
112     {"nghttp2.org/alpha/"_sr, 2},
113     {"nghttp2.org/alpha/"_sr, 3, true},
114     {"nghttp2.org/bravo"_sr, 4},
115     {"nghttp2.org/bravo"_sr, 5, true},
116   });
117 
118   Router router;
119 
120   for (auto &p : patterns) {
121     router.add_route(p.pattern, p.idx, p.wildcard);
122   }
123 
124   assert_ssize(0, ==, router.match("nghttp2.org"_sr, "/"_sr));
125 
126   assert_ssize(1, ==, router.match("nghttp2.org"_sr, "/a"_sr));
127 
128   assert_ssize(1, ==, router.match("nghttp2.org"_sr, "/charlie"_sr));
129 
130   assert_ssize(2, ==, router.match("nghttp2.org"_sr, "/alpha"_sr));
131 
132   assert_ssize(2, ==, router.match("nghttp2.org"_sr, "/alpha/"_sr));
133 
134   assert_ssize(3, ==, router.match("nghttp2.org"_sr, "/alpha/b"_sr));
135 
136   assert_ssize(4, ==, router.match("nghttp2.org"_sr, "/bravo"_sr));
137 
138   assert_ssize(5, ==, router.match("nghttp2.org"_sr, "/bravocharlie"_sr));
139 
140   assert_ssize(5, ==, router.match("nghttp2.org"_sr, "/bravo/"_sr));
141 }
142 
test_shrpx_router_match_prefix(void)143 void test_shrpx_router_match_prefix(void) {
144   auto patterns = std::vector<Pattern>{
145     {"gro.2ptthgn."_sr, 0},
146     {"gro.2ptthgn.www."_sr, 1},
147     {"gro.2ptthgn.gmi."_sr, 2},
148     {"gro.2ptthgn.gmi.ahpla."_sr, 3},
149   };
150 
151   Router router;
152 
153   for (auto &p : patterns) {
154     router.add_route(p.pattern, p.idx);
155   }
156 
157   ssize_t idx;
158   const RNode *node;
159   size_t nread;
160 
161   node = nullptr;
162 
163   idx = router.match_prefix(&nread, &node, "gro.2ptthgn.gmi.ahpla.ovarb"_sr);
164 
165   assert_ssize(0, ==, idx);
166   assert_size(12, ==, nread);
167 
168   idx = router.match_prefix(&nread, &node, "gmi.ahpla.ovarb"_sr);
169 
170   assert_ssize(2, ==, idx);
171   assert_size(4, ==, nread);
172 
173   idx = router.match_prefix(&nread, &node, "ahpla.ovarb"_sr);
174 
175   assert_ssize(3, ==, idx);
176   assert_ssize(6, ==, nread);
177 }
178 
179 } // namespace shrpx
180