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

Commit a1d74187 authored by Luke Huang's avatar Luke Huang
Browse files

Fix asynchronous DNS API flag NO_CACHE_LOOKUP behavior

Previously, this flag won't store response into cache.
Fix it to store the response into cache.
If the entry is already in the cache, ignore storing.

Bug: 122574655

Test: built, flashed, booted
      system/netd/tests/runtests.sh pass
      atest CtsNativeNetDnsTestCases
      atest DnsResolverTest

Change-Id: Ia25fc0601013823c85344a3ca2a9c74e5b2a21a3
parent f3cc6e8a
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -1411,8 +1411,10 @@ static resolv_cache_info* find_cache_info_locked(unsigned netid) REQUIRES(cache_
ResolvCacheStatus _resolv_cache_lookup(unsigned netid, const void* query, int querylen,
                                       void* answer, int answersize, int* answerlen,
                                       uint32_t flags) {
    // Skip cache lookup, return RESOLV_CACHE_NOTFOUND directly so that it is
    // possible to cache the answer of this query.
    if (flags & ANDROID_RESOLV_NO_CACHE_LOOKUP) {
        return RESOLV_CACHE_SKIP;
        return RESOLV_CACHE_NOTFOUND;
    }
    Entry key;
    Entry** lookup;
@@ -1542,7 +1544,8 @@ void _resolv_cache_add(unsigned netid, const void* query, int querylen, const vo
    lookup = _cache_lookup_p(cache, key);
    e = *lookup;

    if (e != NULL) { /* should not happen */
    // Should only happen on ANDROID_RESOLV_NO_CACHE_LOOKUP
    if (e != NULL) {
        LOG(INFO) << __func__ << ": ALREADY IN CACHE (" << e << ") ? IGNORING ADD";
        _cache_notify_waiting_tid_locked(cache, key);
        return;
@@ -1553,7 +1556,7 @@ void _resolv_cache_add(unsigned netid, const void* query, int querylen, const vo
        if (cache->num_entries >= cache->max_entries) {
            _cache_remove_oldest(cache);
        }
        /* need to lookup again */
        // TODO: It looks useless, remove below code after having test to prove it.
        lookup = _cache_lookup_p(cache, key);
        e = *lookup;
        if (e != NULL) {
+30 −0
Original line number Diff line number Diff line
@@ -2116,6 +2116,36 @@ TEST_F(ResolverTest, Async_CacheFlags) {

    // Cache hits,  expect still 6 queries
    EXPECT_EQ(6U, GetNumQueries(dns, host_name));

    // Start to verify if ANDROID_RESOLV_NO_CACHE_LOOKUP does write response into cache
    dns.clearQueries();

    fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa,
                          ANDROID_RESOLV_NO_CACHE_LOOKUP);
    fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa,
                          ANDROID_RESOLV_NO_CACHE_LOOKUP);

    EXPECT_TRUE(fd1 != -1);
    EXPECT_TRUE(fd2 != -1);

    expectAnswersValid(fd2, AF_INET6, "::1.2.3.4");
    expectAnswersValid(fd1, AF_INET6, "::1.2.3.4");

    // Skip cache, expect 2 queries
    EXPECT_EQ(2U, GetNumQueries(dns, host_name));

    // Re-query without flags
    fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0);
    fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0);

    EXPECT_TRUE(fd1 != -1);
    EXPECT_TRUE(fd2 != -1);

    expectAnswersValid(fd2, AF_INET6, "::1.2.3.4");
    expectAnswersValid(fd1, AF_INET6, "::1.2.3.4");

    // Cache hits, expect still 2 queries
    EXPECT_EQ(2U, GetNumQueries(dns, host_name));
}

TEST_F(ResolverTest, Async_NoRetryFlag) {