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
|
#include "bencode2.h"
#include <cppunit/extensions/HelperMacros.h>
#include "RecoverableException.h"
namespace aria2 {
class Bencode2Test:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(Bencode2Test);
CPPUNIT_TEST(testDecode);
CPPUNIT_TEST(testDecode_overflow);
CPPUNIT_TEST(testEncode);
CPPUNIT_TEST_SUITE_END();
private:
public:
void testDecode();
void testDecode_overflow();
void testEncode();
};
CPPUNIT_TEST_SUITE_REGISTRATION( Bencode2Test );
void Bencode2Test::testDecode()
{
{
// string, integer and list in dict
SharedHandle<ValueBase> r =
bencode2::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee");
const Dict* dict = asDict(r);
CPPUNIT_ASSERT(dict);
CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
asString(dict->get("name"))->s());
CPPUNIT_ASSERT_EQUAL(static_cast<Integer::ValueType>(12345678900LL),
asInteger(dict->get("size"))->i());
const List* list = asList(dict->get("files"));
CPPUNIT_ASSERT(list);
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list->size());
CPPUNIT_ASSERT_EQUAL(std::string("bin"),
asString(list->get(0))->s());
CPPUNIT_ASSERT_EQUAL(std::string("doc"),
asString(list->get(1))->s());
}
{
// dict in list
SharedHandle<ValueBase> r = bencode2::decode("ld1:ki123eee");
const List* list = asList(r);
CPPUNIT_ASSERT(list);
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list->size());
const Dict* dict = asDict(list->get(0));
CPPUNIT_ASSERT(dict);
CPPUNIT_ASSERT_EQUAL(static_cast<Integer::ValueType>(123),
asInteger(dict->get("k"))->i());
}
{
// empty key is allowed
SharedHandle<ValueBase> s = bencode2::decode("d0:1:ve");
}
{
// empty string
SharedHandle<ValueBase> s = bencode2::decode("0:");
CPPUNIT_ASSERT_EQUAL(std::string(""), asString(s)->s());
}
{
// empty dict
SharedHandle<ValueBase> d = bencode2::decode("de");
CPPUNIT_ASSERT(asDict(d)->empty());
}
{
// empty list
SharedHandle<ValueBase> l = bencode2::decode("le");
CPPUNIT_ASSERT(asList(l)->empty());
}
{
// integer, without ending 'e'
try {
bencode2::decode("i3");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" Delimiter 'e' not found."),
std::string(e.what()));
}
}
{
// dict, without ending 'e'
try {
bencode2::decode("d");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" Unexpected EOF in dict context."
" 'e' expected."),
std::string(e.what()));
}
}
{
// list, without ending 'e'
try {
bencode2::decode("l");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" Unexpected EOF in list context."
" 'e' expected."),
std::string(e.what()));
}
}
{
// string, less than the specified length.
try {
bencode2::decode("3:ab");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" Expected 3 bytes of data,"
" but only 2 read."),
std::string(e.what()));
}
}
{
// string, but length is invalid
try {
bencode2::decode("x:abc");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" A positive integer expected"
" but none found."),
std::string(e.what()));
}
}
{
// string with minus length
try {
bencode2::decode("-1:a");
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" A positive integer expected"
" but none found."),
std::string(e.what()));
}
}
{
// empty encoded data
CPPUNIT_ASSERT(bencode2::decode("").isNull());
}
{
// ignore trailing garbage at the end of the input.
SharedHandle<ValueBase> s = bencode2::decode("5:aria2trail");
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), asString(s)->s());
}
{
// Get trailing garbage position
size_t end;
SharedHandle<ValueBase> s = bencode2::decode("5:aria2trail", end);
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), asString(s)->s());
CPPUNIT_ASSERT_EQUAL((size_t)7, end);
}
}
void Bencode2Test::testDecode_overflow()
{
std::string s;
size_t depth = bencode2::MAX_STRUCTURE_DEPTH+1;
for(size_t i = 0; i < depth; ++i) {
s += "l";
}
for(size_t i = 0; i < depth; ++i) {
s += "e";
}
try {
bencode2::decode(s);
CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) {
// success
}
}
void Bencode2Test::testEncode()
{
{
Dict dict;
dict["name"] = String::g("aria2");
dict["loc"] = Integer::g(80000);
SharedHandle<List> files = List::g();
files->append(String::g("aria2c"));
dict["files"] = files;
SharedHandle<Dict> attrs = Dict::g();
attrs->put("license", String::g("GPL"));
dict["attrs"] = attrs;
CPPUNIT_ASSERT_EQUAL(std::string("d"
"5:attrsd7:license3:GPLe"
"5:filesl6:aria2ce"
"3:loci80000e"
"4:name5:aria2"
"e"),
bencode2::encode(&dict));
}
}
} // namespace aria2
|