blob: 5af974968a33bf2bd3d9281792ed44b379f3a282 [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
164TEST_F(BandwidthControllerTest, TestGetTetherStats) {
165 int socketPair[2];
166 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
167 ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
168 ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
169 SocketClient cli(socketPair[0], false);
170
171 std::string err;
172 BandwidthController::TetherStats filter;
173 addPopenContents(kIPv4TetherCounters);
174 std::string expected =
175 "114 wlan0 rmnet0 2373 26 2002 27\n"
176 "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
177 "200 Tethering stats list completed\n";
178 mBw.getTetherStats(&cli, filter, err);
179 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
180
181 addPopenContents(kIPv4TetherCounters);
182 filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
183 expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
184 mBw.getTetherStats(&cli, filter, err);
185 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
186
187
188 addPopenContents(kIPv4TetherCounters);
189 filter = BandwidthController::TetherStats("rmnet0", "wlan0", -1, -1, -1, -1);
190 expected = "221 rmnet0 wlan0 2002 27 2373 26\n";
191 mBw.getTetherStats(&cli, filter, err);
192 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
193
194 addPopenContents(kIPv4TetherCounters);
195 filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
196 expected = "200 Tethering stats list completed\n";
197 mBw.getTetherStats(&cli, filter, err);
198 ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
199}