1 // Copyright (c) 2011 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.
10 #include "base/basictypes.h"
11 #include "chrome/installer/mini_installer/mini_string.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using mini_installer::StackString
;
17 class MiniInstallerStringTest
: public testing::Test
{
19 void SetUp() override
{}
20 void TearDown() override
{}
24 // Tests the strcat/strcpy/length support of the StackString class.
25 TEST_F(MiniInstallerStringTest
, StackStringOverflow
) {
26 static const wchar_t kTestString
[] = L
"1234567890";
28 StackString
<MAX_PATH
> str
;
29 EXPECT_EQ(MAX_PATH
, str
.capacity());
31 std::wstring compare_str
;
33 EXPECT_EQ(str
.length(), compare_str
.length());
34 EXPECT_EQ(0, compare_str
.compare(str
.get()));
36 size_t max_chars
= str
.capacity() - 1;
38 while ((str
.length() + (_countof(kTestString
) - 1)) <= max_chars
) {
39 EXPECT_TRUE(str
.append(kTestString
));
40 compare_str
.append(kTestString
);
41 EXPECT_EQ(str
.length(), compare_str
.length());
42 EXPECT_EQ(0, compare_str
.compare(str
.get()));
45 EXPECT_GT(static_cast<size_t>(MAX_PATH
), str
.length());
47 // Now we've exhausted the space we allocated for the string,
48 // so append should fail.
49 EXPECT_FALSE(str
.append(kTestString
));
51 // ...and remain unchanged.
52 EXPECT_EQ(0, compare_str
.compare(str
.get()));
53 EXPECT_EQ(str
.length(), compare_str
.length());
58 EXPECT_EQ(0, compare_str
.compare(str
.get()));
59 EXPECT_EQ(str
.length(), compare_str
.length());
62 // Tests the case insensitive find support of the StackString class.
63 TEST_F(MiniInstallerStringTest
, StackStringFind
) {
64 static const wchar_t kTestStringSource
[] = L
"1234ABcD567890";
65 static const wchar_t kTestStringFind
[] = L
"abcd";
66 static const wchar_t kTestStringNotFound
[] = L
"80";
68 StackString
<MAX_PATH
> str
;
69 EXPECT_TRUE(str
.assign(kTestStringSource
));
70 EXPECT_EQ(str
.get(), str
.findi(kTestStringSource
));
71 EXPECT_EQ(static_cast<const wchar_t*>(NULL
), str
.findi(kTestStringNotFound
));
72 const wchar_t* found
= str
.findi(kTestStringFind
);
73 EXPECT_NE(static_cast<const wchar_t*>(NULL
), found
);
74 std::wstring
check(found
, _countof(kTestStringFind
) - 1);
75 EXPECT_EQ(0, lstrcmpi(check
.c_str(), kTestStringFind
));