blob: ff9738e7f7fd4fb6e01c2b22c292f6102643634e [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>
21#include <stdio.h>
22
23#include <gtest/gtest.h>
24
Lorenzo Colitti13debb82016-03-27 17:46:30 +090025#include <android-base/strings.h>
26
Lorenzo Colitti86a47982016-03-18 17:52:25 +090027#include "BandwidthController.h"
28
29std::vector<std::string> gCmds = {};
Lorenzo Colitti13debb82016-03-27 17:46:30 +090030std::vector<std::string> gRestoreCmds = {};
Lorenzo Colitti86a47982016-03-18 17:52:25 +090031
Lorenzo Colitti464eabe2016-03-25 13:38:19 +090032int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090033 std::string cmd = argv[0];
34 for (int i = 1; i < argc; i++) {
35 cmd += " ";
36 cmd += argv[i];
37 }
38 gCmds.push_back(cmd);
39 *status = 0;
40 return 0;
41}
42
43FILE *fake_popen(const char *, const char *) {
44 return NULL;
45};
46
Lorenzo Colitti13debb82016-03-27 17:46:30 +090047int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
48 EXPECT_EQ(V4V6, target);
49 gRestoreCmds.push_back(commands);
50 return 0;
51}
52
Lorenzo Colitti86a47982016-03-18 17:52:25 +090053void expectIptablesCommands(std::vector<std::string> expectedCmds) {
54 EXPECT_EQ(expectedCmds.size() * 2, gCmds.size());
55 if (expectedCmds.size() * 2 != gCmds.size()) return;
56
57 for (size_t i = 0; i < expectedCmds.size(); i ++) {
58 EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], gCmds[2 * i]);
59 EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], gCmds[2 * i + 1]);
60 }
61
62 gCmds.clear();
63}
64
Lorenzo Colitti13debb82016-03-27 17:46:30 +090065void expectIptablesRestoreCommands(std::vector<std::string> expectedCmds) {
66 EXPECT_EQ(expectedCmds.size(), gRestoreCmds.size());
67 EXPECT_EQ(expectedCmds, gRestoreCmds);
68 gRestoreCmds.clear();
69}
70
Lorenzo Colitti86a47982016-03-18 17:52:25 +090071class BandwidthControllerTest : public ::testing::Test {
72public:
73 BandwidthControllerTest() {
74 BandwidthController::execFunction = fake_android_fork_exec;
75 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti13debb82016-03-27 17:46:30 +090076 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090077 gCmds.clear();
Lorenzo Colitti13debb82016-03-27 17:46:30 +090078 gRestoreCmds.clear();
Lorenzo Colitti86a47982016-03-18 17:52:25 +090079 }
80 BandwidthController mBw;
81};
82
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090083TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
84 mBw.setupIptablesHooks();
85 std::vector<std::string> expected = {
Lorenzo Colitti13debb82016-03-27 17:46:30 +090086 "*filter\n"
87 ":bw_INPUT -\n"
88 ":bw_OUTPUT -\n"
89 ":bw_FORWARD -\n"
90 ":bw_happy_box -\n"
91 ":bw_penalty_box -\n"
92 ":bw_data_saver -\n"
93 ":bw_costly_shared -\n"
94 "COMMIT\n"
95 "*raw\n"
96 ":bw_raw_PREROUTING -\n"
97 "COMMIT\n"
98 "*mangle\n"
99 ":bw_mangle_POSTROUTING -\n"
100 "COMMIT\n\x04"
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900101 };
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900102 expectIptablesRestoreCommands(expected);
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900103}
104
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900105TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
106 mBw.enableBandwidthControl(false);
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900107 std::string expectedFlush =
108 "*filter\n"
109 ":bw_INPUT -\n"
110 ":bw_OUTPUT -\n"
111 ":bw_FORWARD -\n"
112 ":bw_happy_box -\n"
113 ":bw_penalty_box -\n"
114 ":bw_data_saver -\n"
115 ":bw_costly_shared -\n"
116 "COMMIT\n"
117 "*raw\n"
118 ":bw_raw_PREROUTING -\n"
119 "COMMIT\n"
120 "*mangle\n"
121 ":bw_mangle_POSTROUTING -\n"
122 "COMMIT\n\x04";
123 std::string expectedAccounting =
124 "*filter\n"
125 "-A bw_INPUT -m owner --socket-exists\n"
126 "-A bw_OUTPUT -m owner --socket-exists\n"
127 "-A bw_costly_shared --jump bw_penalty_box\n"
128 "-A bw_penalty_box --jump bw_happy_box\n"
129 "-A bw_happy_box --jump bw_data_saver\n"
130 "-A bw_data_saver -j RETURN\n"
131 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
132 "COMMIT\n"
133 "*raw\n"
134 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
135 "COMMIT\n"
136 "*mangle\n"
137 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
138 "COMMIT\n\x04";
139
140 expectIptablesRestoreCommands({ expectedFlush, expectedAccounting });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900141}
142
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900143TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
144 mBw.disableBandwidthControl();
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900145 const std::string expected =
146 "*filter\n"
147 ":bw_INPUT -\n"
148 ":bw_OUTPUT -\n"
149 ":bw_FORWARD -\n"
150 ":bw_happy_box -\n"
151 ":bw_penalty_box -\n"
152 ":bw_data_saver -\n"
153 ":bw_costly_shared -\n"
154 "COMMIT\n"
155 "*raw\n"
156 ":bw_raw_PREROUTING -\n"
157 "COMMIT\n"
158 "*mangle\n"
159 ":bw_mangle_POSTROUTING -\n"
160 "COMMIT\n\x04";
161 expectIptablesRestoreCommands({ expected });
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900162}
163
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900164TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
165 mBw.enableDataSaver(true);
166 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900167 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900168 };
169 expectIptablesCommands(expected);
170
171 mBw.enableDataSaver(false);
172 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900173 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900174 };
175 expectIptablesCommands(expected);
176}