-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpush_parser.cpp
More file actions
205 lines (180 loc) · 7.81 KB
/
Copy pathpush_parser.cpp
File metadata and controls
205 lines (180 loc) · 7.81 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//#include <bencode/detail/parser/input_adapter.hpp>
#include <bencode/detail/parser/push_parser.hpp>
#include <bencode/detail/events/encode_json_to.hpp>
#include <bencode/detail/events/debug_to.hpp>
#include <bencode/detail/bvalue/events.hpp>
#include <catch2/catch.hpp>
#include <sstream>
#include <fstream>
#include "data.hpp"
using namespace std::string_view_literals;
using namespace bencode;
TEST_CASE("test push parser to json - string_parsing_mode::value")
{
auto parser = push_parser();
std::string out{};
auto json_consumer = bencode::events::encode_json_to(std::back_inserter(out));
auto debug_consumer = bencode::events::debug_to(std::back_inserter(out));
SECTION("compare json output") {
const auto [data, expected] = GENERATE_COPY(table<std::string_view, std::string_view>({
{example, example_json_result},
{sintel_torrent, sintel_json_result}
}));
bool success = parser.parse(json_consumer, data);
CHECK(out == expected);
}
SECTION("error - recursion limit - list") {
auto parser2 = bencode::push_parser({.recursion_limit=10});
auto r = parser2.parse(debug_consumer, recursion_limit_list);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::recursion_depth_exceeded);
}
SECTION("error - recursion limit - dict") {
auto parser2 = bencode::push_parser({.recursion_limit=10});
auto r = parser2.parse(debug_consumer, recursion_limit_dict);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::recursion_depth_exceeded);
}
SECTION("error - value limit") {
auto parser2 = bencode::push_parser({.value_limit=10});
auto r = parser2.parse(debug_consumer, sintel_torrent);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::value_limit_exceeded);
}
SECTION("error - integer parsing") {
auto r = parser.parse(debug_consumer, error_integer);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::leading_zero);
}
SECTION("error - string parsing") {
auto r = parser.parse(debug_consumer, error_string);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::unexpected_eof);
}
SECTION("error - dict key parsing") {
auto r = parser.parse(debug_consumer, error_dict_key);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_colon);
}
SECTION("error - missing end - list") {
auto r = parser.parse(debug_consumer, error_missing_end_list);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_list_value_or_end);
}
SECTION("error - missing end - dict") {
auto r = parser.parse(debug_consumer, error_missing_end_dict);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_key_or_end);
}
SECTION("error - missing value") {
auto r = parser.parse(debug_consumer, error_missing_value);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_value);
}
SECTION("error - missing dict value") {
auto r = parser.parse(debug_consumer, error_missing_dict_value);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_value);
}
SECTION("error - missing list value") {
auto r = parser.parse(debug_consumer, error_missing_list_value_or_end);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_list_value_or_end);
}
SECTION("error - error_missing_dict_key_or_end") {
auto r = parser.parse(debug_consumer, error_missing_dict_key_or_end);
CHECK_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_key_or_end);
}
}
TEST_CASE("test push parser to json - string_parsing_mode::view")
{
namespace bc = bencode;
using view_push_parser = push_parser<bc::string_parsing_mode::view>;
view_push_parser parser{};
std::string out{};
auto json_consumer = bencode::events::encode_json_to(std::back_inserter(out));
auto debug_consumer = bencode::events::debug_to(std::back_inserter(out));
SECTION("compare json output") {
const auto [data, expected] = GENERATE_COPY(table<std::string_view, std::string_view>({
{example, example_json_result},
{sintel_torrent, sintel_json_result}
}));
bool success = parser.parse(json_consumer, data);
CHECK(out == expected);
}
SECTION("error - recursion limit - list") {
auto parser2 = bencode::push_parser({.recursion_limit=10});
auto r = parser2.parse(debug_consumer, recursion_limit_list);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::recursion_depth_exceeded);
}
SECTION("error - recursion limit - dict") {
auto parser2 = view_push_parser({.recursion_limit=10});
auto r = parser2.parse(debug_consumer, recursion_limit_dict);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::recursion_depth_exceeded);
}
SECTION("error - value limit") {
auto parser2 = view_push_parser({.value_limit=10});
auto r = parser2.parse(debug_consumer, sintel_torrent);
CHECK_FALSE(r);
CHECK(parser2.error().errc() == parsing_errc::value_limit_exceeded);
}
SECTION("error - integer parsing") {
auto r = parser.parse(debug_consumer, error_integer);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::leading_zero);
}
SECTION("error - string parsing") {
auto r = parser.parse(debug_consumer, error_string);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::unexpected_eof);
}
SECTION("error - dict key parsing") {
auto r = parser.parse(debug_consumer, error_dict_key);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_colon);
}
SECTION("error - missing end - list") {
auto r = parser.parse(debug_consumer, error_missing_end_list);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_list_value_or_end);
}
SECTION("error - missing end - dict") {
auto r = parser.parse(debug_consumer, error_missing_end_dict);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_key_or_end);
}
SECTION("error - missing value") {
auto r = parser.parse(debug_consumer, error_missing_value);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_value);
}
SECTION("error - missing dict value") {
auto r = parser.parse(debug_consumer, error_missing_dict_value);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_value);
}
SECTION("error - missing list value") {
auto r = parser.parse(debug_consumer, error_missing_list_value_or_end);
REQUIRE_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_list_value_or_end);
}
SECTION("error - error_missing_dict_key_or_end") {
auto r = parser.parse(debug_consumer, error_missing_dict_key_or_end);
CHECK_FALSE(r);
CHECK(parser.error().errc() == parsing_errc::expected_dict_key_or_end);
}
}
TEST_CASE("test push parser with input_iterators")
{
std::ifstream ifs(RESOURCES_DIR"/Fedora-Workstation-Live-x86_64-30.torrent");
auto begin = std::istreambuf_iterator<char>(ifs);
auto end = std::istreambuf_iterator<char>();
std::string out{};
auto json_consumer = bencode::events::encode_json_to(std::back_inserter(out));
push_parser<string_parsing_mode::value, decltype(begin), decltype(end)> parser {};
bool success = parser.parse(json_consumer, begin, end);
CHECK(success);
}