1 /*
2 * Copyright (C) 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
17 #include <string>
18 #include <vector>
19
20 #include <gtest/gtest.h>
21
22 #include <android-base/strings.h>
23
24 #include "NetUtilsWrapper.h"
25
26 #define MAX_ARGS 128
27 #define VALID true
28 #define INVALID false
29
30 struct Command {
31 bool valid;
32 std::string cmdString;
33 };
34
35 std::vector<Command> COMMANDS = {
36 {INVALID, "tc qdisc del dev root"},
37 {VALID, "/system/bin/tc qdisc del dev root"},
38 {VALID, "/system/bin/ip -6 addr add dev r_rmnet_data6 2001:db8::/64"},
39 {VALID, "/system/bin/ip -6 addr add dev wwan6 2001:db8::/64"},
40 {INVALID, "/system/bin/ip -6 addr add dev wlan2 2001:db8::/64"},
41 {VALID, "/system/bin/ip6tables -w -A INPUT -j qcom_foo"},
42 {INVALID, "/system/bin/ip6tables -w -A INPUT -j routectrl_MANGLE_INPUT"},
43 {VALID, "/system/bin/ip6tables -w -A INPUT -i rmnet_data9 -j routectrl_MANGLE_INPUT"},
44 {VALID, "/system/bin/ip6tables -w -F nm_pre_ip4"},
45 {INVALID, "/system/bin/ip6tables -w -F INPUT"},
46 {VALID, "/system/bin/ndc network interface add oem10"},
47 {VALID, "/system/bin/ndc network interface add oem10 v_oem9"},
48 {VALID, "/system/bin/ndc network interface add oem10 oem9"},
49 {INVALID, "/system/bin/ndc network interface add 100 v_oem9"},
50 {VALID, "/system/bin/ndc network interface add oem10 r_rmnet_data0"},
51 {VALID, "/system/bin/ndc network interface add oem10 wwan0"},
52 {VALID, "/system/bin/ndc network interface add handle42966108894 v_oem9"},
53 {VALID, "/system/bin/ndc network interface add handle42966108894 oem9"},
54 {VALID, "/system/bin/ndc network interface add handle42966108894 r_rmnet_data0"},
55 {VALID, "/system/bin/ndc network interface add handle42966108894 wwan0"},
56 {INVALID, "/system/bin/ndc network interface add handle42966108894"},
57 {VALID, "/system/bin/ip xfrm state"},
58 };
59
TEST(NetUtilsWrapperTest10,TestCommands)60 TEST(NetUtilsWrapperTest10, TestCommands) {
61 // Overwritten by each test case.
62 char *argv[MAX_ARGS];
63
64 for (const Command& cmd : COMMANDS) {
65 std::vector<std::string> pieces = android::base::Split(cmd.cmdString, " ");
66 ASSERT_LE(pieces.size(), ARRAY_SIZE(argv));
67 for (size_t i = 0; i < pieces.size(); i++) {
68 argv[i] = const_cast<char*>(pieces[i].c_str());
69 }
70 EXPECT_EQ(cmd.valid, checkExpectedCommand(pieces.size(), argv)) <<
71 "Expected command to be " <<
72 (cmd.valid ? "valid" : "invalid") << ", but was " <<
73 (cmd.valid ? "invalid" : "valid") << ": '" << cmd.cmdString << "'";
74 }
75 }
76