blob: 9e75cb63f61d6d6e6cbd594d78d0d502d570697f [file] [log] [blame]
Lorenzo Colitti0f150552016-03-28 02:30:27 +09001/*
2 * Copyright 2016 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 *
16 * IptablesBaseTest.cpp - utility class for tests that use iptables
17 */
18
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090019#include <deque>
Lorenzo Colitti0f150552016-03-28 02:30:27 +090020#include <string>
21#include <vector>
22
23#include <gtest/gtest.h>
24
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090025#include <android-base/stringprintf.h>
26
Lorenzo Colitti0f150552016-03-28 02:30:27 +090027#include "IptablesBaseTest.h"
28#include "NetdConstants.h"
29
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090030#define LOG_TAG "IptablesBaseTest"
31#include <cutils/log.h>
32
Lorenzo Colitti0f150552016-03-28 02:30:27 +090033IptablesBaseTest::IptablesBaseTest() {
34 sCmds.clear();
35 sRestoreCmds.clear();
36}
37
38int 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++) {
41 cmd += " ";
42 cmd += argv[i];
43 }
44 sCmds.push_back(cmd);
45 *status = 0;
46 return 0;
47}
48
Lorenzo Colitti9028d912016-03-28 02:34:54 +090049int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) {
50 std::string cmd = " -w";
51 va_list args;
52 va_start(args, target);
53 const char *arg;
54 do {
55 arg = va_arg(args, const char *);
56 if (arg != nullptr) {
57 cmd += " ";
58 cmd += arg;
59 }
60 } while (arg);
61
62 if (target == V4 || target == V4V6) {
63 sCmds.push_back(IPTABLES_PATH + cmd);
64 }
65 if (target == V6 || target == V4V6) {
66 sCmds.push_back(IP6TABLES_PATH + cmd);
67 }
68
69 return 0;
70}
71
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090072FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) {
73 if (sPopenContents.empty() || strcmp(type, "r") != 0) {
74 return NULL;
75 }
76
77 std::string realCmd = android::base::StringPrintf("echo '%s'", sPopenContents.front().c_str());
78 sPopenContents.pop_front();
79 return popen(realCmd.c_str(), "r");
80}
81
Lorenzo Colitti0f150552016-03-28 02:30:27 +090082int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090083 sRestoreCmds.push_back({ target, commands });
Lorenzo Colitti0f150552016-03-28 02:30:27 +090084 return 0;
85}
86
Lorenzo Colitti9028d912016-03-28 02:34:54 +090087int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos,
88 const std::string& cmd) {
Lorenzo Colitti54ecf162016-05-13 16:57:15 +090089
90 if ((unsigned) pos >= sCmds.size()) {
91 ADD_FAILURE() << "Expected too many iptables commands, want command "
92 << pos + 1 << "/" << sCmds.size();
93 return -1;
94 }
95
Lorenzo Colitti9028d912016-03-28 02:34:54 +090096 if (target == V4 || target == V4V6) {
97 EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]);
98 }
99 if (target == V6 || target == V4V6) {
100 EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]);
101 }
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900102
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900103 return target == V4V6 ? 2 : 1;
104}
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900105
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900106void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) {
107 ExpectedIptablesCommands expected;
108 for (auto cmd : expectedCmds) {
109 expected.push_back({ V4V6, cmd });
110 }
111 expectIptablesCommands(expected);
112}
113
114void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds) {
115 size_t pos = 0;
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900116 for (size_t i = 0; i < expectedCmds.size(); i ++) {
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900117 auto target = expectedCmds[i].first;
118 auto cmd = expectedCmds[i].second;
Lorenzo Colitti54ecf162016-05-13 16:57:15 +0900119 int numConsumed = expectIptablesCommand(target, pos, cmd);
120 if (numConsumed < 0) {
121 // Read past the end of the array.
122 break;
123 }
124 pos += numConsumed;
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900125 }
126
Lorenzo Colitti9028d912016-03-28 02:34:54 +0900127 EXPECT_EQ(pos, sCmds.size());
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900128 sCmds.clear();
129}
130
131void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) {
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900132 ExpectedIptablesCommands expected;
133 for (auto cmd : expectedCmds) {
134 expected.push_back({ V4V6, cmd });
135 }
136 expectIptablesRestoreCommands(expected);
137}
138
139void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds) {
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900140 EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size());
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900141 for (size_t i = 0; i < expectedCmds.size(); i++) {
142 EXPECT_EQ(expectedCmds[i], sRestoreCmds[i]) <<
143 "iptables-restore command " << i << " differs";
144 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +0900145 sRestoreCmds.clear();
146}
147
148std::vector<std::string> IptablesBaseTest::sCmds = {};
Lorenzo Colittie60c0a52016-03-29 00:53:45 +0900149IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900150std::deque<std::string> IptablesBaseTest::sPopenContents = {};