diff options
author | Chalard Jean <jchalard@google.com> | 2022-12-27 05:07:04 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-12-27 05:07:04 +0000 |
commit | caaf22aa548125cded45445753f03fd08c366c51 (patch) | |
tree | 536ee1eeb23db260674f13abff641cc641a0ab9c | |
parent | 0298d499cccdb6ebf09c8a6da3edc14a1eef8382 (diff) | |
parent | 0cb11e791d35e28427f6b269a0d016b787bd52c3 (diff) | |
download | netd-caaf22aa548125cded45445753f03fd08c366c51.tar.gz |
Trim the output of ip rules for more precise testing am: 38bc0844b0 am: f49d6ead05 am: 0cb11e791d
Original change: https://android-review.googlesource.com/c/platform/system/netd/+/2370411
Change-Id: I2ca7d1447ed94c52917e1fe2688bbd415b42a27c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | tests/binder_test.cpp | 34 | ||||
-rw-r--r-- | tests/test_utils.cpp | 7 |
2 files changed, 24 insertions, 17 deletions
diff --git a/tests/binder_test.cpp b/tests/binder_test.cpp index 135c13c7..677018ee 100644 --- a/tests/binder_test.cpp +++ b/tests/binder_test.cpp @@ -16,6 +16,7 @@ * binder_test.cpp - unit tests for netd binder RPCs. */ +#include <algorithm> #include <cerrno> #include <chrono> #include <cinttypes> @@ -739,12 +740,7 @@ TEST_F(NetdBinderTest, BandwidthEnableDataSaver) { static bool ipRuleExists(const char* ipVersion, const std::string& ipRule) { std::vector<std::string> rules = listIpRules(ipVersion); - for (const auto& rule : rules) { - if (rule.find(ipRule) != std::string::npos) { - return true; - } - } - return false; + return std::find(rules.begin(), rules.end(), ipRule) != rules.end(); } static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, @@ -758,11 +754,10 @@ static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& std::string prefix = StringPrintf("%" PRIu32 ":", priority); std::string suffix; if (oif) { - suffix = StringPrintf(" iif lo oif %s uidrange %d-%d %s\n", oif, range.start, range.stop, + suffix = StringPrintf(" iif lo oif %s uidrange %d-%d %s", oif, range.start, range.stop, action.c_str()); } else { - suffix = StringPrintf(" iif lo uidrange %d-%d %s\n", range.start, range.stop, - action.c_str()); + suffix = StringPrintf(" iif lo uidrange %d-%d %s", range.start, range.stop, action.c_str()); } for (const auto& line : rules) { if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) { @@ -865,7 +860,7 @@ TEST_F(NetdBinderTest, NetworkUidRules) { std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 8005, BASE_UID + 8012), makeUidRangeParcel(BASE_UID + 8090, BASE_UID + 8099)}; UidRangeParcel otherRange = makeUidRangeParcel(BASE_UID + 8190, BASE_UID + 8299); - std::string action = StringPrintf("lookup %s ", sTun.name().c_str()); + std::string action = StringPrintf("lookup %s", sTun.name().c_str()); EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); @@ -1687,7 +1682,14 @@ void expectNetworkDefaultIpRuleDoesNotExist() { std::string networkDefaultRule = StringPrintf("%u:\tfrom all fwmark 0x0/0xffff iif lo", RULE_PRIORITY_DEFAULT_NETWORK); - expectRuleForV4AndV6(NONE_EXIST, networkDefaultRule); + for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { + std::vector<std::string> rules = listIpRules(ipVersion); + for (const auto& line : rules) { + if (android::base::StartsWith(line, networkDefaultRule)) { + FAIL(); + } + } + } } void expectNetworkPermissionIpRuleExists(const char* ifName, int permission) { @@ -1980,7 +1982,7 @@ bool ipRuleIpfwdExists(const char* ipVersion, const std::string& ipfwdRule) { void expectIpfwdRuleExists(const char* fromIf, const char* toIf) { std::string ipfwdRule = - StringPrintf("%u:\tfrom all iif %s lookup %s ", RULE_PRIORITY_TETHERING, fromIf, toIf); + StringPrintf("%u:\tfrom all iif %s lookup %s", RULE_PRIORITY_TETHERING, fromIf, toIf); for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { EXPECT_TRUE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); @@ -1989,7 +1991,7 @@ void expectIpfwdRuleExists(const char* fromIf, const char* toIf) { void expectIpfwdRuleNotExists(const char* fromIf, const char* toIf) { std::string ipfwdRule = - StringPrintf("%u:\tfrom all iif %s lookup %s ", RULE_PRIORITY_TETHERING, fromIf, toIf); + StringPrintf("%u:\tfrom all iif %s lookup %s", RULE_PRIORITY_TETHERING, fromIf, toIf); for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { EXPECT_FALSE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); @@ -4080,8 +4082,8 @@ void verifyAppUidRules(std::vector<bool>&& expectedResults, std::vector<UidRange const std::string& iface, int32_t subPriority) { ASSERT_EQ(expectedResults.size(), uidRanges.size()); if (iface.size()) { - std::string action = StringPrintf("lookup %s ", iface.c_str()); - std::string action_local = StringPrintf("lookup %s_local ", iface.c_str()); + std::string action = StringPrintf("lookup %s", iface.c_str()); + std::string action_local = StringPrintf("lookup %s_local", iface.c_str()); for (unsigned long i = 0; i < uidRanges.size(); i++) { EXPECT_EQ(expectedResults[i], ipRuleExistsForRange(RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority, @@ -4120,7 +4122,7 @@ void verifyAppUidRules(std::vector<bool>&& expectedResults, NativeUidRangeConfig void verifyVpnUidRules(std::vector<bool>&& expectedResults, NativeUidRangeConfig& uidRangeConfig, const std::string& iface, bool secure, bool excludeLocalRoutes) { ASSERT_EQ(expectedResults.size(), uidRangeConfig.uidRanges.size()); - std::string action = StringPrintf("lookup %s ", iface.c_str()); + std::string action = StringPrintf("lookup %s", iface.c_str()); int32_t priority; if (secure) { diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp index 93c7dd8c..ec3707e5 100644 --- a/tests/test_utils.cpp +++ b/tests/test_utils.cpp @@ -49,7 +49,12 @@ std::vector<std::string> runCommand(const std::string& command) { size_t bufsize = 0; ssize_t linelen = 0; while ((linelen = getline(&line, &bufsize, f)) >= 0) { - lines.push_back(std::string(line, linelen)); + std::string str = std::string(line, linelen); + const size_t lastNotWhitespace = str.find_last_not_of(" \t\n\r"); + if (lastNotWhitespace != std::string::npos) { + str = str.substr(0, lastNotWhitespace + 1); + } + lines.push_back(str); free(line); line = nullptr; } |