blob: 2b2ce6e43467ca073b63a221a45a90f079b7ad24 [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
44 void addPopenContents(std::string contents) {
45 sPopenContents.push_back(contents);
46 }
Lorenzo Colitti26c91322016-07-11 11:36:25 +090047
48 void addPopenContents(std::string contents1, std::string contents2) {
49 sPopenContents.push_back(contents1);
50 sPopenContents.push_back(contents2);
51 }
52
53 void clearPopenContents() {
54 sPopenContents.clear();
55 }
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +090056
57 void addIptablesRestoreOutput(std::string contents) {
58 sIptablesRestoreOutput.push_back(contents);
59 }
60
61 void clearIptablesRestoreOutput() {
62 sIptablesRestoreOutput.clear();
63 }
64
65 void expectSetupCommands(const std::string& expectedClean, std::string expectedAccounting) {
66 std::string expectedList =
67 "*filter\n"
68 "-S\n"
69 "COMMIT\n";
70
71 std::string expectedFlush =
72 "*filter\n"
73 ":bw_INPUT -\n"
74 ":bw_OUTPUT -\n"
75 ":bw_FORWARD -\n"
76 ":bw_happy_box -\n"
77 ":bw_penalty_box -\n"
78 ":bw_data_saver -\n"
79 ":bw_costly_shared -\n"
80 "COMMIT\n"
81 "*raw\n"
82 ":bw_raw_PREROUTING -\n"
83 "COMMIT\n"
84 "*mangle\n"
85 ":bw_mangle_POSTROUTING -\n"
86 "COMMIT\n";
87
88 ExpectedIptablesCommands expected = {{ V4, expectedList }};
89 if (expectedClean.size()) {
90 expected.push_back({ V4V6, expectedClean });
91 }
92 expected.push_back({ V4V6, expectedFlush });
93 if (expectedAccounting.size()) {
94 expected.push_back({ V4V6, expectedAccounting });
95 }
96
97 expectIptablesRestoreCommands(expected);
98 }
Lorenzo Colitti86a47982016-03-18 17:52:25 +090099};
100
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900101TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900102 // Pretend some bw_costly_shared_<iface> rules already exist...
103 addIptablesRestoreOutput(
104 "-P OUTPUT ACCEPT\n"
105 "-N bw_costly_rmnet_data0\n"
106 "-N bw_costly_shared\n"
107 "-N unrelated\n"
108 "-N bw_costly_rmnet_data7\n");
109
110 // ... and expect that they be flushed and deleted.
111 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900112 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900113 ":bw_costly_rmnet_data0 -\n"
114 "-X bw_costly_rmnet_data0\n"
115 ":bw_costly_rmnet_data7 -\n"
116 "-X bw_costly_rmnet_data7\n"
117 "COMMIT\n";
118
119 mBw.setupIptablesHooks();
120 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900121}
122
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900123TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900124 // Pretend no bw_costly_shared_<iface> rules already exist...
125 addIptablesRestoreOutput(
126 "-P OUTPUT ACCEPT\n"
127 "-N bw_costly_shared\n"
128 "-N unrelated\n");
129
130 // ... so none are flushed or deleted.
131 std::string expectedClean = "";
132
133 std::string expectedAccounting =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900134 "*filter\n"
135 "-A bw_INPUT -m owner --socket-exists\n"
136 "-A bw_OUTPUT -m owner --socket-exists\n"
137 "-A bw_costly_shared --jump bw_penalty_box\n"
138 "-A bw_penalty_box --jump bw_happy_box\n"
139 "-A bw_happy_box --jump bw_data_saver\n"
140 "-A bw_data_saver -j RETURN\n"
141 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
142 "COMMIT\n"
143 "*raw\n"
144 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
145 "COMMIT\n"
146 "*mangle\n"
147 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900148 "COMMIT\n";
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900149
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900150 mBw.enableBandwidthControl(false);
151 expectSetupCommands(expectedClean, expectedAccounting);
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900152}
153
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900154TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900155 // Pretend some bw_costly_shared_<iface> rules already exist...
156 addIptablesRestoreOutput(
157 "-P OUTPUT ACCEPT\n"
158 "-N bw_costly_rmnet_data0\n"
159 "-N bw_costly_shared\n"
160 "-N unrelated\n"
161 "-N bw_costly_rmnet_data7\n");
162
163 // ... and expect that they be flushed.
164 std::string expectedCleanCmds =
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900165 "*filter\n"
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900166 ":bw_costly_rmnet_data0 -\n"
167 ":bw_costly_rmnet_data7 -\n"
168 "COMMIT\n";
169
170 mBw.disableBandwidthControl();
171 expectSetupCommands(expectedCleanCmds, "");
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900172}
173
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900174TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
175 mBw.enableDataSaver(true);
176 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900177 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900178 };
179 expectIptablesCommands(expected);
180
181 mBw.enableDataSaver(false);
182 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900183 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900184 };
185 expectIptablesCommands(expected);
186}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900187
188std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
189 "Chain natctrl_tether_counters (4 references)",
190 " pkts bytes target prot opt in out source destination",
191 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
192 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
193 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
194 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
195}, '\n');
196
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900197std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> {
198 "Chain natctrl_tether_counters (2 references)",
199 " pkts bytes target prot opt in out source destination",
200 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
201 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
202}, '\n');
203
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900204std::string readSocketClientResponse(int fd) {
205 char buf[32768];
206 ssize_t bytesRead = read(fd, buf, sizeof(buf));
207 if (bytesRead < 0) {
208 return "";
209 }
210 for (int i = 0; i < bytesRead; i++) {
211 if (buf[i] == '\0') buf[i] = '\n';
212 }
213 return std::string(buf, bytesRead);
214}
215
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900216void expectNoSocketClientResponse(int fd) {
217 char buf[64];
218 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
219}
220
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900221TEST_F(BandwidthControllerTest, TestGetTetherStats) {
222 int socketPair[2];
223 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
224 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
225 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
226 SocketClient cli(socketPair[0], false);
227
228 std::string err;
229 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900230
231 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
232 addPopenContents(kIPv4TetherCounters, "");
233 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
234 expectNoSocketClientResponse(socketPair[1]);
235 clearPopenContents();
236
237 addPopenContents("", kIPv6TetherCounters);
238 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
239 clearPopenContents();
240
241 // IPv4 and IPv6 counters are properly added together.
242 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
243 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900244 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900245 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900246 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
247 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900248 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900249 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900250 expectNoSocketClientResponse(socketPair[1]);
251 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900252
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900253 // Test filtering.
254 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900255 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
256 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\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 Colitti26c91322016-07-11 11:36:25 +0900259 expectNoSocketClientResponse(socketPair[1]);
260 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900261
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900262 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
263 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
264 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\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 Colitti26c91322016-07-11 11:36:25 +0900267 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900268
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900269 // Select nonexistent interfaces.
270 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900271 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
272 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900273 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900274 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900275 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900276
277 // No stats with a filter: no error.
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900278 addPopenContents("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900279 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
280 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900281 clearPopenContents();
282
283 addPopenContents("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900284 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
285 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900286 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900287
288 // No stats and empty filter: error.
289 filter = BandwidthController::TetherStats();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900290 addPopenContents("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900291 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
292 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900293 clearPopenContents();
294
295 addPopenContents(kIPv4TetherCounters, "");
296 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
297 expectNoSocketClientResponse(socketPair[1]);
298 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900299
300 // Include only one pair of interfaces and things are fine.
301 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
302 std::vector<std::string> brokenCounterLines = counterLines;
303 counterLines.resize(4);
304 std::string counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900305 addPopenContents(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900306 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900307 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900308 "200 Tethering stats list completed\n";
309 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
310 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900311 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900312
313 // But if interfaces aren't paired, it's always an error.
314 counterLines.resize(3);
315 counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900316 addPopenContents(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900317 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
318 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900319 clearPopenContents();
320
321 // popen() failing is always an error.
322 addPopenContents(kIPv4TetherCounters);
323 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
324 expectNoSocketClientResponse(socketPair[1]);
325 clearPopenContents();
326 addPopenContents(kIPv6TetherCounters);
327 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
328 expectNoSocketClientResponse(socketPair[1]);
329 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900330}