summaryrefslogtreecommitdiffstats
path: root/base/string_util_unittest.cc
diff options
context:
space:
mode:
authordeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 14:33:40 +0000
committerdeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 14:33:40 +0000
commit954d58c4d2bd4499ed61da01ee22684669e12f28 (patch)
treeb4e80cfdeea18ecc3fde0aef5cb46754575f3486 /base/string_util_unittest.cc
parent4794938ba46c128bd207c4f4509f86b1aceb1284 (diff)
downloadchromium_src-954d58c4d2bd4499ed61da01ee22684669e12f28.zip
chromium_src-954d58c4d2bd4499ed61da01ee22684669e12f28.tar.gz
chromium_src-954d58c4d2bd4499ed61da01ee22684669e12f28.tar.bz2
Implement an interface compatible wcslcpy and strlcpy. Possibly slightly
slower than the OpenBSD implementation, but a bit clearer and fits our style. Move file_util_posix to use it now that we have it everywhere. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@798 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util_unittest.cc')
-rw-r--r--base/string_util_unittest.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index fb3681b5..7cc9764 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1252,4 +1252,57 @@ TEST(StringUtilTest, MatchPatternTest) {
EXPECT_EQ(MatchPattern("Hello*", "Hello*"), true); // narrow string
}
+TEST(StringUtilTest, LcpyTest) {
+ // Test the normal case where we fit in our buffer.
+ {
+ char dst[10];
+ wchar_t wdst[10];
+ EXPECT_EQ(7, base::strlcpy(dst, "abcdefg", arraysize(dst)));
+ EXPECT_EQ(0, memcmp(dst, "abcdefg", 8));
+ EXPECT_EQ(7, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst)));
+ EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8));
+ }
+
+ // Test dst_size == 0, nothing should be written to |dst| and we should
+ // have the equivalent of strlen(src).
+ {
+ char dst[2] = {1, 2};
+ wchar_t wdst[2] = {1, 2};
+ EXPECT_EQ(7, base::strlcpy(dst, "abcdefg", 0));
+ EXPECT_EQ(1, dst[0]);
+ EXPECT_EQ(2, dst[1]);
+ EXPECT_EQ(7, base::wcslcpy(wdst, L"abcdefg", 0));
+ EXPECT_EQ(1, wdst[0]);
+ EXPECT_EQ(2, wdst[1]);
+ }
+
+ // Test the case were we _just_ competely fit including the null.
+ {
+ char dst[8];
+ wchar_t wdst[8];
+ EXPECT_EQ(7, base::strlcpy(dst, "abcdefg", arraysize(dst)));
+ EXPECT_EQ(0, memcmp(dst, "abcdefg", 8));
+ EXPECT_EQ(7, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst)));
+ EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8));
+ }
+ // Test the case were we we are one smaller, so we can't fit the null.
+ {
+ char dst[7];
+ wchar_t wdst[7];
+ EXPECT_EQ(7, base::strlcpy(dst, "abcdefg", arraysize(dst)));
+ EXPECT_EQ(0, memcmp(dst, "abcdef", 7));
+ EXPECT_EQ(7, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst)));
+ EXPECT_EQ(0, memcmp(wdst, L"abcdef", sizeof(wchar_t) * 7));
+ }
+
+ // Test the case were we are just too small.
+ {
+ char dst[3];
+ wchar_t wdst[3];
+ EXPECT_EQ(7, base::strlcpy(dst, "abcdefg", arraysize(dst)));
+ EXPECT_EQ(0, memcmp(dst, "ab", 3));
+ EXPECT_EQ(7, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst)));
+ EXPECT_EQ(0, memcmp(wdst, L"ab", sizeof(wchar_t) * 3));
+ }
+}