1 /*
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 
19 #include <deque>
20 
21 #include <netdutils/NetNativeTestBase.h>
22 
23 #include "NetdConstants.h"
24 
25 class IptablesBaseTest : public NetNativeTestBase {
26 public:
27     IptablesBaseTest();
28 
29     typedef std::vector<std::pair<IptablesTarget, std::string>> ExpectedIptablesCommands;
30 
31     static int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool);
32     static int fake_android_fork_execvp(int argc, char* argv[], int *status, bool, bool);
33     static int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands);
34     static int fakeExecIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
35                                                  std::string *output);
36     static int fakeExecIptablesRestoreCommand(IptablesTarget target, const std::string& table,
37                                               const std::string& commands, std::string *output);
38     static FILE *fake_popen(const char *cmd, const char *type);
39     void expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds);
40     void expectIptablesRestoreCommands(const ExpectedIptablesCommands& expectedCmds);
41 
setReturnValues(const std::deque<int> & returnValues)42     static void setReturnValues(const std::deque<int>& returnValues) {
43         sReturnValues = returnValues;
44     }
45 
addIptablesRestoreOutput(std::string contents)46     static void addIptablesRestoreOutput(std::string contents) {
47         sIptablesRestoreOutput.push_back(contents);
48     }
49 
addIptablesRestoreOutput(std::string contents1,std::string contents2)50     static void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
51         addIptablesRestoreOutput(contents1);
52         addIptablesRestoreOutput(contents2);
53     }
54 
clearIptablesRestoreOutput()55     static void clearIptablesRestoreOutput() {
56         sIptablesRestoreOutput.clear();
57     }
58 
59 protected:
60     static std::vector<std::string> sCmds;
61     static ExpectedIptablesCommands sRestoreCmds;
62     static std::deque<int> sReturnValues;
63     static std::deque<std::string> sPopenContents;
64     static std::deque<std::string> sIptablesRestoreOutput;
65 };
66