Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +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 | * BandwidthControllerTest.cpp - unit tests for BandwidthController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 25 | #include <android-base/strings.h> |
| 26 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 27 | #include "BandwidthController.h" |
| 28 | |
| 29 | std::vector<std::string> gCmds = {}; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 30 | std::vector<std::string> gRestoreCmds = {}; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 31 | |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 32 | int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) { |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 33 | std::string cmd = argv[0]; |
| 34 | for (int i = 1; i < argc; i++) { |
| 35 | cmd += " "; |
| 36 | cmd += argv[i]; |
| 37 | } |
| 38 | gCmds.push_back(cmd); |
| 39 | *status = 0; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | FILE *fake_popen(const char *, const char *) { |
| 44 | return NULL; |
| 45 | }; |
| 46 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 47 | int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) { |
| 48 | EXPECT_EQ(V4V6, target); |
| 49 | gRestoreCmds.push_back(commands); |
| 50 | return 0; |
| 51 | } |
| 52 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 53 | void expectIptablesCommands(std::vector<std::string> expectedCmds) { |
| 54 | EXPECT_EQ(expectedCmds.size() * 2, gCmds.size()); |
| 55 | if (expectedCmds.size() * 2 != gCmds.size()) return; |
| 56 | |
| 57 | for (size_t i = 0; i < expectedCmds.size(); i ++) { |
| 58 | EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], gCmds[2 * i]); |
| 59 | EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], gCmds[2 * i + 1]); |
| 60 | } |
| 61 | |
| 62 | gCmds.clear(); |
| 63 | } |
| 64 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 65 | void expectIptablesRestoreCommands(std::vector<std::string> expectedCmds) { |
| 66 | EXPECT_EQ(expectedCmds.size(), gRestoreCmds.size()); |
| 67 | EXPECT_EQ(expectedCmds, gRestoreCmds); |
| 68 | gRestoreCmds.clear(); |
| 69 | } |
| 70 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 71 | class BandwidthControllerTest : public ::testing::Test { |
| 72 | public: |
| 73 | BandwidthControllerTest() { |
| 74 | BandwidthController::execFunction = fake_android_fork_exec; |
| 75 | BandwidthController::popenFunction = fake_popen; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 76 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 77 | gCmds.clear(); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 78 | gRestoreCmds.clear(); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 79 | } |
| 80 | BandwidthController mBw; |
| 81 | }; |
| 82 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 83 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
| 84 | mBw.setupIptablesHooks(); |
| 85 | std::vector<std::string> expected = { |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 86 | "*filter\n" |
| 87 | ":bw_INPUT -\n" |
| 88 | ":bw_OUTPUT -\n" |
| 89 | ":bw_FORWARD -\n" |
| 90 | ":bw_happy_box -\n" |
| 91 | ":bw_penalty_box -\n" |
| 92 | ":bw_data_saver -\n" |
| 93 | ":bw_costly_shared -\n" |
| 94 | "COMMIT\n" |
| 95 | "*raw\n" |
| 96 | ":bw_raw_PREROUTING -\n" |
| 97 | "COMMIT\n" |
| 98 | "*mangle\n" |
| 99 | ":bw_mangle_POSTROUTING -\n" |
| 100 | "COMMIT\n\x04" |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 101 | }; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 102 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 105 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
| 106 | mBw.enableBandwidthControl(false); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 107 | std::string expectedFlush = |
| 108 | "*filter\n" |
| 109 | ":bw_INPUT -\n" |
| 110 | ":bw_OUTPUT -\n" |
| 111 | ":bw_FORWARD -\n" |
| 112 | ":bw_happy_box -\n" |
| 113 | ":bw_penalty_box -\n" |
| 114 | ":bw_data_saver -\n" |
| 115 | ":bw_costly_shared -\n" |
| 116 | "COMMIT\n" |
| 117 | "*raw\n" |
| 118 | ":bw_raw_PREROUTING -\n" |
| 119 | "COMMIT\n" |
| 120 | "*mangle\n" |
| 121 | ":bw_mangle_POSTROUTING -\n" |
| 122 | "COMMIT\n\x04"; |
| 123 | std::string expectedAccounting = |
| 124 | "*filter\n" |
| 125 | "-A bw_INPUT -m owner --socket-exists\n" |
| 126 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 127 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 128 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 129 | "-A bw_happy_box --jump bw_data_saver\n" |
| 130 | "-A bw_data_saver -j RETURN\n" |
| 131 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 132 | "COMMIT\n" |
| 133 | "*raw\n" |
| 134 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 135 | "COMMIT\n" |
| 136 | "*mangle\n" |
| 137 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
| 138 | "COMMIT\n\x04"; |
| 139 | |
| 140 | expectIptablesRestoreCommands({ expectedFlush, expectedAccounting }); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 143 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
| 144 | mBw.disableBandwidthControl(); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame^] | 145 | const std::string expected = |
| 146 | "*filter\n" |
| 147 | ":bw_INPUT -\n" |
| 148 | ":bw_OUTPUT -\n" |
| 149 | ":bw_FORWARD -\n" |
| 150 | ":bw_happy_box -\n" |
| 151 | ":bw_penalty_box -\n" |
| 152 | ":bw_data_saver -\n" |
| 153 | ":bw_costly_shared -\n" |
| 154 | "COMMIT\n" |
| 155 | "*raw\n" |
| 156 | ":bw_raw_PREROUTING -\n" |
| 157 | "COMMIT\n" |
| 158 | "*mangle\n" |
| 159 | ":bw_mangle_POSTROUTING -\n" |
| 160 | "COMMIT\n\x04"; |
| 161 | expectIptablesRestoreCommands({ expected }); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 164 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 165 | mBw.enableDataSaver(true); |
| 166 | std::vector<std::string> expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 167 | "-R bw_data_saver 1 --jump REJECT", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 168 | }; |
| 169 | expectIptablesCommands(expected); |
| 170 | |
| 171 | mBw.enableDataSaver(false); |
| 172 | expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 173 | "-R bw_data_saver 1 --jump RETURN", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 174 | }; |
| 175 | expectIptablesCommands(expected); |
| 176 | } |