From 40e296f946c36126953140c1dea3728e0503e60d Mon Sep 17 00:00:00 2001 From: fuzikun Date: Sat, 15 Jul 2023 19:08:41 +0800 Subject: [PATCH] add authdevice function test Signed-off-by: fuzikun --- BUILD.gn | 1 + .../function_test/auth_device/BUILD.gn | 79 +++ .../auth_device/auth_device_test.cpp | 661 ++++++++++++++++++ test/unittest/tdd_framework/tdd_framework.gni | 17 + .../src/os_account_adapter_mock.c | 21 + .../inc/protocol_controller.h | 32 + .../src/protocol_controller.c | 87 +++ 7 files changed, 898 insertions(+) create mode 100644 test/unittest/tdd_framework/function_test/auth_device/BUILD.gn create mode 100644 test/unittest/tdd_framework/function_test/auth_device/auth_device_test.cpp create mode 100644 test/unittest/tdd_framework/tdd_helper/os_account_adapter_mock/src/os_account_adapter_mock.c create mode 100644 test/unittest/tdd_framework/tdd_helper/protocol_controller/inc/protocol_controller.h create mode 100644 test/unittest/tdd_framework/tdd_helper/protocol_controller/src/protocol_controller.c diff --git a/BUILD.gn b/BUILD.gn index 5b1fdd6b..0ad1d15a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -64,6 +64,7 @@ group("deviceauth_test_build") { "test/unittest/deviceauth:device_auth_ipc_test", "test/unittest/deviceauth:deviceauth_llt", "test/unittest/deviceauth:deviceauth_unit_test", + "test/unittest/tdd_framework/function_test/auth_device:auth_device_test", "test/unittest/tdd_framework/unit_test/services/creds_manager:creds_manager_test", "test/unittest/tdd_framework/unit_test/services/session_manager/session/v2/auth_sub_session:auth_sub_session_test", "test/unittest/tdd_framework/unit_test/services/session_manager/session/v2/auth_sub_session/protocol_lib:ec_speke_protocol_test", diff --git a/test/unittest/tdd_framework/function_test/auth_device/BUILD.gn b/test/unittest/tdd_framework/function_test/auth_device/BUILD.gn new file mode 100644 index 00000000..264fdabe --- /dev/null +++ b/test/unittest/tdd_framework/function_test/auth_device/BUILD.gn @@ -0,0 +1,79 @@ +# 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. + +import("../../tdd_framework.gni") + +module_output_path = "device_auth/deviceauth_test" + +ohos_unittest("auth_device_test") { + module_out_path = module_output_path + + include_dirs = device_auth_lib_inc + + include_dirs += [ + "${tdd_framework_path}/tdd_helper/dev_info_mock/inc", + "${tdd_framework_path}/tdd_helper/exception_controller/inc", + "${tdd_framework_path}/tdd_helper/memory_mock/inc", + "${tdd_framework_path}/tdd_helper/memory_monitor/inc", + "${tdd_framework_path}/tdd_helper/protocol_controller/inc", + ] + + sources = device_auth_lib_src + sources -= [ + "${authenticators_path}/src/account_unrelated/iso_task/iso_task_main.c", + "${authenticators_path}/src/account_unrelated/pake_task/pake_v1_task/pake_v1_task_main.c", + ] + sources += [ + "${tdd_framework_path}/tdd_helper/os_account_adapter_mock/src/os_account_adapter_mock.c", + "${tdd_framework_path}/tdd_helper/protocol_controller/src/protocol_controller.c", + ] + sources += [ "auth_device_test.cpp" ] + + defines = [ + "P2P_PAKE_DL_PRIME_LEN_384", + "P2P_PAKE_EC_TYPE", + "ENABLE_EC_SPEKE", + "ENABLE_ISO", + "ENABLE_AUTH_CODE_IMPORT", + "ENABLE_PUB_KEY_EXCHANGE", + "ENABLE_SAVE_TRUSTED_INFO", + "ENABLE_ACCOUNT_AUTH_ISO", + "ENABLE_ACCOUNT_AUTH_EC_SPEKE", + "ENABLE_P2P_BIND_ISO", + "ENABLE_P2P_BIND_EC_SPEKE", + "ENABLE_P2P_AUTH_ISO", + "ENABLE_P2P_AUTH_EC_SPEKE", + "DEV_AUTH_MEMORY_DEBUG", + "DEV_AUTH_FUNC_TEST", + ] + + cflags = [ "-DHILOG_ENABLE" ] + + cflags = [ "--coverage" ] + ldflags = [ "--coverage" ] + + deps = [ + "${tdd_framework_path}/tdd_helper:device_auth_test_hal", + "//third_party/cJSON:cjson", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//third_party/mbedtls:mbedtls_shared", + "//third_party/openssl:libcrypto_static", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "huks:libhukssdk", + ] +} diff --git a/test/unittest/tdd_framework/function_test/auth_device/auth_device_test.cpp b/test/unittest/tdd_framework/function_test/auth_device/auth_device_test.cpp new file mode 100644 index 00000000..230df690 --- /dev/null +++ b/test/unittest/tdd_framework/function_test/auth_device/auth_device_test.cpp @@ -0,0 +1,661 @@ +/* + * 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 +#include +#include +#include "account_module_defines.h" +#include "alg_loader.h" +#include "common_defs.h" +#include "device_auth.h" +#include "device_auth_defines.h" +#include "exception_controller.h" +#include "dev_info_mock.h" +#include "json_utils.h" +#include "memory_mock.h" +#include "memory_monitor.h" +#include "uint8buff_utils.h" +#include "protocol_controller.h" +#include "securec.h" + +using namespace std; +using namespace testing::ext; + +namespace { +#define TEST_REQ_ID 123 +#define TEST_REQ_ID2 321 +#define TEST_APP_ID "TestAppId" +#define TEST_APP_ID2 "TestAppId2" +#define TEST_GROUP_NAME "TestGroup" +#define TEST_AUTH_ID "TestAuthId" +#define TEST_AUTH_ID2 "TestAuthId2" +#define TEST_AUTH_ID3 "TestAuthId3" +#define TEST_UDID "TestUdid" +#define TEST_UDID2 "TestUdid2" +#define TEST_UDID_CLIENT "5420459D93FE773F9945FD64277FBA2CAB8FB996DDC1D0B97676FBB1242B3930" +#define TEST_UDID_SERVER "52E2706717D5C39D736E134CC1E3BE1BAA2AA52DB7C76A37C749558BD2E6492C" +#define TEST_USER_ID_AUTH "4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4" +#define TEST_USER_ID "4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4" +#define TEST_USER_ID2 "9F207900038F91FEF95DD042FE2874E44269DC28B639681698809A67EDAD08E3" +#define TEST_GROUP_ID "E2EE6F830B176B2C96A9F99BFAE2A61F5D1490B9F4A090E9D8C2874C230C7C21" +#define TEST_GROUP_ID2 "F2AA208B1E010542B20A34B03B4B6289EA7C7F6DFE97DA2E370348B826682D3D" +#define TEST_GROUP_ID3 "1234ABCD" +#define TEST_GROUP_ID4 "6B7B805962B8EB8275D73128BFDAA7ECD755A2EC304E36543941874A277FA75F" +#define TEST_AUTH_CODE "37364761534f454d33567a73424e794f33573330507069434b31676f7254706b" +#define TEST_AUTH_CODE2 "2f7562744654535564586e665467546b322b4b506b65626373466f48766a4335" +#define TEST_AUTH_CODE3 "1234567812345678123456781234567812345678123456781234567812345678" +#define TEST_AUTH_TOKEN3 "10F9F0576E61730193D2052B7F771887124A68F1607EFCF7796C1491F834CD92" +#define TEST_QUERY_PARAMS "bac" +#define TEST_PIN_CODE "123456" +#define TEST_DEV_AUTH_TEMP_KEY_PAIR_LEN 32 +#define TEST_DEV_AUTH_SLEEP_TIME 100000 +static const int32_t TEST_AUTH_OS_ACCOUNT_ID = 100; +static const int TEST_DEV_AUTH_BUFFER_SIZE = 128; +static const char *g_createParams = "{\"groupName\":\"TestGroup\",\"deviceId\":\"TestAuthId\",\"groupType\":256,\"group" + "Visibility\":-1,\"userType\":0,\"expireTime\":-1}"; +static const char *g_addParams = + "{\"groupId\":\"E2EE6F830B176B2C96A9F99BFAE2A61F5D1490B9F4A090E9D8C2874C230C7C21\"," + "\"groupType\":256,\"pinCode\":\"123456\"}"; +static const char *g_addParams4 = + "{\"groupType\":1,\"groupId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"deviceList\":[{\"deviceId\":\"52E2706717D5C39D736E134CC1E3BE1BAA2AA52DB7C76A37C749558BD2E6492C\"," + "\"udid\":\"52E2706717D5C39D736E134CC1E3BE1BAA2AA52DB7C76A37C749558BD2E6492C\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"credential\":{\"credentialType\":1," + "\"authCode\":\"10F9F0576E61730193D2052B7F771887124A68F1607EFCF7796C1491F834CD92\"}}," + "{\"deviceId\":\"TestAuthId3\",\"udid\":\"TestUdid2\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"credential\":{\"credentialType\":1," + "\"authCode\":\"2f7562744654535564586e665467546b322b4b506b65626373466f48766a4335\"}}]}"; +static const char *g_addParams5 = + "{\"groupType\":1,\"groupId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"deviceList\":[{\"deviceId\":\"5420459D93FE773F9945FD64277FBA2CAB8FB996DDC1D0B97676FBB1242B3930\"," + "\"udid\":\"5420459D93FE773F9945FD64277FBA2CAB8FB996DDC1D0B97676FBB1242B3930\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"credential\":{\"credentialType\":1," + "\"authCode\":\"10F9F0576E61730193D2052B7F771887124A68F1607EFCF7796C1491F834CD92\"}}," + "{\"deviceId\":\"TestAuthId3\",\"udid\":\"TestUdid2\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"," + "\"credential\":{\"credentialType\":1," + "\"authCode\":\"2f7562744654535564586e665467546b322b4b506b65626373466f48766a4335\"}}]}"; +static const char *g_authParams = "{\"peerConnDeviceId\":\"52E2706717D5C39D736E134CC1E3BE1BAA2AA52DB7C76A37C" + "749558BD2E6492C\",\"servicePkgName\":\"TestAppId\",\"isClient\":true}"; +static const char *g_getRegisterInfoParams = "{\"version\":\"1.0.0\",\"deviceId\":\"client\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"}"; +static const char *g_getRegisterInfoParams2 = "{\"version\":\"1.0.0\",\"deviceId\":\"server\"," + "\"userId\":\"4269DC28B639681698809A67EDAD08E39F207900038F91FEF95DD042FE2874E4\"}"; + +enum AsyncStatus { + ASYNC_STATUS_WAITING = 0, + ASYNC_STATUS_TRANSMIT = 1, + ASYNC_STATUS_FINISH = 2, + ASYNC_STATUS_ERROR = 3 +}; + +static AsyncStatus volatile g_asyncStatus; +static uint32_t g_transmitDataMaxLen = 2048; +static uint8_t g_transmitData[2048] = { 0 }; +static uint32_t g_transmitDataLen = 0; + +static bool OnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) +{ + // printf("%s: -----start-----\n", __func__); + // printf("%s: dataLen: %u\n", __func__, dataLen); + // printf("%s: data: %s\n", __func__, (const char *)data); + // printf("%s: ------end------\n", __func__); + if (memcpy_s(g_transmitData, g_transmitDataMaxLen, data, dataLen) != EOK) { + return false; + } + g_transmitDataLen = dataLen; + g_asyncStatus = ASYNC_STATUS_TRANSMIT; + return true; +} + +static void OnSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) +{ + // printf("%s: -----start-----\n", __func__); + // printf("%s: sessionKeyLen: %u\n", __func__, sessionKeyLen); + // printf("%s: sessionKey:\n", __func__); + // for (uint32_t i = 0; i < sessionKeyLen; i++) { + // printf("%02x ", sessionKey[i]); + // } + // printf("\n"); + // printf("%s: ------end------\n", __func__); + (void)requestId; + (void)sessionKey; + (void)sessionKeyLen; + return; +} + +static void OnFinish(int64_t requestId, int operationCode, const char *authReturn) +{ + // printf("%s: -----start-----\n", __func__); + // printf("%s: operationCode: %d\n", __func__, operationCode); + // printf("%s: returnData: %s\n", __func__, authReturn); + // printf("%s: ------end------\n", __func__); + g_asyncStatus = ASYNC_STATUS_FINISH; +} + +static void OnError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn) +{ + // printf("%s: -----start-----\n", __func__); + // printf("%s: operationCode: %d\n", __func__, operationCode); + // printf("%s: returnData: %s\n", __func__, errorReturn); + // printf("%s: ------end------\n", __func__); + g_asyncStatus = ASYNC_STATUS_ERROR; +} + +static char *OnBindRequest(int64_t requestId, int operationCode, const char* reqParam) +{ + // printf("%s: -----start-----\n", __func__); + CJson *json = CreateJson(); + AddIntToJson(json, FIELD_CONFIRMATION, REQUEST_ACCEPTED); + AddIntToJson(json, FIELD_OS_ACCOUNT_ID, TEST_AUTH_OS_ACCOUNT_ID); + AddStringToJson(json, FIELD_PIN_CODE, TEST_PIN_CODE); + AddStringToJson(json, FIELD_DEVICE_ID, TEST_AUTH_ID2); + char *returnDataStr = PackJsonToString(json); + // printf("%s: returnData: %s\n", __func__, returnDataStr); + // printf("%s: ------end------\n", __func__); + FreeJson(json); + return returnDataStr; +} + +static char *OnAuthRequest(int64_t requestId, int operationCode, const char* reqParam) +{ + // printf("%s: -----start-----\n", __func__); + CJson *json = CreateJson(); + AddIntToJson(json, FIELD_CONFIRMATION, REQUEST_ACCEPTED); + AddIntToJson(json, FIELD_OS_ACCOUNT_ID, TEST_AUTH_OS_ACCOUNT_ID); + AddStringToJson(json, FIELD_PEER_CONN_DEVICE_ID, TEST_UDID_CLIENT); + AddStringToJson(json, FIELD_SERVICE_PKG_NAME, TEST_APP_ID); + char *returnDataStr = PackJsonToString(json); + // printf("%s: returnData: %s\n", __func__, returnDataStr); + // printf("%s: ------end------\n", __func__); + FreeJson(json); + return returnDataStr; +} + +static DeviceAuthCallback g_gmCallback = { + .onTransmit = OnTransmit, + .onSessionKeyReturned = OnSessionKeyReturned, + .onFinish = OnFinish, + .onError = OnError, + .onRequest = OnBindRequest +}; + +static DeviceAuthCallback g_gaCallback = { + .onTransmit = OnTransmit, + .onSessionKeyReturned = OnSessionKeyReturned, + .onFinish = OnFinish, + .onError = OnError, + .onRequest = OnAuthRequest +}; + +static void RemoveDir(const char *path) +{ + char strBuf[TEST_DEV_AUTH_BUFFER_SIZE] = {0}; + if (path == nullptr) { + return; + } + if (sprintf_s(strBuf, sizeof(strBuf) - 1, "rm -rf %s", path) < 0) { + return; + } + system(strBuf); + return; +} + +static void DeleteDatabase() +{ + const char *groupPath = "/data/service/el1/public/deviceauthMock"; + RemoveDir(groupPath); + return; +} + +static bool GenerateTempKeyPair(Uint8Buff *keyAlias) +{ + int ret = GetLoaderInstance()->checkKeyExist(keyAlias); + if (ret != HC_SUCCESS) { + printf("Key pair not exist, start to generate\n"); + int32_t authId = 0; + Uint8Buff authIdBuff = { reinterpret_cast(&authId), sizeof(int32_t)}; + ExtraInfo extInfo = {authIdBuff, -1, -1}; + ret = GetLoaderInstance()->generateKeyPairWithStorage(keyAlias, TEST_DEV_AUTH_TEMP_KEY_PAIR_LEN, P256, + KEY_PURPOSE_SIGN_VERIFY, &extInfo); + } else { + printf("Server key pair already exists\n"); + } + + if (ret != HC_SUCCESS) { + printf("Generate key pair failed\n"); + return false; + } else { + printf("Generate key pair for server success\n"); + } + return true; +} + +static CJson *GetAsyCredentialJson(string registerInfo) +{ + uint8_t keyAliasValue[] = "TestServerKeyPair"; + int32_t keyAliasLen = 18; + Uint8Buff keyAlias = { + .val = keyAliasValue, + .length = keyAliasLen + }; + if (!GenerateTempKeyPair(&keyAlias)) { + return nullptr; + } + uint8_t *serverPkVal = reinterpret_cast(HcMalloc(SERVER_PK_SIZE, 0)); + Uint8Buff serverPk = { + .val = serverPkVal, + .length = SERVER_PK_SIZE + }; + + int32_t ret = GetLoaderInstance()->exportPublicKey(&keyAlias, &serverPk); + if (ret != HC_SUCCESS) { + printf("export PublicKey failed\n"); + HcFree(serverPkVal); + return nullptr; + } + + Uint8Buff messageBuff = { + .val = reinterpret_cast(const_cast(registerInfo.c_str())), + .length = registerInfo.length() + 1 + }; + uint8_t *signatureValue = reinterpret_cast(HcMalloc(SIGNATURE_SIZE, 0)); + Uint8Buff signature = { + .val = signatureValue, + .length = SIGNATURE_SIZE + }; + ret = GetLoaderInstance()->sign(&keyAlias, &messageBuff, P256, &signature, true); + if (ret != HC_SUCCESS) { + printf("Sign pkInfo failed.\n"); + HcFree(serverPkVal); + HcFree(signatureValue); + return nullptr; + } + + CJson *pkInfoJson = CreateJsonFromString(registerInfo.c_str()); + CJson *credentialJson = CreateJson(); + (void)AddIntToJson(credentialJson, FIELD_CREDENTIAL_TYPE, ASYMMETRIC_CRED); + (void)AddByteToJson(credentialJson, FIELD_SERVER_PK, serverPkVal, serverPk.length); + (void)AddByteToJson(credentialJson, FIELD_PK_INFO_SIGNATURE, signatureValue, signature.length); + (void)AddObjToJson(credentialJson, FIELD_PK_INFO, pkInfoJson); + FreeJson(pkInfoJson); + HcFree(serverPkVal); + HcFree(signatureValue); + return credentialJson; +} + +static void CreateDemoGroup(int32_t osAccountId, int64_t reqId, const char *appId, const char *createParams) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + const DeviceGroupManager *gm = GetGmInstance(); + int32_t ret = gm->createGroup(osAccountId, reqId, appId, createParams); + if (ret != HC_SUCCESS) { + g_asyncStatus = ASYNC_STATUS_ERROR; + return; + } + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + usleep(TEST_DEV_AUTH_SLEEP_TIME); +} + +static void CreateDemoIdenticalAccountGroup(int32_t osAccountId, int64_t reqId, const char *appId, + const char *registerInfoParams) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + char *returnData = nullptr; + int32_t ret = gm->getRegisterInfo(registerInfoParams, &returnData); + ASSERT_EQ(ret, HC_SUCCESS); + ASSERT_NE(returnData, nullptr); + string registerInfo(returnData); + + CJson *credJson = GetAsyCredentialJson(registerInfo); + ASSERT_NE(credJson, nullptr); + CJson *json = CreateJson(); + AddIntToJson(json, FIELD_GROUP_TYPE, IDENTICAL_ACCOUNT_GROUP); + AddStringToJson(json, FIELD_USER_ID, TEST_USER_ID); + AddObjToJson(json, FIELD_CREDENTIAL, credJson); + char *jsonStr = PackJsonToString(json); + FreeJson(credJson); + FreeJson(json); + gm->destroyInfo(&returnData); + ASSERT_NE(jsonStr, nullptr); + ret = gm->createGroup(osAccountId, reqId, appId, jsonStr); + FreeJsonString(jsonStr); + ASSERT_EQ(ret, HC_SUCCESS); + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + usleep(TEST_DEV_AUTH_SLEEP_TIME); +} + +static void CreateDemoSymClientIdenticalAccountGroup(void) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + + CJson *credJson = CreateJson(); + (void)AddIntToJson(credJson, FIELD_CREDENTIAL_TYPE, SYMMETRIC_CRED); + (void)AddStringToJson(credJson, FIELD_AUTH_CODE, TEST_AUTH_CODE3); + CJson *json = CreateJson(); + AddIntToJson(json, FIELD_GROUP_TYPE, IDENTICAL_ACCOUNT_GROUP); + AddStringToJson(json, FIELD_USER_ID, TEST_USER_ID_AUTH); + AddObjToJson(json, FIELD_CREDENTIAL, credJson); + char *jsonStr = PackJsonToString(json); + FreeJson(credJson); + FreeJson(json); + if (jsonStr == nullptr) { + return; + } + int32_t ret = gm->createGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, jsonStr); + FreeJsonString(jsonStr); + ASSERT_EQ(ret, HC_SUCCESS); + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); +} + +static void CreateDemoSymServerIdenticalAccountGroup(void) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + + CJson *credJson = CreateJson(); + (void)AddIntToJson(credJson, FIELD_CREDENTIAL_TYPE, SYMMETRIC_CRED); + (void)AddStringToJson(credJson, FIELD_AUTH_CODE, TEST_AUTH_CODE3); + CJson *json = CreateJson(); + AddIntToJson(json, FIELD_GROUP_TYPE, IDENTICAL_ACCOUNT_GROUP); + AddStringToJson(json, FIELD_USER_ID, TEST_USER_ID_AUTH); + AddObjToJson(json, FIELD_CREDENTIAL, credJson); + char *jsonStr = PackJsonToString(json); + FreeJson(credJson); + FreeJson(json); + if (jsonStr == nullptr) { + return; + } + + int32_t ret = gm->createGroup(TEST_AUTH_OS_ACCOUNT_ID, TEST_REQ_ID, TEST_APP_ID, jsonStr); + FreeJsonString(jsonStr); + ASSERT_EQ(ret, HC_SUCCESS); + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); +} + +static void AddDemoMember(void) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + bool isClient = true; + SetDeviceStatus(isClient); + const DeviceGroupManager *gm = GetGmInstance(); + int32_t ret = gm->addMemberToGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, g_addParams); + if (ret != HC_SUCCESS) { + g_asyncStatus = ASYNC_STATUS_ERROR; + return; + } + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + while (g_asyncStatus == ASYNC_STATUS_TRANSMIT) { + isClient = !isClient; + SetDeviceStatus(isClient); + g_asyncStatus = ASYNC_STATUS_WAITING; + if (isClient) { + ret = gm->processData(TEST_REQ_ID, g_transmitData, g_transmitDataLen); + } else { + ret = gm->processData(TEST_REQ_ID2, g_transmitData, g_transmitDataLen); + } + (void)memset_s(g_transmitData, g_transmitDataMaxLen, 0, g_transmitDataMaxLen); + g_transmitDataLen = 0; + if (ret != HC_SUCCESS) { + g_asyncStatus = ASYNC_STATUS_ERROR; + return; + } + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + if (g_asyncStatus == ASYNC_STATUS_ERROR) { + break; + } + if (g_transmitDataLen > 0) { + g_asyncStatus = ASYNC_STATUS_TRANSMIT; + } + } + usleep(TEST_DEV_AUTH_SLEEP_TIME); + SetDeviceStatus(true); +} + +static void AuthDemoMember(void) +{ + g_asyncStatus = ASYNC_STATUS_WAITING; + bool isClient = true; + SetDeviceStatus(isClient); + const GroupAuthManager *ga = GetGaInstance(); + ASSERT_NE(ga, nullptr); + int32_t ret = ga->authDevice(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, g_authParams, &g_gaCallback); + if (ret != HC_SUCCESS) { + g_asyncStatus = ASYNC_STATUS_ERROR; + return; + } + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + while (g_asyncStatus == ASYNC_STATUS_TRANSMIT) { + isClient = !isClient; + SetDeviceStatus(isClient); + g_asyncStatus = ASYNC_STATUS_WAITING; + if (isClient) { + ret = ga->processData(TEST_REQ_ID, g_transmitData, g_transmitDataLen, &g_gaCallback); + } else { + ret = ga->processData(TEST_REQ_ID2, g_transmitData, g_transmitDataLen, &g_gaCallback); + } + (void)memset_s(g_transmitData, g_transmitDataMaxLen, 0, g_transmitDataMaxLen); + g_transmitDataLen = 0; + if (ret != HC_SUCCESS) { + g_asyncStatus = ASYNC_STATUS_ERROR; + return; + } + while (g_asyncStatus == ASYNC_STATUS_WAITING) { + usleep(TEST_DEV_AUTH_SLEEP_TIME); + } + if (g_asyncStatus == ASYNC_STATUS_ERROR) { + break; + } + if (g_transmitDataLen > 0) { + g_asyncStatus = ASYNC_STATUS_TRANSMIT; + } + } + SetDeviceStatus(true); +} + +class AuthDeviceFuncTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void AuthDeviceFuncTest::SetUpTestCase() {} +void AuthDeviceFuncTest::TearDownTestCase() {} + +void AuthDeviceFuncTest::SetUp() +{ + DeleteDatabase(); + InitExceptionController(); + HcInitMallocMonitor(); + cJSON_Hooks hooks = { + .malloc_fn = MockMallocForJson, + .free_fn = MockFree + }; + cJSON_InitHooks(&hooks); +} + +void AuthDeviceFuncTest::TearDown() +{ + bool isMemoryLeak = IsMemoryLeak(); + EXPECT_FALSE(isMemoryLeak); + if (isMemoryLeak) { + ReportMonitor(); + } + cJSON_Hooks hooks = { + .malloc_fn = malloc, + .free_fn = free + }; + cJSON_InitHooks(&hooks); + HcDestroyMallocMonitor(); + DestroyExceptionController(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest001, TestSize.Level0) +{ + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const GroupAuthManager *ga = GetGaInstance(); + ASSERT_NE(ga, nullptr); + ret = ga->authDevice(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, nullptr, &g_gmCallback); + ASSERT_EQ(ret, HC_ERR_INVALID_PARAMS); + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest002, TestSize.Level0) +{ + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const GroupAuthManager *ga = GetGaInstance(); + ASSERT_NE(ga, nullptr); + ret = ga->authDevice(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, g_authParams, nullptr); + ASSERT_NE(ret, HC_SUCCESS); + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest003, TestSize.Level0) +{ + SetIsoSupported(true); + SetPakeV1Supported(false); + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + ret = gm->regCallback(TEST_APP_ID, &g_gmCallback); + ASSERT_EQ(ret, HC_SUCCESS); + CreateDemoGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, g_createParams); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + AddDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + AuthDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest004, TestSize.Level0) +{ + SetIsoSupported(false); + SetPakeV1Supported(true); + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + ret = gm->regCallback(TEST_APP_ID, &g_gmCallback); + ASSERT_EQ(ret, HC_SUCCESS); + CreateDemoGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, g_createParams); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + AddDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + AuthDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest005, TestSize.Level0) +{ + SetIsoSupported(true); + SetPakeV1Supported(true); + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + ret = gm->regCallback(TEST_APP_ID, &g_gmCallback); + ASSERT_EQ(ret, HC_SUCCESS); + + SetDeviceStatus(true); + CreateDemoIdenticalAccountGroup(DEFAULT_OS_ACCOUNT, TEST_REQ_ID, TEST_APP_ID, g_getRegisterInfoParams); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + + SetDeviceStatus(false); + CreateDemoIdenticalAccountGroup(TEST_AUTH_OS_ACCOUNT_ID, TEST_REQ_ID2, TEST_APP_ID, g_getRegisterInfoParams2); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + + SetDeviceStatus(true); + AuthDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest006, TestSize.Level0) +{ + SetIsoSupported(true); + SetPakeV1Supported(false); + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + ret = gm->regCallback(TEST_APP_ID, &g_gmCallback); + ASSERT_EQ(ret, HC_SUCCESS); + CreateDemoSymClientIdenticalAccountGroup(); + ASSERT_EQ(ret, HC_SUCCESS); + SetDeviceStatus(false); + CreateDemoSymServerIdenticalAccountGroup(); + SetDeviceStatus(true); + AuthDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + DestroyDeviceAuthService(); +} + +HWTEST_F(AuthDeviceFuncTest, AuthDeviceFuncTest007, TestSize.Level0) +{ + SetIsoSupported(true); + SetPakeV1Supported(false); + int32_t ret = InitDeviceAuthService(); + ASSERT_EQ(ret, HC_SUCCESS); + const DeviceGroupManager *gm = GetGmInstance(); + ASSERT_NE(gm, nullptr); + ret = gm->regCallback(TEST_APP_ID, &g_gmCallback); + ASSERT_EQ(ret, HC_SUCCESS); + CreateDemoSymClientIdenticalAccountGroup(); + SetDeviceStatus(false); + CreateDemoSymServerIdenticalAccountGroup(); + SetDeviceStatus(true); + ret = gm->addMultiMembersToGroup(DEFAULT_OS_ACCOUNT, TEST_APP_ID, g_addParams4); + ASSERT_EQ(ret, HC_SUCCESS); + SetDeviceStatus(false); + ret = gm->addMultiMembersToGroup(TEST_AUTH_OS_ACCOUNT_ID, TEST_APP_ID, g_addParams5); + ASSERT_EQ(ret, HC_SUCCESS); + SetDeviceStatus(true); + AuthDemoMember(); + ASSERT_EQ(g_asyncStatus, ASYNC_STATUS_FINISH); + DestroyDeviceAuthService(); +} +} diff --git a/test/unittest/tdd_framework/tdd_framework.gni b/test/unittest/tdd_framework/tdd_framework.gni index f2627706..48fd7999 100644 --- a/test/unittest/tdd_framework/tdd_framework.gni +++ b/test/unittest/tdd_framework/tdd_framework.gni @@ -17,3 +17,20 @@ import("//base/security/huks/build/config.gni") import("//build/test.gni") tdd_framework_path = "//base/security/device_auth/test/unittest/tdd_framework" + +third_inc_path = [ + "//third_party/cJSON", + "//commonlibrary/c_utils/base/include", + "//third_party/openssl/include/", + "//third_party/mbedtls/include", + "//third_party/mbedtls/include/mbedtls", +] + +device_auth_lib_inc = inc_path + hals_inc_path + third_inc_path +device_auth_lib_src = dev_frameworks_files + deviceauth_common_files + database_manager_files + + cred_manager_files + session_manager_files + session_v1_files + session_v2_files + iso_protocol_files + + ec_speke_protocol_files + auth_code_import_files + pub_key_exchange_files + save_trusted_info_files + + creds_manager_files + broadcast_manager_files + soft_bus_channel_mock_files + group_auth_files + + group_auth_account_unrelated_files + group_manager_files + group_manager_peer_to_peer_files + + authenticators_p2p_files + authenticators_p2p_iso_files + authenticators_p2p_pake_files + + authenticators_standard_exchange_task_files + account_related_files diff --git a/test/unittest/tdd_framework/tdd_helper/os_account_adapter_mock/src/os_account_adapter_mock.c b/test/unittest/tdd_framework/tdd_helper/os_account_adapter_mock/src/os_account_adapter_mock.c new file mode 100644 index 00000000..874bbe9d --- /dev/null +++ b/test/unittest/tdd_framework/tdd_helper/os_account_adapter_mock/src/os_account_adapter_mock.c @@ -0,0 +1,21 @@ +/* + * 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 "os_account_adapter.h" + +int32_t DevAuthGetRealOsAccountLocalId(int32_t inputId) +{ + return inputId; +} diff --git a/test/unittest/tdd_framework/tdd_helper/protocol_controller/inc/protocol_controller.h b/test/unittest/tdd_framework/tdd_helper/protocol_controller/inc/protocol_controller.h new file mode 100644 index 00000000..1fa4ec0c --- /dev/null +++ b/test/unittest/tdd_framework/tdd_helper/protocol_controller/inc/protocol_controller.h @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef PROTOCOL_CONTROLLER_H +#define PROTOCOL_CONTROLLER_H + +#include "base_sub_task.h" +#include "json_utils.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void SetIsoSupported(bool isSupported); +void SetPakeV1Supported(bool isSupported); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/test/unittest/tdd_framework/tdd_helper/protocol_controller/src/protocol_controller.c b/test/unittest/tdd_framework/tdd_helper/protocol_controller/src/protocol_controller.c new file mode 100644 index 00000000..5462f666 --- /dev/null +++ b/test/unittest/tdd_framework/tdd_helper/protocol_controller/src/protocol_controller.c @@ -0,0 +1,87 @@ +/* + * 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 "hc_log.h" +#include "hc_types.h" +#include "iso_task_main.h" +#include "iso_base_cur_task.h" +#include "iso_client_task.h" +#include "iso_server_task.h" +#include "pake_v1_task_main.h" +#include "pake_base_cur_task.h" +#include "pake_v1_client_task.h" +#include "pake_v1_server_task.h" +#include "protocol_controller.h" + +static bool g_isIsoSupported = true; +static bool g_isPakeV1Supported = true; + +void SetPakeV1Supported(bool isSupported) +{ + g_isPakeV1Supported = isSupported; +} + +bool IsSupportPakeV1() +{ + if (g_isPakeV1Supported) { + LOGI("pake v1 support."); + } else { + LOGI("pake v1 not support."); + } + return g_isPakeV1Supported; +} + +SubTaskBase *CreatePakeV1SubTask(const CJson *in) +{ + bool isClient = true; + if (GetBoolFromJson(in, FIELD_IS_CLIENT, &isClient) != HC_SUCCESS) { + LOGE("Get isClient failed."); + return NULL; + } + if (isClient) { + return CreatePakeV1ClientTask(in); + } else { + return CreatePakeV1ServerTask(in); + } +} + +void SetIsoSupported(bool isSupported) +{ + g_isIsoSupported = isSupported; +} + +bool IsIsoSupported(void) +{ + if (g_isIsoSupported) { + LOGI("iso support."); + } else { + LOGI("iso not support."); + } + return g_isIsoSupported; +} + +SubTaskBase *CreateIsoSubTask(const CJson *in) +{ + bool isClient = true; + if (GetBoolFromJson(in, FIELD_IS_CLIENT, &isClient) != HC_SUCCESS) { + LOGE("Get isClient failed."); + return NULL; + } + if (isClient) { + return CreateIsoClientTask(in); + } else { + return CreateIsoServerTask(in); + } +} \ No newline at end of file -- Gitee