• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
21 
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 
27 #include <gtest/gtest.h>
28 
29 #include <android-base/strings.h>
30 
31 #include "BandwidthController.h"
32 #include "IptablesBaseTest.h"
33 
34 class BandwidthControllerTest : public IptablesBaseTest {
35 public:
BandwidthControllerTest()36     BandwidthControllerTest() {
37         BandwidthController::execFunction = fake_android_fork_exec;
38         BandwidthController::popenFunction = fake_popen;
39         BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
40     }
41     BandwidthController mBw;
42 
addPopenContents(std::string contents)43     void addPopenContents(std::string contents) {
44         sPopenContents.push_back(contents);
45     }
46 
addPopenContents(std::string contents1,std::string contents2)47     void addPopenContents(std::string contents1, std::string contents2) {
48         sPopenContents.push_back(contents1);
49         sPopenContents.push_back(contents2);
50     }
51 
clearPopenContents()52     void clearPopenContents() {
53         sPopenContents.clear();
54     }
55 };
56 
TEST_F(BandwidthControllerTest,TestSetupIptablesHooks)57 TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
58     mBw.setupIptablesHooks();
59     std::vector<std::string> expected = {
60         "*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"
75     };
76     expectIptablesRestoreCommands(expected);
77 }
78 
TEST_F(BandwidthControllerTest,TestEnableBandwidthControl)79 TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
80     mBw.enableBandwidthControl(false);
81     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 });
115 }
116 
TEST_F(BandwidthControllerTest,TestDisableBandwidthControl)117 TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
118     mBw.disableBandwidthControl();
119     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 });
136 }
137 
TEST_F(BandwidthControllerTest,TestEnableDataSaver)138 TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
139     mBw.enableDataSaver(true);
140     std::vector<std::string> expected = {
141         "-R bw_data_saver 1 --jump REJECT",
142     };
143     expectIptablesCommands(expected);
144 
145     mBw.enableDataSaver(false);
146     expected = {
147         "-R bw_data_saver 1 --jump RETURN",
148     };
149     expectIptablesCommands(expected);
150 }
151 
152 std::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 
161 std::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 
readSocketClientResponse(int fd)168 std::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 
expectNoSocketClientResponse(int fd)180 void expectNoSocketClientResponse(int fd) {
181     char buf[64];
182     EXPECT_EQ(-1, read(fd, buf, sizeof(buf)));
183 }
184 
TEST_F(BandwidthControllerTest,TestGetTetherStats)185 TEST_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;
194 
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();
208     std::string expected =
209             "114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
210             "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
211             "200 Tethering stats list completed\n";
212     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
213     ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
214     expectNoSocketClientResponse(socketPair[1]);
215     clearPopenContents();
216 
217     // Test filtering.
218     addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
219     filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
220     expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
221     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
222     ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
223     expectNoSocketClientResponse(socketPair[1]);
224     clearPopenContents();
225 
226     addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
227     filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
228     expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
229     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
230     ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
231     clearPopenContents();
232 
233     // Select nonexistent interfaces.
234     addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
235     filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
236     expected = "200 Tethering stats list completed\n";
237     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
238     ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
239     clearPopenContents();
240 
241     // No stats with a filter: no error.
242     addPopenContents("", "");
243     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
244     ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
245     clearPopenContents();
246 
247     addPopenContents("foo", "foo");
248     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
249     ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
250     clearPopenContents();
251 
252     // No stats and empty filter: error.
253     filter = BandwidthController::TetherStats();
254     addPopenContents("", kIPv6TetherCounters);
255     ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
256     expectNoSocketClientResponse(socketPair[1]);
257     clearPopenContents();
258 
259     addPopenContents(kIPv4TetherCounters, "");
260     ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
261     expectNoSocketClientResponse(socketPair[1]);
262     clearPopenContents();
263 
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";
269     addPopenContents(counters, counters);
270     expected =
271             "114 wlan0 rmnet0 4746 52 4004 54\n"
272             "200 Tethering stats list completed\n";
273     ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
274     ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
275     clearPopenContents();
276 
277     // But if interfaces aren't paired, it's always an error.
278     counterLines.resize(3);
279     counters = android::base::Join(counterLines, "\n") + "\n";
280     addPopenContents(counters, counters);
281     ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
282     expectNoSocketClientResponse(socketPair[1]);
283     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();
294 }
295