diff --git a/services/edm/src/admin_policies_storage_rdb.cpp b/services/edm/src/admin_policies_storage_rdb.cpp deleted file mode 100644 index 96ba788bd8da1ae6cde48af8a4cb3830a9307a07..0000000000000000000000000000000000000000 --- a/services/edm/src/admin_policies_storage_rdb.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "admin_policies_storage_rdb.h" - -#include "edm_constants.h" -#include "edm_log.h" -#include "edm_rdb_filed_const.h" - -namespace OHOS { -namespace EDM { -std::shared_ptr AdminPoliciesStorageRdb::instance_ = nullptr; -std::mutex AdminPoliciesStorageRdb::mutexLock_; - -AdminPoliciesStorageRdb::AdminPoliciesStorageRdb() -{ - EDMLOGD("AdminPoliciesStorageRdb::create database."); - std::string createTableSql = "CREATE TABLE IF NOT EXISTS "; - createTableSql.append(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME + " (") - .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,") - .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,") - .append(EdmRdbFiledConst::FILED_ADMIN_TYPE + " INTEGER NOT NULL,") - .append(EdmRdbFiledConst::FILED_PACKAGE_NAME + " TEXT NOT NULL,") - .append(EdmRdbFiledConst::FILED_CLASS_NAME + " TEXT NOT NULL,") - .append(EdmRdbFiledConst::FILED_ENT_NAME + " TEXT,") - .append(EdmRdbFiledConst::FILED_ENT_DESC + " TEXT,") - .append(EdmRdbFiledConst::FILED_PERMISSIONS + " TEXT,") - .append(EdmRdbFiledConst::FILED_SUBSCRIBE_EVENTS + " TEXT,") - .append(EdmRdbFiledConst::FILED_PARENT_ADMIN + " TEXT,") - .append(EdmRdbFiledConst::FILED_IS_DEBUG + " TEXT);"); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager != nullptr) { - edmRdbDataManager->CreateTable(createTableSql); - } else { - EDMLOGE("AdminPoliciesStorageRdb::create database admin_policies failed."); - } -} - -std::shared_ptr AdminPoliciesStorageRdb::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock(mutexLock_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -bool AdminPoliciesStorageRdb::InsertAdmin(int32_t userId, const Admin &admin) -{ - EDMLOGD("AdminPoliciesStorageRdb::Insert data start."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::InsertAdmin get edmRdbDataManager failed."); - return false; - } - // insert into admin_policies(user_id, admin_type, package_name, class_name, ent_name, ent_desc, permissions, - // is_debug) values(?, ?, ?, ?, ?, ?, ?) - return edmRdbDataManager->Insert(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME, - CreateValuesBucket(userId, admin)); -} - -bool AdminPoliciesStorageRdb::UpdateAdmin(int32_t userId, const Admin &admin) -{ - EDMLOGD("AdminPoliciesStorageRdb::Insert data start."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::UpdateAdmin get edmRdbDataManager failed."); - return false; - } - // update admin_policies set user_id=?, admin_type=?, package_name=?, class_name=?, ent_name=?, ent_desc=?, - // permissions=? where user_id=? and package_name=? - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, admin.adminInfo_.packageName_); - return edmRdbDataManager->Update(CreateValuesBucket(userId, admin), predicates); -} - -NativeRdb::ValuesBucket AdminPoliciesStorageRdb::CreateValuesBucket(int32_t userId, const Admin &admin) -{ - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId); - valuesBucket.PutInt(EdmRdbFiledConst::FILED_ADMIN_TYPE, static_cast(admin.adminInfo_.adminType_)); - valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, admin.adminInfo_.packageName_); - valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, admin.adminInfo_.className_); - valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_NAME, admin.adminInfo_.entInfo_.enterpriseName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_DESC, admin.adminInfo_.entInfo_.description); - - if (!admin.adminInfo_.permission_.empty()) { - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - Json::Value permissionJson; - for (const auto &it : admin.adminInfo_.permission_) { - permissionJson.append(it); - } - valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson)); - } - valuesBucket.PutBool(EdmRdbFiledConst::FILED_IS_DEBUG, admin.adminInfo_.isDebug_); - return valuesBucket; -} - -bool AdminPoliciesStorageRdb::DeleteAdmin(int32_t userId, const std::string &packageName) -{ - EDMLOGD("AdminPoliciesStorageRdb::DeleteAdmin."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::DeleteAdmin get edmRdbDataManager failed."); - return false; - } - // delete from admin_policies where user_id=? and package_name=? - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName); - return edmRdbDataManager->Delete(predicates); -} - -bool AdminPoliciesStorageRdb::UpdateAdmin(int32_t userId, const std::string &packageName, const std::string &className, - const std::vector &permissions) -{ - EDMLOGD("AdminPoliciesStorageRdb::UpdateAdmin."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::UpdateAdmin get edmRdbDataManager failed."); - return false; - } - // update admin_policies set package_name=?, class_name=?, permissions=? where user_id=? and package_name=? - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, className); - - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - Json::Value permissionJson; - for (const auto &it : permissions) { - permissionJson.append(it); - } - valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson)); - - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName); - return edmRdbDataManager->Update(valuesBucket, predicates); -} - -bool AdminPoliciesStorageRdb::UpdateEntInfo(int32_t userId, const std::string &packageName, const EntInfo &entInfo) -{ - EDMLOGD("AdminPoliciesStorageRdb::UpdateEntInfo."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::UpdateEntInfo get edmRdbDataManager failed."); - return false; - } - // update admin_policies set ent_name=?, ent_desc=? where user_id=? and package_name=? - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_NAME, entInfo.enterpriseName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_ENT_DESC, entInfo.description); - - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName); - return edmRdbDataManager->Update(valuesBucket, predicates); -} - -bool AdminPoliciesStorageRdb::UpdateManagedEvents(int32_t userId, const std::string &packageName, - const std::vector &managedEvents) -{ - EDMLOGD("AdminPoliciesStorageRdb::UpdateManagedEvents."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::UpdateManagedEvents get edmRdbDataManager failed."); - return false; - } - // update admin_policies set subscribe_events=? where user_id=? and package_name=? - NativeRdb::ValuesBucket valuesBucket; - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - Json::Value managedEventsJson; - for (const auto &it : managedEvents) { - managedEventsJson.append(static_cast(it)); - } - valuesBucket.PutString(EdmRdbFiledConst::FILED_SUBSCRIBE_EVENTS, Json::writeString(builder, managedEventsJson)); - - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, packageName); - return edmRdbDataManager->Update(valuesBucket, predicates); -} - -std::unordered_map>> AdminPoliciesStorageRdb::QueryAllAdmin() -{ - EDMLOGD("AdminPoliciesStorageRdb::QueryAllAdmin."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - std::unordered_map>> admins; - if (edmRdbDataManager == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::QueryAllAdmin get edmRdbDataManager failed."); - return admins; - } - // select * from admin_policies - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - auto resultSet = edmRdbDataManager->Query(predicates, std::vector()); - if (resultSet == nullptr) { - return admins; - } - int resultSetNum = resultSet->GoToFirstRow(); - while (resultSetNum == NativeRdb::E_OK) { - std::shared_ptr item = std::make_shared(); - int32_t userId = 0; - resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_ONE, userId); - SetAdminItems(resultSet, item); - if (admins.find(userId) != admins.end()) { - admins[userId].push_back(item); - } else { - std::vector> adminItems{item}; - admins[userId] = adminItems; - } - resultSetNum = resultSet->GoToNextRow(); - } - resultSet->Close(); - return admins; -} - -void AdminPoliciesStorageRdb::SetAdminItems(std::shared_ptr resultSet, - std::shared_ptr item) -{ - if (item == nullptr) { - EDMLOGE("AdminPoliciesStorageRdb::SetAdminItems failed."); - return; - } - int32_t adminType = 0; - resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, adminType); - item->adminInfo_.adminType_ = static_cast(adminType); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, item->adminInfo_.packageName_); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FOUR, item->adminInfo_.className_); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FIVE, item->adminInfo_.entInfo_.enterpriseName); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_SIX, item->adminInfo_.entInfo_.description); - std::string permissionStr; - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_SEVEN, permissionStr); - if (!permissionStr.empty() && permissionStr != "null") { - Json::Value permissionJson; - ConvertStrToJson(permissionStr, permissionJson); - for (uint32_t i = 0; i < permissionJson.size(); i++) { - if (permissionJson[i].isString()) { - item->adminInfo_.permission_.push_back(permissionJson[i].asString()); - } - } - } - std::string managedEventsStr; - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_EIGHT, managedEventsStr); - if (!managedEventsStr.empty() && managedEventsStr != "null") { - Json::Value managedEventsJson; - ConvertStrToJson(managedEventsStr, managedEventsJson); - for (uint32_t i = 0; i < managedEventsJson.size(); i++) { - if (managedEventsJson[i].isUInt()) { - item->adminInfo_.managedEvents_.push_back(static_cast(managedEventsJson[i].asUInt())); - } - } - } - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_NINE, item->adminInfo_.parentAdminName_); - int isDebug = 0; - resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_TEN, isDebug); - item->adminInfo_.isDebug_ = isDebug != 0; -} - -void AdminPoliciesStorageRdb::ConvertStrToJson(const std::string &str, Json::Value &json) -{ - Json::String err; - Json::CharReaderBuilder builder; - std::unique_ptr reader(builder.newCharReader()); - reader->parse(str.c_str(), str.c_str() + str.length(), &json, &err); -} - -bool AdminPoliciesStorageRdb::InsertAuthorizedAdmin(const std::string &bundleName, - const std::vector &permissions, const std::string &parentName) -{ - EDMLOGD("AdminPoliciesStorageRdb::InsertAuthorizedAdmin."); - // insert into admin_policies(user_id, admin_type, package_name, class_name, permissions, parent_admin) - // values(?, ?, ?, ?, ?, ?) - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, EdmConstants::DEFAULT_USER_ID); - valuesBucket.PutInt(EdmRdbFiledConst::FILED_ADMIN_TYPE, static_cast(AdminType::SUB_SUPER_ADMIN)); - valuesBucket.PutString(EdmRdbFiledConst::FILED_PACKAGE_NAME, bundleName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_CLASS_NAME, ""); - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - Json::Value permissionJson; - for (const auto &permission : permissions) { - permissionJson.append(permission); - } - valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson)); - valuesBucket.PutString(EdmRdbFiledConst::FILED_PARENT_ADMIN, parentName); - return EdmRdbDataManager::GetInstance()->Insert(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME, valuesBucket); -} - -bool AdminPoliciesStorageRdb::UpdateAuthorizedAdmin(const std::string &bundleName, - const std::vector &permissions, const std::string &parentName) -{ - EDMLOGD("AdminPoliciesStorageRdb::SaveAuthorizedAdmin."); - // update admin_policies set permissions=? where user_id=100 and package_name=? and parent_name=? - NativeRdb::ValuesBucket valuesBucket; - Json::StreamWriterBuilder builder; - builder.settings_["indentation"] = ""; - Json::Value permissionJson; - for (const auto &permission : permissions) { - permissionJson.append(permission); - } - valuesBucket.PutString(EdmRdbFiledConst::FILED_PERMISSIONS, Json::writeString(builder, permissionJson)); - - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(EdmConstants::DEFAULT_USER_ID)); - predicates.EqualTo(EdmRdbFiledConst::FILED_PACKAGE_NAME, bundleName); - predicates.EqualTo(EdmRdbFiledConst::FILED_PARENT_ADMIN, parentName); - return EdmRdbDataManager::GetInstance()->Update(valuesBucket, predicates); -} -} // namespace EDM -} // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/connection/enterprise_bundle_connection.cpp b/services/edm/src/connection/enterprise_bundle_connection.cpp index b7a77c19f323067b9edf7bb6f326fba20341a9bd..b7fede81b6bdff63253ebf43c700138703609c95 100644 --- a/services/edm/src/connection/enterprise_bundle_connection.cpp +++ b/services/edm/src/connection/enterprise_bundle_connection.cpp @@ -1,59 +1,59 @@ -/* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "enterprise_bundle_connection.h" - -#include "edm_log.h" -#include "enterprise_conn_manager.h" -#include "managed_event.h" - -namespace OHOS { -namespace EDM { -EnterpriseBundleConnection::~EnterpriseBundleConnection() {} - -void EnterpriseBundleConnection::OnAbilityConnectDone( - const AppExecFwk::ElementName& element, const sptr& remoteObject, int32_t resultCode) -{ - EDMLOGI("EnterpriseBundleConnection OnAbilityConnectDone"); - proxy_ = (new (std::nothrow) EnterpriseAdminProxy(remoteObject)); - if (proxy_ == nullptr) { - EDMLOGI("EnterpriseBundleConnection get enterpriseAdminProxy failed."); - return; - } - switch (code_) { - case static_cast(ManagedEvent::BUNDLE_ADDED): - proxy_->OnBundleAdded(bundleName_); - break; - case static_cast(ManagedEvent::BUNDLE_REMOVED): - proxy_->OnBundleRemoved(bundleName_); - break; - case static_cast(ManagedEvent::APP_START): - proxy_->OnAppStart(bundleName_); - break; - case static_cast(ManagedEvent::APP_STOP): - proxy_->OnAppStop(bundleName_); - break; - default: - return; - } - EDMLOGI("EnterpriseBundleConnection OnAbilityConnectDone over"); -} - -void EnterpriseBundleConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int32_t resultCode) -{ - EDMLOGI("EnterpriseBundleConnection OnAbilityDisconnectDone"); -} -} // namespace EDM -} // namespace OHOS +/* + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "enterprise_bundle_connection.h" + +#include "edm_log.h" +#include "enterprise_conn_manager.h" +#include "managed_event.h" + +namespace OHOS { +namespace EDM { +EnterpriseBundleConnection::~EnterpriseBundleConnection() {} + +void EnterpriseBundleConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName& element, const sptr& remoteObject, int32_t resultCode) +{ + EDMLOGI("EnterpriseBundleConnection OnAbilityConnectDone"); + proxy_ = (new (std::nothrow) EnterpriseAdminProxy(remoteObject)); + if (proxy_ == nullptr) { + EDMLOGI("EnterpriseBundleConnection get enterpriseAdminProxy failed."); + return; + } + switch (code_) { + case static_cast(ManagedEvent::BUNDLE_ADDED): + proxy_->OnBundleAdded(bundleName_); + break; + case static_cast(ManagedEvent::BUNDLE_REMOVED): + proxy_->OnBundleRemoved(bundleName_); + break; + case static_cast(ManagedEvent::APP_START): + proxy_->OnAppStart(bundleName_); + break; + case static_cast(ManagedEvent::APP_STOP): + proxy_->OnAppStop(bundleName_); + break; + default: + return; + } + EDMLOGI("EnterpriseBundleConnection OnAbilityConnectDone over"); +} + +void EnterpriseBundleConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int32_t resultCode) +{ + EDMLOGI("EnterpriseBundleConnection OnAbilityDisconnectDone"); +} +} // namespace EDM +} // namespace OHOS diff --git a/services/edm/src/database/edm_rdb_data_manager.cpp b/services/edm/src/database/edm_rdb_data_manager.cpp index 1f322655bff8d7ad5acd1bdfa8eaf93d1646c45c..5ddda60fed94f8cccfa1ce4943af2f7cd88c3c18 100644 --- a/services/edm/src/database/edm_rdb_data_manager.cpp +++ b/services/edm/src/database/edm_rdb_data_manager.cpp @@ -1,133 +1,133 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "edm_rdb_data_manager.h" -#include "edm_log.h" -#include "edm_rdb_filed_const.h" -#include "edm_rdb_open_callback.h" - -namespace OHOS { -namespace EDM { -std::shared_ptr EdmRdbDataManager::rdbStore_ = nullptr; -std::shared_ptr EdmRdbDataManager::instance_ = nullptr; -std::mutex EdmRdbDataManager::mutexLock_; - -std::shared_ptr EdmRdbDataManager::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock(mutexLock_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -std::shared_ptr EdmRdbDataManager::GetRdbStore() -{ - EDMLOGD("EdmRdbDataManager GetRdbStore."); - if (rdbStore_ == nullptr) { - std::lock_guard lock(mutexLock_); - if (rdbStore_ == nullptr) { - NativeRdb::RdbStoreConfig rdbStoreConfig(EdmRdbFiledConst::EDM_SERVICE_DATABASE_PATH + - EdmRdbFiledConst::EDM_RDB_NAME); - int32_t errCode = NativeRdb::E_OK; - EdmRdbOpenCallback edmRdbOpenCallback; - rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, EdmRdbFiledConst::EDM_RDB_VERSION, - edmRdbOpenCallback, errCode); - } - } - return rdbStore_; -} - -bool EdmRdbDataManager::CreateTable(const std::string &createTableSql) -{ - EDMLOGD("EdmRdbDataManager CreateTable start."); - auto rdbStore = GetRdbStore(); - if (rdbStore == nullptr) { - EDMLOGE("EdmRdbDataManager GetRdbStore failed."); - return false; - } - if (createTableSql.empty()) { - EDMLOGE("EdmRdbDataManager createTableSql is empty."); - return false; - } - int32_t ret = rdbStore->ExecuteSql(createTableSql); - if (ret != NativeRdb::E_OK) { - EDMLOGE("CreateTable failed, ret: %{public}d", ret); - return false; - } - return true; -} - -bool EdmRdbDataManager::Insert(const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket) -{ - EDMLOGD("EdmRdbDataManager Insert data start."); - auto rdbStore = GetRdbStore(); - if (rdbStore == nullptr) { - EDMLOGE("EdmRdbDataManager GetRdbStore failed."); - return false; - } - int64_t rowId = -1; - auto ret = rdbStore->InsertWithConflictResolution(rowId, tableName, valuesBucket, - NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); - return ret == NativeRdb::E_OK; -} - -bool EdmRdbDataManager::Delete(const NativeRdb::AbsRdbPredicates &predicates) -{ - EDMLOGD("EdmRdbDataManager Delete data start."); - auto rdbStore = GetRdbStore(); - if (rdbStore == nullptr) { - EDMLOGE("EdmRdbDataManager GetRdbStore failed."); - return false; - } - int32_t rowId = -1; - auto ret = rdbStore->Delete(rowId, predicates); - return ret == NativeRdb::E_OK; -} - -bool EdmRdbDataManager::Update(const NativeRdb::ValuesBucket &valuesBucket, - const NativeRdb::AbsRdbPredicates &predicates) -{ - EDMLOGD("EdmRdbDataManager Update data start."); - auto rdbStore = GetRdbStore(); - if (rdbStore == nullptr) { - EDMLOGE("EdmRdbDataManager GetRdbStore failed."); - return false; - } - int32_t rowId = -1; - auto ret = rdbStore->Update(rowId, valuesBucket, predicates); - return ret == NativeRdb::E_OK; -} - -std::shared_ptr EdmRdbDataManager::Query(const NativeRdb::AbsRdbPredicates &predicates, - const std::vector &columns) -{ - EDMLOGD("EdmRdbDataManager Query data start."); - auto rdbStore = GetRdbStore(); - if (rdbStore == nullptr) { - EDMLOGE("EdmRdbDataManager GetRdbStore failed."); - return nullptr; - } - auto absSharedResultSet = rdbStore->Query(predicates, columns); - if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) { - EDMLOGE("EdmRdbDataManager query date failed."); - return nullptr; - } - return absSharedResultSet; -} -} // namespace EDM +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "edm_rdb_data_manager.h" +#include "edm_log.h" +#include "edm_rdb_filed_const.h" +#include "edm_rdb_open_callback.h" + +namespace OHOS { +namespace EDM { +std::shared_ptr EdmRdbDataManager::rdbStore_ = nullptr; +std::shared_ptr EdmRdbDataManager::instance_ = nullptr; +std::mutex EdmRdbDataManager::mutexLock_; + +std::shared_ptr EdmRdbDataManager::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lock(mutexLock_); + if (instance_ == nullptr) { + instance_ = std::make_shared(); + } + } + return instance_; +} + +std::shared_ptr EdmRdbDataManager::GetRdbStore() +{ + EDMLOGD("EdmRdbDataManager GetRdbStore."); + if (rdbStore_ == nullptr) { + std::lock_guard lock(mutexLock_); + if (rdbStore_ == nullptr) { + NativeRdb::RdbStoreConfig rdbStoreConfig(EdmRdbFiledConst::EDM_SERVICE_DATABASE_PATH + + EdmRdbFiledConst::EDM_RDB_NAME); + int32_t errCode = NativeRdb::E_OK; + EdmRdbOpenCallback edmRdbOpenCallback; + rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, EdmRdbFiledConst::EDM_RDB_VERSION, + edmRdbOpenCallback, errCode); + } + } + return rdbStore_; +} + +bool EdmRdbDataManager::CreateTable(const std::string &createTableSql) +{ + EDMLOGD("EdmRdbDataManager CreateTable start."); + auto rdbStore = GetRdbStore(); + if (rdbStore == nullptr) { + EDMLOGE("EdmRdbDataManager GetRdbStore failed."); + return false; + } + if (createTableSql.empty()) { + EDMLOGE("EdmRdbDataManager createTableSql is empty."); + return false; + } + int32_t ret = rdbStore->ExecuteSql(createTableSql); + if (ret != NativeRdb::E_OK) { + EDMLOGE("CreateTable failed, ret: %{public}d", ret); + return false; + } + return true; +} + +bool EdmRdbDataManager::Insert(const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket) +{ + EDMLOGD("EdmRdbDataManager Insert data start."); + auto rdbStore = GetRdbStore(); + if (rdbStore == nullptr) { + EDMLOGE("EdmRdbDataManager GetRdbStore failed."); + return false; + } + int64_t rowId = -1; + auto ret = rdbStore->InsertWithConflictResolution(rowId, tableName, valuesBucket, + NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); + return ret == NativeRdb::E_OK; +} + +bool EdmRdbDataManager::Delete(const NativeRdb::AbsRdbPredicates &predicates) +{ + EDMLOGD("EdmRdbDataManager Delete data start."); + auto rdbStore = GetRdbStore(); + if (rdbStore == nullptr) { + EDMLOGE("EdmRdbDataManager GetRdbStore failed."); + return false; + } + int32_t rowId = -1; + auto ret = rdbStore->Delete(rowId, predicates); + return ret == NativeRdb::E_OK; +} + +bool EdmRdbDataManager::Update(const NativeRdb::ValuesBucket &valuesBucket, + const NativeRdb::AbsRdbPredicates &predicates) +{ + EDMLOGD("EdmRdbDataManager Update data start."); + auto rdbStore = GetRdbStore(); + if (rdbStore == nullptr) { + EDMLOGE("EdmRdbDataManager GetRdbStore failed."); + return false; + } + int32_t rowId = -1; + auto ret = rdbStore->Update(rowId, valuesBucket, predicates); + return ret == NativeRdb::E_OK; +} + +std::shared_ptr EdmRdbDataManager::Query(const NativeRdb::AbsRdbPredicates &predicates, + const std::vector &columns) +{ + EDMLOGD("EdmRdbDataManager Query data start."); + auto rdbStore = GetRdbStore(); + if (rdbStore == nullptr) { + EDMLOGE("EdmRdbDataManager GetRdbStore failed."); + return nullptr; + } + auto absSharedResultSet = rdbStore->Query(predicates, columns); + if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) { + EDMLOGE("EdmRdbDataManager query date failed."); + return nullptr; + } + return absSharedResultSet; +} +} // namespace EDM } // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/database/edm_rdb_open_callback.cpp b/services/edm/src/database/edm_rdb_open_callback.cpp index 629d31b8d0fe841e608063c40c5a5aa60d29583c..3f78f7b6cf5ba369f35829e3fbf2bbe2286ede82 100644 --- a/services/edm/src/database/edm_rdb_open_callback.cpp +++ b/services/edm/src/database/edm_rdb_open_callback.cpp @@ -1,41 +1,41 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "edm_rdb_open_callback.h" - -#include "edm_rdb_filed_const.h" -#include "edm_log.h" - -namespace OHOS { -namespace EDM { -constexpr int32_t EDM_RDB_VERSION_TWO = 2; -int32_t EdmRdbOpenCallback::OnCreate(NativeRdb::RdbStore &rdbStore) -{ - EDMLOGD("EdmRdbOpenCallback OnCreate : database create."); - return NativeRdb::E_OK; -} - -int32_t EdmRdbOpenCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) -{ - EDMLOGD("EdmRdbOpenCallback OnUpgrade : database upgrade. currentVersion = %{public}d, newVersion = %{public}d", - currentVersion, targetVersion); - if (currentVersion < EDM_RDB_VERSION_TWO && targetVersion >= EDM_RDB_VERSION_TWO) { - rdbStore.ExecuteSql("ALTER TABLE " + EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME + " ADD COLUMN " + - EdmRdbFiledConst::FILED_IS_DEBUG + " INTEGER DEFAULT 0;"); - } - return NativeRdb::E_OK; -} -} // namespace EDM +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "edm_rdb_open_callback.h" + +#include "edm_rdb_filed_const.h" +#include "edm_log.h" + +namespace OHOS { +namespace EDM { +constexpr int32_t EDM_RDB_VERSION_TWO = 2; +int32_t EdmRdbOpenCallback::OnCreate(NativeRdb::RdbStore &rdbStore) +{ + EDMLOGD("EdmRdbOpenCallback OnCreate : database create."); + return NativeRdb::E_OK; +} + +int32_t EdmRdbOpenCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) +{ + EDMLOGD("EdmRdbOpenCallback OnUpgrade : database upgrade. currentVersion = %{public}d, newVersion = %{public}d", + currentVersion, targetVersion); + if (currentVersion < EDM_RDB_VERSION_TWO && targetVersion >= EDM_RDB_VERSION_TWO) { + rdbStore.ExecuteSql("ALTER TABLE " + EdmRdbFiledConst::ADMIN_POLICIES_RDB_TABLE_NAME + " ADD COLUMN " + + EdmRdbFiledConst::FILED_IS_DEBUG + " INTEGER DEFAULT 0;"); + } + return NativeRdb::E_OK; +} +} // namespace EDM } // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/device_policies_storage_rdb.cpp b/services/edm/src/device_policies_storage_rdb.cpp index 1a708b16109afc60b4a42d97ce9c1da1b93ad41b..a4b231fd2d80cb5b5d35135e95f03f180a151555 100644 --- a/services/edm/src/device_policies_storage_rdb.cpp +++ b/services/edm/src/device_policies_storage_rdb.cpp @@ -1,298 +1,298 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "device_policies_storage_rdb.h" -#include "edm_log.h" -#include "edm_rdb_filed_const.h" - -namespace OHOS { -namespace EDM { -std::shared_ptr DevicePoliciesStorageRdb::instance_ = nullptr; -std::mutex DevicePoliciesStorageRdb::mutexLock_; - -DevicePoliciesStorageRdb::DevicePoliciesStorageRdb() -{ - CreateDeviceAdminPoliciesTable(); - CreateDeviceCombinedPoliciesTable(); -} - -void DevicePoliciesStorageRdb::CreateDeviceAdminPoliciesTable() -{ - EDMLOGI("DevicePoliciesStorageRdb::CreateDeviceAdminPoliciesTable."); - std::string createTableSql = "CREATE TABLE IF NOT EXISTS "; - createTableSql.append(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME + " (") - .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,") - .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,") - .append(EdmRdbFiledConst::FILED_ADMIN_NAME + " TEXT NOT NULL,") - .append(EdmRdbFiledConst::FILED_POLICY_NAME + " TEXT,") - .append(EdmRdbFiledConst::FILED_POLICY_VALUE + " TEXT);"); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager != nullptr) { - edmRdbDataManager->CreateTable(createTableSql); - } else { - EDMLOGE("DevicePoliciesStorageRdb::create database device_admin_policies failed."); - } -} - -void DevicePoliciesStorageRdb::CreateDeviceCombinedPoliciesTable() -{ - EDMLOGI("DevicePoliciesStorageRdb::CreateDeviceCombinedPoliciesTable."); - std::string createTableSql = "CREATE TABLE IF NOT EXISTS "; - createTableSql.append(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME + " (") - .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,") - .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,") - .append(EdmRdbFiledConst::FILED_POLICY_NAME + " TEXT,") - .append(EdmRdbFiledConst::FILED_POLICY_VALUE + " TEXT);"); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager != nullptr) { - edmRdbDataManager->CreateTable(createTableSql); - } else { - EDMLOGE("DevicePoliciesStorageRdb::create database device_combined_policies failed."); - } -} - -std::shared_ptr DevicePoliciesStorageRdb::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock(mutexLock_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -bool DevicePoliciesStorageRdb::InsertAdminPolicy(int32_t userId, const std::string &adminName, - const std::string &policyName, const std::string &policyValue) -{ - EDMLOGD("DevicePoliciesStorageRdb::InsertAdminPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::InsertAdminPolicy get edmRdbDataManager failed."); - return false; - } - // insert into device_admin_policies(user_id, admin_name, policy_name, policy_value) values(?, ?, ?, ?) - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId); - valuesBucket.PutString(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); - return edmRdbDataManager->Insert(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME, valuesBucket); -} - -bool DevicePoliciesStorageRdb::UpdateAdminPolicy(int32_t userId, const std::string &adminName, - const std::string &policyName, const std::string &policyValue) -{ - EDMLOGD("DevicePoliciesStorageRdb::UpdateAdminPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::UpdateAdminPolicy get edmRdbDataManager failed."); - return false; - } - // update device_admin_policies set policy_value=? where user_id=? and admin_name=? and policy_name=? - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); - predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - return edmRdbDataManager->Update(valuesBucket, predicates); -} - -bool DevicePoliciesStorageRdb::DeleteAdminPolicy(int32_t userId, const std::string &adminName, - const std::string &policyName) -{ - EDMLOGD("AdminPoliciesStorageRdb::DeleteAdminPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::DeleteAdminPolicy get edmRdbDataManager failed."); - return false; - } - // delete from device_admin_policies where user_id=? and admin_name=? and policy_name=? - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); - predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - return edmRdbDataManager->Delete(predicates); -} - -bool DevicePoliciesStorageRdb::QueryAdminPolicy(int32_t userId, std::unordered_map &adminPolicies, std::unordered_map &policyAdmins) -{ - EDMLOGD("DevicePoliciesStorageRdb::QueryAdminPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::QueryAdminPolicy get edmRdbDataManager failed."); - return false; - } - // select * from device_admin_policies - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - auto resultSet = edmRdbDataManager->Query(predicates, std::vector()); - if (resultSet == nullptr) { - EDMLOGD("DevicePoliciesStorageRdb::QueryAdminPolicy failed."); - return false; - } - int resultSetNum = resultSet->GoToFirstRow(); - while (resultSetNum == NativeRdb::E_OK) { - std::string adminName; - std::string policyName; - std::string policyValue; - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, adminName); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, policyName); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FOUR, policyValue); - PraseAdminPolicies(adminName, policyName, policyValue, adminPolicies); - PrasePolicyAdmins(adminName, policyName, policyValue, policyAdmins); - resultSetNum = resultSet->GoToNextRow(); - }; - resultSet->Close(); - return true; -} - -void DevicePoliciesStorageRdb::PraseAdminPolicies(const std::string &adminName, const std::string &policyName, - const std::string &policyValue, std::unordered_map &adminPolicies) -{ - auto iter = adminPolicies.find(adminName); - if (iter == adminPolicies.end()) { - PolicyItemsMap itemMap; - itemMap.insert(std::pair(policyName, policyValue)); - adminPolicies.insert(std::pair(adminName, itemMap)); - return; - } - iter->second.insert(std::pair(policyName, policyValue)); -} - -void DevicePoliciesStorageRdb::PrasePolicyAdmins(const std::string &adminName, const std::string &policyName, - const std::string &policyValue, std::unordered_map &policyAdmins) -{ - auto iter = policyAdmins.find(policyName); - if (iter == policyAdmins.end()) { - AdminValueItemsMap adminValueItem; - adminValueItem.insert(std::pair(adminName, policyValue)); - policyAdmins.insert(std::pair(policyName, adminValueItem)); - return; - } - iter->second.insert(std::pair(adminName, policyValue)); -} - -bool DevicePoliciesStorageRdb::InsertCombinedPolicy(int32_t userId, const std::string &policyName, - const std::string &policyValue) -{ - EDMLOGD("DevicePoliciesStorageRdb::InsertCombinedPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::InsertCombinedPolicy get edmRdbDataManager failed."); - return false; - } - // insert into device_combined_policies(user_id, policy_name, policy_value) values(?, ?, ?) - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId); - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); - return edmRdbDataManager->Insert(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME, - valuesBucket); -} - -bool DevicePoliciesStorageRdb::UpdateCombinedPolicy(int32_t userId, const std::string &policyName, - const std::string &policyValue) -{ - EDMLOGD("DevicePoliciesStorageRdb::UpdateCombinedPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::UpdateCombinedPolicy get edmRdbDataManager failed."); - return false; - } - // update device_combined_policies set policy_value=? where user_id=? and policy_name=? - NativeRdb::ValuesBucket valuesBucket; - valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - return edmRdbDataManager->Update(valuesBucket, predicates); -} - -bool DevicePoliciesStorageRdb::DeleteCombinedPolicy(int32_t userId, const std::string &policyName) -{ - EDMLOGD("DevicePoliciesStorageRdb::DeleteCombinedPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::DeleteCombinedPolicy get edmRdbDataManager failed."); - return false; - } - // delete from device_combined_policies where user_id=? and policy_name=? - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); - return edmRdbDataManager->Delete(predicates); -} - -bool DevicePoliciesStorageRdb::QueryCombinedPolicy(int32_t userId, PolicyItemsMap &itemsMap) -{ - EDMLOGD("DevicePoliciesStorageRdb::QueryCombinedPolicy."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::QueryCombinedPolicy get edmRdbDataManager failed."); - return false; - } - // select * from device_combined_policies - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); - predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); - auto resultSet = edmRdbDataManager->Query(predicates, std::vector()); - if (resultSet == nullptr) { - EDMLOGD("DevicePoliciesStorageRdb::QueryCombinedPolicy failed."); - return false; - } - int resultSetNum = resultSet->GoToFirstRow(); - while (resultSetNum == NativeRdb::E_OK) { - std::string policyName; - std::string policyValue; - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, policyName); - resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, policyValue); - itemsMap.insert(std::pair(policyName, policyValue)); - resultSetNum = resultSet->GoToNextRow(); - }; - resultSet->Close(); - return true; -} - -bool DevicePoliciesStorageRdb::QueryAllUserId(std::vector &userIds) -{ - EDMLOGD("DevicePoliciesStorageRdb::QueryAllUserId."); - auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); - if (edmRdbDataManager == nullptr) { - EDMLOGE("DevicePoliciesStorageRdb::QueryAllUserId get edmRdbDataManager failed."); - return false; - } - // select user_id from device_admin_policies - NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); - predicates.Distinct(); - std::vector queryColumns = { EdmRdbFiledConst::FILED_USER_ID }; - auto resultSet = edmRdbDataManager->Query(predicates, queryColumns); - if (resultSet == nullptr) { - EDMLOGD("DevicePoliciesStorageRdb::QueryAllUserId failed."); - return false; - } - int resultSetNum = resultSet->GoToFirstRow(); - while (resultSetNum == NativeRdb::E_OK) { - int32_t userId = 0; - resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_ZERO, userId); - userIds.push_back(userId); - resultSetNum = resultSet->GoToNextRow(); - }; - resultSet->Close(); - return true; -} -} // namespace EDM +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_policies_storage_rdb.h" +#include "edm_log.h" +#include "edm_rdb_filed_const.h" + +namespace OHOS { +namespace EDM { +std::shared_ptr DevicePoliciesStorageRdb::instance_ = nullptr; +std::mutex DevicePoliciesStorageRdb::mutexLock_; + +DevicePoliciesStorageRdb::DevicePoliciesStorageRdb() +{ + CreateDeviceAdminPoliciesTable(); + CreateDeviceCombinedPoliciesTable(); +} + +void DevicePoliciesStorageRdb::CreateDeviceAdminPoliciesTable() +{ + EDMLOGI("DevicePoliciesStorageRdb::CreateDeviceAdminPoliciesTable."); + std::string createTableSql = "CREATE TABLE IF NOT EXISTS "; + createTableSql.append(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME + " (") + .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,") + .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,") + .append(EdmRdbFiledConst::FILED_ADMIN_NAME + " TEXT NOT NULL,") + .append(EdmRdbFiledConst::FILED_POLICY_NAME + " TEXT,") + .append(EdmRdbFiledConst::FILED_POLICY_VALUE + " TEXT);"); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager != nullptr) { + edmRdbDataManager->CreateTable(createTableSql); + } else { + EDMLOGE("DevicePoliciesStorageRdb::create database device_admin_policies failed."); + } +} + +void DevicePoliciesStorageRdb::CreateDeviceCombinedPoliciesTable() +{ + EDMLOGI("DevicePoliciesStorageRdb::CreateDeviceCombinedPoliciesTable."); + std::string createTableSql = "CREATE TABLE IF NOT EXISTS "; + createTableSql.append(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME + " (") + .append(EdmRdbFiledConst::FILED_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,") + .append(EdmRdbFiledConst::FILED_USER_ID + " INTEGER NOT NULL,") + .append(EdmRdbFiledConst::FILED_POLICY_NAME + " TEXT,") + .append(EdmRdbFiledConst::FILED_POLICY_VALUE + " TEXT);"); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager != nullptr) { + edmRdbDataManager->CreateTable(createTableSql); + } else { + EDMLOGE("DevicePoliciesStorageRdb::create database device_combined_policies failed."); + } +} + +std::shared_ptr DevicePoliciesStorageRdb::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lock(mutexLock_); + if (instance_ == nullptr) { + instance_ = std::make_shared(); + } + } + return instance_; +} + +bool DevicePoliciesStorageRdb::InsertAdminPolicy(int32_t userId, const std::string &adminName, + const std::string &policyName, const std::string &policyValue) +{ + EDMLOGD("DevicePoliciesStorageRdb::InsertAdminPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::InsertAdminPolicy get edmRdbDataManager failed."); + return false; + } + // insert into device_admin_policies(user_id, admin_name, policy_name, policy_value) values(?, ?, ?, ?) + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId); + valuesBucket.PutString(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); + return edmRdbDataManager->Insert(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME, valuesBucket); +} + +bool DevicePoliciesStorageRdb::UpdateAdminPolicy(int32_t userId, const std::string &adminName, + const std::string &policyName, const std::string &policyValue) +{ + EDMLOGD("DevicePoliciesStorageRdb::UpdateAdminPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::UpdateAdminPolicy get edmRdbDataManager failed."); + return false; + } + // update device_admin_policies set policy_value=? where user_id=? and admin_name=? and policy_name=? + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + predicates.EqualTo(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); + predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + return edmRdbDataManager->Update(valuesBucket, predicates); +} + +bool DevicePoliciesStorageRdb::DeleteAdminPolicy(int32_t userId, const std::string &adminName, + const std::string &policyName) +{ + EDMLOGD("AdminPoliciesStorageRdb::DeleteAdminPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::DeleteAdminPolicy get edmRdbDataManager failed."); + return false; + } + // delete from device_admin_policies where user_id=? and admin_name=? and policy_name=? + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + predicates.EqualTo(EdmRdbFiledConst::FILED_ADMIN_NAME, adminName); + predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + return edmRdbDataManager->Delete(predicates); +} + +bool DevicePoliciesStorageRdb::QueryAdminPolicy(int32_t userId, std::unordered_map &adminPolicies, std::unordered_map &policyAdmins) +{ + EDMLOGD("DevicePoliciesStorageRdb::QueryAdminPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::QueryAdminPolicy get edmRdbDataManager failed."); + return false; + } + // select * from device_admin_policies + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + auto resultSet = edmRdbDataManager->Query(predicates, std::vector()); + if (resultSet == nullptr) { + EDMLOGD("DevicePoliciesStorageRdb::QueryAdminPolicy failed."); + return false; + } + int resultSetNum = resultSet->GoToFirstRow(); + while (resultSetNum == NativeRdb::E_OK) { + std::string adminName; + std::string policyName; + std::string policyValue; + resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, adminName); + resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, policyName); + resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_FOUR, policyValue); + PraseAdminPolicies(adminName, policyName, policyValue, adminPolicies); + PrasePolicyAdmins(adminName, policyName, policyValue, policyAdmins); + resultSetNum = resultSet->GoToNextRow(); + }; + resultSet->Close(); + return true; +} + +void DevicePoliciesStorageRdb::PraseAdminPolicies(const std::string &adminName, const std::string &policyName, + const std::string &policyValue, std::unordered_map &adminPolicies) +{ + auto iter = adminPolicies.find(adminName); + if (iter == adminPolicies.end()) { + PolicyItemsMap itemMap; + itemMap.insert(std::pair(policyName, policyValue)); + adminPolicies.insert(std::pair(adminName, itemMap)); + return; + } + iter->second.insert(std::pair(policyName, policyValue)); +} + +void DevicePoliciesStorageRdb::PrasePolicyAdmins(const std::string &adminName, const std::string &policyName, + const std::string &policyValue, std::unordered_map &policyAdmins) +{ + auto iter = policyAdmins.find(policyName); + if (iter == policyAdmins.end()) { + AdminValueItemsMap adminValueItem; + adminValueItem.insert(std::pair(adminName, policyValue)); + policyAdmins.insert(std::pair(policyName, adminValueItem)); + return; + } + iter->second.insert(std::pair(adminName, policyValue)); +} + +bool DevicePoliciesStorageRdb::InsertCombinedPolicy(int32_t userId, const std::string &policyName, + const std::string &policyValue) +{ + EDMLOGD("DevicePoliciesStorageRdb::InsertCombinedPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::InsertCombinedPolicy get edmRdbDataManager failed."); + return false; + } + // insert into device_combined_policies(user_id, policy_name, policy_value) values(?, ?, ?) + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutInt(EdmRdbFiledConst::FILED_USER_ID, userId); + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); + return edmRdbDataManager->Insert(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME, + valuesBucket); +} + +bool DevicePoliciesStorageRdb::UpdateCombinedPolicy(int32_t userId, const std::string &policyName, + const std::string &policyValue) +{ + EDMLOGD("DevicePoliciesStorageRdb::UpdateCombinedPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::UpdateCombinedPolicy get edmRdbDataManager failed."); + return false; + } + // update device_combined_policies set policy_value=? where user_id=? and policy_name=? + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutString(EdmRdbFiledConst::FILED_POLICY_VALUE, policyValue); + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + return edmRdbDataManager->Update(valuesBucket, predicates); +} + +bool DevicePoliciesStorageRdb::DeleteCombinedPolicy(int32_t userId, const std::string &policyName) +{ + EDMLOGD("DevicePoliciesStorageRdb::DeleteCombinedPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::DeleteCombinedPolicy get edmRdbDataManager failed."); + return false; + } + // delete from device_combined_policies where user_id=? and policy_name=? + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + predicates.EqualTo(EdmRdbFiledConst::FILED_POLICY_NAME, policyName); + return edmRdbDataManager->Delete(predicates); +} + +bool DevicePoliciesStorageRdb::QueryCombinedPolicy(int32_t userId, PolicyItemsMap &itemsMap) +{ + EDMLOGD("DevicePoliciesStorageRdb::QueryCombinedPolicy."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::QueryCombinedPolicy get edmRdbDataManager failed."); + return false; + } + // select * from device_combined_policies + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_COMBINED_POLICIES_RDB_TABLE_NAME); + predicates.EqualTo(EdmRdbFiledConst::FILED_USER_ID, std::to_string(userId)); + auto resultSet = edmRdbDataManager->Query(predicates, std::vector()); + if (resultSet == nullptr) { + EDMLOGD("DevicePoliciesStorageRdb::QueryCombinedPolicy failed."); + return false; + } + int resultSetNum = resultSet->GoToFirstRow(); + while (resultSetNum == NativeRdb::E_OK) { + std::string policyName; + std::string policyValue; + resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_TWO, policyName); + resultSet->GetString(EdmRdbFiledConst::FILED_COLUMN_INDEX_THREE, policyValue); + itemsMap.insert(std::pair(policyName, policyValue)); + resultSetNum = resultSet->GoToNextRow(); + }; + resultSet->Close(); + return true; +} + +bool DevicePoliciesStorageRdb::QueryAllUserId(std::vector &userIds) +{ + EDMLOGD("DevicePoliciesStorageRdb::QueryAllUserId."); + auto edmRdbDataManager = EdmRdbDataManager::GetInstance(); + if (edmRdbDataManager == nullptr) { + EDMLOGE("DevicePoliciesStorageRdb::QueryAllUserId get edmRdbDataManager failed."); + return false; + } + // select user_id from device_admin_policies + NativeRdb::AbsRdbPredicates predicates(EdmRdbFiledConst::DEVICE_ADMIN_POLICIES_RDB_TABLE_NAME); + predicates.Distinct(); + std::vector queryColumns = { EdmRdbFiledConst::FILED_USER_ID }; + auto resultSet = edmRdbDataManager->Query(predicates, queryColumns); + if (resultSet == nullptr) { + EDMLOGD("DevicePoliciesStorageRdb::QueryAllUserId failed."); + return false; + } + int resultSetNum = resultSet->GoToFirstRow(); + while (resultSetNum == NativeRdb::E_OK) { + int32_t userId = 0; + resultSet->GetInt(EdmRdbFiledConst::FILED_COLUMN_INDEX_ZERO, userId); + userIds.push_back(userId); + resultSetNum = resultSet->GoToNextRow(); + }; + resultSet->Close(); + return true; +} +} // namespace EDM } // namespace OHOS \ No newline at end of file diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index e30c87ad0366ea5144d69dfae7f5913bbbd0bcf1..11e16868579c47b3215e725641968fc2218b8ecf 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -1055,6 +1055,7 @@ ErrCode EnterpriseDeviceMgrAbility::CheckGetPolicyPermission(MessageParcel &data reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR); return ERR_EDM_PARAM_ERROR; } +#ifndef EDM_FUZZ_TEST std::shared_ptr deviceAdmin = adminMgr_->GetAdminByPkgName(admin->GetBundleName(), userId); if (deviceAdmin == nullptr) { EDMLOGW("GetDevicePolicy: get admin failed"); @@ -1071,6 +1072,7 @@ ErrCode EnterpriseDeviceMgrAbility::CheckGetPolicyPermission(MessageParcel &data reply.WriteInt32(EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED); return EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED; } +#endif elementName.SetBundleName(admin->GetBundleName()); elementName.SetAbilityName(admin->GetAbilityName()); return ERR_OK; diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index cf9bc48fa333281c49331369692da58394312bb8..c82e93ae18f7dc9b9b696aaef234291ca0ddc184 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -17,16 +17,26 @@ group("fuzztest") { deps += [ # deps file + "addosaccountplugin_fuzzer:AddOsAccountPluginFuzzTest", "adminmanager_fuzzer:AdminManagerFuzzTest", "adminpoliciesstoragerdb_fuzzer:AdminPoliciesStorageRdbFuzzTest", "adminserviceinterface_fuzzer:AdminServiceInterfaceFuzzTest", + "allowedbluetoothdevicesplugin_fuzzer:AllowedBluetoothDevicesPluginFuzzTest", "allowedusbdevicesplugin_fuzzer:AllowedUsbDevicesPluginFuzzTest", + "disallowaddosaccountbyuserplugin_fuzzer:DisallowAddOsAccountByUserPluginFuzzTest", + "disallowedrunningbundlesplugin_fuzzer:DisallowedRunningBundlesPluginFuzzTest", "edmplugin_fuzzer:EdmPluginFuzzTest", "enterprisedevicemgrability_fuzzer:EnterpriseDeviceMgrAbilityFuzzTest", "enterprisedevicemgrstubmock_fuzzer:EnterpriseDeviceMgrStubMockFuzzTest", "firewallruleplugin_fuzzer:FireWallRulePluginFuzzTest", + "getbluetoothinfoplugin_fuzzer:GetBluetoothInfoPluginFuzzTest", + "getdeviceinfoplugin_fuzzer:GetDeviceInfoPluginFuzzTest", + "installplugin_fuzzer:InstallPluginFuzzTest", "iptablesruleplugin_fuzzer:IptablesRulePluginFuzzTest", + "powerpolicyplugin_fuzzer:PowerPolicyPluginFuzzTest", + "setbrowserpoliciesplugin_fuzzer:SetBrowserPoliciesPluginFuzzTest", "manageautostartappsplugin_fuzzer:ManageAutoStartAppsPluginFuzzTest", "usbreadonlyplugin_fuzzer:UsbReadOnlyPluginFuzzTest", + "usercertplugin_fuzzer:UserCertPluginFuzzTest", ] } diff --git a/test/fuzztest/addosaccountplugin_fuzzer/BUILD.gn b/test/fuzztest/addosaccountplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..23e5ab50323603ea630dfe09a5401ea1e8b05654 --- /dev/null +++ b/test/fuzztest/addosaccountplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("AddOsAccountPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "add_os_account_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.cpp b/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fa411ced95c976e18da580d2791633c7c0b78e9 --- /dev/null +++ b/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "add_os_account_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 32; +constexpr int32_t WITHOUT_USERID = 0; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + + int32_t pos = 0; + int32_t stringSize = (size - pos) / 4; + + uint32_t code = EdmInterfaceCode::ADD_OS_ACCOUNT; + code = POLICY_FUNC_CODE(static_cast(FuncOperateType::SET), code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + parcel.WriteParcelable(&admin); + + + std::vector key {CommonFuzzer::GetString(data, pos, stringSize, size)}; + std::vector value {CommonFuzzer::GetString(data, pos, stringSize, size)}; + parcel.WriteStringVector(key); + parcel.WriteStringVector(value); + + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.h b/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..db92b89b9439bd89d40ac6d561b9fe8aed2c3c4c --- /dev/null +++ b/test/fuzztest/addosaccountplugin_fuzzer/add_os_account_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_ADD_OS_ACCOUNT_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_ADD_OS_ACCOUNT_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "add_os_account_plugin_fuzzer" + +#endif // TEST_FUZZTEST_ADD_OS_ACCOUNT_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/addosaccountplugin_fuzzer/corpus/init b/test/fuzztest/addosaccountplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/addosaccountplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/addosaccountplugin_fuzzer/project.xml b/test/fuzztest/addosaccountplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/addosaccountplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/BUILD.gn b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ce95c19b8e263d41548206255c14867a11e1c4f4 --- /dev/null +++ b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("AllowedBluetoothDevicesPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "allowed_bluetooth_devices_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.cpp b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..510dd762033c11e4eebda641f89ebfda0377da0d --- /dev/null +++ b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "allowed_bluetooth_devices_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 24; +constexpr int32_t WITHOUT_USERID = 0; +constexpr int32_t HAS_ADMIN = 0; +constexpr int32_t WITHOUT_ADMIN = 1; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + + int32_t pos = 0; + int32_t stringSize = (size - pos) / 3; + + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::ALLOWED_BLUETOOTH_DEVICES; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + std::vector deviceIds; + deviceIds.push_back(CommonFuzzer::GetString(data, pos, stringSize, size)); + parcel.WriteStringVector(deviceIds); + } else { + parcel.WriteString(""); + bool hasAdmin = CommonFuzzer::GetU32Data(data) % 2; + if (hasAdmin) { + parcel.WriteInt32(HAS_ADMIN); + parcel.WriteParcelable(&admin); + } else { + parcel.WriteInt32(WITHOUT_ADMIN); + } + } + + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.h b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..3b1b5aa003d1d85dbc01d8ec58e4c636390970f2 --- /dev/null +++ b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/allowed_bluetooth_devices_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_ALLOWED_BLUETOOTH_DEVICES_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_ALLOWED_BLUETOOTH_DEVICES_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "allowed_bluetooth_devices_plugin_fuzzer" + +#endif // TEST_FUZZTEST_ALLOWED_BLUETOOTH_DEVICES_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/corpus/init b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/project.xml b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/allowedbluetoothdevicesplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/allowedusbdevicesplugin_fuzzer/allowed_usb_devices_plugin_fuzzer.cpp b/test/fuzztest/allowedusbdevicesplugin_fuzzer/allowed_usb_devices_plugin_fuzzer.cpp index c8eb3173648a503a3d6d9a94418931925ba1d084..de37456866629bd8175fff252360f7e4e30afe08 100644 --- a/test/fuzztest/allowedusbdevicesplugin_fuzzer/allowed_usb_devices_plugin_fuzzer.cpp +++ b/test/fuzztest/allowedusbdevicesplugin_fuzzer/allowed_usb_devices_plugin_fuzzer.cpp @@ -41,32 +41,31 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (size < MIN_SIZE) { return 0; } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; for (uint32_t operateType = static_cast(FuncOperateType::GET); operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { uint32_t code = EdmInterfaceCode::ALLOWED_USB_DEVICES; code = POLICY_FUNC_CODE(operateType, code); AppExecFwk::ElementName admin; - admin.SetBundleName("com.example.edmtest"); - admin.SetAbilityName("com.example.edmtest.EnterpriseAdminAbility"); + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); MessageParcel parcel; parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); parcel.WriteInt32(WITHOUT_USERID); if (operateType) { parcel.WriteParcelable(&admin); - parcel.WriteString(""); + parcel.WriteInt32(USB_DEVICE_ID_SIZE); + UsbDeviceId usbDeviceId = GetData(); + std::vector usbDeviceIds = { usbDeviceId }; + std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), + [&](const auto usbDeviceId) { usbDeviceId.Marshalling(parcel); }); } else { parcel.WriteString(""); parcel.WriteInt32(0); parcel.WriteParcelable(&admin); } - parcel.WriteInt32(USB_DEVICE_ID_SIZE); - - UsbDeviceId usbDeviceId = GetData(); - std::vector usbDeviceIds = { usbDeviceId }; - - std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), - [&](const auto usbDeviceId) { usbDeviceId.Marshalling(parcel); }); CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); } return 0; diff --git a/test/fuzztest/common/src/common_fuzzer.cpp b/test/fuzztest/common/src/common_fuzzer.cpp index c898f7449e153921fc1821157e32da66b9a4770d..b17d4905cff78957b9832960dc34cdcf109120fb 100644 --- a/test/fuzztest/common/src/common_fuzzer.cpp +++ b/test/fuzztest/common/src/common_fuzzer.cpp @@ -214,6 +214,7 @@ std::string CommonFuzzer::GetString(const uint8_t* ptr, int32_t& pos, int32_t st if (size <= pos || size - pos < stringSize) { return nullptr; } + stringSize = stringSize > 1024 ? 1024 : stringSize; std::string ret(reinterpret_cast(ptr + pos), stringSize); pos += stringSize; return ret; diff --git a/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/BUILD.gn b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e0b06a62713e2219bf84b8d7a59746ad65624657 --- /dev/null +++ b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("DisallowAddOsAccountByUserPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "disallow_add_os_account_by_user_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/corpus/init b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.cpp b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f79a5c3acee72a12d12bf3f6d7e28511001f04ea --- /dev/null +++ b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "disallow_add_os_account_by_user_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_constants.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 32; +constexpr size_t WITHOUT_USERID = 0; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 4; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::DISALLOW_ADD_OS_ACCOUNT_BY_USER; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + std::vector key {CommonFuzzer::GetString(data, pos, stringSize, size)}; + std::vector value {CommonFuzzer::GetString(data, pos, stringSize, size)}; + parcel.WriteStringVector(key); + parcel.WriteStringVector(value); + } else { + parcel.WriteString(""); + bool hasAdmin = CommonFuzzer::GetU32Data(data) % 2; + if (hasAdmin) { + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + } else { + parcel.WriteInt32(0); + } + parcel.WriteInt32(CommonFuzzer::GetU32Data(data)); + } + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.h b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..ea23a7be04012afee6b1959c22c933b935565133 --- /dev/null +++ b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/disallow_add_os_account_by_user_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_DISALLOW_ADD_OS_ACCOUNT_BY_USER_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_DISALLOW_ADD_OS_ACCOUNT_BY_USER_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "disallow_add_os_account_by_user_plugin_fuzzer" + +#endif // TEST_FUZZTEST_DISALLOW_ADD_OS_ACCOUNT_BY_USER_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/project.xml b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/disallowaddosaccountbyuserplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/BUILD.gn b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3000d095d7e29515f7a2711d7ac2354a4a91c501 --- /dev/null +++ b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("DisallowedRunningBundlesPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "disallowed_running_bundles_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/corpus/init b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.cpp b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af6d04f96d72e29ea70458ac4d985a41a68c7512 --- /dev/null +++ b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "disallowed_running_bundles_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_constants.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 4; +constexpr size_t HAS_USERID = 1; +constexpr int32_t USER_ID = 100; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(HAS_USERID); + parcel.WriteInt32(USER_ID); + if (operateType) { + parcel.WriteParcelable(&admin); + std::vector bundles; + std::string fuzzString(reinterpret_cast(data), size); + bundles.push_back(fuzzString); + parcel.WriteStringVector(bundles); + } else { + parcel.WriteString(EdmConstants::PERMISSION_TAG_VERSION_11); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + } + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.h b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..a7602312d484821ad7aa5931b4db598e09b41fbd --- /dev/null +++ b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/disallowed_running_bundles_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_DISALLOWED_RUNNING_BUNDLES_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_DISALLOWED_RUNNING_BUNDLES_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "disallowed_running_bundles_plugin_fuzzer" + +#endif // TEST_FUZZTEST_DISALLOWED_RUNNING_BUNDLES_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/project.xml b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/disallowedrunningbundlesplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/edmplugin_fuzzer/edm_plugin_fuzzer.cpp b/test/fuzztest/edmplugin_fuzzer/edm_plugin_fuzzer.cpp index bdf96ad3f868ca0ad740d14283b507f282ba4896..9454a2db019d3b77921e5a9144ab67dca279ddde 100644 --- a/test/fuzztest/edmplugin_fuzzer/edm_plugin_fuzzer.cpp +++ b/test/fuzztest/edmplugin_fuzzer/edm_plugin_fuzzer.cpp @@ -35,7 +35,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) return 0; } uint32_t operateType = (CommonFuzzer::GetU32Data(data)) % 3; - uint32_t code = (CommonFuzzer::GetU32Data(data)) % 100 + MAX_EDM_INTERFACE_CODE; + uint32_t code = (CommonFuzzer::GetU32Data(data)) % 60 + MAX_EDM_INTERFACE_CODE; if (code == EdmInterfaceCode::RESET_FACTORY || code == EdmInterfaceCode::SHUTDOWN || code == EdmInterfaceCode::REBOOT || code == EdmInterfaceCode::USB_READ_ONLY || code == EdmInterfaceCode::DISABLED_HDC || code == EdmInterfaceCode::DISABLE_USB || diff --git a/test/fuzztest/firewallruleplugin_fuzzer/firewall_rule_plugin_fuzzer.cpp b/test/fuzztest/firewallruleplugin_fuzzer/firewall_rule_plugin_fuzzer.cpp index 5fc7f051578d087e4fa7d09a9b9b417b9e07ea93..915639c1f48a4543580af91c567965b9a2e0a24c 100644 --- a/test/fuzztest/firewallruleplugin_fuzzer/firewall_rule_plugin_fuzzer.cpp +++ b/test/fuzztest/firewallruleplugin_fuzzer/firewall_rule_plugin_fuzzer.cpp @@ -41,39 +41,41 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (size < MIN_SIZE) { return 0; } + + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; for (uint32_t operateType = static_cast(FuncOperateType::GET); operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { uint32_t code = EdmInterfaceCode::FIREWALL_RULE; code = POLICY_FUNC_CODE(operateType, code); AppExecFwk::ElementName admin; - admin.SetBundleName("com.example.edmtest"); - admin.SetAbilityName("com.example.edmtest.EnterpriseAdminAbility"); + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); MessageParcel parcel; parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); parcel.WriteInt32(WITHOUT_USERID); if (operateType) { parcel.WriteParcelable(&admin); - parcel.WriteString(""); + IPTABLES::FirewallRule firewall; + std::string srcAddr(reinterpret_cast(data), size); + std::string destAddr(reinterpret_cast(data), size); + std::string srcPort(reinterpret_cast(data), size); + std::string destPort(reinterpret_cast(data), size); + std::string uid(reinterpret_cast(data), size); + IPTABLES::Direction directionEnum = + static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); + IPTABLES::Action actionEnum = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); + IPTABLES::Protocol protocolEnum = + static_cast(CommonFuzzer::GetU32Data(data) % MAX_PROTOCOL_LENGTH); + firewall = {directionEnum, actionEnum, protocolEnum, srcAddr, destAddr, srcPort, destPort, uid}; + IPTABLES::FirewallRuleParcel firewallRuleParcel{firewall}; + firewallRuleParcel.Marshalling(parcel); } else { parcel.WriteString(""); parcel.WriteInt32(0); parcel.WriteParcelable(&admin); } - IPTABLES::FirewallRule firewall; - std::string srcAddr(reinterpret_cast(data), size); - std::string destAddr(reinterpret_cast(data), size); - std::string srcPort(reinterpret_cast(data), size); - std::string destPort(reinterpret_cast(data), size); - std::string uid(reinterpret_cast(data), size); - IPTABLES::Direction directionEnum = - static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); - IPTABLES::Action actionEnum = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); - IPTABLES::Protocol protocolEnum = - static_cast(CommonFuzzer::GetU32Data(data) % MAX_PROTOCOL_LENGTH); - firewall = {directionEnum, actionEnum, protocolEnum, srcAddr, destAddr, srcPort, destPort, uid}; - IPTABLES::FirewallRuleParcel firewallRuleParcel{firewall}; - firewallRuleParcel.Marshalling(parcel); CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); } diff --git a/test/fuzztest/getbluetoothinfoplugin_fuzzer/BUILD.gn b/test/fuzztest/getbluetoothinfoplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7732641cfa5869b6a49cd0ddae81bd81a365d6c3 --- /dev/null +++ b/test/fuzztest/getbluetoothinfoplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("GetBluetoothInfoPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "get_bluetooth_info_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/getbluetoothinfoplugin_fuzzer/corpus/init b/test/fuzztest/getbluetoothinfoplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/getbluetoothinfoplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.cpp b/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8506df46648ca9224340357154b3410dbe69b06a --- /dev/null +++ b/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "get_bluetooth_info_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 16; +constexpr size_t WITHOUT_USERID = 0; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + uint32_t code = EdmInterfaceCode::GET_BLUETOOTH_INFO; + code = POLICY_FUNC_CODE(static_cast(FuncOperateType::GET), code); + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + parcel.WriteString(""); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.h b/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..fe3331f66ec6a573aa57b8ffce02a6ddbef3cf63 --- /dev/null +++ b/test/fuzztest/getbluetoothinfoplugin_fuzzer/get_bluetooth_info_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_GET_BLUETOOTH_INFO_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_GET_BLUETOOTH_INFO_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "get_bluetooth_info_plugin_fuzzer" + +#endif // TEST_FUZZTEST_GET_BLUETOOTH_INFO_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/getbluetoothinfoplugin_fuzzer/project.xml b/test/fuzztest/getbluetoothinfoplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/getbluetoothinfoplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getdeviceinfoplugin_fuzzer/BUILD.gn b/test/fuzztest/getdeviceinfoplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7b7dd31d7b221bd09c90cd9e6f16ceb13089f1a3 --- /dev/null +++ b/test/fuzztest/getdeviceinfoplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("GetDeviceInfoPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "get_device_info_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/getdeviceinfoplugin_fuzzer/corpus/init b/test/fuzztest/getdeviceinfoplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/getdeviceinfoplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.cpp b/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98f6ce0588c8695bcdf8947c14e23b30bdc8f0d0 --- /dev/null +++ b/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "get_device_info_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "edm_constants.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 0; +constexpr size_t WITHOUT_USERID = 0; +constexpr const char *WITHOUT_PERMISSION_TAG = ""; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + uint32_t code = EdmInterfaceCode::GET_DEVICE_INFO; + code = POLICY_FUNC_CODE(static_cast(FuncOperateType::GET), code); + AppExecFwk::ElementName admin; + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + parcel.WriteString(WITHOUT_PERMISSION_TAG); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + std::vector deviceLabel = { + "deviceName", + "deviceSerial", + "simInfo", + "slotId", + "MEID", + "IMSI", + "ICCID", + "IMEI", + "invalidString", + }; + int dataPos = 0; + int index = CommonFuzzer::GetU32Data(data, dataPos, size) % deviceLabel.size(); + parcel.WriteString(deviceLabel[index]); + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.h b/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..563d4dec8f8e5b940d8c68f0079629c988b18cb9 --- /dev/null +++ b/test/fuzztest/getdeviceinfoplugin_fuzzer/get_device_info_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_GET_DEVICE_INFO_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_GET_DEVICE_INFO_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "get_device_info_plugin_fuzzer" + +#endif // TEST_FUZZTEST_GET_DEVICE_INFO_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/getdeviceinfoplugin_fuzzer/project.xml b/test/fuzztest/getdeviceinfoplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/getdeviceinfoplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/installplugin_fuzzer/BUILD.gn b/test/fuzztest/installplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c23cde25a864f0933ba3d26f91c5235ff701746c --- /dev/null +++ b/test/fuzztest/installplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("InstallPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "install_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/installplugin_fuzzer/corpus/init b/test/fuzztest/installplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/installplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.cpp b/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb5df39e3fe44f284bb2adede1794c6b1ffecc8 --- /dev/null +++ b/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "install_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 8; +constexpr int32_t WITHOUT_USERID = 0; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::INSTALL; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + int32_t pos = 0; + int32_t stringSize = size - pos; + std::string path(CommonFuzzer::GetString(data, pos, stringSize, size)); + std::vector realPaths = { path }; + parcel.WriteStringVector(realPaths); + int32_t uint32pos = 0; + int32_t installParamUserId = CommonFuzzer::GetU32Data(data, uint32pos, size); + parcel.WriteInt32(installParamUserId); + int32_t installParamInstallFlag = CommonFuzzer::GetU32Data(data, uint32pos, size); + parcel.WriteInt32(installParamInstallFlag); + } else { + parcel.WriteString(""); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + } + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.h b/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..1d7f0c1729ec6f3f2ed623941d28b27085f47b1e --- /dev/null +++ b/test/fuzztest/installplugin_fuzzer/install_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "power_policy_plugin_fuzzer" + +#endif // TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/installplugin_fuzzer/project.xml b/test/fuzztest/installplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/installplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/iptablesruleplugin_fuzzer/iptables_rule_plugin_fuzzer.cpp b/test/fuzztest/iptablesruleplugin_fuzzer/iptables_rule_plugin_fuzzer.cpp index 3a58e53ad918e84fb52f5af649947f8bbc156fef..ca1d5e3fb3051bd8e9a5f3e824a81444d490b8a2 100644 --- a/test/fuzztest/iptablesruleplugin_fuzzer/iptables_rule_plugin_fuzzer.cpp +++ b/test/fuzztest/iptablesruleplugin_fuzzer/iptables_rule_plugin_fuzzer.cpp @@ -38,51 +38,45 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (data == nullptr || size < MIN_SIZE) { return 0; } + + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; for (uint32_t operateType = static_cast(FuncOperateType::GET); operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { uint32_t code = EdmInterfaceCode::IPTABLES_RULE; code = POLICY_FUNC_CODE(operateType, code); AppExecFwk::ElementName admin; - admin.SetBundleName("com.example.edmtest"); - admin.SetAbilityName("com.example.edmtest.EnterpriseAdminAbility"); + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); MessageParcel parcel; parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); parcel.WriteInt32(WITHOUT_USERID); if (operateType) { parcel.WriteParcelable(&admin); - parcel.WriteString(""); + IPTABLES::AddFilter addFilter; + addFilter.ruleNo = CommonFuzzer::GetU32Data(data); + std::string srcAddr(reinterpret_cast(data), size); + std::string destAddr(reinterpret_cast(data), size); + std::string srcPort(reinterpret_cast(data), size); + std::string destPort(reinterpret_cast(data), size); + std::string uid(reinterpret_cast(data), size); + addFilter.srcAddr = srcAddr; + addFilter.destAddr = destAddr; + addFilter.srcPort = srcPort; + addFilter.destPort = destPort; + addFilter.uid = uid; + addFilter.method = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); + addFilter.direction = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); + addFilter.action = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); + addFilter.protocol = static_cast(CommonFuzzer::GetU32Data(data) % MAX_PROTOCOL_LENGTH); + IPTABLES::IptablesUtils::WriteAddFilterConfig(addFilter, parcel); } else { parcel.WriteString(""); parcel.WriteInt32(0); parcel.WriteParcelable(&admin); } - IPTABLES::AddFilter addFilter; - addFilter.ruleNo = CommonFuzzer::GetU32Data(data); - std::string srcAddr(reinterpret_cast(data), size); - std::string destAddr(reinterpret_cast(data), size); - std::string srcPort(reinterpret_cast(data), size); - std::string destPort(reinterpret_cast(data), size); - std::string uid(reinterpret_cast(data), size); - addFilter.srcAddr = srcAddr; - addFilter.destAddr = destAddr; - addFilter.srcPort = srcPort; - addFilter.destPort = destPort; - addFilter.uid = uid; - addFilter.method = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); - addFilter.direction = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); - addFilter.action = static_cast(CommonFuzzer::GetU32Data(data) % MAX_ENUM_LENGTH); - addFilter.protocol = static_cast(CommonFuzzer::GetU32Data(data) % MAX_PROTOCOL_LENGTH); - IPTABLES::IptablesUtils::WriteAddFilterConfig(addFilter, parcel); CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); - - MessageParcel messageParcel; - messageParcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); - messageParcel.WriteInt32(WITHOUT_USERID); - messageParcel.WriteParcelable(&admin); - IPTABLES::AddFilter addFilterFuzzTest = GetData(); - IPTABLES::IptablesUtils::WriteAddFilterConfig(addFilterFuzzTest, messageParcel); - CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, messageParcel); } return 0; } diff --git a/test/fuzztest/manageautostartappsplugin_fuzzer/manage_auto_start_apps_plugin_fuzzer.cpp b/test/fuzztest/manageautostartappsplugin_fuzzer/manage_auto_start_apps_plugin_fuzzer.cpp index 284b91a00dd04cbb5950e8c7964f5fdb91a00d74..6802bbde2b46444ab0bfeb55702ccb4519c7e743 100644 --- a/test/fuzztest/manageautostartappsplugin_fuzzer/manage_auto_start_apps_plugin_fuzzer.cpp +++ b/test/fuzztest/manageautostartappsplugin_fuzzer/manage_auto_start_apps_plugin_fuzzer.cpp @@ -38,32 +38,32 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (size < MIN_SIZE) { return 0; } + + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; for (uint32_t operateType = static_cast(FuncOperateType::GET); operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { uint32_t code = EdmInterfaceCode::MANAGE_AUTO_START_APPS; code = POLICY_FUNC_CODE(operateType, code); AppExecFwk::ElementName admin; - admin.SetBundleName("com.example.edmtest"); - admin.SetAbilityName("com.example.edmtest.EnterpriseAdminAbility"); + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); MessageParcel parcel; parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); parcel.WriteInt32(WITHOUT_USERID); if (operateType) { parcel.WriteParcelable(&admin); - parcel.WriteString(""); + std::vector autoStartAppsString; + std::string bundleName(reinterpret_cast(data), size / 2); + std::string abilityName(reinterpret_cast(data) + size / 2, size / 2); + autoStartAppsString.push_back(bundleName + "/" + abilityName); + parcel.WriteStringVector(autoStartAppsString); } else { parcel.WriteString(""); parcel.WriteInt32(0); parcel.WriteParcelable(&admin); } - - std::vector autoStartAppsString; - std::string bundleName(reinterpret_cast(data), size / 2); - std::string abilityName(reinterpret_cast(data) + size / 2, size / 2); - autoStartAppsString.push_back(bundleName + "/" + abilityName); - parcel.WriteStringVector(autoStartAppsString); - CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); } return 0; diff --git a/test/fuzztest/powerpolicyplugin_fuzzer/BUILD.gn b/test/fuzztest/powerpolicyplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0aedf9c6f7cc2301f2d825830217566ed4bf5190 --- /dev/null +++ b/test/fuzztest/powerpolicyplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("PowerPolicyPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "power_policy_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/powerpolicyplugin_fuzzer/corpus/init b/test/fuzztest/powerpolicyplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/powerpolicyplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.cpp b/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd5a6146183189769ebb13b504f3d8c3f8e0b473 --- /dev/null +++ b/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "power_policy_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "power_policy.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 4; +constexpr int32_t WITHOUT_USERID = 0; +constexpr uint32_t MAX_POWER_POLICY_ACTION_ENUM = 5; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::USB_READ_ONLY; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + int32_t pos = 0; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + uint32_t powerScene = CommonFuzzer::GetU32Data(data); + parcel.WriteUint32(powerScene); + uint32_t powerPolicyAction = CommonFuzzer::GetU32Data(data) % MAX_POWER_POLICY_ACTION_ENUM; + parcel.WriteUint32(powerPolicyAction); + uint32_t delayTime = CommonFuzzer::GetU32Data(data); + parcel.WriteUint32(delayTime); + } else { + parcel.WriteString(""); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + } + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.h b/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..1d7f0c1729ec6f3f2ed623941d28b27085f47b1e --- /dev/null +++ b/test/fuzztest/powerpolicyplugin_fuzzer/power_policy_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "power_policy_plugin_fuzzer" + +#endif // TEST_FUZZTEST_POWER_POLICY_PLUGIN_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/powerpolicyplugin_fuzzer/project.xml b/test/fuzztest/powerpolicyplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/powerpolicyplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/setbrowserpoliciesplugin_fuzzer/BUILD.gn b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8c42b5f1b819863548d70b3a2d2de017d8a5a35f --- /dev/null +++ b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("SetBrowserPoliciesPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "set_browser_policies_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/setbrowserpoliciesplugin_fuzzer/corpus/init b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/setbrowserpoliciesplugin_fuzzer/project.xml b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.cpp b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0789a2719f6b8f11b99e3a49c31e7c5ee2a30112 --- /dev/null +++ b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "set_browser_policies_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_constants.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 24; +constexpr size_t WITHOUT_USERID = 0; +constexpr int32_t HAS_ADMIN = 0; +constexpr int32_t WITHOUT_ADMIN = 1; + +void SetParcelContent(MessageParcel &parcel, uint32_t operateType, + const uint8_t* data, size_t size, AppExecFwk::ElementName admin) { + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + bool isSetAll = CommonFuzzer::GetU32Data(data) % 2; + int32_t pos = 0; + int32_t stringSize = (size - pos) / 3; + std::string appId(CommonFuzzer::GetString(data, pos, stringSize, size)); + std::string policies(CommonFuzzer::GetString(data, pos, stringSize, size)); + if (isSetAll) { + parcel.WriteInt32(EdmConstants::SET_POLICIES_TYPE); + std::vector appIds; + std::vector policiesList; + appIds.push_back(appId); + policiesList.push_back(policies); + parcel.WriteStringVector(appIds); + parcel.WriteStringVector(policiesList); + } else { + parcel.WriteString(""); + parcel.WriteInt32(EdmConstants::SET_POLICY_TYPE); + std::string policyName(CommonFuzzer::GetString(data, pos, stringSize, size)); + std::vector params; + params.push_back(appId); + params.push_back(policyName); + params.push_back(policies); + parcel.WriteStringVector(params); + } + } else { + parcel.WriteString(""); + bool hasAdmin = CommonFuzzer::GetU32Data(data) % 2; + if (hasAdmin) { + parcel.WriteInt32(HAS_ADMIN); + parcel.WriteParcelable(&admin); + } else { + parcel.WriteInt32(WITHOUT_ADMIN); + } + std::string appId(reinterpret_cast(data), size); + parcel.WriteString(appId); + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::SET_BROWSER_POLICIES; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + SetParcelContent(parcel, operateType, data, size, admin); + + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.h b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..3510af56e2714795f31b8cbc94fded65621c5375 --- /dev/null +++ b/test/fuzztest/setbrowserpoliciesplugin_fuzzer/set_browser_policies_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_SET_BROWSER_POLICIES_PLUGIN_FUZZER_FUZZER_H +#define TEST_FUZZTEST_SET_BROWSER_POLICIES_PLUGIN_FUZZER_FUZZER_H + +#define FUZZ_PROJECT_NAME "set_browser_policies_plugin_fuzzer" + +#endif // TEST_FUZZTEST_SET_BROWSER_POLICIES_PLUGIN_FUZZER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/usbreadonlyplugin_fuzzer/usb_read_only_plugin_fuzzer.cpp b/test/fuzztest/usbreadonlyplugin_fuzzer/usb_read_only_plugin_fuzzer.cpp index 4f32de056047a7427ad76a4a6f2f31a6b64e0aca..5839e25f4ec4d253100dd7718bc8a415c9a585e6 100644 --- a/test/fuzztest/usbreadonlyplugin_fuzzer/usb_read_only_plugin_fuzzer.cpp +++ b/test/fuzztest/usbreadonlyplugin_fuzzer/usb_read_only_plugin_fuzzer.cpp @@ -38,28 +38,28 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (size < MIN_SIZE) { return 0; } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 2; for (uint32_t operateType = static_cast(FuncOperateType::GET); operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { uint32_t code = EdmInterfaceCode::USB_READ_ONLY; code = POLICY_FUNC_CODE(operateType, code); AppExecFwk::ElementName admin; - admin.SetBundleName("com.example.edmtest"); - admin.SetAbilityName("com.example.edmtest.EnterpriseAdminAbility"); + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); MessageParcel parcel; parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); parcel.WriteInt32(WITHOUT_USERID); if (operateType) { parcel.WriteParcelable(&admin); - parcel.WriteString(""); + int32_t isReadOnly = CommonFuzzer::GetU32Data(data) % 2; + parcel.WriteInt32(isReadOnly); } else { parcel.WriteString(""); parcel.WriteInt32(0); parcel.WriteParcelable(&admin); } - int32_t isReadOnly = CommonFuzzer::GetU32Data(data) % 2; - parcel.WriteInt32(isReadOnly); - CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); } return 0; diff --git a/test/fuzztest/usercertplugin_fuzzer/BUILD.gn b/test/fuzztest/usercertplugin_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6d19de0aa882d8d9ae52e9e4a885094e98dd95be --- /dev/null +++ b/test/fuzztest/usercertplugin_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "enterprise_device_management/enterprise_device_management" + +##############################fuzztest########################################## +ohos_fuzztest("UserCertPluginFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "../common/src/common_fuzzer.cpp", + "user_cert_plugin_fuzzer.cpp", + ] + + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/common/include", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../common/external:edm_external_adapters", + "../../../common/native:edm_commom", + "../../../interfaces/inner_api:edmservice_kits", + "../../../interfaces/inner_api/plugin_kits:plugin_kits", + "../../unittest/utils:edm_unittest_utils", + "../enterprisedevicemgrstubmock_fuzzer:edmservice_fuzz_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/test/fuzztest/usercertplugin_fuzzer/corpus/init b/test/fuzztest/usercertplugin_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..bc977bd9738ee9a70b362067f57a9c63d3adb801 --- /dev/null +++ b/test/fuzztest/usercertplugin_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/usercertplugin_fuzzer/project.xml b/test/fuzztest/usercertplugin_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /dev/null +++ b/test/fuzztest/usercertplugin_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.cpp b/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9285db07f1446c5f1574e0493b5706942b2b220 --- /dev/null +++ b/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "user_cert_plugin_fuzzer.h" + +#include + +#include "common_fuzzer.h" +#include "edm_constants.h" +#include "edm_ipc_interface_code.h" +#include "ienterprise_device_mgr.h" +#include "func_code.h" +#include "message_parcel.h" +#include "utils.h" + +namespace OHOS { +namespace EDM { +constexpr size_t MIN_SIZE = 24; +constexpr size_t WITHOUT_USERID = 0; + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + if (size < MIN_SIZE) { + return 0; + } + int32_t pos = 0; + int32_t stringSize = (size - pos) / 3; + for (uint32_t operateType = static_cast(FuncOperateType::GET); + operateType <= static_cast(FuncOperateType::REMOVE); operateType++) { + uint32_t code = EdmInterfaceCode::INSTALL_CERTIFICATE; + code = POLICY_FUNC_CODE(operateType, code); + + AppExecFwk::ElementName admin; + admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size)); + admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size)); + MessageParcel parcel; + parcel.WriteInterfaceToken(IEnterpriseDeviceMgr::GetDescriptor()); + parcel.WriteInt32(WITHOUT_USERID); + if (operateType) { + parcel.WriteParcelable(&admin); + if (operateType == static_cast(FuncOperateType::SET)) { + std::vector certArray {(*data)}; + parcel.WriteUInt8Vector(certArray); + } + parcel.WriteString (CommonFuzzer::GetString(data, pos, stringSize, size)); + } else { + parcel.WriteString(""); + parcel.WriteInt32(0); + parcel.WriteParcelable(&admin); + } + CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel); + } + return 0; +} +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.h b/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..0ec6be7eca5ac630b50f890e4a1e50494857b988 --- /dev/null +++ b/test/fuzztest/usercertplugin_fuzzer/user_cert_plugin_fuzzer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_FUZZTEST_USER_CERT_PLUGIN_FUZZER_H +#define TEST_FUZZTEST_USER_CERT_PLUGIN_FUZZER_H + +#define FUZZ_PROJECT_NAME "user_cert_plugin_fuzzer" + +#endif // TEST_FUZZTEST_USER_CERT_PLUGIN_FUZZER_H \ No newline at end of file