From e082984fee89cb582a84d448ab99331975203a69 Mon Sep 17 00:00:00 2001 From: Harald Alvestrand Date: Mon, 12 Apr 2021 04:47:04 +0000 Subject: [PATCH] Add death test for WrappingAsyncResolver Bug: webrtc:12598 Change-Id: Iff70cc2c53da5098514853eb6034874ee2e10b2c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/214961 Reviewed-by: Markus Handell Commit-Queue: Harald Alvestrand Cr-Commit-Position: refs/heads/master@{#33704} --- .../basic_async_resolver_factory_unittest.cc | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/p2p/base/basic_async_resolver_factory_unittest.cc b/p2p/base/basic_async_resolver_factory_unittest.cc index ec13601643..6706f50d61 100644 --- a/p2p/base/basic_async_resolver_factory_unittest.cc +++ b/p2p/base/basic_async_resolver_factory_unittest.cc @@ -79,6 +79,11 @@ TEST(WrappingAsyncDnsResolverFactoryTest, WrapOtherResolver) { resolver.reset(); } +#if GTEST_HAS_DEATH_TEST && defined(WEBRTC_LINUX) +// Tests that the prohibition against deleting the resolver from the callback +// is enforced. This is required by the use of sigslot in the wrapped resolver. +// Checking the error message fails on a number of platforms, so run this +// test only on the platforms where it works. void CallResolver(WrappingAsyncDnsResolverFactory& factory) { rtc::SocketAddress address("", 0); std::unique_ptr resolver(factory.Create()); @@ -86,4 +91,27 @@ void CallResolver(WrappingAsyncDnsResolverFactory& factory) { WAIT(!resolver.get(), 10000 /*ms*/); } +TEST(WrappingAsyncDnsResolverFactoryDeathTest, DestroyResolverInCallback) { + // This test requires the main thread to be wrapped. So we defeat the + // workaround in test/test_main_lib.cc by explicitly wrapping the main + // thread here. + auto thread = rtc::Thread::CreateWithSocketServer(); + thread->WrapCurrent(); + // TODO(bugs.webrtc.org/12652): Rewrite as death test in loop style when it + // works. + WrappingAsyncDnsResolverFactory factory( + std::make_unique()); + + // Since EXPECT_DEATH is thread sensitive, and the resolver creates a thread, + // we wrap the whole creation section in EXPECT_DEATH. + RTC_EXPECT_DEATH(CallResolver(factory), + "Check failed: !within_resolve_result_"); + // If we get here, we have to unwrap the thread. + thread->Quit(); + thread->Run(); + thread->UnwrapCurrent(); + thread = nullptr; +} +#endif + } // namespace webrtc