Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_config_test.cc
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
37 namespace shrpx {
38
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);
44
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);
50
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);
58
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);
63
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);
70 }
71
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);
76
77   p = parse_header("a:  b");
78   CU_ASSERT("a" == p.first);
79   CU_ASSERT("b" == p.second);
80
81   p = parse_header(":a: b");
82   CU_ASSERT(":a" == p.first);
83   CU_ASSERT("b" == p.second);
84
85   p = parse_header("a: :b");
86   CU_ASSERT("a" == p.first);
87   CU_ASSERT(":b" == p.second);
88
89   p = parse_header(": b");
90   CU_ASSERT(p.first.empty());
91
92   p = parse_header("alpha: bravo charlie");
93   CU_ASSERT("alpha" == p.first);
94   CU_ASSERT("bravo charlie" == p.second);
95 }
96
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());
102
103   CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[0].type);
104
105   CU_ASSERT(SHRPX_LOGF_LITERAL == res[1].type);
106   CU_ASSERT(0 == strcmp(" - $remote_user [", res[1].value.get()));
107
108   CU_ASSERT(SHRPX_LOGF_TIME_LOCAL == res[2].type);
109
110   CU_ASSERT(SHRPX_LOGF_LITERAL == res[3].type);
111   CU_ASSERT(0 == strcmp("] \"", res[3].value.get()));
112
113   CU_ASSERT(SHRPX_LOGF_REQUEST == res[4].type);
114
115   CU_ASSERT(SHRPX_LOGF_LITERAL == res[5].type);
116   CU_ASSERT(0 == strcmp("\" ", res[5].value.get()));
117
118   CU_ASSERT(SHRPX_LOGF_STATUS == res[6].type);
119
120   CU_ASSERT(SHRPX_LOGF_LITERAL == res[7].type);
121   CU_ASSERT(0 == strcmp(" ", res[7].value.get()));
122
123   CU_ASSERT(SHRPX_LOGF_BODY_BYTES_SENT == res[8].type);
124
125   CU_ASSERT(SHRPX_LOGF_LITERAL == res[9].type);
126   CU_ASSERT(0 == strcmp(" \"", res[9].value.get()));
127
128   CU_ASSERT(SHRPX_LOGF_HTTP == res[10].type);
129   CU_ASSERT(0 == strcmp("referer", res[10].value.get()));
130
131   CU_ASSERT(SHRPX_LOGF_LITERAL == res[11].type);
132   CU_ASSERT(0 == strcmp("\" \"", res[11].value.get()));
133
134   CU_ASSERT(SHRPX_LOGF_HTTP == res[12].type);
135   CU_ASSERT(0 == strcmp("user-agent", res[12].value.get()));
136
137   CU_ASSERT(SHRPX_LOGF_LITERAL == res[13].type);
138   CU_ASSERT(0 == strcmp("\"", res[13].value.get()));
139 }
140
141 void test_shrpx_config_read_tls_ticket_key_file(void) {
142   char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
143   auto fd1 = mkstemp(file1);
144   assert(fd1 != -1);
145   assert(48 ==
146          write(fd1, "0..............12..............34..............5", 48));
147   char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
148   auto fd2 = mkstemp(file2);
149   assert(fd2 != -1);
150   assert(48 ==
151          write(fd2, "6..............78..............9a..............b", 48));
152
153   close(fd1);
154   close(fd2);
155   auto ticket_keys = read_tls_ticket_key_file({file1, file2});
156   unlink(file1);
157   unlink(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)));
162   CU_ASSERT(0 ==
163             memcmp("2..............3", key->aes_key, sizeof(key->aes_key)));
164   CU_ASSERT(0 ==
165             memcmp("4..............5", key->hmac_key, sizeof(key->hmac_key)));
166
167   key = &ticket_keys->keys[1];
168   CU_ASSERT(0 == memcmp("6..............7", key->name, sizeof(key->name)));
169   CU_ASSERT(0 ==
170             memcmp("8..............9", key->aes_key, sizeof(key->aes_key)));
171   CU_ASSERT(0 ==
172             memcmp("a..............b", key->hmac_key, sizeof(key->hmac_key)));
173 }
174
175 } // namespace shrpx