2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "shrpx_config_test.h"
29 #endif // HAVE_UNISTD_H
33 #include <CUnit/CUnit.h>
35 #include "shrpx_config.h"
39 void test_shrpx_config_parse_config_str_list(void) {
40 auto res = parse_config_str_list("a");
41 CU_ASSERT(1 == res.size());
42 CU_ASSERT(0 == strcmp("a", res[0]));
43 clear_config_str_list(res);
45 res = parse_config_str_list("a,");
46 CU_ASSERT(2 == res.size());
47 CU_ASSERT(0 == strcmp("a", res[0]));
48 CU_ASSERT(0 == strcmp("", res[1]));
49 clear_config_str_list(res);
51 res = parse_config_str_list(",a,,");
52 CU_ASSERT(4 == res.size());
53 CU_ASSERT(0 == strcmp("", res[0]));
54 CU_ASSERT(0 == strcmp("a", res[1]));
55 CU_ASSERT(0 == strcmp("", res[2]));
56 CU_ASSERT(0 == strcmp("", res[3]));
57 clear_config_str_list(res);
59 res = parse_config_str_list("");
60 CU_ASSERT(1 == res.size());
61 CU_ASSERT(0 == strcmp("", res[0]));
62 clear_config_str_list(res);
64 res = parse_config_str_list("alpha,bravo,charlie");
65 CU_ASSERT(3 == res.size());
66 CU_ASSERT(0 == strcmp("alpha", res[0]));
67 CU_ASSERT(0 == strcmp("bravo", res[1]));
68 CU_ASSERT(0 == strcmp("charlie", res[2]));
69 clear_config_str_list(res);
72 void test_shrpx_config_parse_header(void) {
73 auto p = parse_header("a: b");
74 CU_ASSERT("a" == p.first);
75 CU_ASSERT("b" == p.second);
77 p = parse_header("a: b");
78 CU_ASSERT("a" == p.first);
79 CU_ASSERT("b" == p.second);
81 p = parse_header(":a: b");
82 CU_ASSERT(":a" == p.first);
83 CU_ASSERT("b" == p.second);
85 p = parse_header("a: :b");
86 CU_ASSERT("a" == p.first);
87 CU_ASSERT(":b" == p.second);
89 p = parse_header(": b");
90 CU_ASSERT(p.first.empty());
92 p = parse_header("alpha: bravo charlie");
93 CU_ASSERT("alpha" == p.first);
94 CU_ASSERT("bravo charlie" == p.second);
97 void test_shrpx_config_parse_log_format(void) {
98 auto res = parse_log_format("$remote_addr - $remote_user [$time_local] "
99 "\"$request\" $status $body_bytes_sent "
100 "\"$http_referer\" \"$http_user_agent\"");
101 CU_ASSERT(14 == res.size());
103 CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[0].type);
105 CU_ASSERT(SHRPX_LOGF_LITERAL == res[1].type);
106 CU_ASSERT(0 == strcmp(" - $remote_user [", res[1].value.get()));
108 CU_ASSERT(SHRPX_LOGF_TIME_LOCAL == res[2].type);
110 CU_ASSERT(SHRPX_LOGF_LITERAL == res[3].type);
111 CU_ASSERT(0 == strcmp("] \"", res[3].value.get()));
113 CU_ASSERT(SHRPX_LOGF_REQUEST == res[4].type);
115 CU_ASSERT(SHRPX_LOGF_LITERAL == res[5].type);
116 CU_ASSERT(0 == strcmp("\" ", res[5].value.get()));
118 CU_ASSERT(SHRPX_LOGF_STATUS == res[6].type);
120 CU_ASSERT(SHRPX_LOGF_LITERAL == res[7].type);
121 CU_ASSERT(0 == strcmp(" ", res[7].value.get()));
123 CU_ASSERT(SHRPX_LOGF_BODY_BYTES_SENT == res[8].type);
125 CU_ASSERT(SHRPX_LOGF_LITERAL == res[9].type);
126 CU_ASSERT(0 == strcmp(" \"", res[9].value.get()));
128 CU_ASSERT(SHRPX_LOGF_HTTP == res[10].type);
129 CU_ASSERT(0 == strcmp("referer", res[10].value.get()));
131 CU_ASSERT(SHRPX_LOGF_LITERAL == res[11].type);
132 CU_ASSERT(0 == strcmp("\" \"", res[11].value.get()));
134 CU_ASSERT(SHRPX_LOGF_HTTP == res[12].type);
135 CU_ASSERT(0 == strcmp("user-agent", res[12].value.get()));
137 CU_ASSERT(SHRPX_LOGF_LITERAL == res[13].type);
138 CU_ASSERT(0 == strcmp("\"", res[13].value.get()));
141 void test_shrpx_config_read_tls_ticket_key_file(void) {
142 char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
143 auto fd1 = mkstemp(file1);
146 write(fd1, "0..............12..............34..............5", 48));
147 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
148 auto fd2 = mkstemp(file2);
151 write(fd2, "6..............78..............9a..............b", 48));
155 auto ticket_keys = read_tls_ticket_key_file({file1, file2});
158 CU_ASSERT(ticket_keys.get() != nullptr);
159 CU_ASSERT(2 == ticket_keys->keys.size());
160 auto key = &ticket_keys->keys[0];
161 CU_ASSERT(0 == memcmp("0..............1", key->name, sizeof(key->name)));
163 memcmp("2..............3", key->aes_key, sizeof(key->aes_key)));
165 memcmp("4..............5", key->hmac_key, sizeof(key->hmac_key)));
167 key = &ticket_keys->keys[1];
168 CU_ASSERT(0 == memcmp("6..............7", key->name, sizeof(key->name)));
170 memcmp("8..............9", key->aes_key, sizeof(key->aes_key)));
172 memcmp("a..............b", key->hmac_key, sizeof(key->hmac_key)));