summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChalard Jean <jchalard@google.com>2022-12-26 19:36:13 +0900
committerChalard Jean <jchalard@google.com>2022-12-26 19:40:27 +0900
commit38bc0844b02212c0dd04e3e4c36a6bfdd54c5acd (patch)
tree536ee1eeb23db260674f13abff641cc641a0ab9c
parente479f31b991bd12634bfa199902af6719268490e (diff)
downloadnetd-38bc0844b02212c0dd04e3e4c36a6bfdd54c5acd.tar.gz
Trim the output of ip rules for more precise testing
Test: netd_integration_test Change-Id: Ia4f6d690c39cd7671cdee7f3f0c996a6aa6d5b91
-rw-r--r--tests/binder_test.cpp34
-rw-r--r--tests/test_utils.cpp7
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;
}