1 /*
2  * Copyright (C) 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 
17 #include <log/log.h>
18 #include <iostream>
19 
20 #include "VtsCoreUtil.h"
21 
22 namespace testing {
23 
24 // Runs "cmd" and attempts to find the specified feature in its
25 // output.
checkSubstringInCommandOutput(const char * cmd,const char * feature)26 bool checkSubstringInCommandOutput(const char* cmd, const char* feature) {
27   bool hasFeature = false;
28   // This is one of the best stable native interface. Calling AIDL directly
29   // would be problematic if the binder interface changes.
30   FILE* p = popen(cmd, "re");
31   if (p) {
32     char* line = NULL;
33     size_t len = 0;
34     __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
35                         "checkSubstringInCommandOutput check with cmd: %s",
36                         cmd);
37     while (getline(&line, &len, p) > 0) {
38       // TODO: b/148904287, check if we should match the whole line
39       if (strstr(line, feature)) {
40         hasFeature = true;
41         break;
42       }
43     }
44     pclose(p);
45   } else {
46     __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "popen failed: %d", errno);
47     _exit(EXIT_FAILURE);
48   }
49   __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Feature %s: %ssupported",
50                       feature, hasFeature ? "" : "not ");
51   return hasFeature;
52 }
53 
54 // Runs "pm list features" and attempts to find the specified feature in its
55 // output.
deviceSupportsFeature(const char * feature)56 bool deviceSupportsFeature(const char* feature) {
57   return checkSubstringInCommandOutput("/system/bin/pm list features", feature);
58 }
59 
60 }  // namespace testing
61