1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker * Copyright 2017 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker *
4*8542734aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker *
8*8542734aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker *
10*8542734aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker *
16*8542734aSAndroid Build Coastguard Worker * IdletimerControllerTest.cpp - unit tests for IdletimerController.cpp
17*8542734aSAndroid Build Coastguard Worker */
18*8542734aSAndroid Build Coastguard Worker
19*8542734aSAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8542734aSAndroid Build Coastguard Worker
21*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
22*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
23*8542734aSAndroid Build Coastguard Worker
24*8542734aSAndroid Build Coastguard Worker #include "IdletimerController.h"
25*8542734aSAndroid Build Coastguard Worker #include "IptablesBaseTest.h"
26*8542734aSAndroid Build Coastguard Worker
27*8542734aSAndroid Build Coastguard Worker using android::base::Join;
28*8542734aSAndroid Build Coastguard Worker using android::base::StringPrintf;
29*8542734aSAndroid Build Coastguard Worker
30*8542734aSAndroid Build Coastguard Worker class IdletimerControllerTest : public IptablesBaseTest {
31*8542734aSAndroid Build Coastguard Worker protected:
IdletimerControllerTest()32*8542734aSAndroid Build Coastguard Worker IdletimerControllerTest() {
33*8542734aSAndroid Build Coastguard Worker IdletimerController::execIptablesRestore = fakeExecIptablesRestore;
34*8542734aSAndroid Build Coastguard Worker }
35*8542734aSAndroid Build Coastguard Worker IdletimerController mIt;
36*8542734aSAndroid Build Coastguard Worker };
37*8542734aSAndroid Build Coastguard Worker
TEST_F(IdletimerControllerTest,TestSetupIptablesHooks)38*8542734aSAndroid Build Coastguard Worker TEST_F(IdletimerControllerTest, TestSetupIptablesHooks) {
39*8542734aSAndroid Build Coastguard Worker mIt.setupIptablesHooks();
40*8542734aSAndroid Build Coastguard Worker expectIptablesRestoreCommands(ExpectedIptablesCommands{});
41*8542734aSAndroid Build Coastguard Worker }
42*8542734aSAndroid Build Coastguard Worker
makeAddRemoveCommands(bool add)43*8542734aSAndroid Build Coastguard Worker const std::vector<std::string> makeAddRemoveCommands(bool add) {
44*8542734aSAndroid Build Coastguard Worker const char *op = add ? "-A" : "-D";
45*8542734aSAndroid Build Coastguard Worker std::vector<std::string> cmds = {
46*8542734aSAndroid Build Coastguard Worker "*raw",
47*8542734aSAndroid Build Coastguard Worker StringPrintf("%s idletimer_raw_PREROUTING -i wlan0 -j IDLETIMER"
48*8542734aSAndroid Build Coastguard Worker " --timeout 12345 --label hello --send_nl_msg",
49*8542734aSAndroid Build Coastguard Worker op),
50*8542734aSAndroid Build Coastguard Worker "COMMIT",
51*8542734aSAndroid Build Coastguard Worker "*mangle",
52*8542734aSAndroid Build Coastguard Worker StringPrintf("%s idletimer_mangle_POSTROUTING -o wlan0 -j IDLETIMER"
53*8542734aSAndroid Build Coastguard Worker " --timeout 12345 --label hello --send_nl_msg",
54*8542734aSAndroid Build Coastguard Worker op),
55*8542734aSAndroid Build Coastguard Worker "COMMIT\n",
56*8542734aSAndroid Build Coastguard Worker };
57*8542734aSAndroid Build Coastguard Worker return { Join(cmds, '\n') };
58*8542734aSAndroid Build Coastguard Worker }
59*8542734aSAndroid Build Coastguard Worker
TEST_F(IdletimerControllerTest,TestAddRemove)60*8542734aSAndroid Build Coastguard Worker TEST_F(IdletimerControllerTest, TestAddRemove) {
61*8542734aSAndroid Build Coastguard Worker auto expected = makeAddRemoveCommands(true);
62*8542734aSAndroid Build Coastguard Worker mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
63*8542734aSAndroid Build Coastguard Worker expectIptablesRestoreCommands(expected);
64*8542734aSAndroid Build Coastguard Worker
65*8542734aSAndroid Build Coastguard Worker mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
66*8542734aSAndroid Build Coastguard Worker expectIptablesRestoreCommands(expected);
67*8542734aSAndroid Build Coastguard Worker
68*8542734aSAndroid Build Coastguard Worker expected = makeAddRemoveCommands(false);
69*8542734aSAndroid Build Coastguard Worker mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
70*8542734aSAndroid Build Coastguard Worker expectIptablesRestoreCommands(expected);
71*8542734aSAndroid Build Coastguard Worker
72*8542734aSAndroid Build Coastguard Worker mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
73*8542734aSAndroid Build Coastguard Worker expectIptablesRestoreCommands(expected);
74*8542734aSAndroid Build Coastguard Worker }
75