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