Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / qa / systools / test_comtools.cxx
blob073bb79ec2d2fc415d167805546d89ed83e074f0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppunit/extensions/HelperMacros.h>
21 #include <cppunit/plugin/TestPlugIn.h>
22 #include <systools/win32/comtools.hxx>
23 #include <shobjidl.h>
25 namespace {
27 class COMObject : public IUnknown
29 public:
30 COMObject() : ref_count_(0)
34 virtual ~COMObject()
38 ULONG __stdcall AddRef() override
40 ref_count_++;
41 return ref_count_;
44 ULONG __stdcall Release() override
46 ULONG cnt = --ref_count_;
47 if (cnt == 0)
48 delete this;
49 return cnt;
52 HRESULT __stdcall QueryInterface(REFIID riid, void** ppv) override
54 if (riid == IID_IUnknown)
56 AddRef();
57 *ppv = this;
58 return S_OK;
60 return E_NOINTERFACE;
63 ULONG GetRefCount() const
65 return ref_count_;
68 private:
69 ULONG ref_count_;
74 static sal::systools::COMReference<IUnknown> comObjectSource()
76 return sal::systools::COMReference<IUnknown>(new COMObject);
79 static bool comObjectSink(sal::systools::COMReference<IUnknown> r, ULONG expectedRefCountOnReturn)
81 r = sal::systools::COMReference<IUnknown>();
82 COMObject* p = reinterpret_cast<COMObject*>(r.get());
83 if (p)
84 return (p->GetRefCount() == expectedRefCountOnReturn);
85 else
86 return (0 == expectedRefCountOnReturn);
89 static void comObjectSource2(LPVOID* ppv)
91 COMObject* p = new COMObject;
92 p->AddRef();
93 *ppv = p;
96 namespace test_comtools
99 class test_COMReference : public CppUnit::TestFixture
102 public:
103 /// test of COMReference<IUnknown> r;
104 void default_ctor()
106 sal::systools::COMReference<IUnknown> r;
107 CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown *>(nullptr), r.get());
110 void test_ctor_manual_AddRef()
112 COMObject* p = new COMObject;
113 p->AddRef();
114 sal::systools::COMReference<IUnknown> r(p, false);
115 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
118 void test_copy_ctor()
120 sal::systools::COMReference<IUnknown> r(comObjectSource());
121 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
124 void test_copy_assignment()
126 sal::systools::COMReference<IUnknown> r;
127 CPPUNIT_ASSERT_EQUAL_MESSAGE("COMReference should be empty", static_cast<IUnknown *>(nullptr), r.get());
129 r = comObjectSource();
130 CPPUNIT_ASSERT_MESSAGE("COMReference should be empty", r.get() != nullptr);
131 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
134 void test_ref_to_ref_assignment()
136 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
137 sal::systools::COMReference<IUnknown> r2 = r1;
138 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 2 is expected", ULONG(2), reinterpret_cast<COMObject*>(r2.get())->GetRefCount());
141 void test_pointer_to_ref_assignment()
143 sal::systools::COMReference<IUnknown> r;
144 r = new COMObject;
145 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
148 void test_pointer_to_ref_assignment2()
150 sal::systools::COMReference<IUnknown> r = comObjectSource();
151 r = new COMObject;
152 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
155 void test_source_sink()
157 CPPUNIT_ASSERT_MESSAGE("Wrong reference count, 0 is expected", comObjectSink(comObjectSource(), 0));
160 void test_address_operator()
162 sal::systools::COMReference<IUnknown> r;
163 comObjectSource2(reinterpret_cast<LPVOID*>(&r));
164 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
167 void test_address_operator2()
169 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
170 sal::systools::COMReference<IUnknown> r2 = r1;
171 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 2 is expected", ULONG(2), reinterpret_cast<COMObject*>(r2.get())->GetRefCount());
172 comObjectSource2(reinterpret_cast<LPVOID*>(&r1));
173 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r1.get())->GetRefCount());
174 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r2.get())->GetRefCount());
177 void test_clear()
179 sal::systools::COMReference<IUnknown> r = comObjectSource();
180 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count 1 is expected", ULONG(1), reinterpret_cast<COMObject*>(r.get())->GetRefCount());
181 r.clear();
182 CPPUNIT_ASSERT_MESSAGE("Expect reference to be empty", !r.is());
185 void test_query_interface()
187 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
188 sal::systools::COMReference<IUnknown> r2;
189 CPPUNIT_ASSERT_NO_THROW_MESSAGE(
190 "Exception should not have been thrown",
191 r2 = r1.QueryInterface<IUnknown>(sal::systools::COM_QUERY_THROW));
192 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong reference count, 2 is expected", ULONG(2),
193 reinterpret_cast<COMObject*>(r2.get())->GetRefCount());
196 void test_query_interface_throw()
198 sal::systools::COMReference<IUnknown> r1 = comObjectSource();
199 CPPUNIT_ASSERT_THROW_MESSAGE("Exception should have been thrown",
200 auto r2 = r1.QueryInterface<IPersistFile>(sal::systools::COM_QUERY_THROW),
201 sal::systools::ComError);
204 void test_CoCreateInstance()
206 if (FAILED(CoInitialize(nullptr)))
207 return;
209 // Use scope to destroy the reference before calling CoUninitialize
210 sal::systools::COMReference<IFileOpenDialog> r;
211 CPPUNIT_ASSERT_NO_THROW(r.CoCreateInstance(__uuidof(FileOpenDialog)));
212 // Immediately after CoCreateInstance, refcount must be 1; increasing once gives 2
213 CPPUNIT_ASSERT_EQUAL(ULONG(2), r->AddRef());
214 r->Release();
216 CoUninitialize();
219 // Change the following lines only, if you add, remove or rename
220 // member functions of the current class,
221 // because these macros are need by auto register mechanism.
223 CPPUNIT_TEST_SUITE(test_COMReference);
224 CPPUNIT_TEST(default_ctor);
225 CPPUNIT_TEST(test_ctor_manual_AddRef);
226 CPPUNIT_TEST(test_copy_ctor);
227 CPPUNIT_TEST(test_copy_assignment);
228 CPPUNIT_TEST(test_ref_to_ref_assignment);
229 CPPUNIT_TEST(test_pointer_to_ref_assignment);
230 CPPUNIT_TEST(test_pointer_to_ref_assignment2);
231 CPPUNIT_TEST(test_source_sink);
232 CPPUNIT_TEST(test_address_operator);
233 CPPUNIT_TEST(test_address_operator2);
234 CPPUNIT_TEST(test_clear);
235 CPPUNIT_TEST(test_query_interface);
236 CPPUNIT_TEST(test_query_interface_throw);
237 CPPUNIT_TEST(test_CoCreateInstance);
238 CPPUNIT_TEST_SUITE_END();
241 CPPUNIT_TEST_SUITE_REGISTRATION(test_comtools::test_COMReference);
243 } // namespace rtl_OUString
245 CPPUNIT_PLUGIN_IMPLEMENT();
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */