blob: 959ad9a34669627ca9d069582d245a08063243cc [file] [log] [blame]
Lorenzo Colitti86a47982016-03-18 17:52:25 +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 * BandwidthControllerTest.cpp - unit tests for BandwidthController.cpp
17 */
18
19#include <string>
20#include <vector>
Lorenzo Colitti86a47982016-03-18 17:52:25 +090021
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090022#include <fcntl.h>
23#include <unistd.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26
Lorenzo Colitti86a47982016-03-18 17:52:25 +090027#include <gtest/gtest.h>
28
Lorenzo Colitti13debb82016-03-27 17:46:30 +090029#include <android-base/strings.h>
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090030#include <android-base/stringprintf.h>
Lorenzo Colitti13debb82016-03-27 17:46:30 +090031
Lorenzo Colitti86a47982016-03-18 17:52:25 +090032#include "BandwidthController.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090033#include "IptablesBaseTest.h"
Lorenzo Colitti86a47982016-03-18 17:52:25 +090034
Lorenzo Colitti0f150552016-03-28 02:30:27 +090035class BandwidthControllerTest : public IptablesBaseTest {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090036public:
37 BandwidthControllerTest() {
38 BandwidthController::execFunction = fake_android_fork_exec;
39 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090040 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestoreWithOutput;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090041 }
42 BandwidthController mBw;
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090043
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090044 void addIptablesRestoreOutput(std::string contents) {
45 sIptablesRestoreOutput.push_back(contents);
46 }
47
Lorenzo Colittice6748a2017-02-02 01:34:33 +090048 void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
49 sIptablesRestoreOutput.push_back(contents1);
50 sIptablesRestoreOutput.push_back(contents2);
51 }
52
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090053 void clearIptablesRestoreOutput() {
54 sIptablesRestoreOutput.clear();
55 }
56
57 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
58 std::string expectedList =
59 "*filter\n"
60 "-S\n"
61 "COMMIT\n";
62
63 std::string expectedFlush =
64 "*filter\n"
65 ":bw_INPUT -\n"
66 ":bw_OUTPUT -\n"
67 ":bw_FORWARD -\n"
68 ":bw_happy_box -\n"
69 ":bw_penalty_box -\n"
70 ":bw_data_saver -\n"
71 ":bw_costly_shared -\n"
72 "COMMIT\n"
73 "*raw\n"
74 ":bw_raw_PREROUTING -\n"
75 "COMMIT\n"
76 "*mangle\n"
77 ":bw_mangle_POSTROUTING -\n"
78 "COMMIT\n";
79
80 ExpectedIptablesCommands expected = {{ V4, expectedList }};
81 if (expectedClean.size()) {
82 expected.push_back({ V4V6, expectedClean });
83 }
84 expected.push_back({ V4V6, expectedFlush });
85 if (expectedAccounting.size()) {
86 expected.push_back({ V4V6, expectedAccounting });
87 }
88
89 expectIptablesRestoreCommands(expected);
90 }
Lorenzo Colitti86a47982016-03-18 17:52:25 +090091};
92
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090093TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090094 // Pretend some bw_costly_shared_<iface> rules already exist...
95 addIptablesRestoreOutput(
96 "-P OUTPUT ACCEPT\n"
97 "-N bw_costly_rmnet_data0\n"
98 "-N bw_costly_shared\n"
99 "-N unrelated\n"
100 "-N bw_costly_rmnet_data7\n");
101
102 // ... and expect that they be flushed and deleted.
103 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900104 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900105 ":bw_costly_rmnet_data0 -\n"
106 "-X bw_costly_rmnet_data0\n"
107 ":bw_costly_rmnet_data7 -\n"
108 "-X bw_costly_rmnet_data7\n"
109 "COMMIT\n";
110
111 mBw.setupIptablesHooks();
112 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900113}
114
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900115TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900116 // Pretend no bw_costly_shared_<iface> rules already exist...
117 addIptablesRestoreOutput(
118 "-P OUTPUT ACCEPT\n"
119 "-N bw_costly_shared\n"
120 "-N unrelated\n");
121
122 // ... so none are flushed or deleted.
123 std::string expectedClean = "";
124
125 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900126 "*filter\n"
127 "-A bw_INPUT -m owner --socket-exists\n"
128 "-A bw_OUTPUT -m owner --socket-exists\n"
129 "-A bw_costly_shared --jump bw_penalty_box\n"
130 "-A bw_penalty_box --jump bw_happy_box\n"
131 "-A bw_happy_box --jump bw_data_saver\n"
132 "-A bw_data_saver -j RETURN\n"
133 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
134 "COMMIT\n"
135 "*raw\n"
136 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
137 "COMMIT\n"
138 "*mangle\n"
139 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900140 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900141
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900142 mBw.enableBandwidthControl(false);
143 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900144}
145
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900146TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900147 // Pretend some bw_costly_shared_<iface> rules already exist...
148 addIptablesRestoreOutput(
149 "-P OUTPUT ACCEPT\n"
150 "-N bw_costly_rmnet_data0\n"
151 "-N bw_costly_shared\n"
152 "-N unrelated\n"
153 "-N bw_costly_rmnet_data7\n");
154
155 // ... and expect that they be flushed.
156 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900157 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900158 ":bw_costly_rmnet_data0 -\n"
159 ":bw_costly_rmnet_data7 -\n"
160 "COMMIT\n";
161
162 mBw.disableBandwidthControl();
163 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900164}
165
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900166TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
167 mBw.enableDataSaver(true);
168 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900169 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900170 };
171 expectIptablesCommands(expected);
172
173 mBw.enableDataSaver(false);
174 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900175 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900176 };
177 expectIptablesCommands(expected);
178}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900179
180std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
181 "Chain natctrl_tether_counters (4 references)",
182 " pkts bytes target prot opt in out source destination",
183 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
184 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
185 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
186 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
187}, '\n');
188
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900189std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> {
190 "Chain natctrl_tether_counters (2 references)",
191 " pkts bytes target prot opt in out source destination",
192 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
193 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
194}, '\n');
195
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900196std::string readSocketClientResponse(int fd) {
197 char buf[32768];
198 ssize_t bytesRead = read(fd, buf, sizeof(buf));
199 if (bytesRead < 0) {
200 return "";
201 }
202 for (int i = 0; i < bytesRead; i++) {
203 if (buf[i] == '\0') buf[i] = '\n';
204 }
205 return std::string(buf, bytesRead);
206}
207
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900208void expectNoSocketClientResponse(int fd) {
209 char buf[64];
210 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
211}
212
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900213TEST_F(BandwidthControllerTest, TestGetTetherStats) {
214 int socketPair[2];
215 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
216 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
217 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
218 SocketClient cli(socketPair[0], false);
219
220 std::string err;
221 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900222
223 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900224 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900225 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
226 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900227 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900228
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900229 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900230 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900231 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900232
233 // IPv4 and IPv6 counters are properly added together.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900234 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900235 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900236 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900237 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900238 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
239 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900240 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900241 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900242 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900243 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900244
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900245 // Test filtering.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900246 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900247 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
248 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900249 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900250 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900251 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900252 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900253
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900254 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900255 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
256 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900257 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900258 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900259 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900260
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900261 // Select nonexistent interfaces.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900262 addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900263 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
264 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900265 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900266 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900267 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900268
269 // No stats with a filter: no error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900270 addIptablesRestoreOutput("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900271 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
272 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900273 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900274
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900275 addIptablesRestoreOutput("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900276 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
277 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900278 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900279
280 // No stats and empty filter: error.
281 filter = BandwidthController::TetherStats();
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900282 addIptablesRestoreOutput("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900283 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
284 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900285 clearIptablesRestoreOutput();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900286
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900287 addIptablesRestoreOutput(kIPv4TetherCounters, "");
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900288 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
289 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900290 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900291
292 // Include only one pair of interfaces and things are fine.
293 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
294 std::vector<std::string> brokenCounterLines = counterLines;
295 counterLines.resize(4);
296 std::string counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900297 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900298 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900299 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900300 "200 Tethering stats list completed\n";
301 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
302 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900303 clearIptablesRestoreOutput();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900304
305 // But if interfaces aren't paired, it's always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900306 err = "";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900307 counterLines.resize(3);
308 counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900309 addIptablesRestoreOutput(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900310 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
311 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900312 clearIptablesRestoreOutput();
313
314 // Token unit test of the fact that we return the stats in the error message which the caller
315 // ignores.
316 std::string expectedError = counters;
317 EXPECT_EQ(expectedError, err);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900318
319 // popen() failing is always an error.
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900320 addIptablesRestoreOutput(kIPv4TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900321 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
322 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900323 clearIptablesRestoreOutput();
324 addIptablesRestoreOutput(kIPv6TetherCounters);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900325 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
326 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colittice6748a2017-02-02 01:34:33 +0900327 clearIptablesRestoreOutput();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900328}