blob: 6d261eddf3d43a51796bfedce05b73299df526c1 [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
25#include "BandwidthController.h"
26
27std::vector<std::string> gCmds = {};
28
Lorenzo Colitti464eabe2016-03-25 13:38:19 +090029int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090030 std::string cmd = argv[0];
31 for (int i = 1; i < argc; i++) {
32 cmd += " ";
33 cmd += argv[i];
34 }
35 gCmds.push_back(cmd);
36 *status = 0;
37 return 0;
38}
39
40FILE *fake_popen(const char *, const char *) {
41 return NULL;
42};
43
44void expectIptablesCommands(std::vector<std::string> expectedCmds) {
45 EXPECT_EQ(expectedCmds.size() * 2, gCmds.size());
46 if (expectedCmds.size() * 2 != gCmds.size()) return;
47
48 for (size_t i = 0; i < expectedCmds.size(); i ++) {
49 EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], gCmds[2 * i]);
50 EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], gCmds[2 * i + 1]);
51 }
52
53 gCmds.clear();
54}
55
56class BandwidthControllerTest : public ::testing::Test {
57public:
58 BandwidthControllerTest() {
59 BandwidthController::execFunction = fake_android_fork_exec;
60 BandwidthController::popenFunction = fake_popen;
61 gCmds.clear();
62 }
63 BandwidthController mBw;
64};
65
66
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090067TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
68 mBw.setupIptablesHooks();
69 std::vector<std::string> expected = {
70 "-F bw_INPUT",
71 "-F bw_OUTPUT",
72 "-F bw_FORWARD",
73 "-F bw_happy_box",
74 "-F bw_penalty_box",
75 "-F bw_data_saver",
76 "-F bw_costly_shared",
77 "-t raw -F bw_raw_PREROUTING",
78 "-t mangle -F bw_mangle_POSTROUTING",
79 "-X bw_happy_box",
80 "-X bw_penalty_box",
81 "-X bw_data_saver",
82 "-X bw_costly_shared",
83 "-N bw_happy_box",
84 "-N bw_penalty_box",
85 "-N bw_data_saver",
86 "-N bw_costly_shared",
87 };
88 expectIptablesCommands(expected);
89}
90
Lorenzo Colitti86a47982016-03-18 17:52:25 +090091TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
92 mBw.enableBandwidthControl(false);
93 std::vector<std::string> expected = {
94 "-F bw_INPUT",
95 "-F bw_OUTPUT",
96 "-F bw_FORWARD",
97 "-F bw_happy_box",
98 "-F bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +090099 "-F bw_data_saver",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900100 "-F bw_costly_shared",
101 "-t raw -F bw_raw_PREROUTING",
102 "-t mangle -F bw_mangle_POSTROUTING",
103 "-A bw_INPUT -m owner --socket-exists",
104 "-A bw_OUTPUT -m owner --socket-exists",
105 "-t raw -A bw_raw_PREROUTING -m owner --socket-exists",
106 "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists",
107 "-A bw_costly_shared --jump bw_penalty_box",
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900108 "-A bw_penalty_box --jump bw_happy_box",
109 "-A bw_happy_box --jump bw_data_saver",
110 "-A bw_data_saver -j RETURN",
111 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900112 };
113 expectIptablesCommands(expected);
114}
115
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900116TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
117 mBw.disableBandwidthControl();
118 std::vector<std::string> expected = {
119 "-F bw_INPUT",
120 "-F bw_OUTPUT",
121 "-F bw_FORWARD",
122 "-F bw_happy_box",
123 "-F bw_penalty_box",
124 "-F bw_data_saver",
125 "-F bw_costly_shared",
126 "-t raw -F bw_raw_PREROUTING",
127 "-t mangle -F bw_mangle_POSTROUTING",
128 };
129 expectIptablesCommands(expected);
130}
131
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900132TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
133 mBw.enableDataSaver(true);
134 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900135 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900136 };
137 expectIptablesCommands(expected);
138
139 mBw.enableDataSaver(false);
140 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900141 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900142 };
143 expectIptablesCommands(expected);
144}