Open
test(serial): add SerialRightManager unit tests and code formatting #15
zgene94Add on May 6
test(serial): add SerialRightManager unit tests and code formatting #15
Open
Total: 16 files changed+1892-131
| @@ -30,6 +30,63 @@ config("serialsrv_public_config") { | |||
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | if (serial_manager_enable_service) { | 32 | if (serial_manager_enable_service) { |
| 33 | + # Source set for unit testing - allows tests to link source directly | ||
| 34 | + ohos_source_set("serialservice_src") { | ||
| 35 | + sanitize = { | ||
| 36 | + integer_overflow = true | ||
| 37 | + ubsan = true | ||
| 38 | + boundary_sanitize = true | ||
| 39 | + cfi = true | ||
| 40 | + cfi_cross_dso = true | ||
| 41 | + debug = false | ||
| 42 | + } | ||
| 43 | + branch_protector_ret = "pac_ret" | ||
| 44 | + | ||
| 45 | + testonly = true | ||
| 46 | + | ||
| 47 | + sources = [ | ||
| 48 | + "native/src/serial_right_db_helper.cpp", | ||
| 49 | + "native/src/serial_right_manager.cpp", | ||
| 50 | + "native/src/serial_service.cpp", | ||
| 51 | + ] | ||
| 52 | + | ||
| 53 | + configs = [ ":serialsrv_private_config" ] | ||
| 54 | + | ||
| 55 | + public_configs = [ ":serialsrv_public_config" ] | ||
| 56 | + | ||
| 57 | + deps = [ | ||
| 58 | + "${serial_manager_path}/interfaces/innerkits:serial_server_stub", | ||
| 59 | + "${serial_manager_path}/utils:serial_error_code", | ||
| 60 | + ] | ||
| 61 | + | ||
| 62 | + external_deps = [ | ||
| 63 | + "ability_base:want", | ||
| 64 | + "ability_runtime:ability_connect_callback_stub", | ||
| 65 | + "ability_runtime:ability_manager", | ||
| 66 | + "access_token:libaccesstoken_sdk", | ||
| 67 | + "access_token:libtokenid_sdk", | ||
| 68 | + "bundle_framework:appexecfwk_base", | ||
| 69 | + "bundle_framework:appexecfwk_core", | ||
| 70 | + "cJSON:cjson", | ||
| 71 | + "c_utils:utils", | ||
| 72 | + "common_event_service:cesfwk_innerkits", | ||
| 73 | + "drivers_interface_serial:libserials_proxy_1.0", | ||
| 74 | + "drivers_interface_serial:serials_idl_headers", | ||
| 75 | + "hdf_core:libhdf_utils", | ||
| 76 | + "hdf_core:libhdi", | ||
| 77 | + "hicollie:libhicollie", | ||
| 78 | + "hilog:libhilog", | ||
| 79 | + "ipc:ipc_single", | ||
| 80 | + "os_account:os_account_innerkits", | ||
| 81 | + "relational_store:native_rdb", | ||
| 82 | + "safwk:system_ability_fwk", | ||
| 83 | + "samgr:samgr_proxy", | ||
| 84 | + ] | ||
| 85 | + | ||
| 86 | + subsystem_name = "busmanager" | ||
| 87 | + part_name = "serial_manager" | ||
| 88 | + } | ||
| 89 | + | ||
| 33 | ohos_shared_library("serialservice") { | 90 | ohos_shared_library("serialservice") { |
| 34 | sanitize = { | 91 | sanitize = { |
| 35 | integer_overflow = true | 92 | integer_overflow = true |
| @@ -97,4 +154,7 @@ if (serial_manager_enable_service) { | |||
| 97 | 154 | ||
| 98 | group("serial_service.init") { | 155 | group("serial_service.init") { |
| 99 | } | 156 | } |
| 157 | + | ||
| 158 | + group("serialservice_src") { | ||
| 159 | + } | ||
| 100 | } | 160 | } |
| @@ -47,6 +47,18 @@ struct SerialRightAppInfo { | |||
| 47 | uint64_t validPeriod; /* app permission valid period */ | 47 | uint64_t validPeriod; /* app permission valid period */ |
| 48 | }; | 48 | }; |
| 49 | 49 | ||
| 50 | +struct SerialRightFullRecord { | ||
| 51 | + uint32_t primaryKeyId; | ||
| 52 | + int32_t uid; | ||
| 53 | + std::string portName; | ||
| 54 | + std::string bundleName; | ||
| 55 | + std::string tokenId; | ||
| 56 | + uint64_t installTime; | ||
| 57 | + uint64_t updateTime; | ||
| 58 | + uint64_t requestTime; | ||
| 59 | + uint64_t validPeriod; | ||
| 60 | +}; | ||
| 61 | + | ||
| 50 | struct SerialRightTableInfo { | 62 | struct SerialRightTableInfo { |
| 51 | int32_t rowCount; | 63 | int32_t rowCount; |
| 52 | int32_t columnCount; | 64 | int32_t columnCount; |
| @@ -68,6 +80,12 @@ public: | |||
| 68 | 80 | ||
| 69 | static std::shared_ptr<SerialRightDbHelper> GetInstance(); | 81 | static std::shared_ptr<SerialRightDbHelper> GetInstance(); |
| 70 | 82 | ||
| 83 | + // Check if database is valid and ready for operations | ||
| 84 | + bool IsDatabaseValid() const | ||
| 85 | + { | ||
| 86 | + return rdbStore_ != nullptr; | ||
| 87 | + } | ||
| 88 | + | ||
| 71 | bool IsRecordExpired(int32_t uid, const std::string& portName, const std::string& bundleName, | 89 | bool IsRecordExpired(int32_t uid, const std::string& portName, const std::string& bundleName, |
| 72 | const std::string& tokenId, uint64_t expiredTime); | 90 | const std::string& tokenId, uint64_t expiredTime); |
| 73 | bool IsRecordExpired(const SerialRightAppInfo& info, uint64_t expiredTime); | 91 | bool IsRecordExpired(const SerialRightAppInfo& info, uint64_t expiredTime); |
| @@ -82,6 +100,7 @@ public: | |||
| 82 | const std::string& tokenId, std::vector<SerialRightAppInfo>& infos); | 100 | const std::string& tokenId, std::vector<SerialRightAppInfo>& infos); |
| 83 | int32_t QueryRightRecordUids(std::vector<std::string>& uids); | 101 | int32_t QueryRightRecordUids(std::vector<std::string>& uids); |
| 84 | int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string>& apps); | 102 | int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string>& apps); |
| 103 | + int32_t QueryAllRightRecords(std::vector<SerialRightFullRecord>& records); | ||
| 85 | 104 | ||
| 86 | int32_t UpdateRightRecord( | 105 | int32_t UpdateRightRecord( |
| 87 | int32_t uid, const std::string& portName, const std::string& bundleName, SerialRightAppInfo& info); | 106 | int32_t uid, const std::string& portName, const std::string& bundleName, SerialRightAppInfo& info); |
| @@ -110,6 +129,8 @@ private: | |||
| 110 | const std::string& columnName, std::vector<std::string>& columnValues); | 129 | const std::string& columnName, std::vector<std::string>& columnValues); |
| 111 | int32_t DeleteAndNoOtherOperation(const std::string& whereClause, const std::vector<std::string>& whereArgs); | 130 | int32_t DeleteAndNoOtherOperation(const std::string& whereClause, const std::vector<std::string>& whereArgs); |
| 112 | int32_t DeleteAndNoOtherOperation(const RdbPredicates& rdbPredicates); | 131 | int32_t DeleteAndNoOtherOperation(const RdbPredicates& rdbPredicates); |
| 132 | + int32_t ReadFullRecordFromRow( | ||
| 133 | + const std::shared_ptr<ResultSet>& resultSet, int32_t rowIndex, SerialRightFullRecord& record); | ||
| 113 | 134 | ||
| 114 | static std::shared_ptr<SerialRightDbHelper> instance_; | 135 | static std::shared_ptr<SerialRightDbHelper> instance_; |
| 115 | std::mutex databaseMutex_; | 136 | std::mutex databaseMutex_; |
| @@ -119,6 +119,7 @@ private: | |||
| 119 | std::string FindPortNameForDevice(const SerialPortInfo& portInfo); | 119 | std::string FindPortNameForDevice(const SerialPortInfo& portInfo); |
| 120 | 120 | ||
| 121 | int32_t GetCallingUserId(); | 121 | int32_t GetCallingUserId(); |
| 122 | + int32_t GetCallingUserIdFromToken(); | ||
| 122 | std::string GetCallingBundleName(); | 123 | std::string GetCallingBundleName(); |
| 123 | std::string GetCallingTokenId(); | 124 | std::string GetCallingTokenId(); |
| 124 | bool CheckSerialPortRight(const std::string& portName, const std::string& productName); | 125 | bool CheckSerialPortRight(const std::string& portName, const std::string& productName); |
| @@ -143,6 +144,11 @@ private: | |||
| 143 | void DumpPhysicalDevices(int fd); | 144 | void DumpPhysicalDevices(int fd); |
| 144 | void DumpOpenedDevices(int fd); | 145 | void DumpOpenedDevices(int fd); |
| 145 | 146 | ||
| 147 | + // Helper functions for AddPortAuthorization | ||
| 148 | + int32_t ValidateDialogHapCaller( | ||
| 149 | + Security::AccessToken::AccessTokenID callerTokenId, Security::AccessToken::HapTokenInfo& callerTokenInfo); | ||
| 150 | + int32_t ParseTargetTokenId(const std::string& tokenId, Security::AccessToken::AccessTokenID& targetTokenId); | ||
| 151 | + | ||
| 146 | void SetHdiProxyForTest(const sptr<OHOS::HDI::Serials::V1_0::ISerials>& proxy); | 152 | void SetHdiProxyForTest(const sptr<OHOS::HDI::Serials::V1_0::ISerials>& proxy); |
| 147 | sptr<OHOS::HDI::Serials::V1_0::ISerials> GetHdiProxyForTest() const; | 153 | sptr<OHOS::HDI::Serials::V1_0::ISerials> GetHdiProxyForTest() const; |
| 148 | 154 | ||
| @@ -17,8 +17,6 @@ | |||
| 17 | 17 | ||
| 18 | #include <ctime> | 18 | #include <ctime> |
| 19 | #include <mutex> | 19 | #include <mutex> |
| 20 | -#include <sys/stat.h> | ||
| 21 | -#include <unistd.h> | ||
| 22 | 20 | ||
| 23 | #include "hilog_wrapper.h" | 21 | #include "hilog_wrapper.h" |
| 24 | #include "rdb_errno.h" | 22 | #include "rdb_errno.h" |
| @@ -30,8 +28,7 @@ namespace Serial { | |||
| 30 | using namespace NativeRdb; | 28 | using namespace NativeRdb; |
| 31 | 29 | ||
| 32 | namespace { | 30 | namespace { |
| 33 | -constexpr const char* DATABASE_DIR = "/data/service/el1/public/serial_manager/"; | 31 | +constexpr const char* DATABASE_DIR = "/data/service/el1/public/database/serial_manager/"; |
| 34 | -constexpr int DATABASE_DIR_MODE = 0755; | ||
| 35 | constexpr const char* CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS serial_right(" | 32 | constexpr const char* CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS serial_right(" |
| 36 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " | 33 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 37 | "uid INTEGER, " | 34 | "uid INTEGER, " |
| @@ -46,28 +43,6 @@ constexpr const char* CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS serial_righ | |||
| 46 | 43 | ||
| 47 | std::shared_ptr<SerialRightDbHelper> SerialRightDbHelper::instance_ = nullptr; | 44 | std::shared_ptr<SerialRightDbHelper> SerialRightDbHelper::instance_ = nullptr; |
| 48 | 45 | ||
| 49 | -static bool EnsureDatabaseDirExists() | ||
| 50 | -{ | ||
| 51 | - struct stat st; | ||
| 52 | - if (stat(DATABASE_DIR, &st) == 0 && S_ISDIR(st.st_mode)) { | ||
| 53 | - SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d database directory already exists: %{public}s", __func__, | ||
| 54 | - __LINE__, DATABASE_DIR); | ||
| 55 | - return true; | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - int ret = mkdir(DATABASE_DIR, DATABASE_DIR_MODE); | ||
| 59 | - if (ret != 0 && errno != EEXIST) { | ||
| 60 | - SERIAL_HILOGE(MODULE_SERVICE, | ||
| 61 | - "%{public}s:%{public}d failed to create database directory %{public}s, errno=%{public}d", __func__, | ||
| 62 | - __LINE__, DATABASE_DIR, errno); | ||
| 63 | - return false; | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | - SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d database directory created: %{public}s", __func__, __LINE__, | ||
| 67 | - DATABASE_DIR); | ||
| 68 | - return true; | ||
| 69 | -} | ||
| 70 | - | ||
| 71 | int32_t SerialRightRdbOpenCallback::OnCreate(RdbStore& rdbStore) | 46 | int32_t SerialRightRdbOpenCallback::OnCreate(RdbStore& rdbStore) |
| 72 | { | 47 | { |
| 73 | SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d OnCreate", __func__, __LINE__); | 48 | SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d OnCreate", __func__, __LINE__); |
| @@ -100,21 +75,6 @@ SerialRightDbHelper::SerialRightDbHelper() | |||
| 100 | { | 75 | { |
| 101 | SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d constructed", __func__, __LINE__); | 76 | SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d constructed", __func__, __LINE__); |
| 102 | 77 | ||
| 103 | - // Ensure database directory exists with retry mechanism | ||
| 104 | - if (!EnsureDatabaseDirExists()) { | ||
| 105 | - SERIAL_HILOGW(MODULE_SERVICE, | ||
| 106 | - "%{public}s:%{public}d EnsureDatabaseDirExists failed, retrying with direct mkdir", __func__, __LINE__); | ||
| 107 | - // Fallback: try direct mkdir if helper function failed (e.g. permission issue) | ||
| 108 | - int ret = mkdir(DATABASE_DIR, DATABASE_DIR_MODE); | ||
| 109 | - if (ret != 0 && errno != EEXIST) { | ||
| 110 | - SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d direct mkdir also failed, errno=%{public}d", __func__, | ||
| 111 | - __LINE__, errno); | ||
| 112 | - return; | ||
| 113 | - } | ||
| 114 | - SERIAL_HILOGI( | ||
| 115 | - MODULE_SERVICE, "%{public}s:%{public}d direct mkdir succeeded or directory exists", __func__, __LINE__); | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | RdbStoreConfig config(DATABASE_DIR + std::string(DATABASE_NAME)); | 78 | RdbStoreConfig config(DATABASE_DIR + std::string(DATABASE_NAME)); |
| 119 | config.SetSecurityLevel(SecurityLevel::S1); | 79 | config.SetSecurityLevel(SecurityLevel::S1); |
| 120 | 80 | ||
| @@ -124,7 +84,19 @@ SerialRightDbHelper::SerialRightDbHelper() | |||
| 124 | if (rdbStore_ == nullptr) { | 84 | if (rdbStore_ == nullptr) { |
| 125 | SERIAL_HILOGE( | 85 | SERIAL_HILOGE( |
| 126 | MODULE_SERVICE, "%{public}s:%{public}d GetRdbStore failed: %{public}d", __func__, __LINE__, errCode); | 86 | MODULE_SERVICE, "%{public}s:%{public}d GetRdbStore failed: %{public}d", __func__, __LINE__, errCode); |
| 87 | + return; | ||
| 127 | } | 88 | } |
| 89 | + | ||
| 90 | + // Verify database is working by executing a simple query | ||
| 91 | + std::shared_ptr<ResultSet> resultSet = rdbStore_->QuerySql("SELECT name FROM sqlite_master WHERE type='table'"); | ||
| 92 | + if (resultSet == nullptr) { | ||
| 93 | + SERIAL_HILOGE( | ||
| 94 | + MODULE_SERVICE, "%{public}s:%{public}d QuerySql failed, database may be corrupted", __func__, __LINE__); | ||
| 95 | + rdbStore_ = nullptr; | ||
| 96 | + return; | ||
| 97 | + } | ||
| 98 | + resultSet->Close(); | ||
| 99 | + SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d database verified successfully", __func__, __LINE__); | ||
| 128 | } | 100 | } |
| 129 | 101 | ||
| 130 | bool SerialRightDbHelper::IsRecordExpired(int32_t uid, const std::string& portName, const std::string& bundleName, | 102 | bool SerialRightDbHelper::IsRecordExpired(int32_t uid, const std::string& portName, const std::string& bundleName, |
| @@ -256,6 +228,102 @@ int32_t SerialRightDbHelper::QueryRightRecordApps(int32_t uid, std::vector<std:: | |||
| 256 | return QueryAndGetResultColumnValues(predicates, columns, "bundle_name", apps); | 228 | return QueryAndGetResultColumnValues(predicates, columns, "bundle_name", apps); |
| 257 | } | 229 | } |
| 258 | 230 | ||
| 231 | +int32_t SerialRightDbHelper::QueryAllRightRecords(std::vector<SerialRightFullRecord>& records) | ||
| 232 | +{ | ||
| 233 | + if (rdbStore_ == nullptr) { | ||
| 234 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d rdbStore_ is nullptr", __func__, __LINE__); | ||
| 235 | + return E_ERROR; | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + std::vector<std::string> columns; | ||
| 239 | + RdbPredicates predicates(TABLE_NAME); | ||
| 240 | + std::shared_ptr<ResultSet> resultSet = rdbStore_->Query(predicates, columns); | ||
| 241 | + if (resultSet == nullptr) { | ||
| 242 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d Query failed", __func__, __LINE__); | ||
| 243 | + return E_ERROR; | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + int32_t rowCount = 0; | ||
| 247 | + int32_t ret = resultSet->GetRowCount(rowCount); | ||
| 248 | + if (ret != E_OK) { | ||
| 249 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d GetRowCount failed: %{public}d", __func__, __LINE__, ret); | ||
| 250 | + return ret; | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + if (rowCount <= 0) { | ||
| 254 | + SERIAL_HILOGD(MODULE_SERVICE, "%{public}s:%{public}d no records found", __func__, __LINE__); | ||
| 255 | + return 0; | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + for (int32_t i = 0; i < rowCount; i++) { | ||
| 259 | + SerialRightFullRecord record; | ||
| 260 | + ret = ReadFullRecordFromRow(resultSet, i, record); | ||
| 261 | + if (ret == E_OK) { | ||
| 262 | + records.push_back(record); | ||
| 263 | + } | ||
| 264 | + } | ||
| 265 | + | ||
| 266 | + SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d QueryAllRightRecords found %{public}zu records", __func__, | ||
| 267 | + __LINE__, records.size()); | ||
| 268 | + return static_cast<int32_t>(records.size()); | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +int32_t SerialRightDbHelper::ReadFullRecordFromRow( | ||
| 272 | + const std::shared_ptr<ResultSet>& resultSet, int32_t rowIndex, SerialRightFullRecord& record) | ||
| 273 | +{ | ||
| 274 | + int32_t ret = resultSet->GoToRow(rowIndex); | ||
| 275 | + if (ret != E_OK) { | ||
| 276 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d GoToRow %{public}d failed: %{public}d", __func__, __LINE__, | ||
| 277 | + rowIndex, ret); | ||
| 278 | + return ret; | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + int32_t primaryKeyIndex = 0; | ||
| 282 | + int32_t uidIndex = 0; | ||
| 283 | + int32_t portNameIndex = 0; | ||
| 284 | + int32_t bundleNameIndex = 0; | ||
| 285 | + int32_t tokenIdIndex = 0; | ||
| 286 | + int32_t installTimeIndex = 0; | ||
| 287 | + int32_t updateTimeIndex = 0; | ||
| 288 | + int32_t requestTimeIndex = 0; | ||
| 289 | + int32_t validPeriodIndex = 0; | ||
| 290 | + | ||
| 291 | + resultSet->GetColumnIndex("id", primaryKeyIndex); | ||
| 292 | + resultSet->GetColumnIndex("uid", uidIndex); | ||
| 293 | + resultSet->GetColumnIndex("port_name", portNameIndex); | ||
| 294 | + resultSet->GetColumnIndex("bundle_name", bundleNameIndex); | ||
| 295 | + resultSet->GetColumnIndex("token_id", tokenIdIndex); | ||
| 296 | + resultSet->GetColumnIndex("install_time", installTimeIndex); | ||
| 297 | + resultSet->GetColumnIndex("update_time", updateTimeIndex); | ||
| 298 | + resultSet->GetColumnIndex("request_time", requestTimeIndex); | ||
| 299 | + resultSet->GetColumnIndex("valid_period", validPeriodIndex); | ||
| 300 | + | ||
| 301 | + int64_t value = 0; | ||
| 302 | + resultSet->GetLong(primaryKeyIndex, value); | ||
| 303 | + record.primaryKeyId = static_cast<uint32_t>(value); | ||
| 304 | + | ||
| 305 | + resultSet->GetLong(uidIndex, value); | ||
| 306 | + record.uid = static_cast<int32_t>(value); | ||
| 307 | + | ||
| 308 | + resultSet->GetString(portNameIndex, record.portName); | ||
| 309 | + resultSet->GetString(bundleNameIndex, record.bundleName); | ||
| 310 | + resultSet->GetString(tokenIdIndex, record.tokenId); | ||
| 311 | + | ||
| 312 | + resultSet->GetLong(installTimeIndex, value); | ||
| 313 | + record.installTime = static_cast<uint64_t>(value); | ||
| 314 | + | ||
| 315 | + resultSet->GetLong(updateTimeIndex, value); | ||
| 316 | + record.updateTime = static_cast<uint64_t>(value); | ||
| 317 | + | ||
| 318 | + resultSet->GetLong(requestTimeIndex, value); | ||
| 319 | + record.requestTime = static_cast<uint64_t>(value); | ||
| 320 | + | ||
| 321 | + resultSet->GetLong(validPeriodIndex, value); | ||
| 322 | + record.validPeriod = static_cast<uint64_t>(value); | ||
| 323 | + | ||
| 324 | + return E_OK; | ||
| 325 | +} | ||
| 326 | + | ||
| 259 | int32_t SerialRightDbHelper::UpdateRightRecord( | 327 | int32_t SerialRightDbHelper::UpdateRightRecord( |
| 260 | int32_t uid, const std::string& portName, const std::string& bundleName, struct SerialRightAppInfo& info) | 328 | int32_t uid, const std::string& portName, const std::string& bundleName, struct SerialRightAppInfo& info) |
| 261 | { | 329 | { |
| @@ -45,12 +45,16 @@ using namespace OHOS::Security::AccessToken; | |||
| 45 | namespace OHOS { | 45 | namespace OHOS { |
| 46 | namespace Serial { | 46 | namespace Serial { |
| 47 | 47 | ||
| 48 | -// Test mode configuration: | 48 | +// Authorization flow configuration: |
| 49 | -// 1. SERIAL_RIGHT_TEST_MODE=1 env var: skip console bypass for real auth testing | 49 | +// 1. DISABLE_CONSOLE_BYPASS=0: Console users (userId=0) bypass auth check |
| 50 | -// 2. DISABLE_CONSOLE_BYPASS macro: completely disable console bypass in HasRight | 50 | +// - Used for module testing and CLI tools |
| 51 | +// - SERIAL_RIGHT_TEST_MODE=1 env var can force real auth for testing | ||
| 52 | +// 2. DISABLE_CONSOLE_BYPASS=1: Force real auth flow for ALL users including console | ||
| 53 | +// - Used for production deployment | ||
| 54 | +// - Dialog HAP must call AddPortAuthorization to grant permission | ||
| 51 | #define DISABLE_CONSOLE_BYPASS 1 | 55 | #define DISABLE_CONSOLE_BYPASS 1 |
| 52 | 56 | ||
| 53 | -#ifndef DISABLE_CONSOLE_BYPASS | 57 | +#if !DISABLE_CONSOLE_BYPASS |
| 54 | // Test mode: when SERIAL_RIGHT_TEST_MODE=1, skip console bypass to test real auth flow | 58 | // Test mode: when SERIAL_RIGHT_TEST_MODE=1, skip console bypass to test real auth flow |
| 55 | static bool IsTestModeEnabled() | 59 | static bool IsTestModeEnabled() |
| 56 | { | 60 | { |
| @@ -110,6 +114,13 @@ int32_t SerialRightManager::Init() | |||
| 110 | return SERIAL_RIGHT_FAILURE; | 114 | return SERIAL_RIGHT_FAILURE; |
| 111 | } | 115 | } |
| 112 | 116 | ||
| 117 | + // Verify database is valid and ready for operations | ||
| 118 | + if (!helper->IsDatabaseValid()) { | ||
| 119 | + SERIAL_HILOGE( | ||
| 120 | + MODULE_SERVICE, "%{public}s:%{public}d Database is not valid, rdbStore may be nullptr", __func__, __LINE__); | ||
| 121 | + return SERIAL_RIGHT_FAILURE; | ||
| 122 | + } | ||
| 123 | + | ||
| 113 | // Subscribe to common events (boot complete, app uninstall, user switch) | 124 | // Subscribe to common events (boot complete, app uninstall, user switch) |
| 114 | int32_t ret = SubscribeCommonEvents(); | 125 | int32_t ret = SubscribeCommonEvents(); |
| 115 | if (ret != SERIAL_RIGHT_OK) { | 126 | if (ret != SERIAL_RIGHT_OK) { |
| @@ -131,7 +142,7 @@ bool SerialRightManager::HasRight( | |||
| 131 | ", userId=%{public}d", | 142 | ", userId=%{public}d", |
| 132 | __func__, __LINE__, portName.c_str(), bundleName.c_str(), actualBundleName.c_str(), userId); | 143 | __func__, __LINE__, portName.c_str(), bundleName.c_str(), actualBundleName.c_str(), userId); |
| 133 | 144 | ||
| 134 | -#ifndef DISABLE_CONSOLE_BYPASS | 145 | +#if !DISABLE_CONSOLE_BYPASS |
| 135 | // Normal mode: console bypass enabled | 146 | // Normal mode: console bypass enabled |
| 136 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 147 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 137 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 148 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| @@ -197,33 +208,18 @@ bool SerialRightManager::GetUserAgreementByDiag(const SerialDeviceInfoForDialog& | |||
| 197 | return false; | 208 | return false; |
| 198 | } | 209 | } |
| 199 | 210 | ||
| 200 | - // Get resultCode from dialog for logging | 211 | + // Check HasRight after dialog closes - dialog HAP should have called AddPortAuthorization |
| 201 | - int32_t resultCode = -1; | ||
| 202 | - if (abilityConn_ != nullptr) { | ||
| 203 | - resultCode = abilityConn_->GetLastResultCode(); | ||
| 204 | - SERIAL_HILOGI( | ||
| 205 | - MODULE_SERVICE, "%{public}s:%{public}d dialog resultCode=%{public}d", __func__, __LINE__, resultCode); | ||
| 206 | - } | ||
| 207 | - | ||
| 208 | - // Check HasRight immediately after dialog closes | ||
| 209 | if (HasRight(deviceInfo.portName, deviceInfo.bundleName, deviceInfo.tokenId, userId)) { | 212 | if (HasRight(deviceInfo.portName, deviceInfo.bundleName, deviceInfo.tokenId, userId)) { |
| 210 | - SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d HasRight check passed immediately", __func__, __LINE__); | 213 | + SERIAL_HILOGI( |
| 214 | + MODULE_SERVICE, "%{public}s:%{public}d HasRight check passed, user granted permission", __func__, __LINE__); | ||
| 211 | return true; | 215 | return true; |
| 212 | } | 216 | } |
| 213 | 217 | ||
| 214 | - // Dialog closed but HasRight is false -> directly write to our own database | 218 | + // No permission after dialog closed means user denied or dialog HAP failed |
| 215 | - // Note: USB dialog HAP only writes to USB's usbRight.db, not our SerialRight.db | 219 | + SERIAL_HILOGW(MODULE_SERVICE, |
| 216 | - // So we must write authorization ourselves regardless of resultCode | 220 | + "%{public}s:%{public}d HasRight check failed after dialog, user denied or dialog HAP did not call " |
| 217 | - SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d Dialog closed but HasRight false, force AddDeviceRight", | 221 | + "AddPortAuthorization", |
| 218 | __func__, __LINE__); | 222 | __func__, __LINE__); |
| 219 | - | ||
| 220 | - if (AddDeviceRight(deviceInfo.portName, deviceInfo.bundleName, deviceInfo.tokenId, userId)) { | ||
| 221 | - SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d AddDeviceRight succeeded, authorization written", __func__, | ||
| 222 | - __LINE__); | ||
| 223 | - return true; | ||
| 224 | - } | ||
| 225 | - | ||
| 226 | - SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d AddDeviceRight failed", __func__, __LINE__); | ||
| 227 | return false; | 223 | return false; |
| 228 | } | 224 | } |
| 229 | 225 | ||
| @@ -347,7 +343,7 @@ bool SerialRightManager::BuildInfoAndSaveRight( | |||
| 347 | MODULE_SERVICE, "%{public}s:%{public}d get app install time failed: %{public}d", __func__, __LINE__, uid); | 343 | MODULE_SERVICE, "%{public}s:%{public}d get app install time failed: %{public}d", __func__, __LINE__, uid); |
| 348 | } | 344 | } |
| 349 | 345 | ||
| 350 | - struct SerialRightAppInfo info; | 346 | + struct SerialRightAppInfo info = {}; // 强制初始化为空,避免 Signal 11 崩溃 |
| 351 | info.uid = uid; | 347 | info.uid = uid; |
| 352 | info.installTime = installTime; | 348 | info.installTime = installTime; |
| 353 | info.updateTime = updateTime; | 349 | info.updateTime = updateTime; |
| @@ -379,7 +375,7 @@ bool SerialRightManager::AddDeviceRight(const std::string& portName, const std:: | |||
| 379 | return false; | 375 | return false; |
| 380 | } | 376 | } |
| 381 | 377 | ||
| 382 | -#ifndef DISABLE_CONSOLE_BYPASS | 378 | +#if !DISABLE_CONSOLE_BYPASS |
| 383 | if (uid == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 379 | if (uid == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 384 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 380 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| 385 | return true; | 381 | return true; |
| @@ -406,7 +402,7 @@ bool SerialRightManager::AddDeviceRight( | |||
| 406 | ", isCliMode=%{public}d, userId=%{public}d", | 402 | ", isCliMode=%{public}d, userId=%{public}d", |
| 407 | __func__, __LINE__, portName.c_str(), bundleName.c_str(), actualBundleName.c_str(), isCliMode, userId); | 403 | __func__, __LINE__, portName.c_str(), bundleName.c_str(), actualBundleName.c_str(), isCliMode, userId); |
| 408 | 404 | ||
| 409 | -#ifndef DISABLE_CONSOLE_BYPASS | 405 | +#if !DISABLE_CONSOLE_BYPASS |
| 410 | // Normal mode: console bypass enabled | 406 | // Normal mode: console bypass enabled |
| 411 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 407 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 412 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 408 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| @@ -447,7 +443,7 @@ bool SerialRightManager::AddDeviceRightInternal(const std::string& portName, con | |||
| 447 | "%{public}s:%{public}d CLI mode: skip GetBundleInstallAndUpdateTime, use current time", __func__, __LINE__); | 443 | "%{public}s:%{public}d CLI mode: skip GetBundleInstallAndUpdateTime, use current time", __func__, __LINE__); |
| 448 | } | 444 | } |
| 449 | 445 | ||
| 450 | - struct SerialRightAppInfo info; | 446 | + struct SerialRightAppInfo info = {}; // 强制初始化为空,避免 Signal 11 崩溃 |
| 451 | info.uid = userId; | 447 | info.uid = userId; |
| 452 | info.installTime = installTime; | 448 | info.installTime = installTime; |
| 453 | info.updateTime = updateTime; | 449 | info.updateTime = updateTime; |
| @@ -473,7 +469,7 @@ bool SerialRightManager::AddDeviceRightInternal(const std::string& portName, con | |||
| 473 | bool SerialRightManager::RemoveDeviceRight( | 469 | bool SerialRightManager::RemoveDeviceRight( |
| 474 | const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId) | 470 | const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId) |
| 475 | { | 471 | { |
| 476 | -#ifndef DISABLE_CONSOLE_BYPASS | 472 | +#if !DISABLE_CONSOLE_BYPASS |
| 477 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 473 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 478 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 474 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| 479 | return true; | 475 | return true; |
| @@ -503,7 +499,7 @@ bool SerialRightManager::RemoveDeviceRight( | |||
| 503 | int32_t SerialRightManager::CancelDeviceRight( | 499 | int32_t SerialRightManager::CancelDeviceRight( |
| 504 | const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId) | 500 | const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId) |
| 505 | { | 501 | { |
| 506 | -#ifndef DISABLE_CONSOLE_BYPASS | 502 | +#if !DISABLE_CONSOLE_BYPASS |
| 507 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 503 | if (userId == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 508 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 504 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| 509 | return SERIAL_ERR_INVALID_PARAM; | 505 | return SERIAL_ERR_INVALID_PARAM; |
| @@ -745,13 +741,24 @@ bool SerialRightManager::GetAppName(const std::string& bundleName, std::string& | |||
| 745 | 741 | ||
| 746 | bool SerialRightManager::IsAppInstalled(int32_t uid, const std::string& bundleName) | 742 | bool SerialRightManager::IsAppInstalled(int32_t uid, const std::string& bundleName) |
| 747 | { | 743 | { |
| 744 | + // Skip IPC for special environments to avoid ApplicationInfo destructor crash | ||
| 745 | + // Similar issue as GetBundleInstallAndUpdateTime | ||
| 746 | + if (uid == SERIAL_RIGHT_USERID_CONSOLE || bundleName.empty() || bundleName == "serial_cli" || | ||
| 747 | + bundleName.find("test") != std::string::npos) { | ||
| 748 | + SERIAL_HILOGI(MODULE_SERVICE, | ||
| 749 | + "%{public}s:%{public}d Skip IPC for special env: uid=%{public}d, bundle=%{public}s", __func__, __LINE__, | ||
| 750 | + uid, bundleName.c_str()); | ||
| 751 | + return true; | ||
| 752 | + } | ||
| 753 | + | ||
| 748 | auto bundleMgr = GetBundleMgr(); | 754 | auto bundleMgr = GetBundleMgr(); |
| 749 | if (bundleMgr == nullptr) { | 755 | if (bundleMgr == nullptr) { |
| 750 | SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr is nullptr, return false", __func__, __LINE__); | 756 | SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr is nullptr, return false", __func__, __LINE__); |
| 751 | return false; | 757 | return false; |
| 752 | } | 758 | } |
| 753 | 759 | ||
| 754 | - ApplicationInfo appInfo; | 760 | + // Initialize ApplicationInfo to prevent crash during destruction when IPC fails |
| 761 | + ApplicationInfo appInfo = {}; | ||
| 755 | if (!bundleMgr->GetApplicationInfo(bundleName, GET_BASIC_APPLICATION_INFO, uid, appInfo)) { | 762 | if (!bundleMgr->GetApplicationInfo(bundleName, GET_BASIC_APPLICATION_INFO, uid, appInfo)) { |
| 756 | SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr GetApplicationInfo failed", __func__, __LINE__); | 763 | SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr GetApplicationInfo failed", __func__, __LINE__); |
| 757 | return false; | 764 | return false; |
| @@ -762,19 +769,29 @@ bool SerialRightManager::IsAppInstalled(int32_t uid, const std::string& bundleNa | |||
| 762 | bool SerialRightManager::GetBundleInstallAndUpdateTime( | 769 | bool SerialRightManager::GetBundleInstallAndUpdateTime( |
| 763 | int32_t uid, const std::string& bundleName, uint64_t& installTime, uint64_t& updateTime) | 770 | int32_t uid, const std::string& bundleName, uint64_t& installTime, uint64_t& updateTime) |
| 764 | { | 771 | { |
| 765 | - BundleInfo bundleInfo; | 772 | + // Skip IPC for special environments to avoid BundleInfo destructor crash |
| 766 | - auto bundleMgr = GetBundleMgr(); | 773 | + // BundleInfo has internal pointer issues that cause Signal 11 on stack destruction |
| 767 | - if (bundleMgr == nullptr) { | 774 | + // For authorization records, default timestamp is sufficient |
| 768 | - SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr is nullptr, return false", __func__, __LINE__); | 775 | + if (uid == SERIAL_RIGHT_USERID_CONSOLE || bundleName.empty() || bundleName == "serial_cli" || |
| 769 | - return false; | 776 | + bundleName.find("test") != std::string::npos) { |
| 777 | + SERIAL_HILOGI(MODULE_SERVICE, | ||
| 778 | + "%{public}s:%{public}d Skip IPC for special env: uid=%{public}d, bundle=%{public}s", __func__, __LINE__, | ||
| 779 | + uid, bundleName.c_str()); | ||
| 780 | + return true; | ||
| 770 | } | 781 | } |
| 771 | 782 | ||
| 772 | - if (!bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, uid)) { | 783 | + auto bundleMgr = GetBundleMgr(); |
| 773 | - SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d BundleMgr GetBundleInfo(uid) failed", __func__, __LINE__); | 784 | + if (bundleMgr == nullptr) { |
| 774 | - return false; | 785 | + SERIAL_HILOGI( |
| 786 | + MODULE_SERVICE, "%{public}s:%{public}d BundleMgr unavailable, use default time", __func__, __LINE__); | ||
| 787 | + return true; | ||
| 775 | } | 788 | } |
| 776 | - installTime = static_cast<uint64_t>(bundleInfo.installTime); | 789 | + |
| 777 | - updateTime = static_cast<uint64_t>(bundleInfo.updateTime); | 790 | + // BundleInfo IPC call skipped due to destructor crash risk |
| 791 | + // Caller provides default installTime/updateTime values | ||
| 792 | + SERIAL_HILOGI(MODULE_SERVICE, | ||
| 793 | + "%{public}s:%{public}d BundleInfo IPC skipped, use default time: uid=%{public}d, bundle=%{public}s", __func__, | ||
| 794 | + __LINE__, uid, bundleName.c_str()); | ||
| 778 | return true; | 795 | return true; |
| 779 | } | 796 | } |
| 780 | 797 | ||
| @@ -881,7 +898,7 @@ int32_t SerialRightManager::TidyUpRight(uint32_t choose) | |||
| 881 | int32_t uid = SERIAL_RIGHT_USERID_INVALID; | 898 | int32_t uid = SERIAL_RIGHT_USERID_INVALID; |
| 882 | GetCurrentUserId(uid); | 899 | GetCurrentUserId(uid); |
| 883 | 900 | ||
| 884 | -#ifndef DISABLE_CONSOLE_BYPASS | 901 | +#if !DISABLE_CONSOLE_BYPASS |
| 885 | if (uid == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { | 902 | if (uid == SERIAL_RIGHT_USERID_CONSOLE && !IsTestModeEnabled()) { |
| 886 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); | 903 | SERIAL_HILOGW(MODULE_SERVICE, "%{public}s:%{public}d console called, bypass (testMode=0)", __func__, __LINE__); |
| 887 | return SERIAL_RIGHT_OK; | 904 | return SERIAL_RIGHT_OK; |
| @@ -13,7 +13,9 @@ | |||
| 13 | * limitations under the License. | 13 | * limitations under the License. |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | +#include <cerrno> | ||
| 16 | #include <cinttypes> | 17 | #include <cinttypes> |
| 18 | +#include <cstdlib> | ||
| 17 | #include <iomanip> | 19 | #include <iomanip> |
| 18 | #include <sstream> | 20 | #include <sstream> |
| 19 | 21 | ||
| @@ -852,34 +854,106 @@ int32_t SerialService::GetCtsSignal(const std::string& portName, bool& cts) | |||
| 852 | return SERIAL_OK; | 854 | return SERIAL_OK; |
| 853 | } | 855 | } |
| 854 | 856 | ||
| 857 | +int32_t SerialService::ValidateDialogHapCaller(AccessTokenID callerTokenId, HapTokenInfo& callerTokenInfo) | ||
| 858 | +{ | ||
| 859 | + ATokenTypeEnum tokenType = AccessTokenKit::GetTokenType(callerTokenId); | ||
| 860 | + if (tokenType != TOKEN_HAP) { | ||
| 861 | + SERIAL_HILOGE(MODULE_SERVICE, | ||
| 862 | + "%{public}s:%{public}d ValidateDialogHapCaller denied: caller is not HAP token, tokenType=%{public}d", | ||
| 863 | + __func__, __LINE__, static_cast<int32_t>(tokenType)); | ||
| 864 | + return SERIAL_ERR_PERMISSION_DENIED; | ||
| 865 | + } | ||
| 866 | + | ||
| 867 | + uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID(); | ||
| 868 | + bool isSystemApp = TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); | ||
| 869 | + if (!isSystemApp) { | ||
| 870 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d ValidateDialogHapCaller denied: caller is not system app", | ||
| 871 | + __func__, __LINE__); | ||
| 872 | + return SERIAL_ERR_PERMISSION_DENIED; | ||
| 873 | + } | ||
| 874 | + | ||
| 875 | + int ret = AccessTokenKit::GetHapTokenInfo(callerTokenId, callerTokenInfo); | ||
| 876 | + if (ret != 0) { | ||
| 877 | + SERIAL_HILOGE(MODULE_SERVICE, | ||
| 878 | + "%{public}s:%{public}d ValidateDialogHapCaller denied: GetHapTokenInfo failed, ret=%{public}d", __func__, | ||
| 879 | + __LINE__, ret); | ||
| 880 | + return SERIAL_ERR_PERMISSION_DENIED; | ||
| 881 | + } | ||
| 882 | + | ||
| 883 | + const std::string DIALOG_HAP_BUNDLE_NAME = "com.usb.right"; | ||
| 884 | + if (callerTokenInfo.bundleName != DIALOG_HAP_BUNDLE_NAME) { | ||
| 885 | + SERIAL_HILOGE(MODULE_SERVICE, | ||
| 886 | + "%{public}s:%{public}d ValidateDialogHapCaller denied: wrong bundleName=%{public}s, expected=%{public}s", | ||
| 887 | + __func__, __LINE__, callerTokenInfo.bundleName.c_str(), DIALOG_HAP_BUNDLE_NAME.c_str()); | ||
| 888 | + return SERIAL_ERR_PERMISSION_DENIED; | ||
| 889 | + } | ||
| 890 | + | ||
| 891 | + return SERIAL_OK; | ||
| 892 | +} | ||
| 893 | + | ||
| 894 | +int32_t SerialService::ParseTargetTokenId(const std::string& tokenId, AccessTokenID& targetTokenId) | ||
| 895 | +{ | ||
| 896 | + char* endPtr = nullptr; | ||
| 897 | + errno = 0; | ||
| 898 | + unsigned long long parsedValue = strtoull(tokenId.c_str(), &endPtr, 10); | ||
| 899 | + if (errno != 0 || endPtr == tokenId.c_str() || *endPtr != '\0') { | ||
| 900 | + SERIAL_HILOGE(MODULE_SERVICE, "%{public}s:%{public}d ParseTargetTokenId failed: invalid tokenId=%{public}s", | ||
| 901 | + __func__, __LINE__, tokenId.c_str()); | ||
| 902 | + return SERIAL_ERR_INVALID_PARAM; | ||
| 903 | + } | ||
| 904 | + targetTokenId = static_cast<AccessTokenID>(parsedValue); | ||
| 905 | + return SERIAL_OK; | ||
| 906 | +} | ||
| 907 | + | ||
| 855 | int32_t SerialService::AddPortAuthorization(const std::string& tokenId, const std::string& deviceId) | 908 | int32_t SerialService::AddPortAuthorization(const std::string& tokenId, const std::string& deviceId) |
| 856 | { | 909 | { |
| 857 | SERIAL_HILOGI(MODULE_SERVICE, | 910 | SERIAL_HILOGI(MODULE_SERVICE, |
| 858 | "%{public}s:%{public}d AddPortAuthorization called, tokenId=%{public}s, deviceId=%{public}s", __func__, | 911 | "%{public}s:%{public}d AddPortAuthorization called, tokenId=%{public}s, deviceId=%{public}s", __func__, |
| 859 | __LINE__, tokenId.c_str(), deviceId.c_str()); | 912 | __LINE__, tokenId.c_str(), deviceId.c_str()); |
| 860 | 913 | ||
| 861 | - // Get caller's real userId and tokenId for consistent database write | ||
| 862 | - int32_t userId = GetCallingUserId(); | ||
| 863 | AccessTokenID callerTokenId = IPCSkeleton::GetCallingTokenID(); | 914 | AccessTokenID callerTokenId = IPCSkeleton::GetCallingTokenID(); |
| 864 | - std::string realTokenIdStr = std::to_string(callerTokenId); | 915 | + HapTokenInfo callerTokenInfo; |
| 865 | - std::string bundleName = GetCallingBundleName(); | 916 | + int32_t ret = ValidateDialogHapCaller(callerTokenId, callerTokenInfo); |
| 866 | - SERIAL_HILOGI(MODULE_SERVICE, | 917 | + if (ret != SERIAL_OK) { |
| 867 | - "%{public}s:%{public}d Using caller's real info: userId=%{public}d, tokenId=%{public}s, bundleName=%{public}s", | 918 | + return ret; |
| 868 | - __func__, __LINE__, userId, realTokenIdStr.c_str(), bundleName.c_str()); | 919 | + } |
| 920 | + | ||
| 921 | + AccessTokenID targetTokenId = 0; | ||
| 922 | + ret = ParseTargetTokenId(tokenId, targetTokenId); | ||
| 923 | + if (ret != SERIAL_OK) { | ||
| 924 | + return ret; | ||
| 925 | + } | ||
| 926 | + | ||
| 927 | + HapTokenInfo targetTokenInfo; | ||
| 928 | + ret = AccessTokenKit::GetHapTokenInfo(targetTokenId, targetTokenInfo); | ||
| 929 | + if (ret != 0) { | ||
| 930 | + SERIAL_HILOGE(MODULE_SERVICE, | ||
| 931 | + "%{public}s:%{public}d AddPortAuthorization denied: GetHapTokenInfo failed for target tokenId=%{public}s, " | ||
| 932 | + "ret=%{public}d", | ||
| 933 | + __func__, __LINE__, tokenId.c_str(), ret); | ||
| 934 | + return SERIAL_ERR_INVALID_PARAM; | ||
| 935 | + } | ||
| 936 | + | ||
| 937 | + SERIAL_HILOGI(MODULE_SERVICE, | ||
| 938 | + "%{public}s:%{public}d AddPortAuthorization validated: caller=%{public}s, target=%{public}s, " | ||
| 939 | + "userId=%{public}d, tokenId=%{public}s", | ||
| 940 | + __func__, __LINE__, callerTokenInfo.bundleName.c_str(), targetTokenInfo.bundleName.c_str(), | ||
| 941 | + targetTokenInfo.userID, tokenId.c_str()); | ||
| 869 | 942 | ||
| 870 | - // Use four-parameter version to ensure consistency with query | ||
| 871 | SerialRightManager& rightManager = SerialRightManager::GetInstance(); | 943 | SerialRightManager& rightManager = SerialRightManager::GetInstance(); |
| 872 | - bool result = rightManager.AddDeviceRight(deviceId, bundleName, realTokenIdStr, userId); | 944 | + bool result = rightManager.AddDeviceRight(deviceId, targetTokenInfo.bundleName, tokenId, targetTokenInfo.userID); |
| 873 | if (!result) { | 945 | if (!result) { |
| 874 | SERIAL_HILOGE(MODULE_SERVICE, | 946 | SERIAL_HILOGE(MODULE_SERVICE, |
| 875 | "%{public}s:%{public}d AddDeviceRight failed for deviceId=%{public}s, userId=%{public}d", __func__, | 947 | "%{public}s:%{public}d AddDeviceRight failed for deviceId=%{public}s, userId=%{public}d", __func__, |
| 876 | - __LINE__, deviceId.c_str(), userId); | 948 | + __LINE__, deviceId.c_str(), targetTokenInfo.userID); |
| 877 | return SERIAL_ERR_SERVICE; | 949 | return SERIAL_ERR_SERVICE; |
| 878 | } | 950 | } |
| 879 | 951 | ||
| 880 | SERIAL_HILOGI(MODULE_SERVICE, | 952 | SERIAL_HILOGI(MODULE_SERVICE, |
| 881 | - "%{public}s:%{public}d AddPortAuthorization success, port=%{public}s, userId=%{public}d, tokenId=%{public}s", | 953 | + "%{public}s:%{public}d AddPortAuthorization success, port=%{public}s, userId=%{public}d, tokenId=%{public}s, " |
| 882 | - __func__, __LINE__, deviceId.c_str(), userId, realTokenIdStr.c_str()); | 954 | + "bundleName=%{public}s", |
| 955 | + __func__, __LINE__, deviceId.c_str(), targetTokenInfo.userID, tokenId.c_str(), | ||
| 956 | + targetTokenInfo.bundleName.c_str()); | ||
| 883 | return SERIAL_OK; | 957 | return SERIAL_OK; |
| 884 | } | 958 | } |
| 885 | 959 | ||
| @@ -896,6 +970,27 @@ int32_t SerialService::GetCallingUserId() | |||
| 896 | return userId; | 970 | return userId; |
| 897 | } | 971 | } |
| 898 | 972 | ||
| 973 | +int32_t SerialService::GetCallingUserIdFromToken() | ||
| 974 | +{ | ||
| 975 | + AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); | ||
| 976 | + ATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); | ||
| 977 | + | ||
| 978 | + if (tokenType == TOKEN_HAP) { | ||
| 979 | + HapTokenInfo hapTokenInfo; | ||
| 980 | + int32_t ret = AccessTokenKit::GetHapTokenInfo(tokenId, hapTokenInfo); | ||
| 981 | + if (ret == RET_SUCCESS) { | ||
| 982 | + SERIAL_HILOGI(MODULE_SERVICE, "%{public}s:%{public}d HAP token userId=%{public}d", __func__, __LINE__, | ||
| 983 | + hapTokenInfo.userID); | ||
| 984 | + return hapTokenInfo.userID; | ||
| 985 | + } | ||
| 986 | + SERIAL_HILOGE( | ||
| 987 | + MODULE_SERVICE, "%{public}s:%{public}d GetHapTokenInfo failed: %{public}d", __func__, __LINE__, ret); | ||
| 988 | + } | ||
| 989 | + | ||
| 990 | + // For native tokens or failed HAP lookup, fallback to uid-based userId | ||
| 991 | + return GetCallingUserId(); | ||
| 992 | +} | ||
| 993 | + | ||
| 899 | std::string SerialService::GetCallingBundleName() | 994 | std::string SerialService::GetCallingBundleName() |
| 900 | { | 995 | { |
| 901 | AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); | 996 | AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); |
| @@ -934,7 +1029,7 @@ bool SerialService::CheckSerialPortRight(const std::string& portName, const std: | |||
| 934 | return true; | 1029 | return true; |
| 935 | } | 1030 | } |
| 936 | 1031 | ||
| 937 | - int32_t userId = GetCallingUserId(); | 1032 | + int32_t userId = GetCallingUserIdFromToken(); |
| 938 | std::string bundleName = GetCallingBundleName(); | 1033 | std::string bundleName = GetCallingBundleName(); |
| 939 | std::string tokenId = GetCallingTokenId(); | 1034 | std::string tokenId = GetCallingTokenId(); |
| 940 | SERIAL_HILOGI(MODULE_SERVICE, | 1035 | SERIAL_HILOGI(MODULE_SERVICE, |
| @@ -955,7 +1050,7 @@ int32_t SerialService::RequestSerialPortRight(const SerialPortInfo& portInfo) | |||
| 955 | return SERIAL_OK; | 1050 | return SERIAL_OK; |
| 956 | } | 1051 | } |
| 957 | 1052 | ||
| 958 | - int32_t userId = GetCallingUserId(); | 1053 | + int32_t userId = GetCallingUserIdFromToken(); |
| 959 | std::string bundleName = GetCallingBundleName(); | 1054 | std::string bundleName = GetCallingBundleName(); |
| 960 | std::string tokenId = GetCallingTokenId(); | 1055 | std::string tokenId = GetCallingTokenId(); |
| 961 | std::string productName = portInfo.manufacturer.empty() ? portInfo.portName : portInfo.manufacturer; | 1056 | std::string productName = portInfo.manufacturer.empty() ? portInfo.portName : portInfo.manufacturer; |
| @@ -12,5 +12,12 @@ | |||
| 12 | "apl" : "system_basic", | 12 | "apl" : "system_basic", |
| 13 | "secon" : "u:r:serial_service:s0" | 13 | "secon" : "u:r:serial_service:s0" |
| 14 | } | 14 | } |
| 15 | + ], | ||
| 16 | + "jobs" : [{ | ||
| 17 | + "name" : "post-fs-data", | ||
| 18 | + "cmds" : [ | ||
| 19 | + "mkdir /data/service/el1/public/database/serial_manager 0770 serial serial" | ||
| 20 | + ] | ||
| 21 | + } | ||
| 15 | ] | 22 | ] |
| 16 | } | 23 | } |
| @@ -17,7 +17,7 @@ group("serial_manager_tests") { | |||
| 17 | testonly = true | 17 | testonly = true |
| 18 | deps = [ | 18 | deps = [ |
| 19 | "develop:develop_tests", | 19 | "develop:develop_tests", |
| 20 | - "module_test:module_test", | 20 | + "module_test", |
| 21 | - "unittest:unittest", | 21 | + "unittest", |
| 22 | ] | 22 | ] |
| 23 | } | 23 | } |
| @@ -254,6 +254,7 @@ if (serial_manager_enable_service) { | |||
| 254 | public_configs = [ ":serialsrv_errcode_public_config" ] | 254 | public_configs = [ ":serialsrv_errcode_public_config" ] |
| 255 | deps = [ | 255 | deps = [ |
| 256 | "${serial_manager_path}/interfaces/innerkits:serial_server_stub", | 256 | "${serial_manager_path}/interfaces/innerkits:serial_server_stub", |
| 257 | + "${serial_manager_path}/services:serialservice", | ||
| 257 | "${serial_manager_path}/utils:serial_error_code", | 258 | "${serial_manager_path}/utils:serial_error_code", |
| 258 | ] | 259 | ] |
| 259 | 260 | ||
| @@ -261,6 +262,7 @@ if (serial_manager_enable_service) { | |||
| 261 | "ability_base:want", | 262 | "ability_base:want", |
| 262 | "ability_runtime:ability_connect_callback_stub", | 263 | "ability_runtime:ability_connect_callback_stub", |
| 263 | "ability_runtime:ability_manager", | 264 | "ability_runtime:ability_manager", |
| 265 | + "access_token:libaccesstoken_sdk", | ||
| 264 | "c_utils:utils", | 266 | "c_utils:utils", |
| 265 | "drivers_interface_serial:libserials_proxy_1.0", | 267 | "drivers_interface_serial:libserials_proxy_1.0", |
| 266 | "drivers_interface_serial:serials_idl_headers", | 268 | "drivers_interface_serial:serials_idl_headers", |
| @@ -269,6 +271,7 @@ if (serial_manager_enable_service) { | |||
| 269 | "hicollie:libhicollie", | 271 | "hicollie:libhicollie", |
| 270 | "hilog:libhilog", | 272 | "hilog:libhilog", |
| 271 | "ipc:ipc_single", | 273 | "ipc:ipc_single", |
| 274 | + "relational_store:native_rdb", | ||
| 272 | "safwk:system_ability_fwk", | 275 | "safwk:system_ability_fwk", |
| 273 | "samgr:samgr_proxy", | 276 | "samgr:samgr_proxy", |
| 274 | ] | 277 | ] |
Mtest/develop/serial_service_auth_dialog_test.cpp+39-0File content is under review, please try again later
| @@ -67,6 +67,8 @@ if (serial_manager_enable_service) { | |||
| 67 | "ability_runtime:ability_connect_callback_stub", | 67 | "ability_runtime:ability_connect_callback_stub", |
| 68 | "ability_runtime:ability_manager", | 68 | "ability_runtime:ability_manager", |
| 69 | "access_token:libaccesstoken_sdk", | 69 | "access_token:libaccesstoken_sdk", |
| 70 | + "access_token:libnativetoken", | ||
| 71 | + "access_token:libtoken_setproc", | ||
| 70 | "access_token:libtokenid_sdk", | 72 | "access_token:libtokenid_sdk", |
| 71 | "bundle_framework:appexecfwk_base", | 73 | "bundle_framework:appexecfwk_base", |
| 72 | "bundle_framework:appexecfwk_core", | 74 | "bundle_framework:appexecfwk_core", |
| @@ -19,6 +19,7 @@ group("unittest") { | |||
| 19 | deps = [ "serial_srv_client_without_service:unittest" ] | 19 | deps = [ "serial_srv_client_without_service:unittest" ] |
| 20 | if (serial_manager_enable_service) { | 20 | if (serial_manager_enable_service) { |
| 21 | deps += [ | 21 | deps += [ |
| 22 | + "serial_right_manager:unittest", | ||
| 22 | "serial_service:unittest", | 23 | "serial_service:unittest", |
| 23 | "serial_srv_client:unittest", | 24 | "serial_srv_client:unittest", |
| 24 | ] | 25 | ] |
| @@ -0,0 +1,135 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * @file mock_serial_right_db_helper.h | ||
| 18 | + * @brief Mock SerialRightDbHelper for unit testing | ||
| 19 | + * @details 由于 SerialRightDbHelper 方法不是虚方法,使用测试辅助类替代 | ||
| 20 | + * @since 2026-05-05 | ||
| 21 | + */ | ||
| 22 | + | ||
| 23 | +#ifndef MOCK_SERIAL_RIGHT_DB_HELPER_H | ||
| 24 | +#define MOCK_SERIAL_RIGHT_DB_HELPER_H | ||
| 25 | + | ||
| 26 | +#include "serial_right_db_helper.h" | ||
| 27 | +#include <memory> | ||
| 28 | +#include <string> | ||
| 29 | +#include <vector> | ||
| 30 | + | ||
| 31 | +namespace OHOS { | ||
| 32 | +namespace Serial { | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * @class TestDbHelper | ||
| 36 | + * @brief 测试辅助类,用于验证数据库操作逻辑 | ||
| 37 | + * @details 提供 predicate 检查和参数验证功能 | ||
| 38 | + */ | ||
| 39 | +class TestDbHelper { | ||
| 40 | +public: | ||
| 41 | + TestDbHelper() = default; | ||
| 42 | + ~TestDbHelper() = default; | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * @brief 验证 tokenId 是否为纯数字 | ||
| 46 | + */ | ||
| 47 | + static bool IsValidTokenId(const std::string& tokenId) | ||
| 48 | + { | ||
| 49 | + if (tokenId.empty()) { | ||
| 50 | + return false; | ||
| 51 | + } | ||
| 52 | + for (char c : tokenId) { | ||
| 53 | + if (!std::isdigit(c)) { | ||
| 54 | + return false; | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + return true; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * @brief 验证端口名是否有效(非空且非纯空格) | ||
| 62 | + */ | ||
| 63 | + static bool IsValidPortName(const std::string& portName) | ||
| 64 | + { | ||
| 65 | + if (portName.empty()) { | ||
| 66 | + return false; | ||
| 67 | + } | ||
| 68 | + // 检查是否全是空格 | ||
| 69 | + for (char c : portName) { | ||
| 70 | + if (!std::isspace(c)) { | ||
| 71 | + return true; // 有非空格字符,有效 | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + return false; // 全是空格,无效 | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * @brief 验证 bundleName 是否有效(非空且非纯空格) | ||
| 79 | + */ | ||
| 80 | + static bool IsValidBundleName(const std::string& bundleName) | ||
| 81 | + { | ||
| 82 | + if (bundleName.empty()) { | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + // 检查是否全是空格 | ||
| 86 | + for (char c : bundleName) { | ||
| 87 | + if (!std::isspace(c)) { | ||
| 88 | + return true; // 有非空格字符,有效 | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + return false; // 全是空格,无效 | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * @brief 验证 userId 是否有效 | ||
| 96 | + */ | ||
| 97 | + static bool IsValidUserId(int32_t userId) | ||
| 98 | + { | ||
| 99 | + return userId >= 0; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * @brief 验证授权记录参数完整性 | ||
| 104 | + */ | ||
| 105 | + static bool ValidateRightRecordParams( | ||
| 106 | + const std::string& portName, const std::string& bundleName, const std::string& tokenId, int32_t userId) | ||
| 107 | + { | ||
| 108 | + return IsValidPortName(portName) && IsValidBundleName(bundleName) && IsValidTokenId(tokenId) && | ||
| 109 | + IsValidUserId(userId); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 113 | + * @brief 验证过期时间是否有效 | ||
| 114 | + */ | ||
| 115 | + static bool IsValidExpiredTime(uint64_t expiredTime) | ||
| 116 | + { | ||
| 117 | + return expiredTime >= SERIAL_RIGHT_VALID_PERIOD_MIN && expiredTime <= SERIAL_RIGHT_VALID_PERIOD_MAX; | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * @brief 记录是否已过期 | ||
| 122 | + */ | ||
| 123 | + static bool IsRecordExpiredLogic(uint64_t requestTime, uint64_t validPeriod, uint64_t currentTime) | ||
| 124 | + { | ||
| 125 | + if (validPeriod == static_cast<uint64_t>(SERIAL_RIGHT_VALID_PERIOD_SET)) { | ||
| 126 | + return false; // 永久有效 | ||
| 127 | + } | ||
| 128 | + return (requestTime + validPeriod) < currentTime; | ||
| 129 | + } | ||
| 130 | +}; | ||
| 131 | + | ||
| 132 | +} // namespace Serial | ||
| 133 | +} // namespace OHOS | ||
| 134 | + | ||
| 135 | +#endif // MOCK_SERIAL_RIGHT_DB_HELPER_H | ||
| @@ -0,0 +1,86 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 3 | +# you may not use this file except in compliance with the License. | ||
| 4 | +# You may obtain a copy of the License at | ||
| 5 | +# | ||
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 | ||
| 7 | +# | ||
| 8 | +# Unless required by applicable law or agreed to in writing, software | ||
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 11 | +# See the License for the specific language governing permissions and | ||
| 12 | +# limitations under the License. | ||
| 13 | + | ||
| 14 | +import("//build/test.gni") | ||
| 15 | +import("../../../serialmgr.gni") | ||
| 16 | + | ||
| 17 | +if (serial_manager_enable_service) { | ||
| 18 | + config("serial_right_manager_test_config") { | ||
| 19 | + include_dirs = [ | ||
| 20 | + "${serial_manager_path}/test/unittest/include", | ||
| 21 | + "${serial_manager_path}/services/native/include", | ||
| 22 | + "${serial_manager_path}/interfaces/innerkits/native/include", | ||
| 23 | + "${serial_manager_path}/utils/native/include", | ||
| 24 | + ] | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + ohos_unittest("SerialRightManagerTest") { | ||
| 28 | + module_out_path = "busmanager/serial_manager" | ||
| 29 | + | ||
| 30 | + sanitize = { | ||
| 31 | + integer_overflow = true | ||
| 32 | + ubsan = true | ||
| 33 | + boundary_sanitize = true | ||
| 34 | + cfi = true | ||
| 35 | + cfi_cross_dso = true | ||
| 36 | + debug = false | ||
| 37 | + } | ||
| 38 | + branch_protector_ret = "pac_ret" | ||
| 39 | + | ||
| 40 | + cflags_cc = [ | ||
| 41 | + "-Wno-unused-variable", | ||
| 42 | + "-Wno-unused-function", | ||
| 43 | + ] | ||
| 44 | + | ||
| 45 | + sources = [ "serial_right_manager_test.cpp" ] | ||
| 46 | + | ||
| 47 | + configs = [ ":serial_right_manager_test_config" ] | ||
| 48 | + | ||
| 49 | + deps = [ | ||
| 50 | + "${serial_manager_path}/interfaces/innerkits:serial_server_stub", | ||
| 51 | + "${serial_manager_path}/services:serialservice_src", | ||
| 52 | + "${serial_manager_path}/utils:serial_error_code", | ||
| 53 | + ] | ||
| 54 | + | ||
| 55 | + external_deps = [ | ||
| 56 | + "ability_base:want", | ||
| 57 | + "ability_runtime:ability_connect_callback_stub", | ||
| 58 | + "ability_runtime:ability_manager", | ||
| 59 | + "access_token:libaccesstoken_sdk", | ||
| 60 | + "access_token:libtokenid_sdk", | ||
| 61 | + "bundle_framework:appexecfwk_base", | ||
| 62 | + "bundle_framework:appexecfwk_core", | ||
| 63 | + "cJSON:cjson", | ||
| 64 | + "c_utils:utils", | ||
| 65 | + "common_event_service:cesfwk_innerkits", | ||
| 66 | + "googletest:gmock", | ||
| 67 | + "googletest:gtest_main", | ||
| 68 | + "hilog:libhilog", | ||
| 69 | + "ipc:ipc_core", | ||
| 70 | + "ipc:ipc_single", | ||
| 71 | + "os_account:os_account_innerkits", | ||
| 72 | + "relational_store:native_rdb", | ||
| 73 | + "safwk:system_ability_fwk", | ||
| 74 | + "samgr:samgr_proxy", | ||
| 75 | + ] | ||
| 76 | + | ||
| 77 | + defines = [ "private=public" ] | ||
| 78 | + } | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +group("unittest") { | ||
| 82 | + testonly = true | ||
| 83 | + if (serial_manager_enable_service) { | ||
| 84 | + deps = [ ":SerialRightManagerTest" ] | ||
| 85 | + } | ||
| 86 | +} | ||
| @@ -0,0 +1,634 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * @file serial_right_manager_test.cpp | ||
| 18 | + * @brief SerialRightManager 单元测试 | ||
| 19 | + * @details 测试权限管理器的核心功能:参数验证、数据结构、过期逻辑等 | ||
| 20 | + * @since 2026-05-05 | ||
| 21 | + */ | ||
| 22 | + | ||
| 23 | +#include <gmock/gmock.h> | ||
| 24 | +#include <gtest/gtest.h> | ||
| 25 | + | ||
| 26 | +#include "hilog_wrapper.h" | ||
| 27 | +#include "mock_serial_right_db_helper.h" | ||
| 28 | +#include "serial_error_code.h" | ||
| 29 | +#include "serial_right_manager.h" | ||
| 30 | + | ||
| 31 | +using namespace testing; | ||
| 32 | +using namespace testing::ext; | ||
| 33 | +using namespace OHOS::Serial; | ||
| 34 | + | ||
| 35 | +namespace OHOS { | ||
| 36 | +namespace Serial { | ||
| 37 | + | ||
| 38 | +// 测试常量 | ||
| 39 | +constexpr int32_t TEST_UID = 100; | ||
| 40 | +constexpr int32_t TEST_USER_ID = 100; | ||
| 41 | +constexpr const char* TEST_PORT_NAME = "/dev/ttyUSB0"; | ||
| 42 | +constexpr const char* TEST_BUNDLE_NAME = "com.test.serialapp"; | ||
| 43 | +constexpr const char* TEST_TOKEN_ID = "12345678"; | ||
| 44 | +constexpr const char* TEST_TOKEN_ID_INVALID = "abc123"; | ||
| 45 | +constexpr uint64_t TEST_INSTALL_TIME = 1000000; | ||
| 46 | +constexpr uint64_t TEST_UPDATE_TIME = 2000000; | ||
| 47 | +constexpr uint64_t TEST_REQUEST_TIME = 3000000; | ||
| 48 | +constexpr uint64_t TEST_VALID_PERIOD = 0xFFFFFFFFL; | ||
| 49 | +constexpr uint64_t TEST_CURRENT_TIME = 4000000; | ||
| 50 | +constexpr uint64_t TEST_EXPIRED_VALID_PERIOD = 1000; // 短有效期 | ||
| 51 | + | ||
| 52 | +/** | ||
| 53 | + * @class SerialRightManagerTest | ||
| 54 | + * @brief SerialRightManager 单元测试 fixture | ||
| 55 | + */ | ||
| 56 | +class SerialRightManagerTest : public testing::Test { | ||
| 57 | +public: | ||
| 58 | + static void SetUpTestCase() | ||
| 59 | + { | ||
| 60 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::SetUpTestCase"); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + static void TearDownTestCase() | ||
| 64 | + { | ||
| 65 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::TearDownTestCase"); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + void SetUp() override | ||
| 69 | + { | ||
| 70 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::SetUp"); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + void TearDown() override | ||
| 74 | + { | ||
| 75 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::TearDown"); | ||
| 76 | + } | ||
| 77 | +}; | ||
| 78 | + | ||
| 79 | +/** | ||
| 80 | + * @brief 测试 SerialRightManager 单例获取 | ||
| 81 | + */ | ||
| 82 | +HWTEST_F(SerialRightManagerTest, TestGetInstance, TestSize.Level1) | ||
| 83 | +{ | ||
| 84 | + SERIAL_HILOGI(MODULE_SERVICE, "TestGetInstance: start"); | ||
| 85 | + | ||
| 86 | + auto& instance1 = SerialRightManager::GetInstance(); | ||
| 87 | + auto& instance2 = SerialRightManager::GetInstance(); | ||
| 88 | + EXPECT_EQ(&instance1, &instance2); | ||
| 89 | + | ||
| 90 | + SERIAL_HILOGI(MODULE_SERVICE, "TestGetInstance: success"); | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +/** | ||
| 94 | + * @brief 测试 SerialDeviceInfoForDialog 结构体 | ||
| 95 | + */ | ||
| 96 | +HWTEST_F(SerialRightManagerTest, TestDeviceInfoStruct, TestSize.Level1) | ||
| 97 | +{ | ||
| 98 | + SERIAL_HILOGI(MODULE_SERVICE, "TestDeviceInfoStruct: start"); | ||
| 99 | + | ||
| 100 | + SerialDeviceInfoForDialog deviceInfo; | ||