2 * Copyright 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * IptablesBaseTest.cpp - utility class for tests that use iptables
22 #include <gtest/gtest.h>
24 #include "IptablesBaseTest.h"
25 #include "NetdConstants.h"
27 IptablesBaseTest::IptablesBaseTest() {
32 int IptablesBaseTest::fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) {
33 std::string cmd = argv[0];
34 for (int i = 1; i < argc; i++) {
43 int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) {
44 std::string cmd = " -w";
46 va_start(args, target);
49 arg = va_arg(args, const char *);
56 if (target == V4 || target == V4V6) {
57 sCmds.push_back(IPTABLES_PATH + cmd);
59 if (target == V6 || target == V4V6) {
60 sCmds.push_back(IP6TABLES_PATH + cmd);
66 int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
67 sRestoreCmds.push_back({ target, commands });
71 int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
72 const std::string& cmd) {
73 if (target == V4 || target == V4V6) {
74 EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]);
76 if (target == V6 || target == V4V6) {
77 EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]);
79 return target == V4V6 ? 2 : 1;
82 void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) {
83 ExpectedIptablesCommands expected;
84 for (auto cmd : expectedCmds) {
85 expected.push_back({ V4V6, cmd });
87 expectIptablesCommands(expected);
90 void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
92 for (size_t i = 0; i < expectedCmds.size(); i ++) {
93 auto target = expectedCmds[i].first;
94 auto cmd = expectedCmds[i].second;
95 pos += expectIptablesCommand(target, pos, cmd);
98 EXPECT_EQ(pos, sCmds.size());
102 void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
103 ExpectedIptablesCommands expected;
104 for (auto cmd : expectedCmds) {
105 expected.push_back({ V4V6, cmd });
107 expectIptablesRestoreCommands(expected);
110 void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
111 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
112 for (size_t i = 0; i < expectedCmds.size(); i++) {
113 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
114 "iptables-restore command " << i << " differs";
116 sRestoreCmds.clear();
119 std::vector<std::string> IptablesBaseTest::sCmds = {};
120 IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};