aboutsummaryrefslogtreecommitdiff
path: root/tests/unsolicited_listener
diff options
context:
space:
mode:
authorpaulhu <paulhu@google.com>2020-12-15 11:03:52 +0800
committerpaulhu <paulhu@google.com>2021-01-18 16:03:28 +0800
commitdcadba54be95a1f679465d5c6ce549825cb820c8 (patch)
tree8f0554e362ab1a7fa988b917a840c05abf70106d /tests/unsolicited_listener
parent34c67cbd7801abcae091d5e85ebabe9d3077bee5 (diff)
downloadDnsResolver-dcadba54be95a1f679465d5c6ce549825cb820c8.tar.gz
[DRUEL04] Implement unsolicited NAT64 prefix event
Implement unsolicited NAT64 prefix event that will report to the listener when change NAT64 prefix. Bug: 173485754 Test: atest resolv_integration_test resolv_unit_test\ resolv_stress_test resolv_stats_test_utils_test Change-Id: If20353529969ffa956b56d19345519f286046e25
Diffstat (limited to 'tests/unsolicited_listener')
-rw-r--r--tests/unsolicited_listener/unsolicited_event_listener.cpp32
-rw-r--r--tests/unsolicited_listener/unsolicited_event_listener.h21
2 files changed, 51 insertions, 2 deletions
diff --git a/tests/unsolicited_listener/unsolicited_event_listener.cpp b/tests/unsolicited_listener/unsolicited_event_listener.cpp
index f5ebb0b4..8cf75054 100644
--- a/tests/unsolicited_listener/unsolicited_event_listener.cpp
+++ b/tests/unsolicited_listener/unsolicited_event_listener.cpp
@@ -23,11 +23,14 @@
namespace android::net::resolv::aidl {
+using ::aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener;
+using ::aidl::android::net::resolv::aidl::Nat64PrefixEventParcel;
using ::aidl::android::net::resolv::aidl::PrivateDnsValidationEventParcel;
using android::base::ScopedLockAssertion;
using std::chrono::milliseconds;
constexpr milliseconds kEventTimeoutMs{5000};
+constexpr milliseconds kRetryIntervalMs{20};
::ndk::ScopedAStatus UnsolicitedEventListener::onDnsHealthEvent(
const ::aidl::android::net::resolv::aidl::DnsHealthEventParcel&) {
@@ -36,8 +39,15 @@ constexpr milliseconds kEventTimeoutMs{5000};
}
::ndk::ScopedAStatus UnsolicitedEventListener::onNat64PrefixEvent(
- const ::aidl::android::net::resolv::aidl::Nat64PrefixEventParcel&) {
- // default no-op
+ const Nat64PrefixEventParcel& event) {
+ std::lock_guard lock(mMutex);
+ mUnexpectedNat64PrefixUpdates++;
+ if (event.netId == mNetId) {
+ mNat64PrefixAddress = (event.prefixOperation ==
+ IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED)
+ ? event.prefixAddress
+ : "";
+ }
return ::ndk::ScopedAStatus::ok();
}
@@ -77,4 +87,22 @@ bool UnsolicitedEventListener::findAndRemoveValidationRecord(const ServerKey& ke
return false;
}
+bool UnsolicitedEventListener::waitForNat64Prefix(int operation, const milliseconds& timeout) {
+ android::base::Timer t;
+ while (t.duration() < timeout) {
+ {
+ std::lock_guard lock(mMutex);
+ if ((operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED &&
+ !mNat64PrefixAddress.empty()) ||
+ (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_REMOVED &&
+ mNat64PrefixAddress.empty())) {
+ mUnexpectedNat64PrefixUpdates--;
+ return true;
+ }
+ }
+ std::this_thread::sleep_for(kRetryIntervalMs);
+ }
+ return false;
+}
+
} // namespace android::net::resolv::aidl
diff --git a/tests/unsolicited_listener/unsolicited_event_listener.h b/tests/unsolicited_listener/unsolicited_event_listener.h
index 5615f241..e8fce1de 100644
--- a/tests/unsolicited_listener/unsolicited_event_listener.h
+++ b/tests/unsolicited_listener/unsolicited_event_listener.h
@@ -44,15 +44,26 @@ class UnsolicitedEventListener
// Wait for the expected private DNS validation result until timeout.
bool waitForPrivateDnsValidation(const std::string& serverAddr, int validation);
+ // Wait for expected NAT64 prefix operation until timeout.
+ bool waitForNat64Prefix(int operation, const std::chrono::milliseconds& timeout)
+ EXCLUDES(mMutex);
+
// Return true if a validation result for |serverAddr| is found; otherwise, return false.
bool findValidationRecord(const std::string& serverAddr) const EXCLUDES(mMutex) {
std::lock_guard lock(mMutex);
return mValidationRecords.find({mNetId, serverAddr}) != mValidationRecords.end();
}
+ // Returns the number of updates to the NAT64 prefix that have not yet been waited for.
+ int getUnexpectedNat64PrefixUpdates() const EXCLUDES(mMutex) {
+ std::lock_guard lock(mMutex);
+ return mUnexpectedNat64PrefixUpdates;
+ }
+
void reset() EXCLUDES(mMutex) {
std::lock_guard lock(mMutex);
mValidationRecords.clear();
+ mUnexpectedNat64PrefixUpdates = 0;
}
private:
@@ -68,6 +79,16 @@ class UnsolicitedEventListener
// Used to store the data from onPrivateDnsValidationEvent.
std::map<ServerKey, int> mValidationRecords GUARDED_BY(mMutex);
+ // The NAT64 prefix address of the network |mNetId|. It is updated by onNat64PrefixEvent().
+ std::string mNat64PrefixAddress GUARDED_BY(mMutex);
+
+ // The number of updates to the NAT64 prefix of network |mNetId| that have not yet been waited
+ // for. Increases by 1 every time onNat64PrefixEvent is called, and decreases by 1 every time
+ // waitForNat64Prefix returns true.
+ // This allows tests to check that no unexpected events have been received without having to
+ // resort to timeouts that make the tests slower and flakier.
+ int mUnexpectedNat64PrefixUpdates GUARDED_BY(mMutex);
+
mutable std::mutex mMutex;
std::condition_variable mCv;
};