tizen 2.4 release
[external/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 #include <unistd.h>
28
29 #include <cstdlib>
30
31 #include <CUnit/CUnit.h>
32
33 #include "shrpx_config.h"
34
35 namespace shrpx {
36
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);
42
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);
48
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);
56
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);
61
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);
68 }
69
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);
74
75   p = parse_header("a:  b");
76   CU_ASSERT("a" == p.first);
77   CU_ASSERT("b" == p.second);
78
79   p = parse_header(":a: b");
80   CU_ASSERT(":a" == p.first);
81   CU_ASSERT("b" == p.second);
82
83   p = parse_header("a: :b");
84   CU_ASSERT("a" == p.first);
85   CU_ASSERT(":b" == p.second);
86
87   p = parse_header(": b");
88   CU_ASSERT(p.first.empty());
89
90   p = parse_header("alpha: bravo charlie");
91   CU_ASSERT("alpha" == p.first);
92   CU_ASSERT("bravo charlie" == p.second);
93 }
94
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());
100
101   CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[0].type);
102
103   CU_ASSERT(SHRPX_LOGF_LITERAL == res[1].type);
104   CU_ASSERT(0 == strcmp(" - $remote_user [", res[1].value.get()));
105
106   CU_ASSERT(SHRPX_LOGF_TIME_LOCAL == res[2].type);
107
108   CU_ASSERT(SHRPX_LOGF_LITERAL == res[3].type);
109   CU_ASSERT(0 == strcmp("] \"", res[3].value.get()));
110
111   CU_ASSERT(SHRPX_LOGF_REQUEST == res[4].type);
112
113   CU_ASSERT(SHRPX_LOGF_LITERAL == res[5].type);
114   CU_ASSERT(0 == strcmp("\" ", res[5].value.get()));
115
116   CU_ASSERT(SHRPX_LOGF_STATUS == res[6].type);
117
118   CU_ASSERT(SHRPX_LOGF_LITERAL == res[7].type);
119   CU_ASSERT(0 == strcmp(" ", res[7].value.get()));
120
121   CU_ASSERT(SHRPX_LOGF_BODY_BYTES_SENT == res[8].type);
122
123   CU_ASSERT(SHRPX_LOGF_LITERAL == res[9].type);
124   CU_ASSERT(0 == strcmp(" \"", res[9].value.get()));
125
126   CU_ASSERT(SHRPX_LOGF_HTTP == res[10].type);
127   CU_ASSERT(0 == strcmp("referer", res[10].value.get()));
128
129   CU_ASSERT(SHRPX_LOGF_LITERAL == res[11].type);
130   CU_ASSERT(0 == strcmp("\" \"", res[11].value.get()));
131
132   CU_ASSERT(SHRPX_LOGF_HTTP == res[12].type);
133   CU_ASSERT(0 == strcmp("user-agent", res[12].value.get()));
134
135   CU_ASSERT(SHRPX_LOGF_LITERAL == res[13].type);
136   CU_ASSERT(0 == strcmp("\"", res[13].value.get()));
137 }
138
139 void test_shrpx_config_read_tls_ticket_key_file(void) {
140   char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
141   auto fd1 = mkstemp(file1);
142   assert(fd1 != -1);
143   assert(48 ==
144          write(fd1, "0..............12..............34..............5", 48));
145   char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
146   auto fd2 = mkstemp(file2);
147   assert(fd2 != -1);
148   assert(48 ==
149          write(fd2, "6..............78..............9a..............b", 48));
150
151   close(fd1);
152   close(fd2);
153   auto ticket_keys = read_tls_ticket_key_file({file1, file2});
154   unlink(file1);
155   unlink(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)));
160   CU_ASSERT(0 ==
161             memcmp("2..............3", key->aes_key, sizeof(key->aes_key)));
162   CU_ASSERT(0 ==
163             memcmp("4..............5", key->hmac_key, sizeof(key->hmac_key)));
164
165   key = &ticket_keys->keys[1];
166   CU_ASSERT(0 == memcmp("6..............7", key->name, sizeof(key->name)));
167   CU_ASSERT(0 ==
168             memcmp("8..............9", key->aes_key, sizeof(key->aes_key)));
169   CU_ASSERT(0 ==
170             memcmp("a..............b", key->hmac_key, sizeof(key->hmac_key)));
171 }
172
173 } // namespace shrpx