• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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  * test_utils.cpp - miscellaneous unit test utilities.
17  */
18 
19 #include <cstdio>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/stringprintf.h>
24 
25 #include "test_utils.h"
26 
27 #define IP_PATH "/system/bin/ip"
28 
29 using android::base::StringPrintf;
30 
randomUid()31 int randomUid() {
32     // Pick a random UID consisting of:
33     // - Random user profile (0 - 6)
34     // - Random app ID starting from 12000 (FIRST_APPLICATION_UID + 2000). This ensures no conflicts
35     //   with existing app UIDs unless the user has installed more than 2000 apps, and is still less
36     //   than LAST_APPLICATION_UID (19999).
37     return 100000 * arc4random_uniform(7) + 12000 + arc4random_uniform(3000);
38 }
39 
runCommand(const std::string & command)40 std::vector<std::string> runCommand(const std::string& command) {
41     std::vector<std::string> lines;
42     FILE* f = popen(command.c_str(), "r");  // NOLINT(cert-env33-c)
43     if (f == nullptr) {
44         perror("popen");
45         return lines;
46     }
47 
48     char* line = nullptr;
49     size_t bufsize = 0;
50     ssize_t linelen = 0;
51     while ((linelen = getline(&line, &bufsize, f)) >= 0) {
52         std::string str = std::string(line, linelen);
53         const size_t lastNotWhitespace = str.find_last_not_of(" \t\n\r");
54         if (lastNotWhitespace != std::string::npos) {
55             str = str.substr(0, lastNotWhitespace + 1);
56         }
57         lines.push_back(str);
58         free(line);
59         line = nullptr;
60     }
61 
62     pclose(f);
63     return lines;
64 }
65 
listIpRules(const char * ipVersion)66 std::vector<std::string> listIpRules(const char* ipVersion) {
67     std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
68     return runCommand(command);
69 }
70 
listIptablesRule(const char * binary,const char * chainName)71 std::vector<std::string> listIptablesRule(const char* binary, const char* chainName) {
72     std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
73     return runCommand(command);
74 }
75 
iptablesRuleLineLength(const char * binary,const char * chainName)76 int iptablesRuleLineLength(const char* binary, const char* chainName) {
77     return listIptablesRule(binary, chainName).size();
78 }
79 
iptablesRuleExists(const char * binary,const char * chainName,const std::string & expectedRule)80 bool iptablesRuleExists(const char* binary, const char* chainName,
81                         const std::string& expectedRule) {
82     std::vector<std::string> rules = listIptablesRule(binary, chainName);
83     for (const auto& rule : rules) {
84         if (rule.find(expectedRule) != std::string::npos) {
85             return true;
86         }
87     }
88     return false;
89 }
90 
listIpRoutes(const char * ipVersion,const char * table)91 std::vector<std::string> listIpRoutes(const char* ipVersion, const char* table) {
92     std::string command = StringPrintf("%s %s route ls table %s", IP_PATH, ipVersion, table);
93     return runCommand(command);
94 }
95 
ipRouteExists(const char * ipVersion,const char * table,const std::vector<std::string> & ipRouteSubstrings)96 bool ipRouteExists(const char* ipVersion, const char* table,
97                    const std::vector<std::string>& ipRouteSubstrings) {
98     std::vector<std::string> routes = listIpRoutes(ipVersion, table);
99     for (const auto& route : routes) {
100         bool matched = true;
101         for (const auto& substring : ipRouteSubstrings) {
102             if (route.find(substring) == std::string::npos) {
103                 matched = false;
104                 break;
105             }
106         }
107 
108         if (matched) {
109             return true;
110         }
111     }
112     return false;
113 }
114