blob: 9a60402db9390f79a7d09b210becb815bbe92199 [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 Colitti86a47982016-03-18 17:52:25 +090046};
47
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090048TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
49 mBw.setupIptablesHooks();
50 std::vector<std::string> expected = {
Lorenzo Colitti13debb82016-03-27 17:46:30 +090051 "*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 Colittia0dc8a52016-03-26 22:42:07 +090066 };
Lorenzo Colitti13debb82016-03-27 17:46:30 +090067 expectIptablesRestoreCommands(expected);
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090068}
69
Lorenzo Colitti86a47982016-03-18 17:52:25 +090070TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
71 mBw.enableBandwidthControl(false);
Lorenzo Colitti13debb82016-03-27 17:46:30 +090072 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 Colitti86a47982016-03-18 17:52:25 +0900106}
107
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900108TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
109 mBw.disableBandwidthControl();
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900110 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 Colittia0dc8a52016-03-26 22:42:07 +0900127}
128
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900129TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
130 mBw.enableDataSaver(true);
131 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900132 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900133 };
134 expectIptablesCommands(expected);
135
136 mBw.enableDataSaver(false);
137 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900138 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900139 };
140 expectIptablesCommands(expected);
141}
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900142
143std::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
152std::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 Colitti750e8fc2016-07-12 01:19:49 +0900164void expectNoSocketClientResponse(int fd) {
165 char buf[64];
166 EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
167}
168
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900169TEST_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 Colitti750e8fc2016-07-12 01:19:49 +0900183 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900184 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 Colitti750e8fc2016-07-12 01:19:49 +0900189 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900190 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
191
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900192 addPopenContents(kIPv4TetherCounters);
193 filter = BandwidthController::TetherStats("rmnet0", "wlan0", -1, -1, -1, -1);
194 expected = "221 rmnet0 wlan0 2002 27 2373 26\n";
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900195 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900196 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 Colitti750e8fc2016-07-12 01:19:49 +0900201 ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
Lorenzo Colittibbeaf9a2016-07-08 18:24:26 +0900202 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
Lorenzo Colitti750e8fc2016-07-12 01:19:49 +0900203
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 Colittibbeaf9a2016-07-08 18:24:26 +0900237}