1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2014 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_config_test.h"
26
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif // HAVE_UNISTD_H
30
31 #include <cstdlib>
32
33 #include "munitxx.h"
34
35 #include "shrpx_config.h"
36 #include "shrpx_log.h"
37
38 using namespace std::literals;
39
40 namespace shrpx {
41
42 namespace {
43 const MunitTest tests[]{
44 munit_void_test(test_shrpx_config_parse_header),
45 munit_void_test(test_shrpx_config_parse_log_format),
46 munit_void_test(test_shrpx_config_read_tls_ticket_key_file),
47 munit_void_test(test_shrpx_config_read_tls_ticket_key_file_aes_256),
48 munit_test_end(),
49 };
50 } // namespace
51
52 const MunitSuite config_suite{
53 "/config_suite", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
54 };
55
test_shrpx_config_parse_header(void)56 void test_shrpx_config_parse_header(void) {
57 BlockAllocator balloc(4096, 4096);
58
59 auto p = parse_header(balloc, "a: b"_sr);
60 assert_stdsv_equal("a"sv, p.name);
61 assert_stdsv_equal("b"sv, p.value);
62
63 p = parse_header(balloc, "a: b"_sr);
64 assert_stdsv_equal("a"sv, p.name);
65 assert_stdsv_equal("b"sv, p.value);
66
67 p = parse_header(balloc, ":a: b"_sr);
68 assert_true(p.name.empty());
69
70 p = parse_header(balloc, "a: :b"_sr);
71 assert_stdsv_equal("a"sv, p.name);
72 assert_stdsv_equal(":b"sv, p.value);
73
74 p = parse_header(balloc, ": b"_sr);
75 assert_true(p.name.empty());
76
77 p = parse_header(balloc, "alpha: bravo charlie"_sr);
78 assert_stdsv_equal("alpha", p.name);
79 assert_stdsv_equal("bravo charlie", p.value);
80
81 p = parse_header(balloc, "a,: b"_sr);
82 assert_true(p.name.empty());
83
84 p = parse_header(balloc, "a: b\x0a"_sr);
85 assert_true(p.name.empty());
86 }
87
test_shrpx_config_parse_log_format(void)88 void test_shrpx_config_parse_log_format(void) {
89 BlockAllocator balloc(4096, 4096);
90
91 auto res = parse_log_format(
92 balloc, R"($remote_addr - $remote_user [$time_local] )"
93 R"("$request" $status $body_bytes_sent )"
94 R"("${http_referer}" $http_host "$http_user_agent")"_sr);
95 assert_size(16, ==, res.size());
96
97 assert_enum_class(LogFragmentType::REMOTE_ADDR, ==, res[0].type);
98
99 assert_enum_class(LogFragmentType::LITERAL, ==, res[1].type);
100 assert_stdsv_equal(" - $remote_user ["sv, res[1].value);
101
102 assert_enum_class(LogFragmentType::TIME_LOCAL, ==, res[2].type);
103
104 assert_enum_class(LogFragmentType::LITERAL, ==, res[3].type);
105 assert_stdsv_equal("] \""sv, res[3].value);
106
107 assert_enum_class(LogFragmentType::REQUEST, ==, res[4].type);
108
109 assert_enum_class(LogFragmentType::LITERAL, ==, res[5].type);
110 assert_stdsv_equal("\" "sv, res[5].value);
111
112 assert_enum_class(LogFragmentType::STATUS, ==, res[6].type);
113
114 assert_enum_class(LogFragmentType::LITERAL, ==, res[7].type);
115 assert_stdsv_equal(" "sv, res[7].value);
116
117 assert_enum_class(LogFragmentType::BODY_BYTES_SENT, ==, res[8].type);
118
119 assert_enum_class(LogFragmentType::LITERAL, ==, res[9].type);
120 assert_stdsv_equal(" \""sv, res[9].value);
121
122 assert_enum_class(LogFragmentType::HTTP, ==, res[10].type);
123 assert_stdsv_equal("referer"sv, res[10].value);
124
125 assert_enum_class(LogFragmentType::LITERAL, ==, res[11].type);
126 assert_stdsv_equal("\" "sv, res[11].value);
127
128 assert_enum_class(LogFragmentType::AUTHORITY, ==, res[12].type);
129
130 assert_enum_class(LogFragmentType::LITERAL, ==, res[13].type);
131 assert_stdsv_equal(" \""sv, res[13].value);
132
133 assert_enum_class(LogFragmentType::HTTP, ==, res[14].type);
134 assert_stdsv_equal("user-agent"sv, res[14].value);
135
136 assert_enum_class(LogFragmentType::LITERAL, ==, res[15].type);
137 assert_stdsv_equal("\""sv, res[15].value);
138
139 res = parse_log_format(balloc, "$"_sr);
140
141 assert_size(1, ==, res.size());
142
143 assert_enum_class(LogFragmentType::LITERAL, ==, res[0].type);
144 assert_stdsv_equal("$"sv, res[0].value);
145
146 res = parse_log_format(balloc, "${"_sr);
147
148 assert_size(1, ==, res.size());
149
150 assert_enum_class(LogFragmentType::LITERAL, ==, res[0].type);
151 assert_stdsv_equal("${"sv, res[0].value);
152
153 res = parse_log_format(balloc, "${a"_sr);
154
155 assert_size(1, ==, res.size());
156
157 assert_enum_class(LogFragmentType::LITERAL, ==, res[0].type);
158 assert_stdsv_equal("${a"sv, res[0].value);
159
160 res = parse_log_format(balloc, "${a "_sr);
161
162 assert_size(1, ==, res.size());
163
164 assert_enum_class(LogFragmentType::LITERAL, ==, res[0].type);
165 assert_stdsv_equal("${a "sv, res[0].value);
166
167 res = parse_log_format(balloc, "$$remote_addr"_sr);
168
169 assert_size(2, ==, res.size());
170
171 assert_enum_class(LogFragmentType::LITERAL, ==, res[0].type);
172 assert_stdsv_equal("$"sv, res[0].value);
173
174 assert_enum_class(LogFragmentType::REMOTE_ADDR, ==, res[1].type);
175 assert_stdsv_equal(""sv, res[1].value);
176 }
177
test_shrpx_config_read_tls_ticket_key_file(void)178 void test_shrpx_config_read_tls_ticket_key_file(void) {
179 char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
180 auto fd1 = mkstemp(file1);
181 assert_int(-1, !=, fd1);
182 assert_ssize(
183 48, ==,
184 write(fd1, "0..............12..............34..............5", 48));
185 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
186 auto fd2 = mkstemp(file2);
187 assert_int(-1, !=, fd2);
188 assert_ssize(
189 48, ==,
190 write(fd2, "6..............78..............9a..............b", 48));
191
192 close(fd1);
193 close(fd2);
194 auto ticket_keys = read_tls_ticket_key_file(
195 {StringRef{file1}, StringRef{file2}}, EVP_aes_128_cbc(), EVP_sha256());
196 unlink(file1);
197 unlink(file2);
198 assert_not_null(ticket_keys.get());
199 assert_size(2, ==, ticket_keys->keys.size());
200 auto key = &ticket_keys->keys[0];
201 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
202 "0..............1"));
203 assert_true(std::equal(std::begin(key->data.enc_key),
204 std::begin(key->data.enc_key) + 16,
205 "2..............3"));
206 assert_true(std::equal(std::begin(key->data.hmac_key),
207 std::begin(key->data.hmac_key) + 16,
208 "4..............5"));
209 assert_size(16, ==, key->hmac_keylen);
210
211 key = &ticket_keys->keys[1];
212 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
213 "6..............7"));
214 assert_true(std::equal(std::begin(key->data.enc_key),
215 std::begin(key->data.enc_key) + 16,
216 "8..............9"));
217 assert_true(std::equal(std::begin(key->data.hmac_key),
218 std::begin(key->data.hmac_key) + 16,
219 "a..............b"));
220 assert_size(16, ==, key->hmac_keylen);
221 }
222
test_shrpx_config_read_tls_ticket_key_file_aes_256(void)223 void test_shrpx_config_read_tls_ticket_key_file_aes_256(void) {
224 char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
225 auto fd1 = mkstemp(file1);
226 assert_int(-1, !=, fd1);
227 assert_ssize(80, ==,
228 write(fd1,
229 "0..............12..............................34..."
230 "...........................5",
231 80));
232 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
233 auto fd2 = mkstemp(file2);
234 assert_int(-1, !=, fd2);
235 assert_ssize(80, ==,
236 write(fd2,
237 "6..............78..............................9a..."
238 "...........................b",
239 80));
240
241 close(fd1);
242 close(fd2);
243 auto ticket_keys = read_tls_ticket_key_file(
244 {StringRef{file1}, StringRef{file2}}, EVP_aes_256_cbc(), EVP_sha256());
245 unlink(file1);
246 unlink(file2);
247 assert_not_null(ticket_keys.get());
248 assert_size(2, ==, ticket_keys->keys.size());
249 auto key = &ticket_keys->keys[0];
250 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
251 "0..............1"));
252 assert_true(std::equal(std::begin(key->data.enc_key),
253 std::end(key->data.enc_key),
254 "2..............................3"));
255 assert_true(std::equal(std::begin(key->data.hmac_key),
256 std::end(key->data.hmac_key),
257 "4..............................5"));
258
259 key = &ticket_keys->keys[1];
260 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
261 "6..............7"));
262 assert_true(std::equal(std::begin(key->data.enc_key),
263 std::end(key->data.enc_key),
264 "8..............................9"));
265 assert_true(std::equal(std::begin(key->data.hmac_key),
266 std::end(key->data.hmac_key),
267 "a..............................b"));
268 }
269
270 } // namespace shrpx
271