Remove the deleted stats from map

The xt_qtaguid module removes per uid stats when an app get uninstalled.
So eBPF map should not store the uninstalled stats either. This change
help fix the unknown iface problem as well.

Bug: 77987430
Test: android.app.usage.cts.NetworkUsageStatsTest
Change-Id: Ieb08833ecc35f76d27769042f197d889470faf7f
diff --git a/server/TrafficControllerTest.cpp b/server/TrafficControllerTest.cpp
index 2d47985..afe5cd6 100644
--- a/server/TrafficControllerTest.cpp
+++ b/server/TrafficControllerTest.cpp
@@ -78,7 +78,8 @@
     unique_fd mFakePowerSaveUidMap;
 
     void SetUp() {
-        std::lock_guard<std::mutex> guard(mTc.mOwnerMatchMutex);
+        std::lock_guard<std::mutex> ownerGuard(mTc.mOwnerMatchMutex);
+        std::lock_guard<std::mutex> statsGuard(mTc.mDeleteStatsMutex);
         SKIP_IF_BPF_NOT_SUPPORTED;
 
         mFakeCookieTagMap = unique_fd(createMap(BPF_MAP_TYPE_HASH, sizeof(uint64_t),
@@ -124,7 +125,7 @@
         int sock = socket(protocol, SOCK_STREAM, 0);
         EXPECT_LE(0, sock);
         *cookie = getSocketCookie(sock);
-        EXPECT_NE(INET_DIAG_NOCOOKIE, *cookie);
+        EXPECT_NE(NONEXISTENT_COOKIE, *cookie);
         EXPECT_EQ(0, mTc.tagSocket(sock, tag, uid));
         return sock;
     }
@@ -142,7 +143,7 @@
     }
 
     void expectTagMapEmpty() {
-        uint64_t invalidCookie = INET_DIAG_NOCOOKIE;
+        uint64_t invalidCookie = NONEXISTENT_COOKIE;
         uint64_t cookie;
         EXPECT_EQ(-1, getNextMapKey(mFakeCookieTagMap, &invalidCookie, &cookie));
     }
@@ -199,7 +200,7 @@
         auto checkNoOtherUid = [&uidSet](void *key, const base::unique_fd&) {
             int32_t uid = *(int32_t *)key;
             EXPECT_NE(uidSet.end(), uidSet.find(uid));
-            return 0;
+            return BPF_CONTINUE;
         };
         uint32_t nonExistentKey = NONEXISTENT_UID;
         uint8_t dummyValue;
@@ -354,11 +355,6 @@
     StatsValue statsMapResult;
     ASSERT_EQ(-1, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey, &statsMapResult));
     ASSERT_EQ(-1, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey, &statsMapResult));
-    StatsKey removedStatsKey= {.uid = 0, .tag = 0, .counterSet = COUNTERSETS_LIMIT,
-      .ifaceIndex = 0};
-    ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &removedStatsKey, &statsMapResult));
-    ASSERT_EQ((uint64_t)1, statsMapResult.rxPackets);
-    ASSERT_EQ((uint64_t)100, statsMapResult.rxBytes);
 }
 
 TEST_F(TrafficControllerTest, TestDeleteDataWithTwoTags) {
@@ -419,19 +415,10 @@
     ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey1, &statsMapResult));
     ASSERT_EQ((uint64_t)1, statsMapResult.rxPackets);
     ASSERT_EQ((uint64_t)100, statsMapResult.rxBytes);
-    StatsKey removedStatsKey= {.uid = 0, .tag = 0, .counterSet = COUNTERSETS_LIMIT,
-      .ifaceIndex = 0};
-    ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &removedStatsKey, &statsMapResult));
-    ASSERT_EQ((uint64_t)1, statsMapResult.rxPackets);
-    ASSERT_EQ((uint64_t)100, statsMapResult.rxBytes);
 
-    // Delete the stats of the other uid. Check if it is properly added on the
-    // previous removedStats data.
+    // Delete the stats of the other uid.
     ASSERT_EQ(0, mTc.deleteTagData(0, uid1));
     ASSERT_EQ(-1, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey1, &statsMapResult));
-    ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &removedStatsKey, &statsMapResult));
-    ASSERT_EQ((uint64_t)2, statsMapResult.rxPackets);
-    ASSERT_EQ((uint64_t)200, statsMapResult.rxBytes);
 }
 
 TEST_F(TrafficControllerTest, TestUpdateOwnerMapEntry) {