1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // Unit test for string manipulation functions used in the RLZ library. 6 7 #include "rlz/lib/string_utils.h" 8 9 #include "base/basictypes.h" 10 #include "base/logging.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "rlz/lib/assert.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 TEST(StringUtilsUnittest, IsAscii) { 17 rlz_lib::SetExpectedAssertion(""); 18 19 char bad_letters[] = {'\x80', '\xA0', '\xFF'}; 20 for (size_t i = 0; i < arraysize(bad_letters); ++i) 21 EXPECT_FALSE(rlz_lib::IsAscii(bad_letters[i])); 22 23 char good_letters[] = {'A', '~', '\n', 0x7F, 0x00}; 24 for (size_t i = 0; i < arraysize(good_letters); ++i) 25 EXPECT_TRUE(rlz_lib::IsAscii(good_letters[i])); 26 } 27 28 TEST(StringUtilsUnittest, HexStringToInteger) { 29 rlz_lib::SetExpectedAssertion("HexStringToInteger: text is NULL."); 30 EXPECT_EQ(0, rlz_lib::HexStringToInteger(NULL)); 31 32 rlz_lib::SetExpectedAssertion(""); 33 EXPECT_EQ(0, rlz_lib::HexStringToInteger("")); 34 EXPECT_EQ(0, rlz_lib::HexStringToInteger(" ")); 35 EXPECT_EQ(0, rlz_lib::HexStringToInteger(" 0x ")); 36 EXPECT_EQ(0, rlz_lib::HexStringToInteger(" 0x0 ")); 37 EXPECT_EQ(0x12345, rlz_lib::HexStringToInteger("12345")); 38 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger("a34Ed0")); 39 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger("0xa34Ed0")); 40 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger(" 0xa34Ed0")); 41 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger("0xa34Ed0 ")); 42 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger(" 0xa34Ed0 ")); 43 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger(" 0x000a34Ed0 ")); 44 EXPECT_EQ(0xa34Ed0, rlz_lib::HexStringToInteger(" 000a34Ed0 ")); 45 46 rlz_lib::SetExpectedAssertion( 47 "HexStringToInteger: text contains non-hex characters."); 48 EXPECT_EQ(0x12ff, rlz_lib::HexStringToInteger("12ffg")); 49 EXPECT_EQ(0x12f, rlz_lib::HexStringToInteger("12f 121")); 50 EXPECT_EQ(0x12f, rlz_lib::HexStringToInteger("12f 121")); 51 EXPECT_EQ(0, rlz_lib::HexStringToInteger("g12f")); 52 EXPECT_EQ(0, rlz_lib::HexStringToInteger(" 0x0 \n")); 53 54 rlz_lib::SetExpectedAssertion(""); 55 } 56 57 TEST(StringUtilsUnittest, TestBytesToString) { 58 unsigned char data[] = {0x1E, 0x00, 0x21, 0x67, 0xFF}; 59 std::string result; 60 61 EXPECT_FALSE(rlz_lib::BytesToString(NULL, 5, &result)); 62 EXPECT_FALSE(rlz_lib::BytesToString(data, 5, NULL)); 63 EXPECT_FALSE(rlz_lib::BytesToString(NULL, 5, NULL)); 64 65 EXPECT_TRUE(rlz_lib::BytesToString(data, 5, &result)); 66 EXPECT_EQ(std::string("1E002167FF"), result); 67 EXPECT_TRUE(rlz_lib::BytesToString(data, 4, &result)); 68 EXPECT_EQ(std::string("1E002167"), result); 69 } 70