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 * FirewallControllerTest.cpp - unit tests for FirewallController.cpp
23 #include <gtest/gtest.h>
25 #include "FirewallController.h"
28 class FirewallControllerTest : public ::testing::Test {
30 FirewallController mFw;
31 std::string makeUidRules(const char *a, bool b, const std::vector<int32_t>& c) {
32 return mFw.makeUidRules(a, b, c);
37 TEST_F(FirewallControllerTest, TestWhitelist) {
38 std::string expected =
41 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
42 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
43 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
44 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
45 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
46 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
47 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
48 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
49 "-A FW_whitechain -j DROP\n"
52 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
53 EXPECT_EQ(expected, makeUidRules("FW_whitechain", true, uids));
56 TEST_F(FirewallControllerTest, TestBlacklist) {
57 std::string expected =
60 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
61 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
62 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
63 "-A FW_blackchain -j RETURN\n"
66 std::vector<int32_t> uids = { 10023, 10059, 10124 };
67 EXPECT_EQ(expected, makeUidRules("FW_blackchain", false, uids));