blob: 3e1159670f6594e5162021270c00d71707ae7730 [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>
30
Lorenzo Colitti86a47982016-03-18 17:52:25 +090031#include "BandwidthController.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090032#include "IptablesBaseTest.h"
Lorenzo Colitti86a47982016-03-18 17:52:25 +090033
Lorenzo Colitti0f150552016-03-28 02:30:27 +090034class BandwidthControllerTest : public IptablesBaseTest {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090035public:
36 BandwidthControllerTest() {
37 BandwidthController::execFunction = fake_android_fork_exec;
38 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti13debb82016-03-27 17:46:30 +090039 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090040 }
41 BandwidthController mBw;
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +090042
43 void addPopenContents(std::string contents) {
44 sPopenContents.push_back(contents);
45 }
Lorenzo Colitti26c91322016-07-11 11:36:25 +090046
47 void addPopenContents(std::string contents1, std::string contents2) {
48 sPopenContents.push_back(contents1);
49 sPopenContents.push_back(contents2);
50 }
51
52 void clearPopenContents() {
53 sPopenContents.clear();
54 }
Lorenzo Colitti86a47982016-03-18 17:52:25 +090055};
56
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090057TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
58 mBw.setupIptablesHooks();
59 std::vector<std::string> expected = {
Lorenzo Colitti13debb82016-03-27 17:46:30 +090060 "*filter\n"
61 ":bw_INPUT -\n"
62 ":bw_OUTPUT -\n"
63 ":bw_FORWARD -\n"
64 ":bw_happy_box -\n"
65 ":bw_penalty_box -\n"
66 ":bw_data_saver -\n"
67 ":bw_costly_shared -\n"
68 "COMMIT\n"
69 "*raw\n"
70 ":bw_raw_PREROUTING -\n"
71 "COMMIT\n"
72 "*mangle\n"
73 ":bw_mangle_POSTROUTING -\n"
74 "COMMIT\n\x04"
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090075 };
Lorenzo Colitti13debb82016-03-27 17:46:30 +090076 expectIptablesRestoreCommands(expected);
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090077}
78
Lorenzo Colitti86a47982016-03-18 17:52:25 +090079TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
80 mBw.enableBandwidthControl(false);
Lorenzo Colitti13debb82016-03-27 17:46:30 +090081 std::string expectedFlush =
82 "*filter\n"
83 ":bw_INPUT -\n"
84 ":bw_OUTPUT -\n"
85 ":bw_FORWARD -\n"
86 ":bw_happy_box -\n"
87 ":bw_penalty_box -\n"
88 ":bw_data_saver -\n"
89 ":bw_costly_shared -\n"
90 "COMMIT\n"
91 "*raw\n"
92 ":bw_raw_PREROUTING -\n"
93 "COMMIT\n"
94 "*mangle\n"
95 ":bw_mangle_POSTROUTING -\n"
96 "COMMIT\n\x04";
97 std::string expectedAccounting =
98 "*filter\n"
99 "-A bw_INPUT -m owner --socket-exists\n"
100 "-A bw_OUTPUT -m owner --socket-exists\n"
101 "-A bw_costly_shared --jump bw_penalty_box\n"
102 "-A bw_penalty_box --jump bw_happy_box\n"
103 "-A bw_happy_box --jump bw_data_saver\n"
104 "-A bw_data_saver -j RETURN\n"
105 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
106 "COMMIT\n"
107 "*raw\n"
108 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
109 "COMMIT\n"
110 "*mangle\n"
111 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
112 "COMMIT\n\x04";
113
114 expectIptablesRestoreCommands({ expectedFlush, expectedAccounting });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900115}
116
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900117TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
118 mBw.disableBandwidthControl();
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900119 const std::string expected =
120 "*filter\n"
121 ":bw_INPUT -\n"
122 ":bw_OUTPUT -\n"
123 ":bw_FORWARD -\n"
124 ":bw_happy_box -\n"
125 ":bw_penalty_box -\n"
126 ":bw_data_saver -\n"
127 ":bw_costly_shared -\n"
128 "COMMIT\n"
129 "*raw\n"
130 ":bw_raw_PREROUTING -\n"
131 "COMMIT\n"
132 "*mangle\n"
133 ":bw_mangle_POSTROUTING -\n"
134 "COMMIT\n\x04";
135 expectIptablesRestoreCommands({ expected });
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900136}
137
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900138TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
139 mBw.enableDataSaver(true);
140 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900141 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900142 };
143 expectIptablesCommands(expected);
144
145 mBw.enableDataSaver(false);
146 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900147 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900148 };
149 expectIptablesCommands(expected);
150}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900151
152std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
153 "Chain natctrl_tether_counters (4 references)",
154 " pkts bytes target prot opt in out source destination",
155 " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
156 " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
157 " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
158 " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
159}, '\n');
160
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900161std::string kIPv6TetherCounters = android::base::Join(std::vector<std::string> {
162 "Chain natctrl_tether_counters (2 references)",
163 " pkts bytes target prot opt in out source destination",
164 " 10000 10000000 RETURN all wlan0 rmnet0 ::/0 ::/0",
165 " 20000 20000000 RETURN all rmnet0 wlan0 ::/0 ::/0",
166}, '\n');
167
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900168std::string readSocketClientResponse(int fd) {
169 char buf[32768];
170 ssize_t bytesRead = read(fd, buf, sizeof(buf));
171 if (bytesRead < 0) {
172 return "";
173 }
174 for (int i = 0; i < bytesRead; i++) {
175 if (buf[i] == '\0') buf[i] = '\n';
176 }
177 return std::string(buf, bytesRead);
178}
179
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900180void expectNoSocketClientResponse(int fd) {
181 char buf[64];
182 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
183}
184
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900185TEST_F(BandwidthControllerTest, TestGetTetherStats) {
186 int socketPair[2];
187 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
188 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
189 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
190 SocketClient cli(socketPair[0], false);
191
192 std::string err;
193 BandwidthController::TetherStats filter;
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900194
195 // If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
196 addPopenContents(kIPv4TetherCounters, "");
197 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
198 expectNoSocketClientResponse(socketPair[1]);
199 clearPopenContents();
200
201 addPopenContents("", kIPv6TetherCounters);
202 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
203 clearPopenContents();
204
205 // IPv4 and IPv6 counters are properly added together.
206 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
207 filter = BandwidthController::TetherStats();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900208 std::string expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900209 "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900210 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
211 "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900212 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900213 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900214 expectNoSocketClientResponse(socketPair[1]);
215 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900216
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900217 // Test filtering.
218 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900219 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
220 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900221 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900222 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900223 expectNoSocketClientResponse(socketPair[1]);
224 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900225
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900226 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
227 filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
228 expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900229 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900230 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900231 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900232
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900233 // Select nonexistent interfaces.
234 addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900235 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
236 expected = "200 Tethering stats list completed\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900237 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900238 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900239 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900240
241 // No stats with a filter: no error.
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900242 addPopenContents("", "");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900243 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
244 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900245 clearPopenContents();
246
247 addPopenContents("foo", "foo");
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900248 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
249 ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900250 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900251
252 // No stats and empty filter: error.
253 filter = BandwidthController::TetherStats();
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900254 addPopenContents("", kIPv6TetherCounters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900255 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
256 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900257 clearPopenContents();
258
259 addPopenContents(kIPv4TetherCounters, "");
260 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
261 expectNoSocketClientResponse(socketPair[1]);
262 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900263
264 // Include only one pair of interfaces and things are fine.
265 std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
266 std::vector<std::string> brokenCounterLines = counterLines;
267 counterLines.resize(4);
268 std::string counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900269 addPopenContents(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900270 expected =
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900271 "114 wlan0 rmnet0 4746 52 4004 54\n"
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900272 "200 Tethering stats list completed\n";
273 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
274 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900275 clearPopenContents();
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900276
277 // But if interfaces aren't paired, it's always an error.
278 counterLines.resize(3);
279 counters = android::base::Join(counterLines, "\n") + "\n";
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900280 addPopenContents(counters, counters);
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900281 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
282 expectNoSocketClientResponse(socketPair[1]);
Lorenzo Colitti26c91322016-07-11 11:36:25 +0900283 clearPopenContents();
284
285 // popen() failing is always an error.
286 addPopenContents(kIPv4TetherCounters);
287 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
288 expectNoSocketClientResponse(socketPair[1]);
289 clearPopenContents();
290 addPopenContents(kIPv6TetherCounters);
291 ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
292 expectNoSocketClientResponse(socketPair[1]);
293 clearPopenContents();
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900294}