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> |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 21 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
| 28 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 29 | #include <android-base/strings.h> |
| 30 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 31 | #include "BandwidthController.h" |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 32 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 33 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 34 | class BandwidthControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 35 | public: |
| 36 | BandwidthControllerTest() { |
| 37 | BandwidthController::execFunction = fake_android_fork_exec; |
| 38 | BandwidthController::popenFunction = fake_popen; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 39 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 40 | } |
| 41 | BandwidthController mBw; |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 42 | |
| 43 | void addPopenContents(std::string contents) { |
| 44 | sPopenContents.push_back(contents); |
| 45 | } |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 46 | }; |
| 47 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 48 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
| 49 | mBw.setupIptablesHooks(); |
| 50 | std::vector<std::string> expected = { |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 51 | "*filter\n" |
| 52 | ":bw_INPUT -\n" |
| 53 | ":bw_OUTPUT -\n" |
| 54 | ":bw_FORWARD -\n" |
| 55 | ":bw_happy_box -\n" |
| 56 | ":bw_penalty_box -\n" |
| 57 | ":bw_data_saver -\n" |
| 58 | ":bw_costly_shared -\n" |
| 59 | "COMMIT\n" |
| 60 | "*raw\n" |
| 61 | ":bw_raw_PREROUTING -\n" |
| 62 | "COMMIT\n" |
| 63 | "*mangle\n" |
| 64 | ":bw_mangle_POSTROUTING -\n" |
| 65 | "COMMIT\n\x04" |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 66 | }; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 67 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 68 | } |
| 69 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 70 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
| 71 | mBw.enableBandwidthControl(false); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 72 | std::string expectedFlush = |
| 73 | "*filter\n" |
| 74 | ":bw_INPUT -\n" |
| 75 | ":bw_OUTPUT -\n" |
| 76 | ":bw_FORWARD -\n" |
| 77 | ":bw_happy_box -\n" |
| 78 | ":bw_penalty_box -\n" |
| 79 | ":bw_data_saver -\n" |
| 80 | ":bw_costly_shared -\n" |
| 81 | "COMMIT\n" |
| 82 | "*raw\n" |
| 83 | ":bw_raw_PREROUTING -\n" |
| 84 | "COMMIT\n" |
| 85 | "*mangle\n" |
| 86 | ":bw_mangle_POSTROUTING -\n" |
| 87 | "COMMIT\n\x04"; |
| 88 | std::string expectedAccounting = |
| 89 | "*filter\n" |
| 90 | "-A bw_INPUT -m owner --socket-exists\n" |
| 91 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 92 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 93 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 94 | "-A bw_happy_box --jump bw_data_saver\n" |
| 95 | "-A bw_data_saver -j RETURN\n" |
| 96 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 97 | "COMMIT\n" |
| 98 | "*raw\n" |
| 99 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 100 | "COMMIT\n" |
| 101 | "*mangle\n" |
| 102 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
| 103 | "COMMIT\n\x04"; |
| 104 | |
| 105 | expectIptablesRestoreCommands({ expectedFlush, expectedAccounting }); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 108 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
| 109 | mBw.disableBandwidthControl(); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 110 | const std::string expected = |
| 111 | "*filter\n" |
| 112 | ":bw_INPUT -\n" |
| 113 | ":bw_OUTPUT -\n" |
| 114 | ":bw_FORWARD -\n" |
| 115 | ":bw_happy_box -\n" |
| 116 | ":bw_penalty_box -\n" |
| 117 | ":bw_data_saver -\n" |
| 118 | ":bw_costly_shared -\n" |
| 119 | "COMMIT\n" |
| 120 | "*raw\n" |
| 121 | ":bw_raw_PREROUTING -\n" |
| 122 | "COMMIT\n" |
| 123 | "*mangle\n" |
| 124 | ":bw_mangle_POSTROUTING -\n" |
| 125 | "COMMIT\n\x04"; |
| 126 | expectIptablesRestoreCommands({ expected }); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 129 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 130 | mBw.enableDataSaver(true); |
| 131 | std::vector<std::string> expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 132 | "-R bw_data_saver 1 --jump REJECT", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 133 | }; |
| 134 | expectIptablesCommands(expected); |
| 135 | |
| 136 | mBw.enableDataSaver(false); |
| 137 | expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 138 | "-R bw_data_saver 1 --jump RETURN", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 139 | }; |
| 140 | expectIptablesCommands(expected); |
| 141 | } |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 142 | |
| 143 | std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> { |
| 144 | "Chain natctrl_tether_counters (4 references)", |
| 145 | " pkts bytes target prot opt in out source destination", |
| 146 | " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 147 | " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0", |
| 148 | " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0", |
| 149 | " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0", |
| 150 | }, '\n'); |
| 151 | |
| 152 | std::string readSocketClientResponse(int fd) { |
| 153 | char buf[32768]; |
| 154 | ssize_t bytesRead = read(fd, buf, sizeof(buf)); |
| 155 | if (bytesRead < 0) { |
| 156 | return ""; |
| 157 | } |
| 158 | for (int i = 0; i < bytesRead; i++) { |
| 159 | if (buf[i] == '\0') buf[i] = '\n'; |
| 160 | } |
| 161 | return std::string(buf, bytesRead); |
| 162 | } |
| 163 | |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 164 | void expectNoSocketClientResponse(int fd) { |
| 165 | char buf[64]; |
| 166 | EXPECT_EQ(-1, read(fd, buf, sizeof(buf))); |
| 167 | } |
| 168 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 169 | TEST_F(BandwidthControllerTest, TestGetTetherStats) { |
| 170 | int socketPair[2]; |
| 171 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair)); |
| 172 | ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL))); |
| 173 | ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL))); |
| 174 | SocketClient cli(socketPair[0], false); |
| 175 | |
| 176 | std::string err; |
| 177 | BandwidthController::TetherStats filter; |
| 178 | addPopenContents(kIPv4TetherCounters); |
| 179 | std::string expected = |
| 180 | "114 wlan0 rmnet0 2373 26 2002 27\n" |
| 181 | "114 bt-pan rmnet0 107471 1040 1708806 1450\n" |
| 182 | "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 183 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 184 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
| 185 | |
| 186 | addPopenContents(kIPv4TetherCounters); |
| 187 | filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1); |
| 188 | expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 189 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 190 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
| 191 | |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 192 | addPopenContents(kIPv4TetherCounters); |
| 193 | filter = BandwidthController::TetherStats("rmnet0", "wlan0", -1, -1, -1, -1); |
| 194 | expected = "221 rmnet0 wlan0 2002 27 2373 26\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 195 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 196 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
| 197 | |
| 198 | addPopenContents(kIPv4TetherCounters); |
| 199 | filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1); |
| 200 | expected = "200 Tethering stats list completed\n"; |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 201 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 202 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
Lorenzo Colitti | 750e8fc | 2016-07-12 01:19:49 +0900 | [diff] [blame^] | 203 | |
| 204 | // No stats with a filter: no error. |
| 205 | addPopenContents(""); |
| 206 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 207 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
| 208 | addPopenContents("foo"); |
| 209 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 210 | ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1])); |
| 211 | |
| 212 | // No stats and empty filter: error. |
| 213 | filter = BandwidthController::TetherStats(); |
| 214 | addPopenContents(""); |
| 215 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 216 | addPopenContents("foo"); |
| 217 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 218 | expectNoSocketClientResponse(socketPair[1]); |
| 219 | |
| 220 | // Include only one pair of interfaces and things are fine. |
| 221 | std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n"); |
| 222 | std::vector<std::string> brokenCounterLines = counterLines; |
| 223 | counterLines.resize(4); |
| 224 | std::string counters = android::base::Join(counterLines, "\n") + "\n"; |
| 225 | addPopenContents(counters); |
| 226 | expected = |
| 227 | "114 wlan0 rmnet0 2373 26 2002 27\n" |
| 228 | "200 Tethering stats list completed\n"; |
| 229 | ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err)); |
| 230 | ASSERT_EQ(expected, readSocketClientResponse(socketPair[1])); |
| 231 | |
| 232 | // But if interfaces aren't paired, it's always an error. |
| 233 | counterLines.resize(3); |
| 234 | counters = android::base::Join(counterLines, "\n") + "\n"; |
| 235 | ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err)); |
| 236 | expectNoSocketClientResponse(socketPair[1]); |
Lorenzo Colitti | bbeaf9a | 2016-07-08 18:24:26 +0900 | [diff] [blame] | 237 | } |