Snap for 9556825 from ac107a2ef74cb969603d8bb5224bb344f0e12973 to mainline-adservices-release

Change-Id: I3078d14d4e81d798d7989d651f9b39a6e881cfef
diff --git a/server/Android.bp b/server/Android.bp
index 06ea0ba..f9ac890 100644
--- a/server/Android.bp
+++ b/server/Android.bp
@@ -45,7 +45,6 @@
         "system/netd/include",
         "system/netd/server/binder",
     ],
-    header_libs: ["bpf_headers"],
     srcs: [
         "BandwidthController.cpp",
         "Controllers.cpp",
@@ -100,7 +99,6 @@
     required: [
         "bpfloader",
     ],
-    header_libs: ["bpf_headers"],
     shared_libs: [
         "android.system.net.netd@1.0",
         "android.system.net.netd@1.1",
@@ -204,7 +202,6 @@
         "system/netd/server/binder",
         "system/netd/tests",
     ],
-    header_libs: ["bpf_headers"],
     tidy_timeout_srcs: [
         "BandwidthControllerTest.cpp",
         "InterfaceControllerTest.cpp",
diff --git a/server/BandwidthController.cpp b/server/BandwidthController.cpp
index 8dba75a..438dbb8 100644
--- a/server/BandwidthController.cpp
+++ b/server/BandwidthController.cpp
@@ -56,7 +56,6 @@
 #include "Fwmark.h"
 #include "NetdConstants.h"
 #include "android/net/INetd.h"
-#include "bpf/BpfUtils.h"
 
 /* Alphabetical */
 #define ALERT_IPT_TEMPLATE "%s %s -m quota2 ! --quota %" PRId64" --name %s\n"
diff --git a/server/FirewallController.cpp b/server/FirewallController.cpp
index b3c6b02..47975dd 100644
--- a/server/FirewallController.cpp
+++ b/server/FirewallController.cpp
@@ -34,7 +34,6 @@
 #include "Controllers.h"
 #include "FirewallController.h"
 #include "NetdConstants.h"
-#include "bpf/BpfUtils.h"
 
 using android::base::Join;
 using android::base::StringAppendF;
diff --git a/server/FirewallController.h b/server/FirewallController.h
index 6d6f48f..227ceeb 100644
--- a/server/FirewallController.h
+++ b/server/FirewallController.h
@@ -24,7 +24,6 @@
 #include <vector>
 
 #include "NetdConstants.h"
-#include "bpf/BpfUtils.h"
 
 namespace android {
 namespace net {
diff --git a/server/InterfaceControllerTest.cpp b/server/InterfaceControllerTest.cpp
index 006018d..8075431 100644
--- a/server/InterfaceControllerTest.cpp
+++ b/server/InterfaceControllerTest.cpp
@@ -22,6 +22,7 @@
 #include <gtest/gtest.h>
 
 #include <netdutils/MockSyscalls.h>
+#include <netdutils/NetNativeTestBase.h>
 #include <netdutils/Utils.h>
 
 #include "InterfaceController.h"
@@ -66,7 +67,7 @@
 
 }  // namespace
 
-class StablePrivacyTest : public testing::Test {
+class StablePrivacyTest : public NetNativeTestBase {
   protected:
     void expectOpenFile(const std::string& path, const Fd fd, int err) {
         if (err == 0) {
@@ -179,7 +180,7 @@
     EXPECT_NE(ok, enableStablePrivacyAddresses(kTestIface));
 }
 
-class GetIfaceListTest : public testing::Test {};
+class GetIfaceListTest : public NetNativeTestBase {};
 
 TEST_F(GetIfaceListTest, IfaceNames) {
     StatusOr<std::vector<std::string>> ifaceNames = getIfaceNames();
diff --git a/server/IptablesBaseTest.h b/server/IptablesBaseTest.h
index abe3f84..bfcc71a 100644
--- a/server/IptablesBaseTest.h
+++ b/server/IptablesBaseTest.h
@@ -18,9 +18,11 @@
 
 #include <deque>
 
+#include <netdutils/NetNativeTestBase.h>
+
 #include "NetdConstants.h"
 
-class IptablesBaseTest : public ::testing::Test {
+class IptablesBaseTest : public NetNativeTestBase {
 public:
     IptablesBaseTest();
 
diff --git a/server/IptablesRestoreControllerTest.cpp b/server/IptablesRestoreControllerTest.cpp
index a05c76d..1999fbf 100644
--- a/server/IptablesRestoreControllerTest.cpp
+++ b/server/IptablesRestoreControllerTest.cpp
@@ -32,10 +32,10 @@
 #include <android-base/strings.h>
 #include <log/log.h>
 #include <netdutils/MockSyscalls.h>
+#include <netdutils/NetNativeTestBase.h>
 #include <netdutils/Stopwatch.h>
 
 #include "NetdConstants.h"
-#include "bpf/BpfUtils.h"
 
 #define XT_LOCK_NAME "/system/etc/xtables.lock"
 #define XT_LOCK_ATTEMPTS 10
@@ -55,7 +55,7 @@
 using testing::Return;
 using testing::StrictMock;
 
-class IptablesRestoreControllerTest : public ::testing::Test {
+class IptablesRestoreControllerTest : public NetNativeTestBase {
 public:
   IptablesRestoreController con;
   int mDefaultMaxRetries = con.MAX_RETRIES;
diff --git a/server/MDnsSdListener.cpp b/server/MDnsSdListener.cpp
index 1d1ea40..0a4cd61 100644
--- a/server/MDnsSdListener.cpp
+++ b/server/MDnsSdListener.cpp
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/if.h>
+#include <net/if.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <pthread.h>
diff --git a/server/NFLogListenerTest.cpp b/server/NFLogListenerTest.cpp
index 88ab2c6..878c988 100644
--- a/server/NFLogListenerTest.cpp
+++ b/server/NFLogListenerTest.cpp
@@ -25,6 +25,7 @@
 #include <linux/netfilter/nfnetlink_log.h>
 
 #include <netdutils/MockSyscalls.h>
+#include <netdutils/NetNativeTestBase.h>
 #include "NFLogListener.h"
 
 using ::testing::_;
@@ -58,7 +59,7 @@
     MOCK_METHOD1(registerSkErrorHandler, void(const SkErrorHandler& handler));
 };
 
-class NFLogListenerTest : public testing::Test {
+class NFLogListenerTest : public NetNativeTestBase {
   protected:
     NFLogListenerTest() {
         EXPECT_CALL(*mNLListener, subscribe(kNFLogPacketMsgType, _))
diff --git a/server/NetworkController.cpp b/server/NetworkController.cpp
index f144139..ee85910 100644
--- a/server/NetworkController.cpp
+++ b/server/NetworkController.cpp
@@ -30,6 +30,7 @@
 #include <android-base/strings.h>
 #include <cutils/misc.h>  // FIRST_APPLICATION_UID
 #include <netd_resolv/resolv.h>
+#include <net/if.h>
 #include "log/log.h"
 
 #include "Controllers.h"
diff --git a/server/SockDiagTest.cpp b/server/SockDiagTest.cpp
index 49601aa..864d08d 100644
--- a/server/SockDiagTest.cpp
+++ b/server/SockDiagTest.cpp
@@ -24,6 +24,7 @@
 #include <linux/inet_diag.h>
 
 #include <gtest/gtest.h>
+#include <netdutils/NetNativeTestBase.h>
 
 #include "Fwmark.h"
 #include "NetdConstants.h"
@@ -33,7 +34,7 @@
 namespace android {
 namespace net {
 
-class SockDiagTest : public ::testing::Test {
+class SockDiagTest : public NetNativeTestBase {
 protected:
     static bool isLoopbackSocket(const inet_diag_msg *msg) {
         return SockDiag::isLoopbackSocket(msg);
diff --git a/server/TcUtils.h b/server/TcUtils.h
index 4b1b2b9..9b15719 100644
--- a/server/TcUtils.h
+++ b/server/TcUtils.h
@@ -16,17 +16,12 @@
 
 #pragma once
 
-#include <android-base/result.h>
 #include <errno.h>
 #include <linux/if_ether.h>
 #include <linux/if_link.h>
 #include <linux/rtnetlink.h>
 #include <tcutils/tcutils.h>
 
-#include <string>
-
-#include "bpf/BpfUtils.h"
-
 namespace android {
 namespace net {
 
diff --git a/server/TetherController.h b/server/TetherController.h
index d2195f2..4aa2a2d 100644
--- a/server/TetherController.h
+++ b/server/TetherController.h
@@ -27,7 +27,6 @@
 
 #include "NetdConstants.h"
 #include "android-base/result.h"
-#include "bpf/BpfMap.h"
 
 #include "android/net/TetherOffloadRuleParcel.h"
 
diff --git a/server/XfrmControllerTest.cpp b/server/XfrmControllerTest.cpp
index e7f5cfc..f1c3807 100644
--- a/server/XfrmControllerTest.cpp
+++ b/server/XfrmControllerTest.cpp
@@ -41,6 +41,7 @@
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 #include <gtest/gtest.h>
+#include <netdutils/NetNativeTestBase.h>
 
 #include "Fwmark.h"
 #include "NetdConstants.h"
@@ -127,7 +128,7 @@
     EXPECT_EQ(expected, actualStr);
 }
 
-class XfrmControllerTest : public ::testing::Test {
+class XfrmControllerTest : public NetNativeTestBase {
   public:
     testing::StrictMock<netdutils::ScopedMockSyscalls> mockSyscalls;
 };
diff --git a/tests/Android.bp b/tests/Android.bp
index ff918cc..ea3b2c5 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -84,13 +84,11 @@
     srcs: [
         ":netd_integration_test_shared",
         "binder_test.cpp",
-        "bpf_base_test.cpp",
         "kernel_test.cpp",
         "netd_client_test.cpp",
         "netd_test.cpp",
     ],
     include_dirs: ["system/netd/server"],
-    header_libs: ["bpf_connectivity_headers"],
     shared_libs: [
         "libbase",
         "libbinder",
diff --git a/tests/benchmarks/bpf_benchmark.cpp b/tests/benchmarks/bpf_benchmark.cpp
index bf4bd54..4614142 100644
--- a/tests/benchmarks/bpf_benchmark.cpp
+++ b/tests/benchmarks/bpf_benchmark.cpp
@@ -18,8 +18,8 @@
 #include <android-base/stringprintf.h>
 #include <benchmark/benchmark.h>
 
+#define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
 #include "bpf/BpfMap.h"
-#include "bpf/BpfUtils.h"
 
 constexpr uint32_t TEST_MAP_SIZE = 10000;
 
diff --git a/tests/binder_test.cpp b/tests/binder_test.cpp
index bc1e739..5204576 100644
--- a/tests/binder_test.cpp
+++ b/tests/binder_test.cpp
@@ -52,12 +52,11 @@
 #include <android-base/strings.h>
 #include <android/multinetwork.h>
 #include <binder/IPCThreadState.h>
-#include <bpf/BpfMap.h>
-#include <bpf/BpfUtils.h>
 #include <com/android/internal/net/BnOemNetdUnsolicitedEventListener.h>
 #include <com/android/internal/net/IOemNetd.h>
 #include <cutils/multiuser.h>
 #include <gtest/gtest.h>
+#include <netdutils/NetNativeTestBase.h>
 #include <netutils/ifc.h>
 #include <utils/Errors.h>
 #include "Fwmark.h"
@@ -175,7 +174,7 @@
         {// 2001:db8:cafe::8888
          .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x88}}};
 
-class NetdBinderTest : public ::testing::Test {
+class NetdBinderTest : public NetNativeTestBase {
   public:
     NetdBinderTest() {
         sp<IServiceManager> sm = android::defaultServiceManager();
@@ -4792,7 +4791,7 @@
     }
 }
 
-class MDnsBinderTest : public ::testing::Test {
+class MDnsBinderTest : public NetNativeTestBase {
   public:
     MDnsBinderTest() {
         sp<IServiceManager> sm = android::defaultServiceManager();
diff --git a/tests/bpf_base_test.cpp b/tests/bpf_base_test.cpp
deleted file mode 100644
index e402e08..0000000
--- a/tests/bpf_base_test.cpp
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <string>
-
-#include <fcntl.h>
-#include <inttypes.h>
-#include <limits.h>
-#include <linux/inet_diag.h>
-#include <linux/sock_diag.h>
-#include <net/if.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <gtest/gtest.h>
-
-#include <cutils/qtaguid.h>
-#include <processgroup/processgroup.h>
-
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-
-#include "bpf/BpfMap.h"
-#include "bpf/BpfUtils.h"
-#include "bpf_shared.h"
-
-using android::base::Result;
-
-namespace android {
-namespace bpf {
-
-// Use the upper limit of uid to avoid conflict with real app uids. We can't use UID_MAX because
-// it's -1, which is INVALID_UID.
-constexpr uid_t TEST_UID = UID_MAX - 1;
-constexpr uint32_t TEST_TAG = 42;
-
-class BpfBasicTest : public testing::Test {
-  protected:
-    BpfBasicTest() {}
-};
-
-TEST_F(BpfBasicTest, TestCgroupMounted) {
-    std::string cg2_path;
-    ASSERT_EQ(true, CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path));
-    ASSERT_EQ(0, access(cg2_path.c_str(), R_OK));
-    ASSERT_EQ(0, access((cg2_path + "/cgroup.controllers").c_str(), R_OK));
-}
-
-TEST_F(BpfBasicTest, TestTrafficControllerSetUp) {
-    ASSERT_EQ(0, access(BPF_EGRESS_PROG_PATH, R_OK));
-    ASSERT_EQ(0, access(BPF_INGRESS_PROG_PATH, R_OK));
-    ASSERT_EQ(0, access(XT_BPF_INGRESS_PROG_PATH, R_OK));
-    ASSERT_EQ(0, access(XT_BPF_EGRESS_PROG_PATH, R_OK));
-    ASSERT_EQ(0, access(COOKIE_TAG_MAP_PATH, R_OK));
-    ASSERT_EQ(0, access(UID_COUNTERSET_MAP_PATH, R_OK));
-    ASSERT_EQ(0, access(STATS_MAP_A_PATH, R_OK));
-    ASSERT_EQ(0, access(STATS_MAP_B_PATH, R_OK));
-    ASSERT_EQ(0, access(IFACE_INDEX_NAME_MAP_PATH, R_OK));
-    ASSERT_EQ(0, access(IFACE_STATS_MAP_PATH, R_OK));
-    ASSERT_EQ(0, access(CONFIGURATION_MAP_PATH, R_OK));
-    ASSERT_EQ(0, access(UID_OWNER_MAP_PATH, R_OK));
-}
-
-TEST_F(BpfBasicTest, TestSocketFilterSetUp) {
-    SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED;
-
-    ASSERT_EQ(0, access(CGROUP_SOCKET_PROG_PATH, R_OK));
-    ASSERT_EQ(0, access(UID_PERMISSION_MAP_PATH, R_OK));
-}
-
-TEST_F(BpfBasicTest, TestTagSocket) {
-    BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
-    ASSERT_LE(0, cookieTagMap.getMap());
-    int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
-    ASSERT_LE(0, sock);
-    uint64_t cookie = getSocketCookie(sock);
-    ASSERT_NE(NONEXISTENT_COOKIE, cookie);
-    ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
-    Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
-    ASSERT_RESULT_OK(tagResult);
-    ASSERT_EQ(TEST_UID, tagResult.value().uid);
-    ASSERT_EQ(TEST_TAG, tagResult.value().tag);
-    ASSERT_EQ(0, qtaguid_untagSocket(sock));
-    tagResult = cookieTagMap.readValue(cookie);
-    ASSERT_FALSE(tagResult.ok());
-    ASSERT_EQ(ENOENT, tagResult.error().code());
-}
-
-TEST_F(BpfBasicTest, TestCloseSocketWithoutUntag) {
-    BpfMap<uint64_t, UidTagValue> cookieTagMap(COOKIE_TAG_MAP_PATH);
-    ASSERT_LE(0, cookieTagMap.getMap());
-    int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
-    ASSERT_LE(0, sock);
-    uint64_t cookie = getSocketCookie(sock);
-    ASSERT_NE(NONEXISTENT_COOKIE, cookie);
-    ASSERT_EQ(0, qtaguid_tagSocket(sock, TEST_TAG, TEST_UID));
-    Result<UidTagValue> tagResult = cookieTagMap.readValue(cookie);
-    ASSERT_RESULT_OK(tagResult);
-    ASSERT_EQ(TEST_UID, tagResult.value().uid);
-    ASSERT_EQ(TEST_TAG, tagResult.value().tag);
-    ASSERT_EQ(0, close(sock));
-    // Check map periodically until sk destroy handler have done its job.
-    for (int i = 0; i < 10; i++) {
-        usleep(5000);  // 5ms
-        tagResult = cookieTagMap.readValue(cookie);
-        if (!tagResult.ok()) {
-            ASSERT_EQ(ENOENT, tagResult.error().code());
-            return;
-        }
-    }
-    FAIL() << "socket tag still exist after 50ms";
-}
-
-}
-}
diff --git a/tests/sock_diag_test.cpp b/tests/sock_diag_test.cpp
index 8ee9908..5d142dc 100644
--- a/tests/sock_diag_test.cpp
+++ b/tests/sock_diag_test.cpp
@@ -21,6 +21,7 @@
 #include <linux/inet_diag.h>
 
 #include <gtest/gtest.h>
+#include <netdutils/NetNativeTestBase.h>
 
 #include "NetdConstants.h"
 #include "SockDiag.h"
@@ -29,7 +30,7 @@
 #define NUM_SOCKETS 500
 
 
-class SockDiagTest : public ::testing::Test {
+class SockDiagTest : public NetNativeTestBase {
 };
 
 uint16_t bindAndListen(int s) {