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, ==, write(fd1, "0..............12..............34..............5", 48));
184 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
185 auto fd2 = mkstemp(file2);
186 assert_int(-1, !=, fd2);
187 assert_ssize(
188 48, ==, write(fd2, "6..............78..............9a..............b", 48));
189
190 close(fd1);
191 close(fd2);
192 auto ticket_keys = read_tls_ticket_key_file(
193 {StringRef{file1}, StringRef{file2}}, EVP_aes_128_cbc(), EVP_sha256());
194 unlink(file1);
195 unlink(file2);
196 assert_not_null(ticket_keys.get());
197 assert_size(2, ==, ticket_keys->keys.size());
198 auto key = &ticket_keys->keys[0];
199 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
200 "0..............1"));
201 assert_true(std::equal(std::begin(key->data.enc_key),
202 std::begin(key->data.enc_key) + 16,
203 "2..............3"));
204 assert_true(std::equal(std::begin(key->data.hmac_key),
205 std::begin(key->data.hmac_key) + 16,
206 "4..............5"));
207 assert_size(16, ==, key->hmac_keylen);
208
209 key = &ticket_keys->keys[1];
210 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
211 "6..............7"));
212 assert_true(std::equal(std::begin(key->data.enc_key),
213 std::begin(key->data.enc_key) + 16,
214 "8..............9"));
215 assert_true(std::equal(std::begin(key->data.hmac_key),
216 std::begin(key->data.hmac_key) + 16,
217 "a..............b"));
218 assert_size(16, ==, key->hmac_keylen);
219 }
220
test_shrpx_config_read_tls_ticket_key_file_aes_256(void)221 void test_shrpx_config_read_tls_ticket_key_file_aes_256(void) {
222 char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
223 auto fd1 = mkstemp(file1);
224 assert_int(-1, !=, fd1);
225 assert_ssize(80, ==,
226 write(fd1,
227 "0..............12..............................34..."
228 "...........................5",
229 80));
230 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
231 auto fd2 = mkstemp(file2);
232 assert_int(-1, !=, fd2);
233 assert_ssize(80, ==,
234 write(fd2,
235 "6..............78..............................9a..."
236 "...........................b",
237 80));
238
239 close(fd1);
240 close(fd2);
241 auto ticket_keys = read_tls_ticket_key_file(
242 {StringRef{file1}, StringRef{file2}}, EVP_aes_256_cbc(), EVP_sha256());
243 unlink(file1);
244 unlink(file2);
245 assert_not_null(ticket_keys.get());
246 assert_size(2, ==, ticket_keys->keys.size());
247 auto key = &ticket_keys->keys[0];
248 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
249 "0..............1"));
250 assert_true(std::equal(std::begin(key->data.enc_key),
251 std::end(key->data.enc_key),
252 "2..............................3"));
253 assert_true(std::equal(std::begin(key->data.hmac_key),
254 std::end(key->data.hmac_key),
255 "4..............................5"));
256
257 key = &ticket_keys->keys[1];
258 assert_true(std::equal(std::begin(key->data.name), std::end(key->data.name),
259 "6..............7"));
260 assert_true(std::equal(std::begin(key->data.enc_key),
261 std::end(key->data.enc_key),
262 "8..............................9"));
263 assert_true(std::equal(std::begin(key->data.hmac_key),
264 std::end(key->data.hmac_key),
265 "a..............................b"));
266 }
267
268 } // namespace shrpx
269