Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame^] | 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.cpp - utility class for tests that use iptables |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include "IptablesBaseTest.h" |
| 25 | #include "NetdConstants.h" |
| 26 | |
| 27 | IptablesBaseTest::IptablesBaseTest() { |
| 28 | sCmds.clear(); |
| 29 | sRestoreCmds.clear(); |
| 30 | } |
| 31 | |
| 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++) { |
| 35 | cmd += " "; |
| 36 | cmd += argv[i]; |
| 37 | } |
| 38 | sCmds.push_back(cmd); |
| 39 | *status = 0; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) { |
| 44 | EXPECT_EQ(V4V6, target); |
| 45 | sRestoreCmds.push_back(commands); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | void IptablesBaseTest::expectIptablesCommands(const std::vector<std::string>& expectedCmds) { |
| 50 | EXPECT_EQ(expectedCmds.size() * 2, sCmds.size()); |
| 51 | if (expectedCmds.size() * 2 != sCmds.size()) return; |
| 52 | |
| 53 | for (size_t i = 0; i < expectedCmds.size(); i ++) { |
| 54 | EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], sCmds[2 * i]); |
| 55 | EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], sCmds[2 * i + 1]); |
| 56 | } |
| 57 | |
| 58 | sCmds.clear(); |
| 59 | } |
| 60 | |
| 61 | void IptablesBaseTest::expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds) { |
| 62 | EXPECT_EQ(expectedCmds.size(), sRestoreCmds.size()); |
| 63 | EXPECT_EQ(expectedCmds, sRestoreCmds); |
| 64 | sRestoreCmds.clear(); |
| 65 | } |
| 66 | |
| 67 | std::vector<std::string> IptablesBaseTest::sCmds = {}; |
| 68 | std::vector<std::string> IptablesBaseTest::sRestoreCmds = {}; |