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 "test_helpers.h"
20 #include "zcpointer.h"
22 class TestFailure
: public std
::logic_error
{
24 using std
::logic_error
::logic_error
;
27 #define STRING(x) QUOTE(x)
29 #define AT_FILE_LINE " @ " __FILE__ ":" STRING(__LINE__)
31 #define EXPECT(expr) do { if (!(expr)) { throw TestFailure(#expr AT_FILE_LINE); } } while(0)
33 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
35 #define EXPECT_UAF(expr) do { \
38 throw TestFailure("Expected use-after-free: " #expr AT_FILE_LINE); \
39 } catch (zc::UseAfterFreeError) {} \
44 #define EXPECT_UAF(expr) do { \
45 std::cout << ">>> ZCPOINTER_TRACK_REFS not enabled, cannot catch UAF" << std::endl; \
48 } catch (std::logic_error& e) { \
49 std::cout << ">>> Caught error: " << typeid(e).name() << ": " << e.what() << std::endl; \
56 zc
::owned
<C
> c(new C());
57 zc
::ref
<C
> owned
= c
.get();
58 zc
::ref
<C
> owned2
= owned
;
60 EXPECT_UAF(owned2
->DoThing());
65 zc
::owned
<T
> t(new T());
68 zc
::ref
<T
> ref
= t
.get();
76 zc
::owned
<C
> c(new C());
77 zc
::ref
<C
> owned
= c
.get();
79 zc
::owned
<C
> c2(std
::move(c
));
83 EXPECT_UAF(owned
->DoThing());
86 void PtrHelper(zc
::ref
<C
>* out
) {
87 zc
::owned
<C
> c(new C());
94 EXPECT_UAF(ref
->DoThing());
98 zc
::owned
<C
> a(new C());
99 zc
::owned
<C
> b(new C());
105 zc
::ref
<C
> ra
= a
.get();
106 zc
::ref
<C
> rb
= b
.get();
109 EXPECT(ra
== a
.get());
111 EXPECT(rb
== b
.get());
115 zc
::ref
<C
> r
= a
.get();
117 EXPECT(r
== a
.get());
121 zc
::ref
<C
> rc
= nullptr;
123 EXPECT(rc
== c
.get());
124 EXPECT(c
== nullptr);
125 EXPECT(rc
== nullptr);
134 zc
::ref
<C
> rl
= l
.get();
135 zc
::ref
<C
> rr
= r
.get();
140 EXPECT(l
== nullptr);
141 EXPECT(r
== nullptr);
142 EXPECT(rl
== nullptr);
143 EXPECT(rr
== nullptr);
148 std
::vector
<zc
::ref
<C
>> vec
{
154 for (const auto& r
: vec
) {
155 EXPECT(r
== c
.get());
160 std
::vector
<zc
::owned
<C
>> vec
;
161 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
162 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
163 vec
.push_back(std
::move(zc
::owned
<C
>(new C())));
166 EXPECT_UAF(ref
->DoThing());
177 EXPECT_UAF(rc
->DoThing());
182 zc
::ref
<std
::vector
<C
>> vec_ref
;
188 vec_ref
->push_back(C());
189 vec_ref
->push_back(C());
191 vec_ref
->at(1).DoThing();
193 EXPECT_UAF(ref
->DoThing());
194 EXPECT_UAF(vec_ref
->at(1).DoThing());
197 zc
::member
<X
> x("foo bar");
200 EXPECT_UAF(ref
->DoThing());
203 #define TEST_FUNC(fn) { #fn , Test##fn }
221 for (const auto& test
: kTests
) {
222 std
::cout
<< "=== BEGIN " << test
.name
<< " ===" << std
::endl
;
225 std
::cout
<< "+++ PASS " << test
.name
<< " +++" << std
::endl
;
226 } catch (const TestFailure
& e
) {
228 std
::cout
<< "!!! FAIL " << test
.name
229 << ": Assertion failure: " << e
.what() << " ===" << std
::endl
;
233 return passed ?
0 : 1;