blob: 83edce38affa6d88e65f52a7740799eb699aa12f [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "OmapiVtsUtil.h"
#include <VtsCoreUtil.h>
#include <android-base/properties.h>
#include <gtest/gtest.h>
#include <unistd.h>
#include <android-base/logging.h>
#include <stdio.h>
#include <stdlib.h>
namespace android {
namespace se {
namespace omapi {
namespace vts {
const std::string ESE_READER_PREFIX = "eSE";
const std::string FEATURE_SE_OMAPI_ESE = "android.hardware.se.omapi.ese";
const std::string FEATURE_SE_LOW_RAM = "ro.config.low_ram";
const std::string FEATURE_SE_HARDWARE_WATCH = "android.hardware.type.watch";
const std::string PKG_SE_OMAPI_SERVICE = "com.android.se";
const std::string FEATURE_SE_SDK_VERSION = "ro.build.version.sdk";
const std::string FEATURE_SE_API_LEVEL = "ro.product.first_api_level";
bool deviceSupportsPackage(const char* package) {
std::string cmd = "/system/bin/pm list packages " + std::string(package);
// Note: We do not use ::testing::checkSubstringInCommandOutput(cmd.c_str(), package)
// because it performs a substring search, which can lead to false positives
// (e.g., matching "com.android.server.other" when we only want exactly "com.android.se").
// Instead, we read the output and perform an exact match on the package name.
bool hasPackage = false;
FILE* p = popen(cmd.c_str(), "re");
if (p) {
char* line = NULL;
size_t len = 0;
std::string expected = "package:" + std::string(package);
while (getline(&line, &len, p) > 0) {
std::string lineStr(line);
while (!lineStr.empty() && (lineStr.back() == '\n' || lineStr.back() == '\r')) {
lineStr.pop_back();
}
if (lineStr == expected) {
hasPackage = true;
break;
}
}
free(line);
pclose(p);
} else {
PLOG(ERROR) << "popen failed";
return false;
}
LOG(INFO) << "Package " << package << ": " << (hasPackage ? "" : "not ") << "present";
return hasPackage;
}
bool supportsHardware() {
bool lowRamDevice = android::base::GetBoolProperty(FEATURE_SE_LOW_RAM, true);
return !lowRamDevice || ::testing::deviceSupportsFeature(FEATURE_SE_HARDWARE_WATCH.c_str()) ||
deviceSupportsPackage(PKG_SE_OMAPI_SERVICE.c_str());
}
bool supportOMAPIReaders() {
return ::testing::deviceSupportsFeature(FEATURE_SE_OMAPI_ESE.c_str());
}
bool getFirstApiLevel(int32_t* outApiLevel) {
int32_t firstApiLevel = android::base::GetIntProperty(FEATURE_SE_API_LEVEL, -1);
if (firstApiLevel <= 0) {
firstApiLevel = android::base::GetIntProperty(FEATURE_SE_SDK_VERSION, -1);
}
if (firstApiLevel <= 0) {
return false;
}
*outApiLevel = firstApiLevel;
return true;
}
bool isVintfDisabledViaDumpsys() {
std::string cmd = "/system/bin/dumpsys android.se.omapi.ISecureElementService/default";
FILE* p = popen(cmd.c_str(), "re");
if (!p) {
return false;
}
char* line = NULL;
size_t len = 0;
bool disabled = false;
while (getline(&line, &len, p) > 0) {
std::string lineStr(line);
if (lineStr.find("mSecureElementVintfEnabled: false") != std::string::npos) {
disabled = true;
break;
}
}
free(line);
pclose(p);
return disabled;
}
bool isDummySecureElement() {
std::string seApex = android::base::GetProperty(
"ro.boot.vendor.apex.com.android.hardware.secure_element", "");
return seApex == "com.android.hardware.secure_element";
}
} // namespace vts
} // namespace omapi
} // namespace se
} // namespace android