1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 | // Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <http/cfg_http_header.h>
#include <cc/simple_parser.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::http;
using namespace std;
namespace {
// This test verifies copy to response.
TEST(CfgHttpHeaderTest, copy) {
// Create a response.
HttpResponse response(HttpVersion(1, 0), HttpStatusCode::OK);
// Create a HSTS header.
CfgHttpHeader hsts("Strict-Transport-Security", "max-age=31536000");
// Create a random header.
CfgHttpHeader foobar("Foo", "bar");
// Add them to a collection.
CfgHttpHeaders headers;
headers.push_back(hsts);
headers.push_back(foobar);
// Copy headers to response.
EXPECT_NO_THROW(copyHttpHeaders(headers, response));
// Verify.
auto const& got = response.context()->headers_;
ASSERT_EQ(2, got.size());
EXPECT_EQ("Strict-Transport-Security", got[0].name_);
EXPECT_EQ("max-age=31536000", got[0].value_);
EXPECT_EQ("Foo", got[1].name_);
EXPECT_EQ("bar", got[1].value_);
// Unparse.
string expected = "[ ";
expected += "{ \"name\": \"Strict-Transport-Security\", ";
expected += "\"value\": \"max-age=31536000\" }, ";
expected += "{ \"name\": \"Foo\", \"value\": \"bar\" } ]";
EXPECT_EQ(expected, CfgHttpHeaderstoElement(headers)->str());
}
// This test verifies parse and toElement behavior.
TEST(CfgHttpHeaderTest, parse) {<--- syntax error
// Config.
string config = "[\n"
" {\n"
" \"name\": \"Strict-Transport-Security\",\n"
" \"value\": \"max-age=31536000\",\n"
" \"user-context\": { \"comment\": \"HSTS header\" }\n"
" },{\n"
" \"name\": \"Foo\", \"value\": \"bar\"\n"
" }\n"
" ]\n";
ConstElementPtr json;
ASSERT_NO_THROW(json = Element::fromJSON(config));
CfgHttpHeaders headers;
ASSERT_NO_THROW(headers = parseCfgHttpHeaders(json));
ASSERT_EQ(2, headers.size());
EXPECT_EQ("Strict-Transport-Security", headers[0].name_);
EXPECT_EQ("max-age=31536000", headers[0].value_);
ConstElementPtr user_context = headers[0].getContext();
ASSERT_TRUE(user_context);
EXPECT_EQ("{ \"comment\": \"HSTS header\" }", user_context->str());
EXPECT_EQ("Foo", headers[1].name_);
EXPECT_EQ("bar", headers[1].value_);
EXPECT_FALSE(headers[1].getContext());
ConstElementPtr unparsed;
ASSERT_NO_THROW(unparsed = CfgHttpHeaderstoElement(headers));
EXPECT_TRUE(json->equals(*unparsed));
}
// This test verifies parse error cases.
TEST(CfgHttpHeaderTest, parseErrors) {
// Scenarios.
struct Scenario{
string desc_; // Description.
string config_; // Configuration.
string errmsg_; // Error message.
};
vector<Scenario> scenarios = {
{
"Not a list",
"{ \"name\": \"Foo\", \"value\": \"bar\" }",
"invalid type specified for parameter 'http-headers' "
"(<string>:1:2)"
},
{
"Not a map",
"[ \"Foo\", \"bar\" ]",
"invalid type specified for 'http-headers' item (<string>:1:3)"
},
{
"Unknown keyword",
"[ { \"foo\": \"bar\" } ]",
"spurious 'foo' parameter"
},
{
"Bad name type",
"[ { \"name\": 1 } ]",
"'name' parameter is not a string"
},
{
"Bad value type",
"[ { \"value\": false } ]",
"'value' parameter is not a string"
},
{
"Bad user context type",
"[ { \"user-context\": \"bad\" } ]",
"'user-context' parameter is not a map"
},
{
"Missing name",
"[ { } ]",
"missing 'name' parameter"
},
{
"Missing value",
"[ { \"name\": \"Foo\" } ]",
"missing 'value' parameter"
},
{
"Empty name",
"[ { \"name\": \"\", \"value\": \"\" } ]",
"empty 'name' parameter (<string>:1:13)"
},
{
"Empty value",
"[ { \"name\": \"Foo\", \"value\": \"\" } ]",
"empty 'value' parameter (<string>:1:29)"
}
};
// Iterate over Scenarios.
ConstElementPtr json;
CfgHttpHeaders headers;
for (auto const& scenario : scenarios) {
SCOPED_TRACE(scenario.desc_);
{
ASSERT_NO_THROW(json = Element::fromJSON(scenario.config_));
try {
headers = parseCfgHttpHeaders(json);
ADD_FAILURE() << "exception is expected";
} catch (const DhcpConfigError& ex) {
EXPECT_EQ(scenario.errmsg_, ex.what());
} catch (...) {
ADD_FAILURE() << "DhcpConfigError is expected";
}
}
}
// No error cases.
ConstElementPtr null_json;
ASSERT_NO_THROW(headers = parseCfgHttpHeaders(null_json));
EXPECT_TRUE(headers.empty());
ConstElementPtr empty_json = Element::createList();
ASSERT_NO_THROW(headers = parseCfgHttpHeaders(empty_json));
EXPECT_TRUE(headers.empty());
}
}
|