From d1f1f77fe26ef28dfc4121b03faf03bbf48ab2a5 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 8 Oct 2016 20:44:50 -0400 Subject: [PATCH] Introduce a basic EXPECT() and EXPECT_UAF() to test.cc. --- test.cc | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/test.cc b/test.cc index 23c310a..e7171d3 100644 --- a/test.cc +++ b/test.cc @@ -18,21 +18,26 @@ class C { public: - ~C() { - std::cout << "~C" << std::endl; - } + ~C() {} - void DoThing() { - std::cout << "DoThing" << std::endl; - } + void DoThing() {} }; +#define EXPECT(expr) do { if (!(expr)) { throw std::logic_error(#expr); } } while(0) + +#define EXPECT_UAF(expr) do { \ + try { \ + (expr); \ + throw std::logic_error("Expected use-after-free: " #expr); \ + } catch (zc::UseAfterFreeError) {} \ + } while(0) + void TestReset() { zc::owned c(new C()); zc::ref owned = c.get(); zc::ref owned2 = owned; c.reset(); - owned2->DoThing(); + EXPECT_UAF(owned2->DoThing()); } template @@ -52,7 +57,7 @@ void TestMove() { owned->DoThing(); c2.reset(); - owned->DoThing(); + EXPECT_UAF(owned->DoThing()); } void PtrHelper(zc::ref* out) { @@ -63,7 +68,7 @@ void PtrHelper(zc::ref* out) { void TestPtr() { zc::ref ref; PtrHelper(&ref); - ref->DoThing(); + EXPECT_UAF(ref->DoThing()); } #define TEST_FUNC(fn) { #fn , Test##fn } @@ -78,14 +83,18 @@ int main() { TEST_FUNC(Ptr), }; + bool passed = true; for (const auto& test : kTests) { std::cout << "=== BEGIN " << test.name << " ===" << std::endl; try { test.test(); - std::cout << "=== FAIL " << test.name - << ": Did not receive UseAfterFreeException ===" << std::endl; - } catch (zc::UseAfterFreeError) { std::cout << "=== PASS " << test.name << " ===" << std::endl; + } catch (const std::logic_error& e) { + passed = false; + std::cout << "=== FAIL " << test.name + << ": Assertion failure: " << e.what() << " ===" << std::endl; } } + + return passed ? 0 : 1; } -- 2.43.5