Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / src / shrpx_http_test.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2016 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_http_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_http.h"
36 #include "shrpx_config.h"
37 #include "shrpx_log.h"
38
39 namespace shrpx {
40
41 void test_shrpx_http_create_forwarded(void) {
42   BlockAllocator balloc(1024, 1024);
43
44   CU_ASSERT("by=\"example.com:3000\";for=\"[::1]\";host=\"www.example.com\";"
45             "proto=https" ==
46             http::create_forwarded(balloc,
47                                    FORWARDED_BY | FORWARDED_FOR |
48                                        FORWARDED_HOST | FORWARDED_PROTO,
49                                    StringRef::from_lit("example.com:3000"),
50                                    StringRef::from_lit("[::1]"),
51                                    StringRef::from_lit("www.example.com"),
52                                    StringRef::from_lit("https")));
53
54   CU_ASSERT("for=192.168.0.1" ==
55             http::create_forwarded(
56                 balloc, FORWARDED_FOR, StringRef::from_lit("alpha"),
57                 StringRef::from_lit("192.168.0.1"),
58                 StringRef::from_lit("bravo"), StringRef::from_lit("charlie")));
59
60   CU_ASSERT("by=_hidden;for=\"[::1]\"" ==
61             http::create_forwarded(
62                 balloc, FORWARDED_BY | FORWARDED_FOR,
63                 StringRef::from_lit("_hidden"), StringRef::from_lit("[::1]"),
64                 StringRef::from_lit(""), StringRef::from_lit("")));
65
66   CU_ASSERT("by=\"[::1]\";for=_hidden" ==
67             http::create_forwarded(
68                 balloc, FORWARDED_BY | FORWARDED_FOR,
69                 StringRef::from_lit("[::1]"), StringRef::from_lit("_hidden"),
70                 StringRef::from_lit(""), StringRef::from_lit("")));
71
72   CU_ASSERT("" ==
73             http::create_forwarded(
74                 balloc,
75                 FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO,
76                 StringRef::from_lit(""), StringRef::from_lit(""),
77                 StringRef::from_lit(""), StringRef::from_lit("")));
78 }
79
80 void test_shrpx_http_create_via_header_value(void) {
81   std::array<char, 16> buf;
82
83   auto end = http::create_via_header_value(std::begin(buf), 1, 1);
84
85   CU_ASSERT(("1.1 nghttpx" == StringRef{std::begin(buf), end}));
86
87   std::fill(std::begin(buf), std::end(buf), '\0');
88
89   end = http::create_via_header_value(std::begin(buf), 2, 0);
90
91   CU_ASSERT(("2 nghttpx" == StringRef{std::begin(buf), end}));
92 }
93
94 void test_shrpx_http_create_affinity_cookie(void) {
95   BlockAllocator balloc(1024, 1024);
96   StringRef c;
97
98   c = http::create_affinity_cookie(balloc, StringRef::from_lit("cookie-val"),
99                                    0xf1e2d3c4u, StringRef{}, false);
100
101   CU_ASSERT("cookie-val=f1e2d3c4" == c);
102
103   c = http::create_affinity_cookie(balloc, StringRef::from_lit("alpha"),
104                                    0x00000000u, StringRef{}, true);
105
106   CU_ASSERT("alpha=00000000; Secure" == c);
107
108   c = http::create_affinity_cookie(balloc, StringRef::from_lit("bravo"),
109                                    0x01111111u, StringRef::from_lit("bar"),
110                                    false);
111
112   CU_ASSERT("bravo=01111111; Path=bar" == c);
113
114   c = http::create_affinity_cookie(balloc, StringRef::from_lit("charlie"),
115                                    0x01111111u, StringRef::from_lit("bar"),
116                                    true);
117
118   CU_ASSERT("charlie=01111111; Path=bar; Secure" == c);
119 }
120
121 } // namespace shrpx