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