blob: 68d6108c865553135c1c19a888aad70254cb38fc [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
Lorenzo Colittiad46e762017-08-10 19:44:34 +090027using android::base::Join;
Lorenzo Colitti85a21602017-08-10 19:22:45 +090028using android::base::StringPrintf;
29
30class IdletimerControllerTest : public IptablesBaseTest {
31protected:
32 IdletimerControllerTest() {
Lorenzo Colittiad46e762017-08-10 19:44:34 +090033 IdletimerController::execIptablesRestore = fakeExecIptablesRestore;
Lorenzo Colitti85a21602017-08-10 19:22:45 +090034 }
35 IdletimerController mIt;
36};
37
38TEST_F(IdletimerControllerTest, TestSetupIptablesHooks) {
39 mIt.setupIptablesHooks();
Lorenzo Colitti85a21602017-08-10 19:22:45 +090040 expectIptablesRestoreCommands(ExpectedIptablesCommands{});
41}
42
Lorenzo Colitti85a21602017-08-10 19:22:45 +090043const std::vector<std::string> makeAddRemoveCommands(bool add) {
44 const char *op = add ? "-A" : "-D";
Lorenzo Colittiad46e762017-08-10 19:44:34 +090045 std::vector<std::string> cmds = {
Maciej Żenczykowski09d64602021-03-23 15:50:32 -070046 "*raw",
47 StringPrintf("%s idletimer_raw_PREROUTING -i wlan0 -j IDLETIMER"
48 " --timeout 12345 --label hello --send_nl_msg",
49 op),
50 "COMMIT",
51 "*mangle",
52 StringPrintf("%s idletimer_mangle_POSTROUTING -o wlan0 -j IDLETIMER"
53 " --timeout 12345 --label hello --send_nl_msg",
54 op),
55 "COMMIT\n",
Lorenzo Colitti85a21602017-08-10 19:22:45 +090056 };
Lorenzo Colittiad46e762017-08-10 19:44:34 +090057 return { Join(cmds, '\n') };
Lorenzo Colitti85a21602017-08-10 19:22:45 +090058}
59
60TEST_F(IdletimerControllerTest, TestAddRemove) {
61 auto expected = makeAddRemoveCommands(true);
62 mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
Lorenzo Colittiad46e762017-08-10 19:44:34 +090063 expectIptablesRestoreCommands(expected);
Lorenzo Colitti85a21602017-08-10 19:22:45 +090064
65 mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
Lorenzo Colittiad46e762017-08-10 19:44:34 +090066 expectIptablesRestoreCommands(expected);
Lorenzo Colitti85a21602017-08-10 19:22:45 +090067
68 expected = makeAddRemoveCommands(false);
69 mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
Lorenzo Colittiad46e762017-08-10 19:44:34 +090070 expectIptablesRestoreCommands(expected);
Lorenzo Colitti85a21602017-08-10 19:22:45 +090071
72 mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
Lorenzo Colittiad46e762017-08-10 19:44:34 +090073 expectIptablesRestoreCommands(expected);
Lorenzo Colitti85a21602017-08-10 19:22:45 +090074}