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         lines.push_back(std::string(line, linelen));
53         free(line);
54         line = nullptr;
55     }
56 
57     pclose(f);
58     return lines;
59 }
60 
listIpRules(const char * ipVersion)61 std::vector<std::string> listIpRules(const char* ipVersion) {
62     std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
63     return runCommand(command);
64 }
65 
listIptablesRule(const char * binary,const char * chainName)66 std::vector<std::string> listIptablesRule(const char* binary, const char* chainName) {
67     std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
68     return runCommand(command);
69 }
70 
iptablesRuleLineLength(const char * binary,const char * chainName)71 int iptablesRuleLineLength(const char* binary, const char* chainName) {
72     return listIptablesRule(binary, chainName).size();
73 }
74 
iptablesRuleExists(const char * binary,const char * chainName,const std::string & expectedRule)75 bool iptablesRuleExists(const char* binary, const char* chainName,
76                         const std::string& expectedRule) {
77     std::vector<std::string> rules = listIptablesRule(binary, chainName);
78     for (const auto& rule : rules) {
79         if (rule.find(expectedRule) != std::string::npos) {
80             return true;
81         }
82     }
83     return false;
84 }
85 
listIpRoutes(const char * ipVersion,const char * table)86 std::vector<std::string> listIpRoutes(const char* ipVersion, const char* table) {
87     std::string command = StringPrintf("%s %s route ls table %s", IP_PATH, ipVersion, table);
88     return runCommand(command);
89 }
90 
ipRouteExists(const char * ipVersion,const char * table,const std::string & ipRoute)91 bool ipRouteExists(const char* ipVersion, const char* table, const std::string& ipRoute) {
92     std::vector<std::string> routes = listIpRoutes(ipVersion, table);
93     for (const auto& route : routes) {
94         if (route.find(ipRoute) != std::string::npos) {
95             return true;
96         }
97     }
98     return false;
99 }
100