blob: 51f19e3fd5472acbd184e6033784288697feca8d [file] [log] [blame]
Lorenzo Colitti85a21602017-08-10 19:22:45 +09001/*
2 * Copyright 2017 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 * IdletimerControllerTest.cpp - unit tests for IdletimerController.cpp
17 */
18
19#include <gtest/gtest.h>
20
21#include <android-base/strings.h>
22#include <android-base/stringprintf.h>
23
24#include "IdletimerController.h"
25#include "IptablesBaseTest.h"
26
27using android::base::StringPrintf;
28
29class IdletimerControllerTest : public IptablesBaseTest {
30protected:
31 IdletimerControllerTest() {
32 IdletimerController::execFunction = fake_android_fork_exec;
33 }
34 IdletimerController mIt;
35};
36
37TEST_F(IdletimerControllerTest, TestSetupIptablesHooks) {
38 mIt.setupIptablesHooks();
39 expectIptablesCommands(ExpectedIptablesCommands{});
40 expectIptablesRestoreCommands(ExpectedIptablesCommands{});
41}
42
43TEST_F(IdletimerControllerTest, TestEnableDisable) {
44 std::vector<std::string> expected = {
45 "-t raw -F idletimer_raw_PREROUTING",
46 "-t mangle -F idletimer_mangle_POSTROUTING",
47 };
48
49 mIt.enableIdletimerControl();
50 expectIptablesCommands(expected);
51
52 mIt.enableIdletimerControl();
53 expectIptablesCommands(expected);
54
55 mIt.disableIdletimerControl();
56 expectIptablesCommands(expected);
57
58 mIt.disableIdletimerControl();
59 expectIptablesCommands(expected);
60}
61
62const std::vector<std::string> makeAddRemoveCommands(bool add) {
63 const char *op = add ? "-A" : "-D";
64 return {
65 StringPrintf("-t raw %s idletimer_raw_PREROUTING -i wlan0 -j IDLETIMER"
66 " --timeout 12345 --label hello --send_nl_msg 1", op),
67 StringPrintf("-t mangle %s idletimer_mangle_POSTROUTING -o wlan0 -j IDLETIMER"
68 " --timeout 12345 --label hello --send_nl_msg 1", op),
69 };
70}
71
72TEST_F(IdletimerControllerTest, TestAddRemove) {
73 auto expected = makeAddRemoveCommands(true);
74 mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
75 expectIptablesCommands(expected);
76
77 mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
78 expectIptablesCommands(expected);
79
80 expected = makeAddRemoveCommands(false);
81 mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
82 expectIptablesCommands(expected);
83
84 mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
85 expectIptablesCommands(expected);
86}