blob: bfcc71a1eb67b7cd4f8b99910d524ff274a44ab2 [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.h - utility class for tests that use iptables
17 */
18
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090019#include <deque>
20
Paul Hu1c64f082022-06-21 03:42:50 +000021#include <netdutils/NetNativeTestBase.h>
22
Lorenzo Colitti0f150552016-03-28 02:30:27 +090023#include "NetdConstants.h"
24
Paul Hu1c64f082022-06-21 03:42:50 +000025class IptablesBaseTest : public NetNativeTestBase {
Lorenzo Colitti0f150552016-03-28 02:30:27 +090026public:
27 IptablesBaseTest();
28
Lorenzo Colitti9028d912016-03-28 02:34:54 +090029 typedef std::vector<std::pair<IptablesTarget, std::string>> ExpectedIptablesCommands;
30
Lorenzo Colitti0f150552016-03-28 02:30:27 +090031 static int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool);
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090032 static int fake_android_fork_execvp(int argc, char* argv[], int *status, bool, bool);
Lorenzo Colitti0f150552016-03-28 02:30:27 +090033 static int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands);
Lorenzo Colitticd283772017-01-31 19:00:49 +090034 static int fakeExecIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
35 std::string *output);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090036 static int fakeExecIptablesRestoreCommand(IptablesTarget target, const std::string& table,
37 const std::string& commands, std::string *output);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090038 static FILE *fake_popen(const char *cmd, const char *type);
Lorenzo Colitti0f150552016-03-28 02:30:27 +090039 void expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds);
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090040 void expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds);
Lorenzo Colittia93126d2017-08-24 13:28:19 +090041
42 static void setReturnValues(const std::deque<int>& returnValues) {
43 sReturnValues = returnValues;
44 }
45
46 static void addIptablesRestoreOutput(std::string contents) {
47 sIptablesRestoreOutput.push_back(contents);
48 }
49
50 static void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
51 addIptablesRestoreOutput(contents1);
52 addIptablesRestoreOutput(contents2);
53 }
54
55 static void clearIptablesRestoreOutput() {
56 sIptablesRestoreOutput.clear();
57 }
Lorenzo Colitti0f150552016-03-28 02:30:27 +090058
59protected:
60 static std::vector<std::string> sCmds;
Lorenzo Colittie60c0a52016-03-29 00:53:45 +090061 static ExpectedIptablesCommands sRestoreCmds;
Lorenzo Colitti849a11c2017-02-27 23:01:16 +090062 static std::deque<int> sReturnValues;
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090063 static std::deque<std::string> sPopenContents;
Lorenzo Colitticd283772017-01-31 19:00:49 +090064 static std::deque<std::string> sIptablesRestoreOutput;
Lorenzo Colitti0f150552016-03-28 02:30:27 +090065};