1/* Copyright (c) 2010, Google Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * ---
27 * Author: Timur Iskhodzhanov
28 *
29 * This file contains a set of unit tests for memory error detection tools.
30 */
31
32#include "test_utils.h"
33#include "old_test_suite.h"
34
35#include <gtest/gtest.h>
36
37#ifdef WIN32
38#include <Wbemidl.h>
39#pragma comment(lib, "Wbemuuid.lib")
40#pragma comment(lib, "Ole32.lib")
41#endif
42
43void Noop() {}
44
45namespace NoopTest {
46  REGISTER_TEST(Noop, 0);
47  // Dummy to initialize 'TheMapOfTests'
48}
49
50TEST(Wrappers, StrchrTest) {
51  // There were bugs in TSan and Dr. Memory with strchr wrappers.
52  // Fix for TSan bug: http://code.google.com/p/data-race-test/source/diff?spec=svn1641&old=1527&r=1645&format=side&path=/trunk/tsan/ts_replace.h
53  // Dr. Memory bug:   http://code.google.com/p/dynamorio/issues/detail?id=275
54  char foo[8] = {10, 20, 127, (char)128, (char)250, -50, 0};
55  EXPECT_TRUE(strchr(foo, 10) != 0);
56  EXPECT_TRUE(strchr(foo, 127) != 0);
57  EXPECT_TRUE(strchr(foo, 128) != 0);
58  EXPECT_TRUE(strchr(foo, 250) != 0);
59  EXPECT_TRUE(strchr(foo, -50) != 0);
60  EXPECT_TRUE(strchr(foo, -60) == 0);
61  EXPECT_TRUE(strchr(foo, 0) != 0);
62  EXPECT_TRUE(strchr(foo, 0) == foo + strlen(foo));
63
64  EXPECT_TRUE(strrchr(foo, 10) != 0);
65  EXPECT_TRUE(strrchr(foo, 0) != 0);
66  EXPECT_TRUE(strrchr(foo, 0) == foo + strlen(foo));
67  EXPECT_TRUE(strrchr(foo, 250) != 0);
68  EXPECT_TRUE(strrchr(foo, -60) == 0);
69
70#ifdef WIN32
71  EXPECT_TRUE(lstrlenA(NULL) == 0);
72  EXPECT_TRUE(lstrlenW(NULL) == 0);
73#endif
74  //EXPECT_EQ(
75}
76
77TEST(Threads, EmptyThreadTest) {
78  // DrMemory bug http://code.google.com/p/dynamorio/issues/detail?id=286
79  MyThread mt(Noop);
80  mt.Start();
81  mt.Join();
82}
83
84#ifdef WIN32
85TEST(SyscallTests, OutputDebugStringTest) {
86  // DrMemory bug http://code.google.com/p/dynamorio/issues/detail?id=281
87  OutputDebugString("Hello!\n");
88}
89
90TEST(ComTests, IWbemLocator_ConnectServerTest) {
91  // DrMemory crashes on this test,
92  // see http://code.google.com/p/drmemory/issues/detail?id=21
93  HRESULT hr;
94  ::CoInitialize(NULL);
95  IWbemLocator *wmi_locator = NULL;
96  hr = ::CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
97                          __uuidof(IWbemLocator),
98                          reinterpret_cast<void**>(&wmi_locator));
99  ASSERT_FALSE(FAILED(hr));
100
101  printf("before ConnectServer...\n");
102  IWbemServices *wmi_services_r = NULL;
103  hr = wmi_locator->ConnectServer(L"ROOT\\CIMV2", NULL, NULL, 0,
104                                  NULL, 0, 0, &wmi_services_r);
105  printf("after  ConnectServer...\n");
106  EXPECT_FALSE(FAILED(hr));
107
108  wmi_locator->Release();
109  wmi_services_r->Release();
110  ::CoUninitialize();
111}
112
113namespace HeapTests {
114class MyMutex {
115 public:
116  MyMutex() {
117    ::InitializeCriticalSectionAndSpinCount(&lock_, 2000);
118  }
119  ~MyMutex() {
120    ::DeleteCriticalSection(&lock_);
121  }
122 private:
123  CRITICAL_SECTION lock_;
124};
125
126TEST(HeapTest, MutexAllocatedOnHeapTest) {
127  MyMutex *m = new MyMutex();
128  delete m;
129}
130
131class MyClass {
132 public:
133  explicit MyClass(int size) : ptr_(NULL) {
134    ptr_ = realloc(ptr_, size);
135  }
136  ~MyClass() {
137    free(ptr_);
138  }
139 private:
140  void *ptr_;
141};
142
143TEST(HeapTest, ReallocInHeapObjectTest) {
144  const MyClass m(50031);
145  MyClass *obj = new MyClass(50031);
146  delete obj;
147}
148
149}  // namespace HeapTests
150
151#endif
152