diff --git a/BUILD.gn b/BUILD.gn index 1e5de35b7..1c1244e9f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -28,6 +28,7 @@ group("account_build_unittest") { "${os_account_path}/frameworks/osaccount/native/test/benchmarktest:benchmarktest", "${os_account_path}/frameworks/osaccount/native/test/unittest:unittest", "${os_account_path}/frameworks/test/unittest:unittest", + "${os_account_path}/interfaces/kits/capi/test/unittest:unittest", "${os_account_path}/services/accountmgr/test:unittest", "${os_account_path}/test/systemtest:systemtest", "${os_account_path}/tools/test:unittest", diff --git a/bundle.json b/bundle.json index a59b0a0cd..8c113e14d 100644 --- a/bundle.json +++ b/bundle.json @@ -44,6 +44,7 @@ "access_token", "ace_engine", "asset", + "bounds_checking_function", "bundle_framework", "common_event_service", "c_utils", @@ -73,13 +74,13 @@ ], "third_party": [ "mbedtls", - "bounds_checking_function", "jsoncpp" ] }, "build": { "group_type": { "base_group": [ + "//base/account/os_account/interfaces/kits/capi:capi_packages", "//base/account/os_account/interfaces/kits/napi:napi_packages", "//base/account/os_account/sa_profile:account_sa_profile", "//base/account/os_account/tools:tools_acm" diff --git a/frameworks/common/include/accountmgr_service_ipc_interface_code.h b/frameworks/common/include/accountmgr_service_ipc_interface_code.h index 6965f75ba..5546abb26 100644 --- a/frameworks/common/include/accountmgr_service_ipc_interface_code.h +++ b/frameworks/common/include/accountmgr_service_ipc_interface_code.h @@ -92,6 +92,7 @@ enum class OsAccountInterfaceCode : uint32_t { SET_DEFAULT_ACTIVATED_OS_ACCOUNT, GET_DEFAULT_ACTIVATED_OS_ACCOUNT, GET_OS_ACCOUNT_SHORT_NAME, + GET_OS_ACCOUNT_NAME, GET_OS_ACCOUNT_SHORT_NAME_BY_ID, IS_OS_ACCOUNT_FOREGROUND, GET_FOREGROUND_OS_ACCOUNT_LOCAL_ID, diff --git a/frameworks/osaccount/core/include/ios_account.h b/frameworks/osaccount/core/include/ios_account.h index a6ca36b06..c15c3976c 100644 --- a/frameworks/osaccount/core/include/ios_account.h +++ b/frameworks/osaccount/core/include/ios_account.h @@ -102,6 +102,7 @@ public: virtual ErrCode SetDefaultActivatedOsAccount(const int32_t id) = 0; virtual ErrCode GetDefaultActivatedOsAccount(int32_t &id) = 0; virtual ErrCode GetOsAccountShortName(std::string &shortName) = 0; + virtual ErrCode GetOsAccountName(std::string &name) = 0; virtual ErrCode GetOsAccountShortNameById(const int32_t id, std::string &shortName) = 0; virtual ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) = 0; virtual ErrCode GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) = 0; diff --git a/frameworks/osaccount/core/include/os_account.h b/frameworks/osaccount/core/include/os_account.h index 89934f6ad..b0a96d234 100644 --- a/frameworks/osaccount/core/include/os_account.h +++ b/frameworks/osaccount/core/include/os_account.h @@ -90,6 +90,7 @@ public: ErrCode SetDefaultActivatedOsAccount(const int32_t id); ErrCode GetDefaultActivatedOsAccount(int32_t &id); ErrCode GetOsAccountShortName(std::string &shortName); + ErrCode GetOsAccountName(std::string &name); ErrCode GetOsAccountShortNameById(const int32_t id, std::string &shortName); ErrCode IsOsAccountForeground(bool &isForeground); diff --git a/frameworks/osaccount/core/include/os_account_proxy.h b/frameworks/osaccount/core/include/os_account_proxy.h index 70c575430..090e9901e 100644 --- a/frameworks/osaccount/core/include/os_account_proxy.h +++ b/frameworks/osaccount/core/include/os_account_proxy.h @@ -95,6 +95,7 @@ public: ErrCode SetDefaultActivatedOsAccount(const int32_t id) override; ErrCode GetDefaultActivatedOsAccount(int32_t &id) override; ErrCode GetOsAccountShortName(std::string &shortName) override; + ErrCode GetOsAccountName(std::string &name) override; ErrCode GetOsAccountShortNameById(const int32_t id, std::string &shortName) override; ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) override; ErrCode GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) override; diff --git a/frameworks/osaccount/core/src/os_account.cpp b/frameworks/osaccount/core/src/os_account.cpp index d1278f308..3f96d000c 100644 --- a/frameworks/osaccount/core/src/os_account.cpp +++ b/frameworks/osaccount/core/src/os_account.cpp @@ -868,6 +868,15 @@ ErrCode OsAccount::GetOsAccountShortName(std::string &shortName) return proxy->GetOsAccountShortName(shortName); } +ErrCode OsAccount::GetOsAccountName(std::string &name) +{ + auto proxy = GetOsAccountProxy(); + if (proxy == nullptr) { + return ERR_ACCOUNT_COMMON_GET_PROXY; + } + return proxy->GetOsAccountName(name); +} + ErrCode OsAccount::GetOsAccountShortNameById(const int32_t id, std::string &shortName) { auto proxy = GetOsAccountProxy(); diff --git a/frameworks/osaccount/core/src/os_account_proxy.cpp b/frameworks/osaccount/core/src/os_account_proxy.cpp index d91ad17f8..8fb1f517a 100644 --- a/frameworks/osaccount/core/src/os_account_proxy.cpp +++ b/frameworks/osaccount/core/src/os_account_proxy.cpp @@ -1453,6 +1453,37 @@ ErrCode OsAccountProxy::GetOsAccountShortName(std::string &shortName) return ERR_OK; } +ErrCode OsAccountProxy::GetOsAccountName(std::string &name) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + ACCOUNT_LOGE("Failed to write descriptor!"); + return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR; + } + + MessageParcel reply; + ErrCode result = SendRequest(OsAccountInterfaceCode::GET_OS_ACCOUNT_NAME, data, reply); + if (result != ERR_OK) { + ACCOUNT_LOGE("SendRequest err, result %{public}d.", result); + return result; + } + + if (!reply.ReadInt32(result)) { + ACCOUNT_LOGE("Read result from reply failed."); + return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR; + } + if (result != ERR_OK) { + ACCOUNT_LOGE("Failed to read reply for is current os account verified, result=%{public}d.", result); + return result; + } + if (!reply.ReadString(name)) { + ACCOUNT_LOGE("Failed to read short name"); + return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR; + } + + return ERR_OK; +} + ErrCode OsAccountProxy::GetOsAccountShortNameById(const int32_t id, std::string &shortName) { MessageParcel data; diff --git a/frameworks/osaccount/native/libos_account_innerkits.map b/frameworks/osaccount/native/libos_account_innerkits.map index 474b49265..e3d8bed8f 100644 --- a/frameworks/osaccount/native/libos_account_innerkits.map +++ b/frameworks/osaccount/native/libos_account_innerkits.map @@ -144,6 +144,7 @@ "OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(std::__h::vector>&)"; "OHOS::AccountSA::OsAccountManager::SetGlobalOsAccountConstraints(std::__h::vector, std::__h::allocator>, std::__h::allocator, std::__h::allocator>>> const&, bool, int, bool)"; "OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(std::__h::basic_string, std::__h::allocator>&)"; + "OHOS::AccountSA::OsAccountManager::GetOsAccountName(std::__h::basic_string, std::__h::allocator>&)"; "OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(int, std::__h::basic_string, std::__h::allocator>&)"; OHOS::AccountSA::OsAccountManager::IsOsAccountForeground*; OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId*; diff --git a/frameworks/osaccount/native/src/os_account_manager.cpp b/frameworks/osaccount/native/src/os_account_manager.cpp index b4bdc2bd3..b90e42a9c 100644 --- a/frameworks/osaccount/native/src/os_account_manager.cpp +++ b/frameworks/osaccount/native/src/os_account_manager.cpp @@ -342,6 +342,11 @@ ErrCode OsAccountManager::GetOsAccountShortName(const int32_t id, std::string &s return OsAccount::GetInstance().GetOsAccountShortNameById(id, shortName); } +ErrCode OsAccountManager::GetOsAccountName(std::string &name) +{ + return OsAccount::GetInstance().GetOsAccountName(name); +} + ErrCode OsAccountManager::IsOsAccountForeground(bool &isForeground) { return OsAccount::GetInstance().IsOsAccountForeground(isForeground); diff --git a/frameworks/osaccount/native/test/unittest/os_account_info_test.cpp b/frameworks/osaccount/native/test/unittest/os_account_info_test.cpp index 8657e798f..60b3a0ede 100644 --- a/frameworks/osaccount/native/test/unittest/os_account_info_test.cpp +++ b/frameworks/osaccount/native/test/unittest/os_account_info_test.cpp @@ -30,6 +30,9 @@ const int INT_ID = 12; const std::string STRING_NAME = "account"; const OsAccountType INT_TYPE = OsAccountType::ADMIN; const int64_t STRING_SERIAL_NUMBER = 121012012; +constexpr std::int32_t UID_TRANSFORM_DIVISOR = 200000; +const int32_t ROOT_UID = 0; +const int32_t TEST_UID = 100; const std::vector VECTOR_CONSTRAINTS {"one", "two", "three", "four", "five"}; const bool BOOL_IS_OS_ACCOUNT_VERIFIED = true; const bool BOOL_IS_OS_ACCOUNT_COMPLETED = true; @@ -470,13 +473,57 @@ HWTEST_F(OsAccountInfoTest, OsAccountInfo_Marshalling_0100, TestSize.Level1) EXPECT_EQ(INT_ID, infoPtr->localId_); } +/** + * @tc.name: GetOsAccountName01 + * @tc.desc: Test GetOsAccountName + * @tc.type: FUNC + * @tc.require: issueI8ZEEN + */ +HWTEST_F(OsAccountInfoTest, GetOsAccountName01, TestSize.Level1) +{ + std::string name; + setuid(TEST_UID * UID_TRANSFORM_DIVISOR); + EXPECT_EQ(ERR_OK, OsAccountManager::GetOsAccountName(name)); + OsAccountInfo osAccountInfo; + setuid(ROOT_UID); + EXPECT_EQ(ERR_OK, OsAccountManager::QueryOsAccountById(TEST_UID, osAccountInfo)); + EXPECT_EQ(name, osAccountInfo.GetLocalName()); +} + +#ifdef ENABLE_MULTIPLE_OS_ACCOUNTS +/** + * @tc.name: GetOsAccountName02 + * @tc.desc: Test GetOsAccountName + * @tc.type: FUNC + * @tc.require: issueI8ZEEN + */ +HWTEST_F(OsAccountInfoTest, GetOsAccountName02, TestSize.Level1) +{ + OsAccountInfo osAccountInfo; + EXPECT_EQ(ERR_OK, OsAccountManager::CreateOsAccount("GetOsAccountName02", OsAccountType::NORMAL, osAccountInfo)); + uint32_t localId = osAccountInfo.GetLocalId(); + setuid(localId * UID_TRANSFORM_DIVISOR); + std::string name; + EXPECT_EQ(ERR_OK, OsAccountManager::GetOsAccountName(name)); + EXPECT_EQ("GetOsAccountName02", name); + setuid(ROOT_UID); + EXPECT_EQ(ERR_OK, OsAccountManager::SetOsAccountName(localId, "updateName")); + setuid(localId * UID_TRANSFORM_DIVISOR); + EXPECT_EQ(ERR_OK, OsAccountManager::GetOsAccountName(name)); + EXPECT_EQ(name, "updateName"); + setuid(ROOT_UID); + EXPECT_EQ(ERR_OK, OsAccountManager::RemoveOsAccount(localId)); + setuid(localId * UID_TRANSFORM_DIVISOR); + EXPECT_NE(ERR_OK, OsAccountManager::GetOsAccountName(name)); + setuid(ROOT_UID); +} + /** * @tc.name: CreateOsAccountWithFullInfo0100 * @tc.desc: Test CreateOsAccountWithFullInfo ERR_ACCOUNT_COMMON_INVALID_PARAMETER * @tc.type: FUNC * @tc.require: */ -#ifdef ENABLE_MULTIPLE_OS_ACCOUNTS HWTEST_F(OsAccountInfoTest, CreateOsAccountWithFullInfo0100, TestSize.Level1) { OsAccountInfo osAccountInfo; diff --git a/interfaces/innerkits/osaccount/native/include/os_account_manager.h b/interfaces/innerkits/osaccount/native/include/os_account_manager.h index fbc0e9c0b..58372ea59 100644 --- a/interfaces/innerkits/osaccount/native/include/os_account_manager.h +++ b/interfaces/innerkits/osaccount/native/include/os_account_manager.h @@ -556,6 +556,13 @@ public: */ static ErrCode GetOsAccountShortName(std::string &shortName); + /** + * @brief Gets the currend user local name. + * @param shortName - Indicates the current user local name of the OS account. + * @return error code, see account_error_no.h + */ + static ErrCode GetOsAccountName(std::string &name); + /** * @brief Gets the user short name, based on account id. * @permission ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS diff --git a/interfaces/kits/capi/BUILD.gn b/interfaces/kits/capi/BUILD.gn new file mode 100644 index 000000000..4d15a8136 --- /dev/null +++ b/interfaces/kits/capi/BUILD.gn @@ -0,0 +1,18 @@ +# 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. + +import("//build/ohos.gni") + +group("capi_packages") { + deps = [ "osaccount:libos_account_ndk" ] +} diff --git a/interfaces/kits/capi/osaccount/BUILD.gn b/interfaces/kits/capi/osaccount/BUILD.gn new file mode 100644 index 000000000..9ac5478b1 --- /dev/null +++ b/interfaces/kits/capi/osaccount/BUILD.gn @@ -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. + +import("//build/ohos.gni") +import("../../../../os_account.gni") + +ohos_shared_library("libos_account_ndk") { + innerapi_tags = [ "ndk" ] + output_extension = "so" + + sanitize = { + integer_overflow = true + cfi = true + debug = false + cfi_cross_dso = true + boundary_sanitize = true + ubsan = true + } + + sources = [ "src/os_account.cpp" ] + include_dirs = [ + "include", + "${common_path}/log/", + ] + + defines = [ + "ACCOUNT_LOG_TAG = \"OsAccountNDK\"", + "LOG_DOMAIN = 0xD001B00", + ] + + deps = [ + "${common_path}:libaccount_common", + "${os_account_innerkits_native_path}:os_account_innerkits", + ] + + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags_cc = [ + "-Wall", + "-Werror", + ] + + subsystem_name = "account" + part_name = "os_account" +} diff --git a/interfaces/kits/capi/osaccount/include/os_account.h b/interfaces/kits/capi/osaccount/include/os_account.h new file mode 100644 index 000000000..89c966a48 --- /dev/null +++ b/interfaces/kits/capi/osaccount/include/os_account.h @@ -0,0 +1,60 @@ +/* + * 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 OS_ACCOUNT_H +#define OS_ACCOUNT_H + +/** + * @addtogroup OsAccount + * @{ + * + * @brief Provide the definition of the C interface for the native OsAccount. + * @since 12 + */ +/** + * @file os_account.h + * + * @brief Declares the APIs for accessing and managing the OS account information. + * @library libos_account.so + * @syscap SystemCapability.Account.OsAccount + * @since 12 + */ + +#include +#include "os_account_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Gets the name of the OS account to which the caller process belongs. + * + * @param buffer The name character array which should have space for the name and the terminating character ('\0'). + * @param buffer_size The size of the name character array. + * @return OS_ACCOUNT_ERR_OK - Indicates successful;
+ * OS_ACCOUNT_ERR_INTERNAL_ERROR - Indicates the internal error.
+ * OS_ACCOUNT_ERR_INVALID_PARAMETER - Indicates the buf is NULL pointer or the size of the name, + * including the terminating character ('\0'), is larger then buffer_size; + * @syscap SystemCapability.Account.OsAccount + * @since 12 + */ +OsAccount_ErrCode OH_OsAccount_GetName(char *buffer, size_t buffer_size); + +#ifdef __cplusplus +}; +#endif + +#endif // OS_ACCOUNT_H \ No newline at end of file diff --git a/interfaces/kits/capi/osaccount/include/os_account_common.h b/interfaces/kits/capi/osaccount/include/os_account_common.h new file mode 100644 index 000000000..1e8315505 --- /dev/null +++ b/interfaces/kits/capi/osaccount/include/os_account_common.h @@ -0,0 +1,65 @@ +/* + * 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 OS_ACCOUNT_COMMON_H +#define OS_ACCOUNT_COMMON_H + +/** + * @addtogroup OsAccount + * @{ + * + * @brief Provide the definition of the C interface for the native OsAccount. + * @since 12 + */ +/** + * @file os_account_common.h + * + * @brief Declare the common types for the native OsAccount. + * @library libos_account.so + * @syscap SystemCapability.Account.OsAccount + * @since 12 + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the error codes. + * + * @since 12 + */ +typedef enum OsAccount_ErrCode { + /** + * Operation is successful. + */ + OS_ACCOUNT_ERR_OK = 0, + + /** + * Internal error. + */ + OS_ACCOUNT_ERR_INTERNAL_ERROR = 12300001, + + /** + * Invalid parameter. + */ + OS_ACCOUNT_ERR_INVALID_PARAMETER = 12300002 +} OsAccount_ErrCode; + +#ifdef __cplusplus +} +#endif + +#endif // OS_ACCOUNT_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/capi/osaccount/libos_account.ndk.json b/interfaces/kits/capi/osaccount/libos_account.ndk.json new file mode 100644 index 000000000..6b18c9380 --- /dev/null +++ b/interfaces/kits/capi/osaccount/libos_account.ndk.json @@ -0,0 +1,6 @@ +[ + { + "first_introduced": "12", + "name":"OH_OsAccount_GetName" + } +] \ No newline at end of file diff --git a/interfaces/kits/capi/osaccount/src/os_account.cpp b/interfaces/kits/capi/osaccount/src/os_account.cpp new file mode 100644 index 000000000..e3f26b185 --- /dev/null +++ b/interfaces/kits/capi/osaccount/src/os_account.cpp @@ -0,0 +1,49 @@ +/* + * 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 "os_account.h" +#include +#include +#include "account_log_wrapper.h" +#include "os_account_common.h" +#include "os_account_manager.h" + +using namespace OHOS; +using namespace OHOS::AccountSA; +OsAccount_ErrCode OH_OsAccount_GetName(char *buffer, size_t buffer_size) +{ + if ((buffer == nullptr) || (buffer_size == 0)) { + ACCOUNT_LOGE("Buffer is nullptr or length is zero."); + return OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER; + } + std::string accountName; + ErrCode err = AccountSA::OsAccountManager::GetOsAccountName(accountName); + if (err != ERR_OK) { + ACCOUNT_LOGE("Internal error(%{public}d).", err); + return OsAccount_ErrCode::OS_ACCOUNT_ERR_INTERNAL_ERROR; + } + size_t accountSize = accountName.size(); + if (buffer_size <= accountSize) { + ACCOUNT_LOGE( + "Buffer size(%{public}zu) is less than length of account name(%{public}d).", buffer_size, accountSize); + return OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER; + } + err = strncpy_s(buffer, buffer_size, accountName.c_str(), accountSize); + if (err != EOK) { + ACCOUNT_LOGE("Failed to strncpy_s, err(%{public}d).", err); + return OsAccount_ErrCode::OS_ACCOUNT_ERR_INTERNAL_ERROR; + } + buffer[accountSize] = '\0'; + return OsAccount_ErrCode::OS_ACCOUNT_ERR_OK; +} \ No newline at end of file diff --git a/interfaces/kits/capi/test/unittest/BUILD.gn b/interfaces/kits/capi/test/unittest/BUILD.gn new file mode 100644 index 000000000..8a8beafef --- /dev/null +++ b/interfaces/kits/capi/test/unittest/BUILD.gn @@ -0,0 +1,22 @@ +# 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("../../../../../os_account.gni") + +group("unittest") { + testonly = true + + deps = [ "osaccount:unittest" ] +} diff --git a/interfaces/kits/capi/test/unittest/osaccount/BUILD.gn b/interfaces/kits/capi/test/unittest/osaccount/BUILD.gn new file mode 100644 index 000000000..472621f46 --- /dev/null +++ b/interfaces/kits/capi/test/unittest/osaccount/BUILD.gn @@ -0,0 +1,51 @@ +# 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. + +import("//build/test.gni") +import("../../../../../../os_account.gni") + +module_output_path = "os_account/interfaces/kit/ndk" + +ohos_unittest("os_account_ndk_test") { + branch_protector_ret = "pac_ret" + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + module_out_path = module_output_path + + sources = [ "os_account_ndk_test.cpp" ] + include_dirs = [ + "./", + "${os_account_path}/interfaces/kits/capi/osaccount/include", + ] + + configs = [ "${account_coverage_config_path}:coverage_flags" ] + + deps = [ + "${os_account_path}/interfaces/kits/capi/osaccount:libos_account_ndk", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ "c_utils:utils" ] +} + +group("unittest") { + testonly = true + + deps = [ ":os_account_ndk_test" ] +} diff --git a/interfaces/kits/capi/test/unittest/osaccount/os_account_ndk_test.cpp b/interfaces/kits/capi/test/unittest/osaccount/os_account_ndk_test.cpp new file mode 100644 index 000000000..fa4333d88 --- /dev/null +++ b/interfaces/kits/capi/test/unittest/osaccount/os_account_ndk_test.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 + +#include +#include "errors.h" +#include "os_account.h" +#include "os_account_common.h" + +namespace OHOS { +namespace AccountSA { +using namespace testing::ext; +using namespace OHOS::AccountSA; +using namespace OHOS; +using namespace AccountSA; +namespace { +static const uint32_t MAX_NAME_LENGTH = 1024; +} +class OsAccountNDKTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(void) override; + void TearDown(void) override; +}; + +void OsAccountNDKTest::SetUpTestCase(void) +{ +} + +void OsAccountNDKTest::TearDownTestCase(void) +{} + +void OsAccountNDKTest::SetUp(void) __attribute__((no_sanitize("cfi"))) +{ + testing::UnitTest *test = testing::UnitTest::GetInstance(); + ASSERT_NE(test, nullptr); + const testing::TestInfo *testinfo = test->current_test_info(); + ASSERT_NE(testinfo, nullptr); + string testCaseName = string(testinfo->name()); + GTEST_LOG_(INFO) <<"[SetUp] " << testCaseName.c_str() << " start." << std::endl; +} + +void OsAccountNDKTest::TearDown(void) +{} + +/** + * @tc.name: GetOsAccountNameTest001 + * @tc.desc: Test invalid paramter. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(OsAccountNDKTest, GetOsAccountNameTest001, TestSize.Level0) +{ + char str[MAX_NAME_LENGTH] = { 0 }; + EXPECT_EQ(OH_OsAccount_GetName(nullptr, MAX_NAME_LENGTH), OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER); + EXPECT_EQ(OH_OsAccount_GetName(str, 0), OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER); + EXPECT_EQ(OH_OsAccount_GetName(str, MAX_NAME_LENGTH), OsAccount_ErrCode::OS_ACCOUNT_ERR_OK); +} +} // namespace AccountTest +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/osaccount/include/napi_os_account.h b/interfaces/kits/napi/osaccount/include/napi_os_account.h index 57253c902..0fc535a70 100644 --- a/interfaces/kits/napi/osaccount/include/napi_os_account.h +++ b/interfaces/kits/napi/osaccount/include/napi_os_account.h @@ -128,6 +128,10 @@ struct GetOAPhotoAsyncContext : public CommonAsyncContext { std::string photo; }; +struct GetOsAccountNameContext : public CommonAsyncContext { + std::string name; +}; + struct CurrentOAAsyncContext : public CommonAsyncContext { OsAccountInfo osAccountInfos; }; @@ -274,6 +278,8 @@ napi_value QueryActivatedOsAccountIdsInner(napi_env env, napi_callback_info cbIn napi_value GetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo); +napi_value GetOsAccountName(napi_env env, napi_callback_info cbInfo); + napi_value GetCurrentOsAccount(napi_env env, napi_callback_info cbInfo); napi_value QueryCurrentOsAccount(napi_env env, napi_callback_info cbInfo); diff --git a/interfaces/kits/napi/osaccount/include/napi_os_account_common.h b/interfaces/kits/napi/osaccount/include/napi_os_account_common.h index 8e22a09e3..c75eafdcb 100644 --- a/interfaces/kits/napi/osaccount/include/napi_os_account_common.h +++ b/interfaces/kits/napi/osaccount/include/napi_os_account_common.h @@ -149,6 +149,10 @@ void QueryOAInfoForResult(napi_env env, const std::vector &info, bool ParseParaGetPhoto(napi_env env, napi_callback_info cbInfo, GetOAPhotoAsyncContext *asyncContext); +void GetOsAccountNameExecuteCB(napi_env env, void *data); + +void GetOsAccountNameCallbackCompletedCB(napi_env env, napi_status status, void *data); + void GetOAPhotoExecuteCB(napi_env env, void *data); void GetOAPhotoCallbackCompletedCB(napi_env env, napi_status status, void *data); diff --git a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp index 1e0551b6e..da16dfdcb 100644 --- a/interfaces/kits/napi/osaccount/src/napi_os_account.cpp +++ b/interfaces/kits/napi/osaccount/src/napi_os_account.cpp @@ -60,6 +60,7 @@ static napi_property_descriptor g_osAccountProperties[] = { DECLARE_NAPI_FUNCTION("getActivatedOsAccountLocalIds", GetActivatedOsAccountIds), DECLARE_NAPI_FUNCTION("getForegroundOsAccountLocalId", GetForegroundOsAccountLocalId), DECLARE_NAPI_FUNCTION("getOsAccountProfilePhoto", GetOsAccountProfilePhoto), + DECLARE_NAPI_FUNCTION("getOsAccountName", GetOsAccountName), DECLARE_NAPI_FUNCTION("queryCurrentOsAccount", QueryCurrentOsAccount), DECLARE_NAPI_FUNCTION("getCurrentOsAccount", GetCurrentOsAccount), DECLARE_NAPI_FUNCTION("getOsAccountLocalIdFromUid", GetOsAccountLocalIdFromUid), @@ -718,6 +719,25 @@ napi_value GetForegroundOsAccountLocalId(napi_env env, napi_callback_info cbInfo return result; } +napi_value GetOsAccountName(napi_env env, napi_callback_info cbInfo) +{ + auto context = std::make_unique(); + context->env = env; + + napi_value result = nullptr; + NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result)); + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "GetOsAccountName", NAPI_AUTO_LENGTH, &resource)); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetOsAccountNameExecuteCB, + GetOsAccountNameCallbackCompletedCB, + reinterpret_cast(context.get()), &context->work)); + + NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_default)); + context.release(); + return result; +} + napi_value GetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo) { auto getPhoto = std::make_unique(); diff --git a/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp b/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp index 73ee54246..05bc60bd0 100644 --- a/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp +++ b/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp @@ -986,6 +986,30 @@ bool ParseParaGetPhoto(napi_env env, napi_callback_info cbInfo, GetOAPhotoAsyncC return ParseCallbackAndId(env, cbInfo, asyncContext->callbackRef, asyncContext->id, asyncContext->throwErr); } +void GetOsAccountNameExecuteCB(napi_env env, void *data) +{ + ACCOUNT_LOGD("Running napi_create_async_work"); + GetOsAccountNameContext *asyncContext = reinterpret_cast(data); + asyncContext->errCode = OsAccountManager::GetOsAccountName(asyncContext->name); +} + +void GetOsAccountNameCallbackCompletedCB(napi_env env, napi_status status, void *data) +{ + ACCOUNT_LOGD("Complete napi_create_async_work"); + GetOsAccountNameContext *asyncContext = reinterpret_cast(data); + napi_value errJs = nullptr; + napi_value dataJs = nullptr; + if (asyncContext->errCode == napi_ok) { + napi_get_null(env, &errJs); + napi_create_string_utf8(env, asyncContext->name.c_str(), NAPI_AUTO_LENGTH, &dataJs); + } else { + errJs = GenerateBusinessError(env, asyncContext->errCode); + napi_get_null(env, &dataJs); + } + ProcessCallbackOrPromise(env, asyncContext, errJs, dataJs); + delete asyncContext; +} + void GetOAPhotoExecuteCB(napi_env env, void *data) { ACCOUNT_LOGD("napi_create_async_work running"); diff --git a/services/accountmgr/include/osaccount/iinner_os_account.h b/services/accountmgr/include/osaccount/iinner_os_account.h index 391e5893d..3ead96f01 100644 --- a/services/accountmgr/include/osaccount/iinner_os_account.h +++ b/services/accountmgr/include/osaccount/iinner_os_account.h @@ -48,6 +48,7 @@ public: virtual ErrCode QueryAllCreatedOsAccounts(std::vector &osAccountInfos) = 0; virtual ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) = 0; virtual ErrCode GetOsAccountShortName(const int id, std::string &shortName) = 0; + virtual ErrCode GetOsAccountName(const int id, std::string &name) = 0; virtual ErrCode GetOsAccountType(const int id, OsAccountType &type) = 0; virtual ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) = 0; virtual ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) = 0; diff --git a/services/accountmgr/include/osaccount/iinner_os_account_manager.h b/services/accountmgr/include/osaccount/iinner_os_account_manager.h index 6eec4badf..0df13debf 100644 --- a/services/accountmgr/include/osaccount/iinner_os_account_manager.h +++ b/services/accountmgr/include/osaccount/iinner_os_account_manager.h @@ -52,6 +52,7 @@ public: ErrCode QueryAllCreatedOsAccounts(std::vector &osAccountInfos) override; ErrCode QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) override; ErrCode GetOsAccountShortName(const int id, std::string &shortName) override; + ErrCode GetOsAccountName(const int id, std::string &name) override; ErrCode GetOsAccountType(const int id, OsAccountType &type) override; ErrCode GetOsAccountProfilePhoto(const int id, std::string &photo) override; ErrCode IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) override; diff --git a/services/accountmgr/include/osaccount/os_account_manager_service.h b/services/accountmgr/include/osaccount/os_account_manager_service.h index dd4942e73..9f50b6928 100644 --- a/services/accountmgr/include/osaccount/os_account_manager_service.h +++ b/services/accountmgr/include/osaccount/os_account_manager_service.h @@ -121,6 +121,7 @@ public: ErrCode SetDefaultActivatedOsAccount(const int32_t id) override; ErrCode GetDefaultActivatedOsAccount(int32_t &id) override; ErrCode GetOsAccountShortName(std::string &shortName) override; + ErrCode GetOsAccountName(std::string &name) override; ErrCode GetOsAccountShortNameById(const int32_t id, std::string &shortName) override; ErrCode IsOsAccountForeground(const int32_t localId, const uint64_t displayId, bool &isForeground) override; diff --git a/services/accountmgr/include/osaccount/os_account_stub.h b/services/accountmgr/include/osaccount/os_account_stub.h index 9d9a9587a..8800dcf98 100644 --- a/services/accountmgr/include/osaccount/os_account_stub.h +++ b/services/accountmgr/include/osaccount/os_account_stub.h @@ -91,6 +91,7 @@ public: ErrCode ProcSetDefaultActivatedOsAccount(MessageParcel &data, MessageParcel &reply); ErrCode ProcGetDefaultActivatedOsAccount(MessageParcel &data, MessageParcel &reply); ErrCode ProcGetOsAccountShortName(MessageParcel &data, MessageParcel &reply); + ErrCode ProcGetOsAccountName(MessageParcel &data, MessageParcel &reply); ErrCode ProcGetOsAccountShortNameById(MessageParcel &data, MessageParcel &reply); ErrCode ProcIsOsAccountForeground(MessageParcel &data, MessageParcel &reply); diff --git a/services/accountmgr/src/osaccount/inner_os_account_manager.cpp b/services/accountmgr/src/osaccount/inner_os_account_manager.cpp index 8d2360851..4f74fdc3c 100644 --- a/services/accountmgr/src/osaccount/inner_os_account_manager.cpp +++ b/services/accountmgr/src/osaccount/inner_os_account_manager.cpp @@ -1198,6 +1198,18 @@ ErrCode IInnerOsAccountManager::GetOsAccountShortName(const int id, std::string return ERR_OK; } +ErrCode IInnerOsAccountManager::GetOsAccountName(const int id, std::string &name) +{ + OsAccountInfo osAccountInfo; + ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); + if (errCode != ERR_OK) { + ACCOUNT_LOGE("Get osaccount info fail, code=%{public}d.", errCode); + return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; + } + name = osAccountInfo.GetLocalName(); + return ERR_OK; +} + ErrCode IInnerOsAccountManager::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) { ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); diff --git a/services/accountmgr/src/osaccount/os_account_manager_service.cpp b/services/accountmgr/src/osaccount/os_account_manager_service.cpp index 26388f5e9..a91ddd56d 100644 --- a/services/accountmgr/src/osaccount/os_account_manager_service.cpp +++ b/services/accountmgr/src/osaccount/os_account_manager_service.cpp @@ -1024,6 +1024,18 @@ ErrCode OsAccountManagerService::GetOsAccountShortName(std::string &shortName) return GetOsAccountShortNameCommon(id, shortName); } +ErrCode OsAccountManagerService::GetOsAccountName(std::string &name) +{ + int32_t id = IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR; + ErrCode errCode = innerManager_.GetOsAccountName(id, name); + if (errCode != ERR_OK) { + ACCOUNT_LOGE("Failed get account name, errCode=%{public}d, uid=%{public}d", errCode, + IPCSkeleton::GetCallingUid()); + return errCode; + } + return ERR_OK; +} + ErrCode OsAccountManagerService::GetOsAccountShortNameById(const int32_t id, std::string &shortName) { if (!PermissionCheck(INTERACT_ACROSS_LOCAL_ACCOUNTS, "")) { diff --git a/services/accountmgr/src/osaccount/os_account_stub.cpp b/services/accountmgr/src/osaccount/os_account_stub.cpp index 13c1d1718..b6ac11e76 100644 --- a/services/accountmgr/src/osaccount/os_account_stub.cpp +++ b/services/accountmgr/src/osaccount/os_account_stub.cpp @@ -371,6 +371,13 @@ const std::map messageProcMap = { .isSyetemApi = true, } }, + { + static_cast(OsAccountInterfaceCode::GET_OS_ACCOUNT_NAME), + { + .messageProcFunction = &OsAccountStub::ProcGetOsAccountName, + .isSyetemApi = false, + } + }, { static_cast(OsAccountInterfaceCode::IS_OS_ACCOUNT_FOREGROUND), { @@ -1437,6 +1444,21 @@ ErrCode OsAccountStub::ProcGetOsAccountShortName(MessageParcel &data, MessagePar return ERR_NONE; } +ErrCode OsAccountStub::ProcGetOsAccountName(MessageParcel &data, MessageParcel &reply) +{ + std::string name; + ErrCode result = GetOsAccountName(name); + if (!reply.WriteInt32(result)) { + ACCOUNT_LOGE("Failed to write result, result=%{public}d.", result); + return IPC_STUB_WRITE_PARCEL_ERR; + } + if (!reply.WriteString(name)) { + ACCOUNT_LOGE("Failed to write name"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return ERR_NONE; +} + ErrCode OsAccountStub::ProcGetOsAccountShortNameById(MessageParcel &data, MessageParcel &reply) { int32_t id;