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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// Copyright (C) 2017 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 <cc/data.h>
#include <cc/json_feed.h>
#include <gtest/gtest.h>
#include <sstream>
#include <string>
using namespace isc::config;
using namespace isc::data;
namespace {
/// @brief Test fixture class for @ref JSONFeed class.
class JSONFeedTest : public ::testing::Test {
public:
/// @brief Constructor.
///
/// Initializes @ref json_map_ and @ref json_list_ which hold reference
/// JSON structures.
JSONFeedTest()
: json_map_(), json_list_() {
ElementPtr m = Element::fromJSON(createJSON());
ElementPtr l = Element::createList();
l->add(m);
json_map_ = m;
json_list_ = l;
}
/// @brief Creates a JSON map holding 20 elements.
///
/// Each map value is a list of 20 elements.
std::string createJSON() const {
// Create a list of 20 elements.
ElementPtr list_element = Element::createList();
for (unsigned i = 0; i < 20; ++i) {
std::ostringstream s;
s << "list_element" << i;
list_element->add(Element::create(s.str()));
}
// Create a map of 20 elements. Each map element holds a list
// of 20 elements.
ElementPtr map_element = Element::createMap();
for (unsigned i = 0; i < 20; ++i) {
std::ostringstream s;
s << "map_element" << i;
map_element->set(s.str(), list_element);
}
return (prettyPrint(map_element));
}
/// @brief Test that the JSONFeed correctly recognizes the beginning
/// and the end of the JSON structure.
///
/// @param input_json A string holding an input JSON structure.
/// @param expected_output A structure holding expected output from the
/// @ref JSONFeed::toElement.
void testRead(const std::string& input_json,
const ConstElementPtr& expected_output) {
JSONFeed feed;
ASSERT_NO_THROW(feed.initModel());
// Post the data into the feed in 10 bytes long chunks.
size_t chunk = 10;
for (size_t i = 0; i < input_json.size(); i += chunk) {
bool done = false;
// When we're near the end of the data stream, the chunk length may
// vary.
if (i + chunk >= input_json.size()) {
chunk = input_json.size() - i;
done = true;
}
// Feed the parser with a data chunk and parse it.
feed.postBuffer(&input_json[i], chunk);
feed.poll();
if (!done) {
ASSERT_TRUE(feed.needData());
}
}
// Convert parsed/collected data in the feed into the structure of
// elements.
ConstElementPtr element_from_feed = feed.toElement();
EXPECT_TRUE(element_from_feed->equals(*expected_output));
}
/// @brief Test that the @ref JSONFeed signals an error when the input
/// string holds invalid data.
///
/// @param input_json A string holding an input JSON structure.
void testInvalidRead(const std::string& input_json) {
JSONFeed feed;
ASSERT_NO_THROW(feed.initModel());
ASSERT_NO_THROW(feed.postBuffer(&input_json[0], input_json.size()));
ASSERT_NO_THROW(feed.poll());
EXPECT_FALSE(feed.needData());
EXPECT_FALSE(feed.feedOk());
}
/// @brief JSON map holding a number of lists.
ConstElementPtr json_map_;
/// @brief JSON list holding a map of lists.
ConstElementPtr json_list_;
};
// This test verifies that a JSON structure starting with '{' is accepted
// and parsed.
TEST_F(JSONFeedTest, startWithBrace) {
std::string json = createJSON();
testRead(json, json_map_);
}
// This test verifies that a JSON structure starting with '[' is accepted
// and parsed.
TEST_F(JSONFeedTest, startWithSquareBracket) {
std::string json = createJSON();
json = std::string("[") + json + std::string("]");
testRead(json, json_list_);
}
// This test verifies that input JSON can be preceded with whitespaces.
TEST_F(JSONFeedTest, startWithWhitespace) {
std::string json = createJSON();
json = std::string(" \r\r\t ") + json;
testRead(json, json_map_);
}
// This test verifies that an empty map is accepted and parsed.
TEST_F(JSONFeedTest, emptyMap) {
std::string json = "{}";
testRead(json, Element::createMap());
}
// This test verifies that an empty list is accepted and parsed.
TEST_F(JSONFeedTest, emptyList) {
std::string json = "[ ]";
testRead(json, Element::createList());
}
// This test verifies that an error is signalled when a JSON structure
// is preceded by invalid character.
TEST_F(JSONFeedTest, unexpectedCharacter) {
std::string json = "a {}";
testInvalidRead(json);
}
// This test verifies that an error is signalled when a JSON structure
// lacks an opening brace character.
TEST_F(JSONFeedTest, noOpeningBrace) {
std::string json = "\"x\": \"y\" }";
testInvalidRead(json);
}
// This test verifies that an error is signalled when a JSON structure
// lacks an opening square bracket.
TEST_F(JSONFeedTest, noOpeningSquareBracket) {
std::string json = "\"x\", \"y\" ]";
testInvalidRead(json);
}
// This test verifies that a string is correctly handled
TEST_F(JSONFeedTest, string) {
std::string json = "{ \"braces\": \"}}}}\" }";
ElementPtr expected = Element::createMap();
expected->set("braces", Element::create("}}}}"));
testRead(json, expected);
}
// This test verifies that a string with escapes is correctly handled
TEST_F(JSONFeedTest, escape) {
std::string json = "{ \"escapes\": \"\\n\\t\\\"\\\\\" }";
ElementPtr expected = Element::createMap();
expected->set("escapes", Element::create("\n\t\"\\"));
testRead(json, expected);
}
} // end of anonymous namespace.
|