Open
feat: 新增 AddPortAuthorization 接口及权限管理重构 #18
zgene94Add on May 7
feat: 新增 AddPortAuthorization 接口及权限管理重构 #18
Open
Total: 18 files changed+2972-2216
Minterfaces/kits/js/napi/src/serial_info.cpp+35-70File content is under review, please try again later
| @@ -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 deviceId; | ||
| 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; |
| @@ -56,7 +68,7 @@ struct SerialRightTableInfo { | |||
| 56 | int32_t updateTimeIndex; | 68 | int32_t updateTimeIndex; |
| 57 | int32_t requestTimeIndex; | 69 | int32_t requestTimeIndex; |
| 58 | int32_t validPeriodIndex; | 70 | int32_t validPeriodIndex; |
| 59 | - int32_t portNameIndex; | 71 | + int32_t deviceIdIndex; |
| 60 | int32_t bundleNameIndex; | 72 | int32_t bundleNameIndex; |
| 61 | }; | 73 | }; |
| 62 | 74 | ||
| @@ -68,48 +80,41 @@ public: | |||
| 68 | 80 | ||
| 69 | static std::shared_ptr<SerialRightDbHelper> GetInstance(); | 81 | static std::shared_ptr<SerialRightDbHelper> GetInstance(); |
| 70 | 82 | ||
| 71 | - bool IsRecordExpired(int32_t uid, const std::string& portName, const std::string& bundleName, | 83 | + // Check if database is valid and ready for operations |
| 72 | - const std::string& tokenId, uint64_t expiredTime); | 84 | + bool IsDatabaseValid() const { return rdbStore_ != nullptr; } |
| 73 | - bool IsRecordExpired(const SerialRightAppInfo& info, uint64_t expiredTime); | ||
| 74 | 85 | ||
| 75 | - int32_t AddRightRecord(const std::string& portName, const std::string& bundleName, const std::string& tokenId, | 86 | + // 延迟创建模式:OnStart 时尝试打开已有数据库(不创建) |
| 87 | + // OnStop 时创建数据库并保存缓存 | ||
| 88 | + bool OpenDatabaseIfExists(); | ||
| 89 | + bool CreateDatabaseAndOpen(); | ||
| 90 | + void CloseDatabase(); // 关闭数据库连接 | ||
| 91 | + | ||
| 92 | + int32_t AddRightRecord(const std::string& deviceId, const std::string& bundleName, const std::string& tokenId, | ||
| 76 | SerialRightAppInfo& info); | 93 | SerialRightAppInfo& info); |
| 77 | 94 | ||
| 78 | int32_t QueryUserRightRecord(int32_t uid, std::vector<SerialRightAppInfo>& infos); | 95 | int32_t QueryUserRightRecord(int32_t uid, std::vector<SerialRightAppInfo>& infos); |
| 79 | - int32_t QueryPortRightRecord(int32_t uid, const std::string& portName, std::vector<SerialRightAppInfo>& infos); | 96 | + int32_t QueryPortRightRecord(int32_t uid, const std::string& deviceId, std::vector<SerialRightAppInfo>& infos); |
| 80 | int32_t QueryAppRightRecord(int32_t uid, const std::string& bundleName, std::vector<SerialRightAppInfo>& infos); | 97 | int32_t QueryAppRightRecord(int32_t uid, const std::string& bundleName, std::vector<SerialRightAppInfo>& infos); |
| 81 | - int32_t QueryRightRecord(int32_t uid, const std::string& portName, const std::string& bundleName, | 98 | + int32_t QueryRightRecord(int32_t uid, const std::string& deviceId, const std::string& bundleName, |
| 82 | const std::string& tokenId, std::vector<SerialRightAppInfo>& infos); | 99 | const std::string& tokenId, std::vector<SerialRightAppInfo>& infos); |
| 83 | int32_t QueryRightRecordUids(std::vector<std::string>& uids); | 100 | int32_t QueryRightRecordUids(std::vector<std::string>& uids); |
| 84 | int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string>& apps); | 101 | int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string>& apps); |
| 102 | + int32_t QueryAllRightRecords(std::vector<SerialRightFullRecord>& records); | ||
| 85 | 103 | ||
| 86 | - int32_t UpdateRightRecord( | 104 | + // OnStop 保存时批量插入 |
| 87 | - int32_t uid, const std::string& portName, const std::string& bundleName, SerialRightAppInfo& info); | 105 | + int32_t InsertAllRecords(const std::vector<SerialRightFullRecord>& records); |
| 88 | - int32_t AddOrUpdateRightRecord(int32_t uid, const std::string& portName, const std::string& bundleName, | ||
| 89 | - const std::string& tokenId, SerialRightAppInfo& info); | ||
| 90 | 106 | ||
| 91 | - int32_t DeleteRightRecord( | 107 | + // 数据库文件操作 |
| 92 | - int32_t uid, const std::string& portName, const std::string& bundleName, const std::string& tokenId); | 108 | + static std::string GetDatabasePath(); |
| 93 | - int32_t DeletePortRightRecord(int32_t uid, const std::string& portName); | 109 | + bool DeleteDatabaseFile(); // 非静态方法,需要调用 CloseDatabase() |
| 94 | - int32_t DeleteAppRightRecord(int32_t uid, const std::string& bundleName); | ||
| 95 | - int32_t DeleteAppsRightRecord(int32_t uid, const std::vector<std::string>& bundleNames); | ||
| 96 | - int32_t DeleteUidRightRecord(int32_t uid); | ||
| 97 | - int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime); | ||
| 98 | - int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string& portName); | ||
| 99 | 110 | ||
| 100 | private: | 111 | private: |
| 101 | - int32_t CheckIfNeedUpdateEx(bool& isUpdate, int32_t uid, const std::string& portName, const std::string& bundleName, | ||
| 102 | - const std::string& tokenId); | ||
| 103 | - int32_t AddOrUpdateRightRecordEx(bool isUpdate, int32_t uid, const std::string& portName, | ||
| 104 | - const std::string& bundleName, const std::string& tokenId, SerialRightAppInfo& info); | ||
| 105 | int32_t GetResultSetTableInfo(const std::shared_ptr<ResultSet>& resultSet, SerialRightTableInfo& table); | 112 | int32_t GetResultSetTableInfo(const std::shared_ptr<ResultSet>& resultSet, SerialRightTableInfo& table); |
| 106 | int32_t GetResultRightRecordEx(const std::shared_ptr<ResultSet>& resultSet, std::vector<SerialRightAppInfo>& infos); | 113 | int32_t GetResultRightRecordEx(const std::shared_ptr<ResultSet>& resultSet, std::vector<SerialRightAppInfo>& infos); |
| 107 | int32_t QueryAndGetResult(const RdbPredicates& rdbPredicates, const std::vector<std::string>& columns, | 114 | int32_t QueryAndGetResult(const RdbPredicates& rdbPredicates, const std::vector<std::string>& columns, |
| 108 | std::vector<SerialRightAppInfo>& infos); | 115 | std::vector<SerialRightAppInfo>& infos); |
| 109 | - int32_t QueryAndGetResultColumnValues(const RdbPredicates& rdbPredicates, const std::vector<std::string>& columns, | 116 | + int32_t ReadFullRecordFromRow( |
| 110 | - const std::string& columnName, std::vector<std::string>& columnValues); | 117 | + const std::shared_ptr<ResultSet>& resultSet, int32_t rowIndex, SerialRightFullRecord& record); |
| 111 | - int32_t DeleteAndNoOtherOperation(const std::string& whereClause, const std::vector<std::string>& whereArgs); | ||
| 112 | - int32_t DeleteAndNoOtherOperation(const RdbPredicates& rdbPredicates); | ||
| 113 | 118 | ||
| 114 | static std::shared_ptr<SerialRightDbHelper> instance_; | 119 | static std::shared_ptr<SerialRightDbHelper> instance_; |
| 115 | std::mutex databaseMutex_; | 120 | std::mutex databaseMutex_; |
| @@ -23,10 +23,6 @@ | |||
| 23 | #include <vector> | 23 | #include <vector> |
| 24 | 24 | ||
| 25 | #include "ability_connect_callback_stub.h" | 25 | #include "ability_connect_callback_stub.h" |
| 26 | -#include "bundle_mgr_interface.h" | ||
| 27 | -#include "bundle_resource_interface.h" | ||
| 28 | -#include "common_event_subscribe_info.h" | ||
| 29 | -#include "common_event_subscriber.h" | ||
| 30 | #include "serial_right_db_helper.h" | 26 | #include "serial_right_db_helper.h" |
| 31 | 27 | ||
| 32 | // 前向声明,避免头文件依赖 | 28 | // 前向声明,避免头文件依赖 |
| @@ -43,7 +39,6 @@ namespace OHOS { | |||
| 43 | namespace Serial { | 39 | namespace Serial { |
| 44 | 40 | ||
| 45 | using namespace OHOS::AppExecFwk; | 41 | using namespace OHOS::AppExecFwk; |
| 46 | -using namespace OHOS::EventFwk; | ||
| 47 | 42 | ||
| 48 | constexpr int32_t SERIAL_RIGHT_USERID_INVALID = -1; | 43 | constexpr int32_t SERIAL_RIGHT_USERID_INVALID = -1; |
| 49 | constexpr int32_t SERIAL_RIGHT_USERID_DEFAULT = 100; | 44 | constexpr int32_t SERIAL_RIGHT_USERID_DEFAULT = 100; |
| @@ -55,7 +50,7 @@ constexpr int32_t SERIAL_RIGHT_NOP = 1; | |||
| 55 | const std::string SERIAL_RIGHT_DIALOG_TITLE = "Serial Port Authorization"; | 50 | const std::string SERIAL_RIGHT_DIALOG_TITLE = "Serial Port Authorization"; |
| 56 | 51 | ||
| 57 | struct SerialDeviceInfoForDialog { | 52 | struct SerialDeviceInfoForDialog { |
| 58 | - std::string portName; | 53 | + std::string deviceId; |
| 59 | std::string bundleName; | 54 | std::string bundleName; |
| 60 | std::string abilityName; | 55 | std::string abilityName; |
| 61 | std::string tokenId; | 56 | std::string tokenId; |
| @@ -63,7 +58,7 @@ struct SerialDeviceInfoForDialog { | |||
| 63 | std::string productId; | 58 | std::string productId; |
| 64 | std::string vendorId; | 59 | std::string vendorId; |
| 65 | std::string serialNumber; | 60 | std::string serialNumber; |
| 66 | - std::string deviceId; | 61 | + std::string portName; |
| 67 | int32_t portId = 0; | 62 | int32_t portId = 0; |
| 68 | }; | 63 | }; |
| 69 | 64 | ||
| @@ -71,42 +66,31 @@ class SerialRightManager { | |||
| 71 | public: | 66 | public: |
| 72 | static SerialRightManager& GetInstance(); | 67 | static SerialRightManager& GetInstance(); |
| 73 | 68 | ||
| 69 | + // OnStart: 加载缓存(数据库存在则加载,不存在则空缓存) | ||
| 74 | int32_t Init(); | 70 | int32_t Init(); |
| 75 | 71 | ||
| 72 | + // OnStop: 缓存有数据时创建数据库并保存 | ||
| 73 | + int32_t OnStop(); | ||
| 74 | + | ||
| 75 | + // HasRight: 查缓存(不访问数据库) | ||
| 76 | bool HasRight( | 76 | bool HasRight( |
| 77 | - const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); | 77 | + const std::string& deviceId, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); |
| 78 | + | ||
| 79 | + // RequestRight: 弹窗授权流程 | ||
| 78 | int32_t RequestRight(const SerialDeviceInfoForDialog& deviceInfo, const int32_t& userId); | 80 | int32_t RequestRight(const SerialDeviceInfoForDialog& deviceInfo, const int32_t& userId); |
| 79 | - bool AddDeviceRight(const std::string& portName, const std::string& tokenIdStr); | 81 | + |
| 82 | + // AddDeviceRight: 写缓存(不访问数据库) | ||
| 83 | + bool AddDeviceRight(const std::string& deviceId, const std::string& tokenIdStr); | ||
| 80 | bool AddDeviceRight( | 84 | bool AddDeviceRight( |
| 81 | - const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); | 85 | + const std::string& deviceId, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); |
| 82 | - bool RemoveDeviceRight( | 86 | + |
| 83 | - const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); | ||
| 84 | - int32_t CancelDeviceRight( | ||
| 85 | - const std::string& portName, const std::string& bundleName, const std::string& tokenId, const int32_t& userId); | ||
| 86 | - bool RemoveDeviceAllRight(const std::string& portName); | ||
| 87 | bool IsSystemAppOrSa(); | 87 | bool IsSystemAppOrSa(); |
| 88 | - int32_t CleanUpRightExpired(std::vector<std::string>& ports); | ||
| 89 | - | ||
| 90 | - static int32_t CleanUpRightUserDeleted(int32_t& totalUsers, int32_t& deleteUsers); | ||
| 91 | - static int32_t CleanUpRightUserStopped(int32_t uid); | ||
| 92 | - static int32_t CleanUpRightAppUninstalled(int32_t uid, const std::string& bundleName); | ||
| 93 | - | ||
| 94 | - void OnBootComplete(); | ||
| 95 | - int32_t ClearAllRights(); | ||
| 96 | 88 | ||
| 97 | private: | 89 | private: |
| 98 | SerialRightManager(); | 90 | SerialRightManager(); |
| 99 | ~SerialRightManager(); | 91 | ~SerialRightManager(); |
| 100 | DISALLOW_COPY_AND_MOVE(SerialRightManager); | 92 | DISALLOW_COPY_AND_MOVE(SerialRightManager); |
| 101 | 93 | ||
| 102 | - class SerialRightEventSubscriber : public CommonEventSubscriber { | ||
| 103 | - public: | ||
| 104 | - explicit SerialRightEventSubscriber(const CommonEventSubscribeInfo& subscribeInfo); | ||
| 105 | - ~SerialRightEventSubscriber() override = default; | ||
| 106 | - | ||
| 107 | - void OnReceiveEvent(const CommonEventData& data) override; | ||
| 108 | - }; | ||
| 109 | - | ||
| 110 | class SerialAbilityConn : public AAFwk::AbilityConnectionStub { | 94 | class SerialAbilityConn : public AAFwk::AbilityConnectionStub { |
| 111 | public: | 95 | public: |
| 112 | SerialAbilityConn() = default; | 96 | SerialAbilityConn() = default; |
| @@ -116,10 +100,7 @@ private: | |||
| 116 | const ElementName& element, const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override; | 100 | const ElementName& element, const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override; |
| 117 | void OnAbilityDisconnectDone(const ElementName& element, int32_t resultCode) override; | 101 | void OnAbilityDisconnectDone(const ElementName& element, int32_t resultCode) override; |
| 118 | void CloseDialog(); | 102 | void CloseDialog(); |
| 119 | - int32_t GetLastResultCode() const | 103 | + int32_t GetLastResultCode() const { return lastResultCode_; } |
| 120 | - { | ||
| 121 | - return lastResultCode_; | ||
| 122 | - } | ||
| 123 | 104 | ||
| 124 | private: | 105 | private: |
| 125 | std::mutex remoteMutex_; | 106 | std::mutex remoteMutex_; |
| @@ -127,44 +108,33 @@ private: | |||
| 127 | int32_t lastResultCode_ = -1; | 108 | int32_t lastResultCode_ = -1; |
| 128 | }; | 109 | }; |
| 129 | 110 | ||
| 111 | + // 授权弹窗相关 | ||
| 130 | bool GetUserAgreementByDiag(const SerialDeviceInfoForDialog& deviceInfo, const int32_t& userId); | 112 | bool GetUserAgreementByDiag(const SerialDeviceInfoForDialog& deviceInfo, const int32_t& userId); |
| 131 | bool ShowSerialDialog(const SerialDeviceInfoForDialog& deviceInfo, const int32_t userId); | 113 | bool ShowSerialDialog(const SerialDeviceInfoForDialog& deviceInfo, const int32_t userId); |
| 132 | int32_t ConnectAbility(const int32_t userId); | 114 | int32_t ConnectAbility(const int32_t userId); |
| 133 | bool UnShowSerialDialog(); | 115 | bool UnShowSerialDialog(); |
| 134 | - int32_t SubscribeCommonEvents(); | ||
| 135 | 116 | ||
| 136 | - sptr<IBundleMgr> GetBundleMgr(); | 117 | + // 缓存操作 |
| 137 | - sptr<IBundleResource> GetBundleResMgr(); | 118 | + bool LoadCacheFromDb(); |
| 138 | - bool GetAppName(const std::string& bundleName, std::string& appName); | 119 | + bool SaveCacheToDb(); |
| 139 | - bool IsAppInstalled(int32_t uid, const std::string& bundleName); | 120 | + std::string BuildCacheKey(const std::string& deviceId, const std::string& bundleName, |
| 140 | - bool GetBundleInstallAndUpdateTime( | 121 | + const std::string& tokenId, int32_t userId); |
| 141 | - int32_t uid, const std::string& bundleName, uint64_t& installTime, uint64_t& updateTime); | ||
| 142 | - void GetCurrentUserId(int32_t& uid); | ||
| 143 | - uint64_t GetCurrentTimestamp(); | ||
| 144 | - bool IsAllDigits(const std::string& str); | ||
| 145 | 122 | ||
| 146 | - int32_t TidyUpRight(uint32_t choose); | 123 | + // 辅助函数 |
| 147 | - int32_t CleanUpRightTemporaryExpired(const std::string& portName); | ||
| 148 | - int32_t CleanUpRightNormalExpired(int32_t uid); | ||
| 149 | - int32_t CleanUpRightAppUninstalledInternal(int32_t uid, int32_t& totalApps, int32_t& deleteApps); | ||
| 150 | - int32_t CleanUpRightAppReinstalled(int32_t uid, uint32_t& totalApps, uint32_t& deleteApps); | ||
| 151 | - | ||
| 152 | - // Internal helper functions for function decomposition | ||
| 153 | bool ValidateTokenAndGetHapInfo(const std::string& tokenIdStr, Security::AccessToken::AccessTokenID& tokenId, | 124 | bool ValidateTokenAndGetHapInfo(const std::string& tokenIdStr, Security::AccessToken::AccessTokenID& tokenId, |
| 154 | Security::AccessToken::HapTokenInfo& hapTokenInfoRes, int32_t& uid); | 125 | Security::AccessToken::HapTokenInfo& hapTokenInfoRes, int32_t& uid); |
| 155 | - bool BuildInfoAndSaveRight( | 126 | + uint64_t GetCurrentTimestamp(); |
| 156 | - int32_t uid, const std::string& portName, const std::string& bundleName, const std::string& tokenIdStr); | 127 | + bool IsAllDigits(const std::string& str); |
| 157 | - bool AddDeviceRightInternal(const std::string& portName, const std::string& actualBundleName, | ||
| 158 | - const std::string& tokenId, int32_t userId, bool isCliMode); | ||
| 159 | - std::vector<std::string> FindReinstalledBundleNames(int32_t uid, const std::vector<std::string>& apps); | ||
| 160 | 128 | ||
| 161 | static sem_t waitDialogDisappear_; | 129 | static sem_t waitDialogDisappear_; |
| 162 | static std::mutex dialogParamsMutex_; | 130 | static std::mutex dialogParamsMutex_; |
| 163 | static std::map<std::string, std::string> dialogParams_; | 131 | static std::map<std::string, std::string> dialogParams_; |
| 164 | 132 | ||
| 165 | std::mutex dialogRunning_; | 133 | std::mutex dialogRunning_; |
| 134 | + std::mutex cacheMutex_; | ||
| 135 | + std::map<std::string, SerialRightFullRecord> rightCache_; // 权限缓存 | ||
| 136 | + bool cacheLoaded_ = false; | ||
| 166 | sptr<SerialAbilityConn> abilityConn_ = nullptr; | 137 | sptr<SerialAbilityConn> abilityConn_ = nullptr; |
| 167 | - std::shared_ptr<SerialRightEventSubscriber> eventSubscriber_ = nullptr; | ||
| 168 | }; | 138 | }; |
| 169 | 139 | ||
| 170 | } // namespace Serial | 140 | } // namespace Serial |
| @@ -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 | ||
| @@ -12,5 +12,13 @@ | |||
| 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 | + "rm /data/service/el1/public/database/serial_manager/SerialRight.db", | ||
| 20 | + "mkdir /data/service/el1/public/database/serial_manager 0770 serial serial" | ||
| 21 | + ] | ||
| 22 | + } | ||
| 15 | ] | 23 | ] |
| 16 | } | 24 | } |
| @@ -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 | } |
| @@ -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", |
| @@ -68,6 +68,55 @@ if (serial_manager_enable_service) { | |||
| 68 | part_name = "serial_manager" | 68 | part_name = "serial_manager" |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | + # SerialRightTest - Unit tests for SerialRightDbHelper and SerialRightManager | ||
| 72 | + ohos_unittest("SerialRightTest") { | ||
| 73 | + module_out_path = "busmanager/serial_manager" | ||
| 74 | + | ||
| 75 | + sanitize = { | ||
| 76 | + integer_overflow = true | ||
| 77 | + ubsan = true | ||
| 78 | + boundary_sanitize = true | ||
| 79 | + cfi = true | ||
| 80 | + cfi_cross_dso = true | ||
| 81 | + debug = false | ||
| 82 | + } | ||
| 83 | + branch_protector_ret = "pac_ret" | ||
| 84 | + | ||
| 85 | + sources = [ | ||
| 86 | + "serial_right_test.cpp", | ||
| 87 | + "${serial_manager_path}/services/native/src/serial_right_db_helper.cpp", | ||
| 88 | + "${serial_manager_path}/services/native/src/serial_right_manager.cpp", | ||
| 89 | + ] | ||
| 90 | + | ||
| 91 | + cflags_cc = [ | ||
| 92 | + "-Wno-unused-variable", | ||
| 93 | + "-Wno-unused-function", | ||
| 94 | + ] | ||
| 95 | + | ||
| 96 | + configs = [ ":serial_service_test_config" ] | ||
| 97 | + | ||
| 98 | + external_deps = [ | ||
| 99 | + "ability_base:want", | ||
| 100 | + "ability_runtime:ability_connect_callback_stub", | ||
| 101 | + "ability_runtime:ability_manager", | ||
| 102 | + "access_token:libaccesstoken_sdk", | ||
| 103 | + "access_token:libtokenid_sdk", | ||
| 104 | + "bundle_framework:appexecfwk_base", | ||
| 105 | + "bundle_framework:appexecfwk_core", | ||
| 106 | + "cJSON:cjson", | ||
| 107 | + "c_utils:utils", | ||
| 108 | + "common_event_service:cesfwk_innerkits", | ||
| 109 | + "googletest:gmock", | ||
| 110 | + "googletest:gtest_main", | ||
| 111 | + "hilog:libhilog", | ||
| 112 | + "ipc:ipc_core", | ||
| 113 | + "os_account:os_account_innerkits", | ||
| 114 | + "relational_store:native_rdb", | ||
| 115 | + ] | ||
| 116 | + | ||
| 117 | + defines = [ "private=public" ] | ||
| 118 | + } | ||
| 119 | + | ||
| 71 | # SerialServiceTest - Unit tests for SerialService using Mock HDI | 120 | # SerialServiceTest - Unit tests for SerialService using Mock HDI |
| 72 | ohos_unittest("SerialServiceTest") { | 121 | ohos_unittest("SerialServiceTest") { |
| 73 | module_out_path = "busmanager/serial_manager" | 122 | module_out_path = "busmanager/serial_manager" |
| @@ -82,19 +131,23 @@ if (serial_manager_enable_service) { | |||
| 82 | } | 131 | } |
| 83 | branch_protector_ret = "pac_ret" | 132 | branch_protector_ret = "pac_ret" |
| 84 | 133 | ||
| 134 | + sources = [ | ||
| 135 | + "serial_service_test.cpp", | ||
| 136 | + "${serial_manager_path}/services/native/src/serial_right_db_helper.cpp", | ||
| 137 | + "${serial_manager_path}/services/native/src/serial_right_manager.cpp", | ||
| 138 | + "${serial_manager_path}/services/native/src/serial_service.cpp", | ||
| 139 | + ] | ||
| 140 | + | ||
| 85 | cflags_cc = [ | 141 | cflags_cc = [ |
| 86 | "-Wno-unused-variable", | 142 | "-Wno-unused-variable", |
| 87 | "-Wno-unused-function", | 143 | "-Wno-unused-function", |
| 88 | ] | 144 | ] |
| 89 | 145 | ||
| 90 | - sources = [ "serial_service_test.cpp" ] | ||
| 91 | - | ||
| 92 | configs = [ ":serial_service_test_config" ] | 146 | configs = [ ":serial_service_test_config" ] |
| 93 | 147 | ||
| 94 | deps = [ | 148 | deps = [ |
| 95 | ":mock_serial_hdi_src", | 149 | ":mock_serial_hdi_src", |
| 96 | "${serial_manager_path}/interfaces/innerkits:serial_server_stub", | 150 | "${serial_manager_path}/interfaces/innerkits:serial_server_stub", |
| 97 | - "${serial_manager_path}/services:serialservice", | ||
| 98 | "${serial_manager_path}/utils:serial_error_code", | 151 | "${serial_manager_path}/utils:serial_error_code", |
| 99 | ] | 152 | ] |
| 100 | 153 | ||
| @@ -132,6 +185,9 @@ if (serial_manager_enable_service) { | |||
| 132 | group("unittest") { | 185 | group("unittest") { |
| 133 | testonly = true | 186 | testonly = true |
| 134 | if (serial_manager_enable_service) { | 187 | if (serial_manager_enable_service) { |
| 135 | - deps = [ ":SerialServiceTest" ] | 188 | + deps = [ |
| 189 | + ":SerialServiceTest", | ||
| 190 | + ":SerialRightTest", | ||
| 191 | + ] | ||
| 136 | } | 192 | } |
| 137 | -} | 193 | +} |
Mtest/unittest/serial_service/mock_serial_hdi.cpp+84-44File content is under review, please try again later
Mtest/unittest/serial_service/mock_serial_hdi.h+35-42File content is under review, please try again later
| @@ -0,0 +1,500 @@ | |||
| 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_test.cpp | ||
| 18 | + * @brief SerialRightDbHelper 和 SerialRightManager 单元测试(缓存模式) | ||
| 19 | + * @details 本文件测试: | ||
| 20 | + * - SerialRightDbHelper 数据库加载和保存操作 | ||
| 21 | + * - SerialRightManager 缓存机制 | ||
| 22 | + * - OnStart 加载、OnStop 保存流程 | ||
| 23 | + * @since 2026-05-08 | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | +#include <gmock/gmock.h> | ||
| 27 | +#include <gtest/gtest.h> | ||
| 28 | +#include <sys/stat.h> | ||
| 29 | +#include <unistd.h> | ||
| 30 | + | ||
| 31 | +#include "hilog_wrapper.h" | ||
| 32 | +#include "serial_error_code.h" | ||
| 33 | +#include "serial_right_db_helper.h" | ||
| 34 | +#include "serial_right_manager.h" | ||
| 35 | + | ||
| 36 | +using namespace testing; | ||
| 37 | +using namespace testing::ext; | ||
| 38 | +using namespace OHOS::Serial; | ||
| 39 | + | ||
| 40 | +namespace OHOS { | ||
| 41 | +namespace Serial { | ||
| 42 | + | ||
| 43 | +namespace { | ||
| 44 | +constexpr const char* TEST_DB_DIR = "/data/service/el1/public/database/serial_manager/"; | ||
| 45 | +constexpr int32_t TEST_USER_ID = 100; | ||
| 46 | +constexpr int32_t TEST_USER_ID_2 = 101; | ||
| 47 | +constexpr const char* TEST_BUNDLE_NAME = "com.test.serialapp"; | ||
| 48 | +constexpr const char* TEST_BUNDLE_NAME_2 = "com.test.serialapp2"; | ||
| 49 | +constexpr const char* TEST_TOKEN_ID = "12345678"; | ||
| 50 | +constexpr const char* TEST_TOKEN_ID_2 = "87654321"; | ||
| 51 | +constexpr const char* TEST_DEVICE_ID_1 = "5678_1234_ttyUSB0"; | ||
| 52 | +constexpr const char* TEST_DEVICE_ID_2 = "2222_1111_ttyUSB1"; | ||
| 53 | +constexpr const char* TEST_DEVICE_ID_WITH_SN = "5678_1234_SN123456_ttyUSB0"; | ||
| 54 | +constexpr uint64_t TEST_TIMESTAMP = 1700000000; | ||
| 55 | +} // namespace | ||
| 56 | + | ||
| 57 | +// ============================================================================ | ||
| 58 | +// SerialRightDbHelper Test Fixture (简化版 - 只测试加载和保存) | ||
| 59 | +// ============================================================================ | ||
| 60 | + | ||
| 61 | +class SerialRightDbHelperTest : public testing::Test { | ||
| 62 | +public: | ||
| 63 | + static void SetUpTestCase() | ||
| 64 | + { | ||
| 65 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightDbHelperTest::SetUpTestCase"); | ||
| 66 | + struct stat st; | ||
| 67 | + if (stat(TEST_DB_DIR, &st) != 0) { | ||
| 68 | + mkdir(TEST_DB_DIR, 0755); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + static void TearDownTestCase() | ||
| 73 | + { | ||
| 74 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightDbHelperTest::TearDownTestCase"); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + void SetUp() override | ||
| 78 | + { | ||
| 79 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightDbHelperTest::SetUp"); | ||
| 80 | + dbHelper_ = SerialRightDbHelper::GetInstance(); | ||
| 81 | + ASSERT_NE(dbHelper_, nullptr); | ||
| 82 | + // 删除测试数据库文件 | ||
| 83 | + dbHelper_->DeleteDatabaseFile(); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + void TearDown() override | ||
| 87 | + { | ||
| 88 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightDbHelperTest::TearDown"); | ||
| 89 | + dbHelper_->DeleteDatabaseFile(); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | +protected: | ||
| 93 | + std::shared_ptr<SerialRightDbHelper> dbHelper_; | ||
| 94 | +}; | ||
| 95 | + | ||
| 96 | +// ============================================================================ | ||
| 97 | +// 1. 数据库基本状态测试 | ||
| 98 | +// ============================================================================ | ||
| 99 | + | ||
| 100 | +HWTEST_F(SerialRightDbHelperTest, GetInstance_ReturnsSingleton, TestSize.Level1) | ||
| 101 | +{ | ||
| 102 | + auto instance1 = SerialRightDbHelper::GetInstance(); | ||
| 103 | + auto instance2 = SerialRightDbHelper::GetInstance(); | ||
| 104 | + EXPECT_EQ(instance1.get(), instance2.get()); | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +HWTEST_F(SerialRightDbHelperTest, OpenDatabaseIfExists_NotExists_ReturnsFalse, TestSize.Level1) | ||
| 108 | +{ | ||
| 109 | + // 确保数据库文件不存在 | ||
| 110 | + dbHelper_->DeleteDatabaseFile(); | ||
| 111 | + | ||
| 112 | + bool result = dbHelper_->OpenDatabaseIfExists(); | ||
| 113 | + EXPECT_FALSE(result); | ||
| 114 | + EXPECT_FALSE(dbHelper_->IsDatabaseValid()); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +HWTEST_F(SerialRightDbHelperTest, CreateDatabaseAndOpen_Success, TestSize.Level1) | ||
| 118 | +{ | ||
| 119 | + bool result = dbHelper_->CreateDatabaseAndOpen(); | ||
| 120 | + EXPECT_TRUE(result); | ||
| 121 | + EXPECT_TRUE(dbHelper_->IsDatabaseValid()); | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +HWTEST_F(SerialRightDbHelperTest, CreateDatabaseAndOpen_ThenOpenIfExists_ReturnsTrue, TestSize.Level1) | ||
| 125 | +{ | ||
| 126 | + // 先创建 | ||
| 127 | + dbHelper_->CreateDatabaseAndOpen(); | ||
| 128 | + EXPECT_TRUE(dbHelper_->IsDatabaseValid()); | ||
| 129 | + | ||
| 130 | + // 关闭数据库连接 | ||
| 131 | + dbHelper_->CloseDatabase(); | ||
| 132 | + EXPECT_FALSE(dbHelper_->IsDatabaseValid()); | ||
| 133 | + | ||
| 134 | + // 重新打开已存在的数据库 | ||
| 135 | + bool result = dbHelper_->OpenDatabaseIfExists(); | ||
| 136 | + EXPECT_TRUE(result); | ||
| 137 | + EXPECT_TRUE(dbHelper_->IsDatabaseValid()); | ||
| 138 | +} | ||
| 139 | + | ||
| 140 | +HWTEST_F(SerialRightDbHelperTest, QueryAllRightRecords_EmptyDb_ReturnsZero, TestSize.Level1) | ||
| 141 | +{ | ||
| 142 | + dbHelper_->CreateDatabaseAndOpen(); | ||
| 143 | + | ||
| 144 | + std::vector<SerialRightFullRecord> records; | ||
| 145 | + int32_t ret = dbHelper_->QueryAllRightRecords(records); | ||
| 146 | + EXPECT_EQ(ret, 0); | ||
| 147 | + EXPECT_EQ(records.size(), 0); | ||
| 148 | +} | ||
| 149 | + | ||
| 150 | +HWTEST_F(SerialRightDbHelperTest, InsertAllRecords_Success, TestSize.Level1) | ||
| 151 | +{ | ||
| 152 | + // 确保先删除旧数据库 | ||
| 153 | + dbHelper_->DeleteDatabaseFile(); | ||
| 154 | + | ||
| 155 | + dbHelper_->CreateDatabaseAndOpen(); | ||
| 156 | + EXPECT_TRUE(dbHelper_->IsDatabaseValid()); | ||
| 157 | + | ||
| 158 | + SerialRightFullRecord record1 = {}; | ||
| 159 | + record1.uid = TEST_USER_ID; | ||
| 160 | + record1.deviceId = TEST_DEVICE_ID_1; | ||
| 161 | + record1.bundleName = TEST_BUNDLE_NAME; | ||
| 162 | + record1.tokenId = TEST_TOKEN_ID; | ||
| 163 | + record1.installTime = TEST_TIMESTAMP; | ||
| 164 | + record1.updateTime = TEST_TIMESTAMP; | ||
| 165 | + record1.requestTime = TEST_TIMESTAMP; | ||
| 166 | + record1.validPeriod = SERIAL_RIGHT_VALID_PERIOD_MAX; | ||
| 167 | + | ||
| 168 | + SerialRightFullRecord record2 = {}; | ||
| 169 | + record2.uid = TEST_USER_ID; | ||
| 170 | + record2.deviceId = TEST_DEVICE_ID_2; | ||
| 171 | + record2.bundleName = TEST_BUNDLE_NAME; | ||
| 172 | + record2.tokenId = TEST_TOKEN_ID; | ||
| 173 | + record2.installTime = TEST_TIMESTAMP; | ||
| 174 | + record2.updateTime = TEST_TIMESTAMP; | ||
| 175 | + record2.requestTime = TEST_TIMESTAMP; | ||
| 176 | + record2.validPeriod = SERIAL_RIGHT_VALID_PERIOD_MAX; | ||
| 177 | + | ||
| 178 | + std::vector<SerialRightFullRecord> records = {record1, record2}; | ||
| 179 | + int32_t ret = dbHelper_->InsertAllRecords(records); | ||
| 180 | + EXPECT_EQ(ret, E_OK); | ||
| 181 | + | ||
| 182 | + // 关闭后重新打开再查询 | ||
| 183 | + dbHelper_->CloseDatabase(); | ||
| 184 | + dbHelper_->OpenDatabaseIfExists(); | ||
| 185 | + | ||
| 186 | + std::vector<SerialRightFullRecord> queryRecords; | ||
| 187 | + ret = dbHelper_->QueryAllRightRecords(queryRecords); | ||
| 188 | + EXPECT_GT(ret, 0); | ||
| 189 | + EXPECT_EQ(queryRecords.size(), 2); | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +HWTEST_F(SerialRightDbHelperTest, InsertAllRecords_EmptyList_ReturnsOk, TestSize.Level1) | ||
| 193 | +{ | ||
| 194 | + dbHelper_->CreateDatabaseAndOpen(); | ||
| 195 | + | ||
| 196 | + std::vector<SerialRightFullRecord> emptyRecords; | ||
| 197 | + int32_t ret = dbHelper_->InsertAllRecords(emptyRecords); | ||
| 198 | + EXPECT_EQ(ret, E_OK); | ||
| 199 | +} | ||
| 200 | + | ||
| 201 | +HWTEST_F(SerialRightDbHelperTest, GetDatabasePath_ReturnsCorrectPath, TestSize.Level1) | ||
| 202 | +{ | ||
| 203 | + std::string path = SerialRightDbHelper::GetDatabasePath(); | ||
| 204 | + EXPECT_TRUE(path.find("SerialRight.db") != std::string::npos); | ||
| 205 | + EXPECT_TRUE(path.find("serial_manager") != std::string::npos); | ||
| 206 | +} | ||
| 207 | + | ||
| 208 | +HWTEST_F(SerialRightDbHelperTest, DeleteDatabaseFile_RemovesFile, TestSize.Level1) | ||
| 209 | +{ | ||
| 210 | + // 创建数据库文件 | ||
| 211 | + dbHelper_->CreateDatabaseAndOpen(); | ||
| 212 | + EXPECT_TRUE(dbHelper_->IsDatabaseValid()); | ||
| 213 | + | ||
| 214 | + // 先关闭数据库连接 | ||
| 215 | + dbHelper_->CloseDatabase(); | ||
| 216 | + EXPECT_FALSE(dbHelper_->IsDatabaseValid()); | ||
| 217 | + | ||
| 218 | + // 删除文件 | ||
| 219 | + bool result = dbHelper_->DeleteDatabaseFile(); | ||
| 220 | + EXPECT_TRUE(result); | ||
| 221 | + | ||
| 222 | + // 验证文件不存在 | ||
| 223 | + std::string path = SerialRightDbHelper::GetDatabasePath(); | ||
| 224 | + struct stat st; | ||
| 225 | + EXPECT_NE(stat(path.c_str(), &st), 0); | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +// ============================================================================ | ||
| 229 | +// SerialRightManager Test Fixture (缓存测试) | ||
| 230 | +// ============================================================================ | ||
| 231 | + | ||
| 232 | +class SerialRightManagerTest : public testing::Test { | ||
| 233 | +public: | ||
| 234 | + static void SetUpTestCase() | ||
| 235 | + { | ||
| 236 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::SetUpTestCase"); | ||
| 237 | + struct stat st; | ||
| 238 | + if (stat(TEST_DB_DIR, &st) != 0) { | ||
| 239 | + mkdir(TEST_DB_DIR, 0755); | ||
| 240 | + } | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + static void TearDownTestCase() | ||
| 244 | + { | ||
| 245 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::TearDownTestCase"); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + void SetUp() override | ||
| 249 | + { | ||
| 250 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::SetUp"); | ||
| 251 | + dbHelper_ = SerialRightDbHelper::GetInstance(); | ||
| 252 | + // 删除测试数据库文件,确保干净状态 | ||
| 253 | + dbHelper_->DeleteDatabaseFile(); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + void TearDown() override | ||
| 257 | + { | ||
| 258 | + SERIAL_HILOGI(MODULE_SERVICE, "SerialRightManagerTest::TearDown"); | ||
| 259 | + dbHelper_->DeleteDatabaseFile(); | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | +protected: | ||
| 263 | + std::shared_ptr<SerialRightDbHelper> dbHelper_; | ||
| 264 | +}; | ||
| 265 | + | ||
| 266 | +// ============================================================================ | ||
| 267 | +// 1. SerialRightManager 基本测试 | ||
| 268 | +// ============================================================================ | ||
| 269 | + | ||
| 270 | +HWTEST_F(SerialRightManagerTest, GetInstance_ReturnsSingleton, TestSize.Level1) | ||
| 271 | +{ | ||
| 272 | + auto& instance1 = SerialRightManager::GetInstance(); | ||
| 273 | + auto& instance2 = SerialRightManager::GetInstance(); | ||
| 274 | + EXPECT_EQ(&instance1, &instance2); | ||
| 275 | +} | ||
| 276 | + | ||
| 277 | +HWTEST_F(SerialRightManagerTest, Init_NoDb_UsesEmptyCache, TestSize.Level1) | ||
| 278 | +{ | ||
| 279 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 280 | + | ||
| 281 | + // 数据库不存在,Init 应成功加载空缓存 | ||
| 282 | + int32_t ret = manager.Init(); | ||
| 283 | + EXPECT_EQ(ret, SERIAL_RIGHT_OK); | ||
| 284 | + | ||
| 285 | + // HasRight 应返回 false(缓存为空) | ||
| 286 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 287 | + SERIAL_HILOGI(MODULE_SERVICE, "HasRight after Init with empty cache: %{public}d", hasRight); | ||
| 288 | +} | ||
| 289 | + | ||
| 290 | +HWTEST_F(SerialRightManagerTest, AddDeviceRight_WritesToCache, TestSize.Level1) | ||
| 291 | +{ | ||
| 292 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 293 | + manager.Init(); // 加载空缓存 | ||
| 294 | + | ||
| 295 | + // 添加授权 | ||
| 296 | + bool result = manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 297 | + EXPECT_TRUE(result); | ||
| 298 | + | ||
| 299 | + // 验证 HasRight 返回 true(缓存中有记录) | ||
| 300 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 301 | + EXPECT_TRUE(hasRight); | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +HWTEST_F(SerialRightManagerTest, AddDeviceRight_DuplicateKey_UpdatesCache, TestSize.Level1) | ||
| 305 | +{ | ||
| 306 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 307 | + manager.Init(); | ||
| 308 | + | ||
| 309 | + // 第一次添加 | ||
| 310 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 311 | + | ||
| 312 | + // 第二次添加(相同key,应更新) | ||
| 313 | + bool result = manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 314 | + EXPECT_TRUE(result); | ||
| 315 | + | ||
| 316 | + // HasRight 应仍返回 true | ||
| 317 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 318 | + EXPECT_TRUE(hasRight); | ||
| 319 | +} | ||
| 320 | + | ||
| 321 | +HWTEST_F(SerialRightManagerTest, HasRight_MismatchDeviceId_ReturnsFalse, TestSize.Level1) | ||
| 322 | +{ | ||
| 323 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 324 | + manager.Init(); | ||
| 325 | + | ||
| 326 | + // 添加授权 | ||
| 327 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 328 | + | ||
| 329 | + // 查询不同 deviceId | ||
| 330 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_2, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 331 | + EXPECT_FALSE(hasRight); | ||
| 332 | +} | ||
| 333 | + | ||
| 334 | +HWTEST_F(SerialRightManagerTest, HasRight_MismatchBundleName_ReturnsFalse, TestSize.Level1) | ||
| 335 | +{ | ||
| 336 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 337 | + manager.Init(); | ||
| 338 | + | ||
| 339 | + // 添加授权 | ||
| 340 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 341 | + | ||
| 342 | + // 查询不同 bundleName | ||
| 343 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME_2, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 344 | + EXPECT_FALSE(hasRight); | ||
| 345 | +} | ||
| 346 | + | ||
| 347 | +HWTEST_F(SerialRightManagerTest, HasRight_MismatchUserId_ReturnsFalse, TestSize.Level1) | ||
| 348 | +{ | ||
| 349 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 350 | + manager.Init(); | ||
| 351 | + | ||
| 352 | + // 添加授权(userId=100) | ||
| 353 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 354 | + | ||
| 355 | + // 查询不同 userId | ||
| 356 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID_2); | ||
| 357 | + EXPECT_FALSE(hasRight); | ||
| 358 | +} | ||
| 359 | + | ||
| 360 | +HWTEST_F(SerialRightManagerTest, OnStop_SavesCacheToDb, TestSize.Level1) | ||
| 361 | +{ | ||
| 362 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 363 | + manager.Init(); | ||
| 364 | + | ||
| 365 | + // 添加授权到缓存 | ||
| 366 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 367 | + manager.AddDeviceRight(TEST_DEVICE_ID_2, TEST_BUNDLE_NAME_2, TEST_TOKEN_ID_2, TEST_USER_ID); | ||
| 368 | + | ||
| 369 | + // OnStop 保存缓存到数据库 | ||
| 370 | + int32_t ret = manager.OnStop(); | ||
| 371 | + EXPECT_EQ(ret, SERIAL_RIGHT_OK); | ||
| 372 | + | ||
| 373 | + // 验证数据库文件存在 | ||
| 374 | + std::string dbPath = SerialRightDbHelper::GetDatabasePath(); | ||
| 375 | + struct stat st; | ||
| 376 | + EXPECT_EQ(stat(dbPath.c_str(), &st), 0); | ||
| 377 | + | ||
| 378 | + // 验证数据库有记录 | ||
| 379 | + auto dbHelper = SerialRightDbHelper::GetInstance(); | ||
| 380 | + dbHelper->OpenDatabaseIfExists(); | ||
| 381 | + std::vector<SerialRightFullRecord> records; | ||
| 382 | + ret = dbHelper->QueryAllRightRecords(records); | ||
| 383 | + EXPECT_GT(ret, 0); | ||
| 384 | + EXPECT_EQ(records.size(), 2); | ||
| 385 | +} | ||
| 386 | + | ||
| 387 | +HWTEST_F(SerialRightManagerTest, OnStop_EmptyCache_SkipsSave, TestSize.Level1) | ||
| 388 | +{ | ||
| 389 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 390 | + manager.Init(); // 空缓存 | ||
| 391 | + | ||
| 392 | + // OnStop 不应创建数据库文件 | ||
| 393 | + int32_t ret = manager.OnStop(); | ||
| 394 | + EXPECT_EQ(ret, SERIAL_RIGHT_OK); | ||
| 395 | + | ||
| 396 | + // 验证数据库文件不存在(因为缓存为空) | ||
| 397 | + std::string dbPath = SerialRightDbHelper::GetDatabasePath(); | ||
| 398 | + struct stat st; | ||
| 399 | + // 注意:之前测试可能已创建文件,这里不强制断言 | ||
| 400 | + SERIAL_HILOGI(MODULE_SERVICE, "Database exists after OnStop with empty cache: %{public}d", | ||
| 401 | + stat(dbPath.c_str(), &st) == 0); | ||
| 402 | +} | ||
| 403 | + | ||
| 404 | +// ============================================================================ | ||
| 405 | +// 3. 多用户场景测试 | ||
| 406 | +// ============================================================================ | ||
| 407 | + | ||
| 408 | +HWTEST_F(SerialRightManagerTest, MultiUser_AuthorizationsIndependent, TestSize.Level1) | ||
| 409 | +{ | ||
| 410 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 411 | + manager.Init(); | ||
| 412 | + | ||
| 413 | + // 用户1授权 | ||
| 414 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 415 | + | ||
| 416 | + // 用户2授权同一设备 | ||
| 417 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID_2); | ||
| 418 | + | ||
| 419 | + // 用户1的授权独立 | ||
| 420 | + bool hasRight1 = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 421 | + EXPECT_TRUE(hasRight1); | ||
| 422 | + | ||
| 423 | + // 用户2的授权独立 | ||
| 424 | + bool hasRight2 = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID_2); | ||
| 425 | + EXPECT_TRUE(hasRight2); | ||
| 426 | +} | ||
| 427 | + | ||
| 428 | +// ============================================================================ | ||
| 429 | +// 4. deviceId 不同格式测试 | ||
| 430 | +// ============================================================================ | ||
| 431 | + | ||
| 432 | +HWTEST_F(SerialRightManagerTest, AddDeviceRight_DifferentDeviceIdFormats, TestSize.Level1) | ||
| 433 | +{ | ||
| 434 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 435 | + manager.Init(); | ||
| 436 | + | ||
| 437 | + // 格式1: vid_pid_portName | ||
| 438 | + bool result1 = manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 439 | + EXPECT_TRUE(result1); | ||
| 440 | + | ||
| 441 | + // 格式2: vid_pid_sn_portName | ||
| 442 | + bool result2 = manager.AddDeviceRight(TEST_DEVICE_ID_WITH_SN, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 443 | + EXPECT_TRUE(result2); | ||
| 444 | + | ||
| 445 | + // 验证两种格式都能正确查询 | ||
| 446 | + bool hasRight1 = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 447 | + EXPECT_TRUE(hasRight1); | ||
| 448 | + | ||
| 449 | + bool hasRight2 = manager.HasRight(TEST_DEVICE_ID_WITH_SN, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 450 | + EXPECT_TRUE(hasRight2); | ||
| 451 | +} | ||
| 452 | + | ||
| 453 | +// ============================================================================ | ||
| 454 | +// 5. 缓存加载测试(模拟 SA 重启) | ||
| 455 | +// ============================================================================ | ||
| 456 | + | ||
| 457 | +HWTEST_F(SerialRightManagerTest, Init_WithExistingDb_LoadsCache, TestSize.Level1) | ||
| 458 | +{ | ||
| 459 | + // 场景:模拟 SA 空闲自杀后重新拉起 | ||
| 460 | + | ||
| 461 | + // 第一步:初始授权并保存 | ||
| 462 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 463 | + manager.Init(); | ||
| 464 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 465 | + manager.OnStop(); // 模拟 SA 空闲自杀,保存缓存 | ||
| 466 | + | ||
| 467 | + // 第二步:模拟 SA 重新拉起,Init 加载缓存 | ||
| 468 | + // 注意:GetInstance 返回单例,实际上 Init 会重新加载 | ||
| 469 | + int32_t ret = manager.Init(); | ||
| 470 | + EXPECT_EQ(ret, SERIAL_RIGHT_OK); | ||
| 471 | + | ||
| 472 | + // 第三步:验证缓存中有之前的授权 | ||
| 473 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 474 | + EXPECT_TRUE(hasRight); | ||
| 475 | +} | ||
| 476 | + | ||
| 477 | +HWTEST_F(SerialRightManagerTest, Init_AfterDbDeleted_UsesEmptyCache, TestSize.Level1) | ||
| 478 | +{ | ||
| 479 | + // 场景:模拟设备重启(init 删除数据库) | ||
| 480 | + | ||
| 481 | + // 先创建授权并保存 | ||
| 482 | + auto& manager = SerialRightManager::GetInstance(); | ||
| 483 | + manager.Init(); | ||
| 484 | + manager.AddDeviceRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 485 | + manager.OnStop(); | ||
| 486 | + | ||
| 487 | + // 模拟 init 删除数据库文件(重启) | ||
| 488 | + dbHelper_->DeleteDatabaseFile(); | ||
| 489 | + | ||
| 490 | + // Init 应加载空缓存 | ||
| 491 | + int32_t ret = manager.Init(); | ||
| 492 | + EXPECT_EQ(ret, SERIAL_RIGHT_OK); | ||
| 493 | + | ||
| 494 | + // 验证无授权 | ||
| 495 | + bool hasRight = manager.HasRight(TEST_DEVICE_ID_1, TEST_BUNDLE_NAME, TEST_TOKEN_ID, TEST_USER_ID); | ||
| 496 | + EXPECT_FALSE(hasRight); | ||
| 497 | +} | ||
| 498 | + | ||
| 499 | +} // namespace Serial | ||
| 500 | +} // namespace OHOS | ||