diff options
author | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-27 16:11:15 +0000 |
---|---|---|
committer | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-27 16:11:15 +0000 |
commit | 5d0c17fe26c9ecc7887ae976f6ee1ca7a73642da (patch) | |
tree | da57a5847f6d6ca51766969160ec4215308008c9 /base/string_util_unittest.cc | |
parent | d39d60d7205f47ae4e9bb4a6bf0081e54fe2a2e0 (diff) | |
download | chromium_src-5d0c17fe26c9ecc7887ae976f6ee1ca7a73642da.zip chromium_src-5d0c17fe26c9ecc7887ae976f6ee1ca7a73642da.tar.gz chromium_src-5d0c17fe26c9ecc7887ae976f6ee1ca7a73642da.tar.bz2 |
Add HexStringToBytes which takes an arbitrary length hex string and creates a vector of bytes.
Review URL: http://codereview.chromium.org/18814
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8716 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util_unittest.cc')
-rw-r--r-- | base/string_util_unittest.cc | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index a3ca7ee..7676f81 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -945,6 +945,58 @@ TEST(StringUtilTest, HexStringToInt) { EXPECT_EQ(0xc0ffee, output); } +TEST(StringUtilTest, HexStringToBytes) { + static const struct { + const std::string input; + const char* output; + size_t output_len; + bool success; + } cases[] = { + {"0", "", 0, false}, // odd number of characters fails + {"00", "\0", 1, true}, + {"42", "\x42", 1, true}, + {"-42", "", 0, false}, // any non-hex value fails + {"+42", "", 0, false}, + {"7fffffff", "\x7f\xff\xff\xff", 4, true}, + {"80000000", "\x80\0\0\0", 4, true}, + {"deadbeef", "\xde\xad\xbe\xef", 4, true}, + {"DeadBeef", "\xde\xad\xbe\xef", 4, true}, + {"0x42", "", 0, false}, // leading 0x fails (x is not hex) + {"0f", "\xf", 1, true}, + {"45 ", "\x45", 1, false}, + {"efgh", "\xef", 1, false}, + {"", "", 0, false}, + {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true}, + {"0123456789ABCDEF012345", + "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true}, + }; + + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::vector<uint8> output; + std::vector<uint8> compare; + EXPECT_EQ(cases[i].success, HexStringToBytes(cases[i].input, &output)) << + i << ": " << cases[i].input; + for (size_t j = 0; j < cases[i].output_len; ++j) + compare.push_back(static_cast<uint8>(cases[i].output[j])); + ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; + EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << + i << ": " << cases[i].input; + + output.clear(); + compare.clear(); + + std::wstring wide_input = ASCIIToWide(cases[i].input); + EXPECT_EQ(cases[i].success, HexStringToBytes(wide_input, &output)) << + i << ": " << cases[i].input; + for (size_t j = 0; j < cases[i].output_len; ++j) + compare.push_back(static_cast<uint8>(cases[i].output[j])); + ASSERT_EQ(output.size(), compare.size()) << i << ": " << cases[i].input; + EXPECT_TRUE(std::equal(output.begin(), output.end(), compare.begin())) << + i << ": " << cases[i].input; + } +} + TEST(StringUtilTest, StringToDouble) { static const struct { std::string input; |