Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5be8bf29 authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

Test for unexpected prefix updates in SetAndClearNat64Prefix.

Add code to check that no onNat64PrefixEvent calls were sent by
the resolver other than the ones expected by the test.

This adds a simple counter of unexpected events to the test
DnsMetricsListener. It does not offer the ability to check which
events occur and in what order, but it does at least not require
adding timeouts to ensure that the unexpected events do not
occur.

Bug: 156914456
Test: test-only change
Original-Change: https://android-review.googlesource.com/1313745
Merged-In: Ic86f93161293b4ad5366fd9cab2e26d4a6f636c4
Change-Id: Ic86f93161293b4ad5366fd9cab2e26d4a6f636c4
parent 0d234b06
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ constexpr milliseconds kEventTimeoutMs{5000};
                                                            const std::string& prefixString,
                                                            int32_t /*prefixLength*/) {
    std::lock_guard lock(mMutex);
    mUnexpectedNat64PrefixUpdates++;
    if (netId == mNetId) mNat64Prefix = added ? prefixString : "";
    return ::ndk::ScopedAStatus::ok();
}
@@ -49,16 +50,17 @@ constexpr milliseconds kEventTimeoutMs{5000};
    return ::ndk::ScopedAStatus::ok();
}

bool DnsMetricsListener::waitForNat64Prefix(ExpectNat64PrefixStatus status,
                                            milliseconds timeout) const {
bool DnsMetricsListener::waitForNat64Prefix(ExpectNat64PrefixStatus status, milliseconds timeout) {
    android::base::Timer t;
    while (t.duration() < timeout) {
        {
            std::lock_guard lock(mMutex);
            if ((status == EXPECT_FOUND && !mNat64Prefix.empty()) ||
                (status == EXPECT_NOT_FOUND && mNat64Prefix.empty()))
                (status == EXPECT_NOT_FOUND && mNat64Prefix.empty())) {
                mUnexpectedNat64PrefixUpdates--;
                return true;
            }
        }
        std::this_thread::sleep_for(kRetryIntervalMs);
    }
    return false;
+21 −2
Original line number Diff line number Diff line
@@ -51,8 +51,14 @@ class DnsMetricsListener : public BaseMetricsListener {
                                                     bool validated) override;

    // Wait for expected NAT64 prefix status until timeout.
    bool waitForNat64Prefix(ExpectNat64PrefixStatus status,
                            std::chrono::milliseconds timeout) const;
    bool waitForNat64Prefix(ExpectNat64PrefixStatus status, std::chrono::milliseconds timeout)
            EXCLUDES(mMutex);

    // 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;
    }

    // Wait for the expected private DNS validation result until timeout.
    bool waitForPrivateDnsValidation(const std::string& serverAddr, const bool validated);
@@ -64,6 +70,12 @@ class DnsMetricsListener : public BaseMetricsListener {
        return mValidationRecords.find({mNetId, serverAddr}) != mValidationRecords.end();
    }

    void reset() EXCLUDES(mMutex) {
        std::lock_guard lock(mMutex);
        mUnexpectedNat64PrefixUpdates = 0;
        mValidationRecords.clear();
    }

  private:
    typedef std::pair<int32_t, std::string> ServerKey;

@@ -77,6 +89,13 @@ class DnsMetricsListener : public BaseMetricsListener {
    // The NAT64 prefix of the network |mNetId|. It is updated by the event onNat64PrefixEvent().
    std::string mNat64Prefix 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);

    // Used to store the data from onPrivateDnsValidationEvent.
    std::map<ServerKey, bool> mValidationRecords GUARDED_BY(mMutex);

+7 −1
Original line number Diff line number Diff line
@@ -185,7 +185,11 @@ class ResolverTest : public ::testing::Test {
    static void TearDownTestSuite() { AIBinder_DeathRecipient_delete(sResolvDeathRecipient); }

  protected:
    void SetUp() { mDnsClient.SetUp(); }
    void SetUp() {
        mDnsClient.SetUp();
        sDnsMetricsListener->reset();
    }

    void TearDown() {
        // Ensure the dump works at the end of each test.
        DumpResolverService();
@@ -3816,6 +3820,8 @@ TEST_F(ResolverTest, SetAndClearNat64Prefix) {

    EXPECT_TRUE(resolvService->stopPrefix64Discovery(TEST_NETID).isOk());
    EXPECT_TRUE(WaitForNat64Prefix(EXPECT_NOT_FOUND));

    EXPECT_EQ(0, sDnsMetricsListener->getUnexpectedNat64PrefixUpdates());
}

namespace {