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.
17 #include "zcpointer.h"
26 #define EXPECT(expr) do { if (!(expr)) { throw std::logic_error(#expr); } } while(0)
28 #define EXPECT_UAF(expr) do { \
31 throw std::logic_error("Expected use-after-free: " #expr); \
32 } catch (zc::UseAfterFreeError) {} \
36 zc
::owned
<C
> c(new C());
37 zc
::ref
<C
> owned
= c
.get();
38 zc
::ref
<C
> owned2
= owned
;
40 EXPECT_UAF(owned2
->DoThing());
45 zc
::owned
<T
> t(new T());
46 //T* unwrap = t.get();
48 zc
::ref
<T
> ref
= t
.get();
53 zc
::owned
<C
> c(new C());
54 zc
::ref
<C
> owned
= c
.get();
56 zc
::owned
<C
> c2(std
::move(c
));
60 EXPECT_UAF(owned
->DoThing());
63 void PtrHelper(zc
::ref
<C
>* out
) {
64 zc
::owned
<C
> c(new C());
71 EXPECT_UAF(ref
->DoThing());
75 zc
::owned
<C
> a(new C());
76 zc
::owned
<C
> b(new C());
82 zc
::ref
<C
> ra
= a
.get();
83 zc
::ref
<C
> rb
= b
.get();
86 EXPECT(ra
== a
.get());
88 EXPECT(rb
== b
.get());
92 zc
::ref
<C
> r
= a
.get();
98 zc
::ref
<C
> rc
= nullptr;
100 EXPECT(rc
== c
.get());
101 EXPECT(c
== nullptr);
102 EXPECT(rc
== nullptr);
111 zc
::ref
<C
> rl
= l
.get();
112 zc
::ref
<C
> rr
= r
.get();
117 EXPECT(l
== nullptr);
118 EXPECT(r
== nullptr);
119 EXPECT(rl
== nullptr);
120 EXPECT(rr
== nullptr);
123 #define TEST_FUNC(fn) { #fn , Test##fn }
138 for (const auto& test
: kTests
) {
139 std
::cout
<< "=== BEGIN " << test
.name
<< " ===" << std
::endl
;
142 std
::cout
<< "=== PASS " << test
.name
<< " ===" << std
::endl
;
143 } catch (const std
::logic_error
& e
) {
145 std
::cout
<< "=== FAIL " << test
.name
146 << ": Assertion failure: " << e
.what() << " ===" << std
::endl
;
150 return passed ?
0 : 1;