summaryrefslogtreecommitdiffstats
path: root/chrome/installer/mini_installer/mini_string_test.cc
blob: 67b11f98d8e963b7fe50f9177b3607e0d8d52c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>

#include <string>

#include "base/basictypes.h"
#include "chrome/installer/mini_installer/mini_string.h"
#include "testing/gtest/include/gtest/gtest.h"

using mini_installer::StackString;

namespace {
class MiniInstallerStringTest : public testing::Test {
 protected:
  virtual void SetUp() {
  }
  virtual void TearDown() {
  }
};
}

// Tests the strcat/strcpy/length support of the StackString class.
TEST_F(MiniInstallerStringTest, StackStringOverflow) {
  static const wchar_t kTestString[] = L"1234567890";

  StackString<MAX_PATH> str;
  EXPECT_EQ(MAX_PATH, str.capacity());

  std::wstring compare_str;

  EXPECT_EQ(str.length(), compare_str.length());
  EXPECT_EQ(0, compare_str.compare(str.get()));

  size_t max_chars = str.capacity() - 1;

  while ((str.length() + (arraysize(kTestString) - 1)) <= max_chars) {
    EXPECT_TRUE(str.append(kTestString));
    compare_str.append(kTestString);
    EXPECT_EQ(str.length(), compare_str.length());
    EXPECT_EQ(0, compare_str.compare(str.get()));
  }

  EXPECT_GT(static_cast<size_t>(MAX_PATH), str.length());

  // Now we've exhausted the space we allocated for the string,
  // so append should fail.
  EXPECT_FALSE(str.append(kTestString));

  // ...and remain unchanged.
  EXPECT_EQ(0, compare_str.compare(str.get()));
  EXPECT_EQ(str.length(), compare_str.length());

  // Last test for fun.
  str.clear();
  compare_str.clear();
  EXPECT_EQ(0, compare_str.compare(str.get()));
  EXPECT_EQ(str.length(), compare_str.length());
}

// Tests the case insensitive find support of the StackString class.
TEST_F(MiniInstallerStringTest, StackStringFind) {
  static const wchar_t kTestStringSource[] = L"1234ABcD567890";
  static const wchar_t kTestStringFind[] = L"abcd";
  static const wchar_t kTestStringNotFound[] = L"80";

  StackString<MAX_PATH> str;
  EXPECT_TRUE(str.assign(kTestStringSource));
  EXPECT_EQ(str.get(), str.findi(kTestStringSource));
  EXPECT_EQ(static_cast<const wchar_t*>(NULL), str.findi(kTestStringNotFound));
  const wchar_t* found = str.findi(kTestStringFind);
  EXPECT_NE(static_cast<const wchar_t*>(NULL), found);
  std::wstring check(found, arraysize(kTestStringFind) - 1);
  EXPECT_EQ(0, lstrcmpi(check.c_str(), kTestStringFind));
}