mirror of
https://gitee.com/openharmony/account_os_account
synced 2024-11-26 19:50:37 +00:00
CAPI
Signed-off-by: zhoushimiao <zhoushimiao@huawei.com> Change-Id: I052113da1e74372f67412067e1c4abe3ca22d90d
This commit is contained in:
parent
5e2ffa41e4
commit
90db8cabe8
1
BUILD.gn
1
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",
|
||||
|
@ -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"
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -144,6 +144,7 @@
|
||||
"OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(std::__h::vector<int, std::__h::allocator<int>>&)";
|
||||
"OHOS::AccountSA::OsAccountManager::SetGlobalOsAccountConstraints(std::__h::vector<std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>, std::__h::allocator<std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>>> const&, bool, int, bool)";
|
||||
"OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&)";
|
||||
"OHOS::AccountSA::OsAccountManager::GetOsAccountName(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&)";
|
||||
"OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(int, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>>&)";
|
||||
OHOS::AccountSA::OsAccountManager::IsOsAccountForeground*;
|
||||
OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId*;
|
||||
|
@ -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);
|
||||
|
@ -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<std::string> 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;
|
||||
|
@ -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
|
||||
|
18
interfaces/kits/capi/BUILD.gn
Normal file
18
interfaces/kits/capi/BUILD.gn
Normal file
@ -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" ]
|
||||
}
|
58
interfaces/kits/capi/osaccount/BUILD.gn
Normal file
58
interfaces/kits/capi/osaccount/BUILD.gn
Normal file
@ -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"
|
||||
}
|
60
interfaces/kits/capi/osaccount/include/os_account.h
Normal file
60
interfaces/kits/capi/osaccount/include/os_account.h
Normal file
@ -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 <stddef.h>
|
||||
#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;<br>
|
||||
* OS_ACCOUNT_ERR_INTERNAL_ERROR - Indicates the internal error.<br>
|
||||
* OS_ACCOUNT_ERR_INVALID_PARAMETER - Indicates the <i>buf</i> is NULL pointer or the size of the name,
|
||||
* including the terminating character ('\0'), is larger then <i>buffer_size</i>;
|
||||
* @syscap SystemCapability.Account.OsAccount
|
||||
* @since 12
|
||||
*/
|
||||
OsAccount_ErrCode OH_OsAccount_GetName(char *buffer, size_t buffer_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif // OS_ACCOUNT_H
|
65
interfaces/kits/capi/osaccount/include/os_account_common.h
Normal file
65
interfaces/kits/capi/osaccount/include/os_account_common.h
Normal file
@ -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
|
6
interfaces/kits/capi/osaccount/libos_account.ndk.json
Normal file
6
interfaces/kits/capi/osaccount/libos_account.ndk.json
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
{
|
||||
"first_introduced": "12",
|
||||
"name":"OH_OsAccount_GetName"
|
||||
}
|
||||
]
|
49
interfaces/kits/capi/osaccount/src/os_account.cpp
Normal file
49
interfaces/kits/capi/osaccount/src/os_account.cpp
Normal file
@ -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 <string>
|
||||
#include <securec.h>
|
||||
#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;
|
||||
}
|
22
interfaces/kits/capi/test/unittest/BUILD.gn
Normal file
22
interfaces/kits/capi/test/unittest/BUILD.gn
Normal file
@ -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" ]
|
||||
}
|
51
interfaces/kits/capi/test/unittest/osaccount/BUILD.gn
Normal file
51
interfaces/kits/capi/test/unittest/osaccount/BUILD.gn
Normal file
@ -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" ]
|
||||
}
|
@ -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 <string>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#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
|
@ -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);
|
||||
|
@ -149,6 +149,10 @@ void QueryOAInfoForResult(napi_env env, const std::vector<OsAccountInfo> &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);
|
||||
|
@ -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<GetOsAccountNameContext>();
|
||||
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<void *>(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<GetOAPhotoAsyncContext>();
|
||||
|
@ -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<GetOsAccountNameContext *>(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<GetOsAccountNameContext *>(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");
|
||||
|
@ -48,6 +48,7 @@ public:
|
||||
virtual ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &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;
|
||||
|
@ -52,6 +52,7 @@ public:
|
||||
ErrCode QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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, "")) {
|
||||
|
@ -371,6 +371,13 @@ const std::map<uint32_t, OsAccountStub::OsAccountMessageProc> messageProcMap = {
|
||||
.isSyetemApi = true,
|
||||
}
|
||||
},
|
||||
{
|
||||
static_cast<uint32_t>(OsAccountInterfaceCode::GET_OS_ACCOUNT_NAME),
|
||||
{
|
||||
.messageProcFunction = &OsAccountStub::ProcGetOsAccountName,
|
||||
.isSyetemApi = false,
|
||||
}
|
||||
},
|
||||
{
|
||||
static_cast<uint32_t>(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;
|
||||
|
Loading…
Reference in New Issue
Block a user