Home | History | Annotate | Download | only in mini_installer
      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.
      4 
      5 #include <windows.h>
      6 
      7 #include <string>
      8 
      9 #include "base/basictypes.h"
     10 #include "chrome/installer/mini_installer/mini_string.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using mini_installer::StackString;
     14 
     15 namespace {
     16 class MiniInstallerStringTest : public testing::Test {
     17  protected:
     18   virtual void SetUp() {
     19   }
     20   virtual void TearDown() {
     21   }
     22 };
     23 }
     24 
     25 // Tests the strcat/strcpy/length support of the StackString class.
     26 TEST_F(MiniInstallerStringTest, StackStringOverflow) {
     27   static const wchar_t kTestString[] = L"1234567890";
     28 
     29   StackString<MAX_PATH> str;
     30   EXPECT_EQ(MAX_PATH, str.capacity());
     31 
     32   std::wstring compare_str;
     33 
     34   EXPECT_EQ(str.length(), compare_str.length());
     35   EXPECT_EQ(0, compare_str.compare(str.get()));
     36 
     37   size_t max_chars = str.capacity() - 1;
     38 
     39   while ((str.length() + (arraysize(kTestString) - 1)) <= max_chars) {
     40     EXPECT_TRUE(str.append(kTestString));
     41     compare_str.append(kTestString);
     42     EXPECT_EQ(str.length(), compare_str.length());
     43     EXPECT_EQ(0, compare_str.compare(str.get()));
     44   }
     45 
     46   EXPECT_GT(static_cast<size_t>(MAX_PATH), str.length());
     47 
     48   // Now we've exhausted the space we allocated for the string,
     49   // so append should fail.
     50   EXPECT_FALSE(str.append(kTestString));
     51 
     52   // ...and remain unchanged.
     53   EXPECT_EQ(0, compare_str.compare(str.get()));
     54   EXPECT_EQ(str.length(), compare_str.length());
     55 
     56   // Last test for fun.
     57   str.clear();
     58   compare_str.clear();
     59   EXPECT_EQ(0, compare_str.compare(str.get()));
     60   EXPECT_EQ(str.length(), compare_str.length());
     61 }
     62 
     63 // Tests the case insensitive find support of the StackString class.
     64 TEST_F(MiniInstallerStringTest, StackStringFind) {
     65   static const wchar_t kTestStringSource[] = L"1234ABcD567890";
     66   static const wchar_t kTestStringFind[] = L"abcd";
     67   static const wchar_t kTestStringNotFound[] = L"80";
     68 
     69   StackString<MAX_PATH> str;
     70   EXPECT_TRUE(str.assign(kTestStringSource));
     71   EXPECT_EQ(str.get(), str.findi(kTestStringSource));
     72   EXPECT_EQ(static_cast<const wchar_t*>(NULL), str.findi(kTestStringNotFound));
     73   const wchar_t* found = str.findi(kTestStringFind);
     74   EXPECT_NE(static_cast<const wchar_t*>(NULL), found);
     75   std::wstring check(found, arraysize(kTestStringFind) - 1);
     76   EXPECT_EQ(0, lstrcmpi(check.c_str(), kTestStringFind));
     77 }
     78