blob: aa245831dfc2c1ccaa4b652ce7cc546e9d1efc09 [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
22#include <gtest/gtest.h>
23
Lorenzo Colitti13debb82016-03-27 17:46:30 +090024#include <android-base/strings.h>
25
Lorenzo Colitti86a47982016-03-18 17:52:25 +090026#include "BandwidthController.h"
Lorenzo Colitti0f150552016-03-28 02:30:27 +090027#include "IptablesBaseTest.h"
Lorenzo Colitti86a47982016-03-18 17:52:25 +090028
29FILE *fake_popen(const char *, const char *) {
30 return NULL;
31};
32
Lorenzo Colitti0f150552016-03-28 02:30:27 +090033class BandwidthControllerTest : public IptablesBaseTest {
Lorenzo Colitti86a47982016-03-18 17:52:25 +090034public:
35 BandwidthControllerTest() {
36 BandwidthController::execFunction = fake_android_fork_exec;
37 BandwidthController::popenFunction = fake_popen;
Lorenzo Colitti13debb82016-03-27 17:46:30 +090038 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
Lorenzo Colitti86a47982016-03-18 17:52:25 +090039 }
40 BandwidthController mBw;
41};
42
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090043TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
44 mBw.setupIptablesHooks();
45 std::vector<std::string> expected = {
Lorenzo Colitti13debb82016-03-27 17:46:30 +090046 "*filter\n"
47 ":bw_INPUT -\n"
48 ":bw_OUTPUT -\n"
49 ":bw_FORWARD -\n"
50 ":bw_happy_box -\n"
51 ":bw_penalty_box -\n"
52 ":bw_data_saver -\n"
53 ":bw_costly_shared -\n"
54 "COMMIT\n"
55 "*raw\n"
56 ":bw_raw_PREROUTING -\n"
57 "COMMIT\n"
58 "*mangle\n"
59 ":bw_mangle_POSTROUTING -\n"
60 "COMMIT\n\x04"
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090061 };
Lorenzo Colitti13debb82016-03-27 17:46:30 +090062 expectIptablesRestoreCommands(expected);
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +090063}
64
Lorenzo Colitti86a47982016-03-18 17:52:25 +090065TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
66 mBw.enableBandwidthControl(false);
Lorenzo Colitti13debb82016-03-27 17:46:30 +090067 std::string expectedFlush =
68 "*filter\n"
69 ":bw_INPUT -\n"
70 ":bw_OUTPUT -\n"
71 ":bw_FORWARD -\n"
72 ":bw_happy_box -\n"
73 ":bw_penalty_box -\n"
74 ":bw_data_saver -\n"
75 ":bw_costly_shared -\n"
76 "COMMIT\n"
77 "*raw\n"
78 ":bw_raw_PREROUTING -\n"
79 "COMMIT\n"
80 "*mangle\n"
81 ":bw_mangle_POSTROUTING -\n"
82 "COMMIT\n\x04";
83 std::string expectedAccounting =
84 "*filter\n"
85 "-A bw_INPUT -m owner --socket-exists\n"
86 "-A bw_OUTPUT -m owner --socket-exists\n"
87 "-A bw_costly_shared --jump bw_penalty_box\n"
88 "-A bw_penalty_box --jump bw_happy_box\n"
89 "-A bw_happy_box --jump bw_data_saver\n"
90 "-A bw_data_saver -j RETURN\n"
91 "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n"
92 "COMMIT\n"
93 "*raw\n"
94 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
95 "COMMIT\n"
96 "*mangle\n"
97 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
98 "COMMIT\n\x04";
99
100 expectIptablesRestoreCommands({ expectedFlush, expectedAccounting });
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900101}
102
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900103TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
104 mBw.disableBandwidthControl();
Lorenzo Colitti13debb82016-03-27 17:46:30 +0900105 const std::string expected =
106 "*filter\n"
107 ":bw_INPUT -\n"
108 ":bw_OUTPUT -\n"
109 ":bw_FORWARD -\n"
110 ":bw_happy_box -\n"
111 ":bw_penalty_box -\n"
112 ":bw_data_saver -\n"
113 ":bw_costly_shared -\n"
114 "COMMIT\n"
115 "*raw\n"
116 ":bw_raw_PREROUTING -\n"
117 "COMMIT\n"
118 "*mangle\n"
119 ":bw_mangle_POSTROUTING -\n"
120 "COMMIT\n\x04";
121 expectIptablesRestoreCommands({ expected });
Lorenzo Colittia0dc8a52016-03-26 22:42:07 +0900122}
123
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900124TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
125 mBw.enableDataSaver(true);
126 std::vector<std::string> expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900127 "-R bw_data_saver 1 --jump REJECT",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900128 };
129 expectIptablesCommands(expected);
130
131 mBw.enableDataSaver(false);
132 expected = {
Lorenzo Colitti464eabe2016-03-25 13:38:19 +0900133 "-R bw_data_saver 1 --jump RETURN",
Lorenzo Colitti86a47982016-03-18 17:52:25 +0900134 };
135 expectIptablesCommands(expected);
136}