From af39c78205caa4582d188da73dfd9bc7e5eada5a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 2 May 2014 07:49:56 +0000 Subject: [PATCH] extend lazy_bdecode unit test --- test/test_bencoding.cpp | 79 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index faea07d06..61ce48ac5 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -363,16 +363,16 @@ int test_main() , get_bdecode_category())); } - // test unexpected EOF while parsing dict key + // test expected string while parsing dict key { - char b[] = "d1000:"; + char b[] = "df00:"; lazy_entry e; error_code ec; int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec, NULL); TEST_CHECK(ret != 0); printf("%s\n", print_entry(e).c_str()); - TEST_EQUAL(ec, error_code(bdecode_errors::unexpected_eof + TEST_EQUAL(ec, error_code(bdecode_errors::expected_string , get_bdecode_category())); } @@ -416,6 +416,69 @@ int test_main() , get_bdecode_category())); } + // test empty string + { + char b[] = ""; + + lazy_entry e; + error_code ec; + int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec, NULL); + TEST_EQUAL(ret, 0); + printf("%s\n", print_entry(e).c_str()); + } + + // test partial string + { + char b[] = "100:.."; + + lazy_entry e; + error_code ec; + int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec, NULL); + TEST_CHECK(ret != 0); + printf("%s\n", print_entry(e).c_str()); + TEST_EQUAL(ec, error_code(bdecode_errors::unexpected_eof + , get_bdecode_category())); + } + + // test pascal string dict + { + char b[] = "d6:foobar6:barfooe"; + + lazy_entry e; + error_code ec; + int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec, NULL); + TEST_EQUAL(ret, 0); + printf("%s\n", print_entry(e).c_str()); + + pascal_string ps = e.dict_find_pstr("foobar"); + TEST_EQUAL(memcmp(ps.ptr, "barfoo", ps.len), 0); + TEST_EQUAL(ps.len, 6); + + ps = e.dict_find_pstr("foobar2"); + TEST_EQUAL(ps.ptr, NULL); + TEST_EQUAL(ps.len, 0); + } + + // test pascal string in list + { + char b[] = "l6:foobari4ee"; + + lazy_entry e; + error_code ec; + int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec, NULL); + TEST_EQUAL(ret, 0); + printf("%s\n", print_entry(e).c_str()); + + TEST_EQUAL(e.list_size(), 2); + pascal_string ps = e.list_pstr_at(0); + TEST_EQUAL(memcmp(ps.ptr, "foobar", ps.len), 0); + TEST_EQUAL(ps.len, 6); + + ps = e.list_pstr_at(1); + TEST_EQUAL(ps.ptr, NULL); + TEST_EQUAL(ps.len, 0); + } + { unsigned char buf[] = { 0x44 , 0x91 , 0x3a }; entry ent = bdecode(buf, buf + sizeof(buf)); @@ -432,6 +495,16 @@ int test_main() TEST_EQUAL(e, b + sizeof(b) - 2); } + // test invalid digit + { + char b[] = "0o"; + boost::int64_t val = 0; + bdecode_errors::error_code_enum ec; + char const* e = parse_int(b, b + sizeof(b)-1, 'e', val, ec); + TEST_EQUAL(ec, bdecode_errors::expected_string); + TEST_EQUAL(e, b + 1); + } + { char b[] = "9223372036854775808:"; boost::int64_t val = 0;