[NETD-TC#13] Make TrafficControllerTest as traffic_controller_unit_test

Delete tagSocket(), privilegedTagSocket() and untagSocket() revelant
test cases since thay are moved out of TrafficController in
aosp/1849156.

Bug: 202086915
Test: atest traffic_controller_unit_test passed
Change-Id: I605577ee4d7076f0c8ad75888a4d32bff90f6104
This commit is contained in:
Wayne Ma
2022-01-12 16:29:49 +08:00
parent a9716ffcb8
commit 7be6bce52d
3 changed files with 67 additions and 27 deletions

View File

@@ -23,6 +23,9 @@
}, },
{ {
"name": "TetheringIntegrationTests" "name": "TetheringIntegrationTests"
},
{
"name": "traffic_controller_unit_test"
} }
], ],
"postsubmit": [ "postsubmit": [
@@ -35,6 +38,9 @@
}, },
{ {
"name": "libclat_test" "name": "libclat_test"
},
{ "name": "traffic_controller_unit_test",
"keywords": ["netd-device-kernel-4.9", "netd-device-kernel-4.14"]
} }
], ],
"mainline-presubmit": [ "mainline-presubmit": [
@@ -51,6 +57,9 @@
}, },
{ {
"name": "ConnectivityCoverageTests[CaptivePortalLoginGoogle.apk+NetworkStackGoogle.apk+com.google.android.resolv.apex+com.google.android.tethering.apex]" "name": "ConnectivityCoverageTests[CaptivePortalLoginGoogle.apk+NetworkStackGoogle.apk+com.google.android.resolv.apex+com.google.android.tethering.apex]"
},
{
"name": "traffic_controller_unit_test[CaptivePortalLoginGoogle.apk+NetworkStackGoogle.apk+com.google.android.resolv.apex+com.google.android.tethering.apex]"
} }
], ],
"mainline-postsubmit": [ "mainline-postsubmit": [

View File

@@ -47,3 +47,25 @@ cc_library {
], ],
min_sdk_version: "30", min_sdk_version: "30",
} }
cc_test {
name: "traffic_controller_unit_test",
test_suites: ["general-tests"],
require_root: true,
local_include_dirs: ["include"],
header_libs: [
"bpf_connectivity_headers",
],
srcs: [
"TrafficControllerTest.cpp",
],
static_libs: [
"libbase",
"libgmock",
"liblog",
"libnetdutils",
"libtraffic_controller",
"libutils",
"netd_aidl_interface-lateststable-ndk",
],
}

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2017 The Android Open Source Project * Copyright 2022 The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <binder/Status.h>
#include <netdutils/MockSyscalls.h> #include <netdutils/MockSyscalls.h>
@@ -43,6 +44,7 @@ using namespace android::bpf; // NOLINT(google-build-using-namespace): grandfat
namespace android { namespace android {
namespace net { namespace net {
using android::netdutils::Status;
using base::Result; using base::Result;
using netdutils::isOk; using netdutils::isOk;
@@ -273,6 +275,17 @@ class TrafficControllerTest : public ::testing::Test {
EXPECT_EQ((uint64_t)1, appStatsResult.value().rxPackets); EXPECT_EQ((uint64_t)1, appStatsResult.value().rxPackets);
EXPECT_EQ((uint64_t)100, appStatsResult.value().rxBytes); EXPECT_EQ((uint64_t)100, appStatsResult.value().rxBytes);
} }
Status updateUidOwnerMaps(const std::vector<uint32_t>& appUids,
UidOwnerMatchType matchType, TrafficController::IptOp op) {
Status ret(0);
for (auto uid : appUids) {
ret = mTc.updateUidOwnerMap(uid, matchType, op);
if(!isOk(ret)) break;
}
return ret;
}
}; };
TEST_F(TrafficControllerTest, TestSetCounterSet) { TEST_F(TrafficControllerTest, TestSetCounterSet) {
@@ -478,66 +491,62 @@ TEST_F(TrafficControllerTest, TestReplaceSameChain) {
TEST_F(TrafficControllerTest, TestDenylistUidMatch) { TEST_F(TrafficControllerTest, TestDenylistUidMatch) {
std::vector<uint32_t> appUids = {1000, 1001, 10012}; std::vector<uint32_t> appUids = {1000, 1001, 10012};
ASSERT_TRUE(isOk( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, PENALTY_BOX_MATCH,
mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, TrafficController::IptOpInsert))); TrafficController::IptOpInsert)));
expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0);
ASSERT_TRUE(isOk( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, PENALTY_BOX_MATCH,
mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, TrafficController::IptOpDelete))); TrafficController::IptOpDelete)));
expectMapEmpty(mFakeUidOwnerMap); expectMapEmpty(mFakeUidOwnerMap);
} }
TEST_F(TrafficControllerTest, TestAllowlistUidMatch) { TEST_F(TrafficControllerTest, TestAllowlistUidMatch) {
std::vector<uint32_t> appUids = {1000, 1001, 10012}; std::vector<uint32_t> appUids = {1000, 1001, 10012};
ASSERT_TRUE( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpInsert)));
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpInsert)));
expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0);
ASSERT_TRUE( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpDelete)));
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpDelete)));
expectMapEmpty(mFakeUidOwnerMap); expectMapEmpty(mFakeUidOwnerMap);
} }
TEST_F(TrafficControllerTest, TestReplaceMatchUid) { TEST_F(TrafficControllerTest, TestReplaceMatchUid) {
std::vector<uint32_t> appUids = {1000, 1001, 10012}; std::vector<uint32_t> appUids = {1000, 1001, 10012};
// Add appUids to the denylist and expect that their values are all PENALTY_BOX_MATCH. // Add appUids to the denylist and expect that their values are all PENALTY_BOX_MATCH.
ASSERT_TRUE(isOk( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, PENALTY_BOX_MATCH,
mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, TrafficController::IptOpInsert))); TrafficController::IptOpInsert)));
expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0);
// Add the same UIDs to the allowlist and expect that we get PENALTY_BOX_MATCH | // Add the same UIDs to the allowlist and expect that we get PENALTY_BOX_MATCH |
// HAPPY_BOX_MATCH. // HAPPY_BOX_MATCH.
ASSERT_TRUE( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpInsert)));
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpInsert)));
expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH | PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH | PENALTY_BOX_MATCH, 0);
// Remove the same UIDs from the allowlist and check the PENALTY_BOX_MATCH is still there. // Remove the same UIDs from the allowlist and check the PENALTY_BOX_MATCH is still there.
ASSERT_TRUE( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpDelete)));
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpDelete)));
expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, PENALTY_BOX_MATCH, 0);
// Remove the same UIDs from the denylist and check the map is empty. // Remove the same UIDs from the denylist and check the map is empty.
ASSERT_TRUE(isOk( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, PENALTY_BOX_MATCH,
mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, TrafficController::IptOpDelete))); TrafficController::IptOpDelete)));
ASSERT_FALSE(mFakeUidOwnerMap.getFirstKey().ok()); ASSERT_FALSE(mFakeUidOwnerMap.getFirstKey().ok());
} }
TEST_F(TrafficControllerTest, TestDeleteWrongMatchSilentlyFails) { TEST_F(TrafficControllerTest, TestDeleteWrongMatchSilentlyFails) {
std::vector<uint32_t> appUids = {1000, 1001, 10012}; std::vector<uint32_t> appUids = {1000, 1001, 10012};
// If the uid does not exist in the map, trying to delete a rule about it will fail. // If the uid does not exist in the map, trying to delete a rule about it will fail.
ASSERT_FALSE( ASSERT_FALSE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH,
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpDelete))); TrafficController::IptOpDelete)));
expectMapEmpty(mFakeUidOwnerMap); expectMapEmpty(mFakeUidOwnerMap);
// Add denylist rules for appUids. // Add denylist rules for appUids.
ASSERT_TRUE( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, HAPPY_BOX_MATCH,
isOk(mTc.updateUidOwnerMap(appUids, HAPPY_BOX_MATCH, TrafficController::IptOpInsert))); TrafficController::IptOpInsert)));
expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0);
// Delete (non-existent) denylist rules for appUids, and check that this silently does // Delete (non-existent) denylist rules for appUids, and check that this silently does
// nothing if the uid is in the map but does not have denylist match. This is required because // nothing if the uid is in the map but does not have denylist match. This is required because
// NetworkManagementService will try to remove a uid from denylist after adding it to the // NetworkManagementService will try to remove a uid from denylist after adding it to the
// allowlist and if the remove fails it will not update the uid status. // allowlist and if the remove fails it will not update the uid status.
ASSERT_TRUE(isOk( ASSERT_TRUE(isOk(updateUidOwnerMaps(appUids, PENALTY_BOX_MATCH,
mTc.updateUidOwnerMap(appUids, PENALTY_BOX_MATCH, TrafficController::IptOpDelete))); TrafficController::IptOpDelete)));
expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0); expectUidOwnerMapValues(appUids, HAPPY_BOX_MATCH, 0);
} }
@@ -586,7 +595,7 @@ TEST_F(TrafficControllerTest, TestRemoveUidInterfaceFilteringRules) {
TEST_F(TrafficControllerTest, TestUidInterfaceFilteringRulesCoexistWithExistingMatches) { TEST_F(TrafficControllerTest, TestUidInterfaceFilteringRulesCoexistWithExistingMatches) {
// Set up existing PENALTY_BOX_MATCH rules // Set up existing PENALTY_BOX_MATCH rules
ASSERT_TRUE(isOk(mTc.updateUidOwnerMap({1000, 1001, 10012}, PENALTY_BOX_MATCH, ASSERT_TRUE(isOk(updateUidOwnerMaps({1000, 1001, 10012}, PENALTY_BOX_MATCH,
TrafficController::IptOpInsert))); TrafficController::IptOpInsert)));
expectUidOwnerMapValues({1000, 1001, 10012}, PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues({1000, 1001, 10012}, PENALTY_BOX_MATCH, 0);
@@ -598,7 +607,7 @@ TEST_F(TrafficControllerTest, TestUidInterfaceFilteringRulesCoexistWithExistingM
expectUidOwnerMapValues({10013, 10014}, IIF_MATCH, iif1); expectUidOwnerMapValues({10013, 10014}, IIF_MATCH, iif1);
// Removing some PENALTY_BOX_MATCH rules should not change uid interface rule // Removing some PENALTY_BOX_MATCH rules should not change uid interface rule
ASSERT_TRUE(isOk(mTc.updateUidOwnerMap({1001, 10012}, PENALTY_BOX_MATCH, ASSERT_TRUE(isOk(updateUidOwnerMaps({1001, 10012}, PENALTY_BOX_MATCH,
TrafficController::IptOpDelete))); TrafficController::IptOpDelete)));
expectUidOwnerMapValues({1000}, PENALTY_BOX_MATCH, 0); expectUidOwnerMapValues({1000}, PENALTY_BOX_MATCH, 0);
expectUidOwnerMapValues({10012, 10013, 10014}, IIF_MATCH, iif1); expectUidOwnerMapValues({10012, 10013, 10014}, IIF_MATCH, iif1);