2 * Copyright 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * BandwidthControllerTest.cpp - unit tests for BandwidthController.cpp
22 #include <gtest/gtest.h>
24 #include <android-base/strings.h>
26 #include "BandwidthController.h"
27 #include "IptablesBaseTest.h"
29 FILE *fake_popen(const char *, const char *) {
33 class BandwidthControllerTest : public IptablesBaseTest {
35 BandwidthControllerTest() {
36 BandwidthController::execFunction = fake_android_fork_exec;
37 BandwidthController::popenFunction = fake_popen;
38 BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
40 BandwidthController mBw;
43 TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
44 mBw.setupIptablesHooks();
45 std::vector<std::string> expected = {
53 ":bw_costly_shared -\n"
56 ":bw_raw_PREROUTING -\n"
59 ":bw_mangle_POSTROUTING -\n"
62 expectIptablesRestoreCommands(expected);
65 TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) {
66 mBw.enableBandwidthControl(false);
67 std::string expectedFlush =
75 ":bw_costly_shared -\n"
78 ":bw_raw_PREROUTING -\n"
81 ":bw_mangle_POSTROUTING -\n"
83 std::string expectedAccounting =
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"
94 "-A bw_raw_PREROUTING -m owner --socket-exists\n"
97 "-A bw_mangle_POSTROUTING -m owner --socket-exists\n"
100 expectIptablesRestoreCommands({ expectedFlush, expectedAccounting });
103 TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) {
104 mBw.disableBandwidthControl();
105 const std::string expected =
111 ":bw_penalty_box -\n"
113 ":bw_costly_shared -\n"
116 ":bw_raw_PREROUTING -\n"
119 ":bw_mangle_POSTROUTING -\n"
121 expectIptablesRestoreCommands({ expected });
124 TEST_F(BandwidthControllerTest, TestEnableDataSaver) {
125 mBw.enableDataSaver(true);
126 std::vector<std::string> expected = {
127 "-R bw_data_saver 1 --jump REJECT",
129 expectIptablesCommands(expected);
131 mBw.enableDataSaver(false);
133 "-R bw_data_saver 1 --jump RETURN",
135 expectIptablesCommands(expected);