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"
31 #include <CUnit/CUnit.h>
33 #include "shrpx_config.h"
37 void test_shrpx_config_parse_config_str_list(void) {
38 auto res = parse_config_str_list("a");
39 CU_ASSERT(1 == res.size());
40 CU_ASSERT(0 == strcmp("a", res[0]));
41 clear_config_str_list(res);
43 res = parse_config_str_list("a,");
44 CU_ASSERT(2 == res.size());
45 CU_ASSERT(0 == strcmp("a", res[0]));
46 CU_ASSERT(0 == strcmp("", res[1]));
47 clear_config_str_list(res);
49 res = parse_config_str_list(",a,,");
50 CU_ASSERT(4 == res.size());
51 CU_ASSERT(0 == strcmp("", res[0]));
52 CU_ASSERT(0 == strcmp("a", res[1]));
53 CU_ASSERT(0 == strcmp("", res[2]));
54 CU_ASSERT(0 == strcmp("", res[3]));
55 clear_config_str_list(res);
57 res = parse_config_str_list("");
58 CU_ASSERT(1 == res.size());
59 CU_ASSERT(0 == strcmp("", res[0]));
60 clear_config_str_list(res);
62 res = parse_config_str_list("alpha,bravo,charlie");
63 CU_ASSERT(3 == res.size());
64 CU_ASSERT(0 == strcmp("alpha", res[0]));
65 CU_ASSERT(0 == strcmp("bravo", res[1]));
66 CU_ASSERT(0 == strcmp("charlie", res[2]));
67 clear_config_str_list(res);
70 void test_shrpx_config_parse_header(void) {
71 auto p = parse_header("a: b");
72 CU_ASSERT("a" == p.first);
73 CU_ASSERT("b" == p.second);
75 p = parse_header("a: b");
76 CU_ASSERT("a" == p.first);
77 CU_ASSERT("b" == p.second);
79 p = parse_header(":a: b");
80 CU_ASSERT(":a" == p.first);
81 CU_ASSERT("b" == p.second);
83 p = parse_header("a: :b");
84 CU_ASSERT("a" == p.first);
85 CU_ASSERT(":b" == p.second);
87 p = parse_header(": b");
88 CU_ASSERT(p.first.empty());
90 p = parse_header("alpha: bravo charlie");
91 CU_ASSERT("alpha" == p.first);
92 CU_ASSERT("bravo charlie" == p.second);
95 void test_shrpx_config_parse_log_format(void) {
96 auto res = parse_log_format("$remote_addr - $remote_user [$time_local] "
97 "\"$request\" $status $body_bytes_sent "
98 "\"$http_referer\" \"$http_user_agent\"");
99 CU_ASSERT(14 == res.size());
101 CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[0].type);
103 CU_ASSERT(SHRPX_LOGF_LITERAL == res[1].type);
104 CU_ASSERT(0 == strcmp(" - $remote_user [", res[1].value.get()));
106 CU_ASSERT(SHRPX_LOGF_TIME_LOCAL == res[2].type);
108 CU_ASSERT(SHRPX_LOGF_LITERAL == res[3].type);
109 CU_ASSERT(0 == strcmp("] \"", res[3].value.get()));
111 CU_ASSERT(SHRPX_LOGF_REQUEST == res[4].type);
113 CU_ASSERT(SHRPX_LOGF_LITERAL == res[5].type);
114 CU_ASSERT(0 == strcmp("\" ", res[5].value.get()));
116 CU_ASSERT(SHRPX_LOGF_STATUS == res[6].type);
118 CU_ASSERT(SHRPX_LOGF_LITERAL == res[7].type);
119 CU_ASSERT(0 == strcmp(" ", res[7].value.get()));
121 CU_ASSERT(SHRPX_LOGF_BODY_BYTES_SENT == res[8].type);
123 CU_ASSERT(SHRPX_LOGF_LITERAL == res[9].type);
124 CU_ASSERT(0 == strcmp(" \"", res[9].value.get()));
126 CU_ASSERT(SHRPX_LOGF_HTTP == res[10].type);
127 CU_ASSERT(0 == strcmp("referer", res[10].value.get()));
129 CU_ASSERT(SHRPX_LOGF_LITERAL == res[11].type);
130 CU_ASSERT(0 == strcmp("\" \"", res[11].value.get()));
132 CU_ASSERT(SHRPX_LOGF_HTTP == res[12].type);
133 CU_ASSERT(0 == strcmp("user-agent", res[12].value.get()));
135 CU_ASSERT(SHRPX_LOGF_LITERAL == res[13].type);
136 CU_ASSERT(0 == strcmp("\"", res[13].value.get()));
139 void test_shrpx_config_read_tls_ticket_key_file(void) {
140 char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
141 auto fd1 = mkstemp(file1);
144 write(fd1, "0..............12..............34..............5", 48));
145 char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
146 auto fd2 = mkstemp(file2);
149 write(fd2, "6..............78..............9a..............b", 48));
153 auto ticket_keys = read_tls_ticket_key_file({file1, file2});
156 CU_ASSERT(ticket_keys.get() != nullptr);
157 CU_ASSERT(2 == ticket_keys->keys.size());
158 auto key = &ticket_keys->keys[0];
159 CU_ASSERT(0 == memcmp("0..............1", key->name, sizeof(key->name)));
161 memcmp("2..............3", key->aes_key, sizeof(key->aes_key)));
163 memcmp("4..............5", key->hmac_key, sizeof(key->hmac_key)));
165 key = &ticket_keys->keys[1];
166 CU_ASSERT(0 == memcmp("6..............7", key->name, sizeof(key->name)));
168 memcmp("8..............9", key->aes_key, sizeof(key->aes_key)));
170 memcmp("a..............b", key->hmac_key, sizeof(key->hmac_key)));