OSDN Git Service

Merge "Fix P2TP VPNs by adding an exception for VPN user." into nyc-dev
[android-x86/system-netd.git] / server / IptablesBaseTest.cpp
index b3065f5..5ce6667 100644 (file)
@@ -40,29 +40,81 @@ int IptablesBaseTest::fake_android_fork_exec(int argc, char* argv[], int *status
     return 0;
 }
 
+int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) {
+    std::string cmd = " -w";
+    va_list args;
+    va_start(args, target);
+    const char *arg;
+    do {
+        arg = va_arg(args, const char *);
+        if (arg != nullptr) {
+            cmd += " ";
+            cmd += arg;
+        }
+    } while (arg);
+
+    if (target == V4 || target == V4V6) {
+        sCmds.push_back(IPTABLES_PATH + cmd);
+    }
+    if (target == V6 || target == V4V6) {
+        sCmds.push_back(IP6TABLES_PATH + cmd);
+    }
+
+    return 0;
+}
+
 int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
-    EXPECT_EQ(V4V6, target);
-    sRestoreCmds.push_back(commands);
+    sRestoreCmds.push_back({ target, commands });
     return 0;
 }
 
+int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
+                                            const std::string& cmd) {
+    if (target == V4 || target == V4V6) {
+        EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]);
+    }
+    if (target == V6 || target == V4V6) {
+        EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]);
+    }
+    return target == V4V6 ? 2 : 1;
+}
+
 void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) {
-    EXPECT_EQ(expectedCmds.size() * 2, sCmds.size());
-    if (expectedCmds.size() * 2 != sCmds.size()) return;
+    ExpectedIptablesCommands expected;
+    for (auto cmd : expectedCmds) {
+        expected.push_back({ V4V6, cmd });
+    }
+    expectIptablesCommands(expected);
+}
 
+void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
+    size_t pos = 0;
     for (size_t i = 0; i < expectedCmds.size(); i ++) {
-        EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], sCmds[2 * i]);
-        EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], sCmds[2 * i + 1]);
+        auto target = expectedCmds[i].first;
+        auto cmd = expectedCmds[i].second;
+        pos += expectIptablesCommand(target, pos, cmd);
     }
 
+    EXPECT_EQ(pos, sCmds.size());
     sCmds.clear();
 }
 
 void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
+    ExpectedIptablesCommands expected;
+    for (auto cmd : expectedCmds) {
+        expected.push_back({ V4V6, cmd });
+    }
+    expectIptablesRestoreCommands(expected);
+}
+
+void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
     EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
-    EXPECT_EQ(expectedCmds, sRestoreCmds);
+    for (size_t i = 0; i < expectedCmds.size(); i++) {
+        EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
+            "iptables-restore command " << i << " differs";
+    }
     sRestoreCmds.clear();
 }
 
 std::vector<std::string> IptablesBaseTest::sCmds = {};
-std::vector<std::string> IptablesBaseTest::sRestoreCmds = {};
+IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};