e8cbdf295c6d009e2f821eef65b29120c821ef2c
1 // Copyright 2016 Google Inc. All rights reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
19 #include "zcpointer.h"
28 class TestFailure
: public std
::logic_error
{
30 using std
::logic_error
::logic_error
;
33 #define STRING(x) QUOTE(x)
35 #define AT_FILE_LINE " @ " __FILE__ ":" STRING(__LINE__)
37 #define EXPECT(expr) do { if (!(expr)) { throw TestFailure(#expr AT_FILE_LINE); } } while(0)
39 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
41 #define EXPECT_UAF(expr) do { \
44 throw TestFailure("Expected use-after-free: " #expr AT_FILE_LINE); \
45 } catch (zc::UseAfterFreeError) {} \
50 #define EXPECT_UAF(expr) do { \
51 std::cout << ">>> ZCPOINTER_TRACK_REFS not enabled, cannot catch UAF" << std::endl; \
54 } catch (std::logic_error& e) { \
55 std::cout << ">>> Caught error: " << typeid(e).name() << ": " << e.what() << std::endl; \
62 zc
::owned
<C
> c(new C());
63 zc
::ref
<C
> owned
= c
.get();
64 zc
::ref
<C
> owned2
= owned
;
66 EXPECT_UAF(owned2
->DoThing());
71 zc
::owned
<T
> t(new T());
72 //T* unwrap = t.get();
74 zc
::ref
<T
> ref
= t
.get();
79 zc
::owned
<C
> c(new C());
80 zc
::ref
<C
> owned
= c
.get();
82 zc
::owned
<C
> c2(std
::move(c
));
86 EXPECT_UAF(owned
->DoThing());
89 void PtrHelper(zc
::ref
<C
>* out
) {
90 zc
::owned
<C
> c(new C());
97 EXPECT_UAF(ref
->DoThing());
100 void TestEquality() {
101 zc
::owned
<C
> a(new C());
102 zc
::owned
<C
> b(new C());
108 zc
::ref
<C
> ra
= a
.get();
109 zc
::ref
<C
> rb
= b
.get();
112 EXPECT(ra
== a
.get());
114 EXPECT(rb
== b
.get());
118 zc
::ref
<C
> r
= a
.get();
120 EXPECT(r
== a
.get());
124 zc
::ref
<C
> rc
= nullptr;
126 EXPECT(rc
== c
.get());
127 EXPECT(c
== nullptr);
128 EXPECT(rc
== nullptr);
137 zc
::ref
<C
> rl
= l
.get();
138 zc
::ref
<C
> rr
= r
.get();
143 EXPECT(l
== nullptr);
144 EXPECT(r
== nullptr);
145 EXPECT(rl
== nullptr);
146 EXPECT(rr
== nullptr);
151 std
::vector
<zc
::ref
<C
>> vec
{
157 for (const auto& r
: vec
) {
158 EXPECT(r
== c
.get());
163 std
::vector
<zc
::owned
<C
>> vec
;
164 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
165 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
166 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
169 EXPECT_UAF(ref
->DoThing());
172 #define TEST_FUNC(fn) { #fn , Test##fn }
188 for (const auto& test
: kTests
) {
189 std
::cout
<< "=== BEGIN " << test
.name
<< " ===" << std
::endl
;
192 std
::cout
<< "+++ PASS " << test
.name
<< " +++" << std
::endl
;
193 } catch (const TestFailure
& e
) {
195 std
::cout
<< "!!! FAIL " << test
.name
196 << ": Assertion failure: " << e
.what() << " ===" << std
::endl
;
200 return passed ?
0 : 1;