File: ValueBaseBencodeParserTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (223 lines) | stat: -rw-r--r-- 7,326 bytes parent folder | download | duplicates (2)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "ValueBaseBencodeParser.h"

#include <cppunit/extensions/HelperMacros.h>

#include "ValueBase.h"
#include "BencodeParser.h"

namespace aria2 {

class ValueBaseBencodeParserTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(ValueBaseBencodeParserTest);
  CPPUNIT_TEST(testParseUpdate);
  CPPUNIT_TEST_SUITE_END();

public:
  void testParseUpdate();
};

CPPUNIT_TEST_SUITE_REGISTRATION(ValueBaseBencodeParserTest);

namespace {
void checkDecodeError(const std::string& src)
{
  bittorrent::ValueBaseBencodeParser parser;
  ssize_t error;
  std::shared_ptr<ValueBase> r =
      parser.parseFinal(src.c_str(), src.size(), error);
  CPPUNIT_ASSERT(!r);
  CPPUNIT_ASSERT(error < 0);
}
} // namespace

void ValueBaseBencodeParserTest::testParseUpdate()
{
  bittorrent::ValueBaseBencodeParser parser;
  ssize_t error;
  {
    // empty string
    std::string src = "0:";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL(std::string(""), downcast<String>(s)->s());
  }
  {
    // integer 0
    std::string src = "i0e";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(s)->i());
  }
  {
    // empty dict
    std::string src = "de";
    std::shared_ptr<ValueBase> d =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT(downcast<Dict>(d)->empty());
  }
  {
    // empty list
    std::string src = "le";
    std::shared_ptr<ValueBase> l =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT(downcast<List>(l)->empty());
  }
  {
    // string
    std::string src = "3:foo";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL(std::string("foo"), downcast<String>(s)->s());
  }
  {
    // integer
    std::string src = "i9223372036854775807e";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
                         downcast<Integer>(s)->i());
  }
  {
    // float number, ignored and always 0.
    std::string src = "i+343243.342E-1333e";
    auto s = parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(s)->i());
  }
  {
    // dict, size 1
    std::string src = "d3:fooi123ee";
    std::shared_ptr<ValueBase> d =
        parser.parseFinal(src.c_str(), src.size(), error);
    Dict* dict = downcast<Dict>(d);
    CPPUNIT_ASSERT(dict);
    CPPUNIT_ASSERT(dict->get("foo"));
    CPPUNIT_ASSERT_EQUAL((int64_t)123,
                         downcast<Integer>(dict->get("foo"))->i());
  }
  {
    // dict, size 2
    std::string src = "d3:fooi123e3:bar1:ee";
    std::shared_ptr<ValueBase> d =
        parser.parseFinal(src.c_str(), src.size(), error);
    Dict* dict = downcast<Dict>(d);
    CPPUNIT_ASSERT(dict);
    CPPUNIT_ASSERT_EQUAL((size_t)2, dict->size());
    CPPUNIT_ASSERT(dict->get("foo"));
    CPPUNIT_ASSERT_EQUAL((int64_t)123,
                         downcast<Integer>(dict->get("foo"))->i());
    CPPUNIT_ASSERT(dict->get("bar"));
    CPPUNIT_ASSERT_EQUAL(std::string("e"),
                         downcast<String>(dict->get("bar"))->s());
  }
  {
    // list, size 1
    std::string src = "l3:fooe";
    std::shared_ptr<ValueBase> l =
        parser.parseFinal(src.c_str(), src.size(), error);
    List* list = downcast<List>(l);
    CPPUNIT_ASSERT(list);
    CPPUNIT_ASSERT_EQUAL((size_t)1, list->size());
    CPPUNIT_ASSERT_EQUAL(std::string("foo"),
                         downcast<String>(list->get(0))->s());
  }
  {
    // list, size 2
    std::string src = "l3:fooi123ee";
    std::shared_ptr<ValueBase> l =
        parser.parseFinal(src.c_str(), src.size(), error);
    List* list = downcast<List>(l);
    CPPUNIT_ASSERT(list);
    CPPUNIT_ASSERT_EQUAL((size_t)2, list->size());
    CPPUNIT_ASSERT_EQUAL(std::string("foo"),
                         downcast<String>(list->get(0))->s());
    CPPUNIT_ASSERT_EQUAL((int64_t)123, downcast<Integer>(list->get(1))->i());
  }
  {
    // string, integer and list in dict
    std::string src = "d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee";
    std::shared_ptr<ValueBase> r =
        parser.parseFinal(src.c_str(), src.size(), error);
    const Dict* dict = downcast<Dict>(r);
    CPPUNIT_ASSERT(dict);
    CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
                         downcast<String>(dict->get("name"))->s());
    CPPUNIT_ASSERT_EQUAL(static_cast<Integer::ValueType>(12345678900LL),
                         downcast<Integer>(dict->get("size"))->i());
    const List* list = downcast<List>(dict->get("files"));
    CPPUNIT_ASSERT(list);
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list->size());
    CPPUNIT_ASSERT_EQUAL(std::string("bin"),
                         downcast<String>(list->get(0))->s());
    CPPUNIT_ASSERT_EQUAL(std::string("doc"),
                         downcast<String>(list->get(1))->s());
  }
  {
    // dict in list
    std::string src = "ld1:ki123eee";
    std::shared_ptr<ValueBase> r =
        parser.parseFinal(src.c_str(), src.size(), error);
    const List* list = downcast<List>(r);
    CPPUNIT_ASSERT(list);
    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list->size());
    const Dict* dict = downcast<Dict>(list->get(0));
    CPPUNIT_ASSERT(dict);
    CPPUNIT_ASSERT_EQUAL(static_cast<Integer::ValueType>(123),
                         downcast<Integer>(dict->get("k"))->i());
  }
  {
    // empty key is allowed
    std::string src = "d0:1:ve";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
  }
  {
    // empty encoded data
    std::string src = "";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT(!s);
  }
  // integer, without ending 'e'
  checkDecodeError("i3");
  // dict, without ending 'e'
  checkDecodeError("d");
  // list, without ending 'e'
  checkDecodeError("l");
  // string, less than the specified length.
  checkDecodeError("3:ab");
  // string, but length is invalid
  checkDecodeError("x:abc");
  // string with minus length
  checkDecodeError("-1:a");
  // too deep structure
  checkDecodeError(std::string(51, 'l') + std::string(51, 'e'));
  checkDecodeError(std::string(50, 'l') + "d3:fooi100ee" +
                   std::string(50, 'e'));
  // float number, but including bad characters
  checkDecodeError("i-1.134a+33e");
  checkDecodeError("ixe");
  // empty number
  checkDecodeError("ie");
  {
    // ignore trailing garbage at the end of the input.
    std::string src = "5:aria2trail";
    std::shared_ptr<ValueBase> s =
        parser.parseFinal(src.c_str(), src.size(), error);
    CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast<String>(s)->s());
    // Get trailing garbage position
    CPPUNIT_ASSERT_EQUAL((ssize_t)7, error);
  }
  {
    // dict, empty member name
    std::string src = "d0:i123ee";
    std::shared_ptr<ValueBase> d =
        parser.parseFinal(src.c_str(), src.size(), error);
    Dict* dict = downcast<Dict>(d);
    CPPUNIT_ASSERT(dict);
    CPPUNIT_ASSERT(dict->get(""));
    CPPUNIT_ASSERT_EQUAL((int64_t)123, downcast<Integer>(dict->get(""))->i());
  }
}

} // namespace aria2