Loading dns_responder/dns_responder.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -712,6 +712,7 @@ void DNSResponder::requestHandler() { continue; } ALOGI("read %zd bytes", len); std::lock_guard lock(cv_mutex_); char response[4096]; size_t response_len = sizeof(response); if (handleDNSRequest(buffer, len, response, &response_len) && Loading @@ -734,6 +735,7 @@ void DNSResponder::requestHandler() { } else { ALOGI("not responding"); } cv.notify_one(); } } Loading dns_responder/dns_responder.h +5 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <arpa/nameser.h> #include <atomic> #include <condition_variable> #include <mutex> #include <string> #include <thread> Loading Loading @@ -68,6 +69,8 @@ class DNSResponder { std::vector<std::pair<std::string, ns_type>> queries() const; std::string dumpQueries() const; void clearQueries(); std::condition_variable& getCv() { return cv; } std::mutex& getCvMutex() { return cv_mutex_; } private: // Key used for accessing mappings. Loading Loading @@ -146,6 +149,8 @@ class DNSResponder { // Thread for handling incoming threads. std::thread handler_thread_ GUARDED_BY(update_mutex_); std::mutex update_mutex_; std::condition_variable cv; std::mutex cv_mutex_; }; } // namespace test Loading resolver_test.cpp +19 −16 Original line number Diff line number Diff line Loading @@ -1725,14 +1725,20 @@ TEST_F(ResolverTest, Async_EmptyAnswer) { ASSERT_TRUE(SetResolversForNetwork(servers, mDefaultSearchDomains, mDefaultParams_Binder)); dns.clearQueries(); // TODO: Disable retry to make this test explicit. auto& cv = dns.getCv(); auto& cvMutex = dns.getCvMutex(); int fd1; // Wait on the condition variable to ensure that the DNS server has handled our first query. { std::unique_lock lk(cvMutex); // A 1 AAAA 28 int fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 28); fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 28); EXPECT_TRUE(fd1 != -1); EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lk, std::chrono::seconds(1))); } // make sure setResponseProbability effective dns.stopServer(); dns.setResponseProbability(0.0); ASSERT_TRUE(dns.startServer()); int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd2 != -1); Loading @@ -1740,22 +1746,19 @@ TEST_F(ResolverTest, Async_EmptyAnswer) { int fd3 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd3 != -1); u_char buf[MAXPACKET] = {}; uint8_t buf[MAXPACKET] = {}; int rcode; // expect no response, ETIMEDOUT = 110 int res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); EXPECT_EQ(res, -110); // expect no response int res = getAsyncResponse(fd3, &rcode, buf, MAXPACKET); EXPECT_EQ(-ETIMEDOUT, res); // expect no response, ETIMEDOUT = 110 // expect no response memset(buf, 0, MAXPACKET); res = getAsyncResponse(fd3, &rcode, buf, MAXPACKET); EXPECT_EQ(res, -110); res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); EXPECT_EQ(-ETIMEDOUT, res); // make sure setResponseProbability effective dns.stopServer(); dns.setResponseProbability(1.0); ASSERT_TRUE(dns.startServer()); int fd4 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd4 != -1); Loading Loading
dns_responder/dns_responder.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -712,6 +712,7 @@ void DNSResponder::requestHandler() { continue; } ALOGI("read %zd bytes", len); std::lock_guard lock(cv_mutex_); char response[4096]; size_t response_len = sizeof(response); if (handleDNSRequest(buffer, len, response, &response_len) && Loading @@ -734,6 +735,7 @@ void DNSResponder::requestHandler() { } else { ALOGI("not responding"); } cv.notify_one(); } } Loading
dns_responder/dns_responder.h +5 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <arpa/nameser.h> #include <atomic> #include <condition_variable> #include <mutex> #include <string> #include <thread> Loading Loading @@ -68,6 +69,8 @@ class DNSResponder { std::vector<std::pair<std::string, ns_type>> queries() const; std::string dumpQueries() const; void clearQueries(); std::condition_variable& getCv() { return cv; } std::mutex& getCvMutex() { return cv_mutex_; } private: // Key used for accessing mappings. Loading Loading @@ -146,6 +149,8 @@ class DNSResponder { // Thread for handling incoming threads. std::thread handler_thread_ GUARDED_BY(update_mutex_); std::mutex update_mutex_; std::condition_variable cv; std::mutex cv_mutex_; }; } // namespace test Loading
resolver_test.cpp +19 −16 Original line number Diff line number Diff line Loading @@ -1725,14 +1725,20 @@ TEST_F(ResolverTest, Async_EmptyAnswer) { ASSERT_TRUE(SetResolversForNetwork(servers, mDefaultSearchDomains, mDefaultParams_Binder)); dns.clearQueries(); // TODO: Disable retry to make this test explicit. auto& cv = dns.getCv(); auto& cvMutex = dns.getCvMutex(); int fd1; // Wait on the condition variable to ensure that the DNS server has handled our first query. { std::unique_lock lk(cvMutex); // A 1 AAAA 28 int fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 28); fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 28); EXPECT_TRUE(fd1 != -1); EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lk, std::chrono::seconds(1))); } // make sure setResponseProbability effective dns.stopServer(); dns.setResponseProbability(0.0); ASSERT_TRUE(dns.startServer()); int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd2 != -1); Loading @@ -1740,22 +1746,19 @@ TEST_F(ResolverTest, Async_EmptyAnswer) { int fd3 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd3 != -1); u_char buf[MAXPACKET] = {}; uint8_t buf[MAXPACKET] = {}; int rcode; // expect no response, ETIMEDOUT = 110 int res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); EXPECT_EQ(res, -110); // expect no response int res = getAsyncResponse(fd3, &rcode, buf, MAXPACKET); EXPECT_EQ(-ETIMEDOUT, res); // expect no response, ETIMEDOUT = 110 // expect no response memset(buf, 0, MAXPACKET); res = getAsyncResponse(fd3, &rcode, buf, MAXPACKET); EXPECT_EQ(res, -110); res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); EXPECT_EQ(-ETIMEDOUT, res); // make sure setResponseProbability effective dns.stopServer(); dns.setResponseProbability(1.0); ASSERT_TRUE(dns.startServer()); int fd4 = resNetworkQuery(TEST_NETID, "howdy.example.com", 1, 1); EXPECT_TRUE(fd4 != -1); Loading