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
23 #include <gtest/gtest.h>
25 #include <android-base/stringprintf.h>
27 #include "IptablesBaseTest.h"
28 #include "NetdConstants.h"
30 #define LOG_TAG "IptablesBaseTest"
31 #include <cutils/log.h>
33 IptablesBaseTest::IptablesBaseTest() {
38 int IptablesBaseTest::fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) {
39 std::string cmd = argv[0];
40 for (int i = 1; i < argc; i++) {
49 int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) {
50 std::string cmd = " -w";
52 va_start(args, target);
55 arg = va_arg(args, const char *);
62 if (target == V4 || target == V4V6) {
63 sCmds.push_back(IPTABLES_PATH + cmd);
65 if (target == V6 || target == V4V6) {
66 sCmds.push_back(IP6TABLES_PATH + cmd);
72 FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) {
73 if (sPopenContents.empty() || strcmp(type, "r") != 0) {
77 std::string realCmd = android::base::StringPrintf("echo '%s'", sPopenContents.front().c_str());
78 sPopenContents.pop_front();
79 return popen(realCmd.c_str(), "r");
82 int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
83 sRestoreCmds.push_back({ target, commands });
87 int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
88 const std::string& cmd) {
90 if ((unsigned) pos >= sCmds.size()) {
91 ADD_FAILURE() << "Expected too many iptables commands, want command "
92 << pos + 1 << "/" << sCmds.size();
96 if (target == V4 || target == V4V6) {
97 EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]);
99 if (target == V6 || target == V4V6) {
100 EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]);
103 return target == V4V6 ? 2 : 1;
106 void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) {
107 ExpectedIptablesCommands expected;
108 for (auto cmd : expectedCmds) {
109 expected.push_back({ V4V6, cmd });
111 expectIptablesCommands(expected);
114 void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
116 for (size_t i = 0; i < expectedCmds.size(); i ++) {
117 auto target = expectedCmds[i].first;
118 auto cmd = expectedCmds[i].second;
119 int numConsumed = expectIptablesCommand(target, pos, cmd);
120 if (numConsumed < 0) {
121 // Read past the end of the array.
127 EXPECT_EQ(pos, sCmds.size());
131 void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
132 ExpectedIptablesCommands expected;
133 for (auto cmd : expectedCmds) {
134 expected.push_back({ V4V6, cmd });
136 expectIptablesRestoreCommands(expected);
139 void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
140 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
141 for (size_t i = 0; i < expectedCmds.size(); i++) {
142 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
143 "iptables-restore command " << i << " differs";
145 sRestoreCmds.clear();
148 std::vector<std::string> IptablesBaseTest::sCmds = {};
149 IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
150 std::deque<std::string> IptablesBaseTest::sPopenContents = {};