提交 d527b885 编写于 作者: Q qiaozzzh

deviceauth代码整改

Signed-off-by: Nqiaozzzh <qiaozhang@huawei.com>
上级 4ecb7b3b
# Copyright (C) 2021 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("//test/xts/acts/security_lite/deviceauth_basic_deps/deviceauth.gni")
import("//test/xts/tools/lite/build/suite_lite.gni")
hctest_suite("ActsSecurityHichainBasicDeps") {
suite_name = "acts"
sources = [ "test.c" ]
sources += DEVICEAUTH_BASIC_DEPS_SOURCE
include_dirs = [
"//commonlibrary/c_utils/base/include",
"//commonlibrary/utils_lite/include",
"//test/xts/tools/lite/hctest/include",
"//third_party/unity/src",
"//foundation/systemabilitymgr/samgr_lite/interfaces/kits/samgr",
]
include_dirs += DEVICEAUTH_BASIC_DEPS_INC
deps = []
cflags = [ "-Werror" ]
ldflags = [
"-lstdc++",
"-lm",
"-lpthread",
]
defines = [
"MKDIR_IMPLEMENTED=0",
"STAT_IMPLEMENTED=0",
"ACCESS_IMPLEMENTED=0",
"F_API_IMPLEMENTED=0",
"FLASH_START_ADDRESS_HICHAIN=0",
"UTILS_FILE_IMPLEMENTED=1",
"TEST_PTHREAD_CREATE_DETACHED=0",
"TEST_HC_FILE_OPEN_SERIES=1",
]
defines += DEVICEAUTH_BASIC_DEPS_ALGORITHM_DEFINES
if (defined(ohos_lite)) {
include_dirs += [
"//base/security/device_auth/deps_adapter/os_adapter/interfaces/liteos",
]
if (ohos_kernel_type == "liteos_m") {
# liteos m
include_dirs +=
[ "//base/startup/init/interfaces/innerkits/include/syspara" ]
deps += [ "//base/startup/init/interfaces/innerkits:libbegetutil" ]
if (board_name == "rtl8720") {
cflags +=
[ "-isystem${ohos_root_path}/kernel/liteos_m/kal/posix/include" ]
defines -= [
"FLASH_START_ADDRESS_HICHAIN=0",
"TEST_HC_FILE_OPEN_SERIES=1",
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE=1",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_GENERATE_KEY_PAIR=1",
"TEST_EXPORT_PUBLIC_KEY=1",
"TEST_ALGORITHM_SIGN=1",
"TEST_ALGORITHM_VERIFY=1",
"TEST_IMPORT_PUBLIC_KEY=1",
"TEST_CHECK_KEY_EXIST=1",
"TEST_DELETE_KEY=1",
"TEST_AES_GCM_ENCRYPT=1",
"TEST_AES_GCM_DECRYPT=1",
]
defines += [
"FLASH_START_ADDRESS_HICHAIN=0x1F6000",
"TEST_HC_FILE_OPEN_SERIES=0",
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=0",
"TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE=0",
"TEST_HASH_TO_POINT=0",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET=0",
"TEST_GENERATE_KEY_PAIR=0",
"TEST_EXPORT_PUBLIC_KEY=0",
"TEST_ALGORITHM_SIGN=0",
"TEST_ALGORITHM_VERIFY=0",
"TEST_IMPORT_PUBLIC_KEY=0",
"TEST_CHECK_KEY_EXIST=0",
"TEST_DELETE_KEY=0",
"TEST_AES_GCM_ENCRYPT=0",
"TEST_AES_GCM_DECRYPT=0",
]
defines += [ "DO_NOT_TEST_DEPRECATED_IMPORT_SYMMETRIC_KEY=1" ]
}
if (board_name == "hispark_pegasus") {
# 3861
defines -= [
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_GENERATE_KEY_PAIR=1",
"TEST_EXPORT_PUBLIC_KEY=1",
"TEST_ALGORITHM_SIGN=1",
"TEST_ALGORITHM_VERIFY=1",
"TEST_IMPORT_PUBLIC_KEY=1",
]
defines += [
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=0",
"TEST_HASH_TO_POINT=0",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=0",
"TEST_AGREE_SHARED_SECRET=0",
"TEST_GENERATE_KEY_PAIR=0",
"TEST_EXPORT_PUBLIC_KEY=0",
"TEST_ALGORITHM_SIGN=0",
"TEST_ALGORITHM_VERIFY=0",
"TEST_IMPORT_PUBLIC_KEY=0",
]
}
} else {
#liteos a or L1 linux
include_dirs +=
[ "//base/startup/init/interfaces/innerkits/include/syspara" ]
deps += [ "//base/startup/init/interfaces/innerkits:libbegetutil" ]
}
} else {
# linux
include_dirs += [
"//base/startup/init/interfaces/innerkits/include/syspara",
"//base/security/device_auth/deps_adapter/os_adapter/interfaces/linux",
]
deps += [ "//base/startup/init/interfaces/innerkits:libbegetutil" ]
}
}
## wifiiot_hispark_pegasus
1. compile command:
``` bash
cd test/xts/tools/lite
./build.sh product=wifiiot xts=acts target=//test/xts/acts/security_lite/deviceauth_basic_deps:ActsSecurityHichainBasicDeps
```
{
"description": "Config for $module test cases",
"environment": [
{
"type": "device",
"label": "wifiiot"
}
],
"kits": [
{
"type": "DeployKit",
"timeout": "20000",
"burn_file": "$subsystem/$module.bin"
}
],
"driver": {
"type": "CTestLite"
}
}
\ No newline at end of file
# Copyright (C) 2021 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.
DEVICEAUTH_BASIC_DEPS_DIR =
"//test/xts/acts/security_lite/deviceauth_basic_deps"
DEVICEAUTH_BASIC_DEPS_SOURCE = [
"${DEVICEAUTH_BASIC_DEPS_DIR}/print_log.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/test_timer.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_mutex_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_f_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_thread_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_dev_info_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_condition_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_utils_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_iot_flash_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_time_test.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_file_common.c",
"${DEVICEAUTH_BASIC_DEPS_DIR}/hc_alg_test.c",
]
DEVICEAUTH_BASIC_DEPS_INC = [
"//base/iothardware/peripheral/interfaces/inner_api",
"//commonlibrary/utils_lite/include", # utils_file.h ohos_types.h
# alg test
"//base/security/device_auth/deps_adapter/key_management_adapter/interfaces",
"//base/security/device_auth/deps_adapter/os_adapter/interfaces",
"//base/security/device_auth/common_lib/interfaces",
"//base/security/device_auth/interfaces/innerkits",
]
DEVICEAUTH_BASIC_DEPS_ALGORITHM_DEFINES = [
"TEST_HKDF_WITH_KEY_ALIAS_TRUE=1",
"TEST_IMPORT_SYMMETRIC_KEY=1",
"TEST_COMPUTEHMAC_WITH_ISALIAS_TRUE=1",
"TEST_HASH_TO_POINT=1",
"TEST_GENERATE_KEY_PAIR_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET_WITH_STORAGE=1",
"TEST_AGREE_SHARED_SECRET=1",
"TEST_BIG_NUM_EXP_MOD=1",
"TEST_EXPORT_IMPORT_SIGN_VERIFY_ED25519=1",
"TEST_EXPORT_IMPORT_SIGN_VERIFY_P256=1",
"TEST_BIG_NUM_COMPARE=1",
"TEST_GENERATE_KEY_PAIR=1",
"TEST_EXPORT_PUBLIC_KEY=1",
"TEST_ALGORITHM_SIGN=1",
"TEST_ALGORITHM_VERIFY=1",
"TEST_IMPORT_PUBLIC_KEY=1",
"TEST_CHECK_DL_PUBLIC_KEY=1",
"TEST_CHECK_KEY_EXIST=1",
"TEST_DELETE_KEY=1",
"TEST_AES_GCM_ENCRYPT=1",
"TEST_AES_GCM_DECRYPT=1",
]
/*
* Copyright (C) 2021 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 HC_ALG_TEST_H
#define HC_ALG_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcAlg(void);
#ifdef __cplusplus
}
#endif
#endif // HC_ALG_TEST_H
/*
* Copyright (C) 2021 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_condition_test.h"
#include <hctest.h>
#include <parameter.h>
#include <pthread.h>
#include <securec.h>
#include <semaphore.h>
#include <stdlib.h>
#include <unistd.h>
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
pthread_cond_t *cond;
pthread_mutex_t *mutex;
sem_t *sem;
int32_t semState;
int32_t condState;
} ThreadTestArgs;
#define REQUIRED_SEM_DEFAULT_COUNT 20
#define REQUIRED_SEM_COUNT 20
#define REQUIRED_ATTR_DEFAULT_COUNT 20
#define REQUIRED_ATTR_COUNT 20
/* sem_wait thread test */
static void *ThreadSemWait(void *args)
{
LOGI("into ThreadSemWait");
int res;
ThreadTestArgs *semArgs = (ThreadTestArgs *)args;
TEST_ASSERT_NOT_NULL(semArgs->sem);
TEST_ASSERT_NOT_EQUAL(0, semArgs->semState);
RUN_AND_PRINT_ELAPSED_TIME(res, sem_wait((sem_t *)semArgs->sem));
// after sem_post
LOGI("sem_wait result = %d", res);
TEST_ASSERT_EQUAL(0, res);
semArgs->semState = 0;
return args;
}
/* sem_post thread test */
static void *ThreadSemPost(void *args)
{
LOGI("into ThreadSemPost");
int res;
ThreadTestArgs *semArgs = (ThreadTestArgs *)args;
TEST_ASSERT_NOT_NULL(semArgs->sem);
RUN_AND_PRINT_ELAPSED_TIME(res, sem_post((sem_t *)semArgs->sem));
LOGI("sem_post result = %d", res);
TEST_ASSERT_EQUAL(0, res);
return args;
}
/* pthread_cond_wait thread test */
static void *ThreadCondWait(void *args)
{
LOGI("into ThreadCondWait");
int res;
ThreadTestArgs *condArgs = (ThreadTestArgs *)args;
TEST_ASSERT_NOT_NULL(condArgs->cond);
TEST_ASSERT_NOT_NULL(condArgs->mutex);
TEST_ASSERT_NOT_EQUAL(0, condArgs->condState);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_wait(condArgs->cond, condArgs->mutex));
// after pthread_cond_signal
LOGI("pthread_cond_wait result = %d", res);
TEST_ASSERT_EQUAL(0, res);
condArgs->condState = 0;
return args;
}
/* pthread_cond_signal thread test */
static void *ThreadCondSignal(void *args)
{
LOGI("into ThreadCondSignal");
int res;
ThreadTestArgs *testArgs = (ThreadTestArgs *)args;
TEST_ASSERT_NOT_NULL(testArgs->cond);
TEST_ASSERT_NOT_NULL(testArgs->mutex);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_signal(testArgs->cond));
LOGI("pthread_cond_signal result = %d", res);
return args;
}
/* sem_init and sem_destroy test */
static void TestSemInit(int count)
{
int res;
sem_t sem[count];
res = memset_s(sem, sizeof(sem), 0, sizeof(sem));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_init(&sem[i], 0, 0));
LOGI("sem_init result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_destroy(&sem[i]));
LOGI("sem_destroy result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
}
/* sem_wait test */
static void TestSemWait(int count)
{
int res;
pthread_attr_t attr;
pthread_t threads[count];
res = memset_s(threads, sizeof(threads), 0, sizeof(threads));
TEST_ASSERT_EQUAL(0, res);
sem_t sem = {0};
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_init(&attr));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_init(&sem, 0, 0));
LOGI("sem_init result = %d", res);
ThreadTestArgs args = {
.sem = &sem,
.semState = -1,
};
TEST_ASSERT_EQUAL(0, res);
/* create phread to wait */
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &attr, ThreadSemWait, &args));
LOGI("create sem_wait thread result = %d", res);
int sleepSeconds = rand() % (WAIT_IN_SEM_SECONDS_MAX - WAIT_IN_SEM_SECONDS_MIN) + WAIT_IN_SEM_SECONDS_MIN;
sleep(sleepSeconds);
/* sleep seconds then create thread to post */
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &attr, ThreadSemPost, &args));
LOGI("create sem_post thread result = %d", res);
TEST_ASSERT_EQUAL(0, res);
sleep(sleepSeconds);
TEST_ASSERT_EQUAL(0, args.semState);
}
}
/* sem_post test */
static void TestSemPost(int count)
{
int res;
sem_t sem[count];
res = memset_s(sem, sizeof(sem), 0, sizeof(sem));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_init(&sem[i], 0, 0));
LOGI("sem_init int TestSemPost result = %d", res);
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, sem_post(&sem[i]));
LOGI("sem_post result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_post(&sem[i]));
LOGI("sem_post result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, sem_destroy(&sem[i]));
LOGI("sem_destroy in TestSemPost result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
}
/* pthread_condattr_init and pthread_condattr_destroy test */
static void TestAttrConInit(int count)
{
int res;
pthread_condattr_t attr[count];
res = memset_s(attr, sizeof(attr), 0, sizeof(attr));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_condattr_init(&attr[i]));
LOGI("pthread_condattr_init result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_condattr_destroy(&attr[i]));
LOGI("pthread_condattr_destroy result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
}
/* pthread_cond_init and pthread_cond_destroy test */
static void TestConInit(int count)
{
int res;
pthread_condattr_t attr = {0};
pthread_cond_t cond[count];
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_condattr_init(&attr));
LOGI("pthread_condattr_init in TestConInit result = %d", res);
TEST_ASSERT_EQUAL(0, res);
res = memset_s(cond, sizeof(cond), 0, sizeof(cond));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_init(&cond[i], &attr));
LOGI("pthread_cond_init result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_destroy(&cond[i]));
LOGI("pthread_cond_destroy result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
}
/* pthread_cond_wait and pthread_cond_signal test */
static void TestConWait(int count)
{
int res;
pthread_attr_t pthreadAttr;
pthread_condattr_t attr = {0};
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_t threads[count];
res = memset_s(&mutex, sizeof(mutex), 0, sizeof(mutex));
TEST_ASSERT_EQUAL(0, res);
res = memset_s(threads, sizeof(threads), 0, sizeof(threads));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_init(&pthreadAttr));
TEST_ASSERT_EQUAL(0, res);
// init attr and cond and mutex
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_condattr_init(&attr));
LOGI("pthread_condattr_init in TestConWait result = %d", res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_init(&cond, &attr));
LOGI("pthread_cond_init in TestConWait result = %d", res);
for (int i = 0; i < count; ++i) {
ThreadTestArgs args = {
.cond = &cond,
.mutex = &mutex,
.condState = -1,
};
/* create phread to wait */
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &pthreadAttr, ThreadCondWait, &args));
LOGI("create pthread_cond_wait thread result = %d", res);
TEST_ASSERT_EQUAL(0, res);
int sleepSeconds = rand() % (WAIT_IN_SEM_SECONDS_MAX - WAIT_IN_SEM_SECONDS_MIN) + WAIT_IN_SEM_SECONDS_MIN;
sleep(sleepSeconds);
/* sleep seconds then create thread to send signal */
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &pthreadAttr, ThreadCondSignal, &args));
LOGI("create pthread_cond_signal thread result = %d", res);
TEST_ASSERT_EQUAL(0, res);
sleep(sleepSeconds);
TEST_ASSERT_EQUAL(0, args.condState);
}
}
/* pthread_cond_signal test */
static void TestConSignal(int count)
{
int res;
pthread_condattr_t attr = {0};
pthread_cond_t cond;
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_init(&cond, &attr));
LOGI("pthread_cond_init in TestConWait result = %d", res);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_cond_signal(&cond));
LOGI("pthread_cond_signal result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
}
/* test semaphore with sem series */
void TestHcSemCondition(void)
{
TestSemInit(REQUIRED_SEM_DEFAULT_COUNT);
TestSemPost(REQUIRED_SEM_DEFAULT_COUNT);
TestSemWait(REQUIRED_SEM_DEFAULT_COUNT);
}
/* test semaphore with phread cond */
void TestHcPthreadCondition(void)
{
TestConWait(REQUIRED_ATTR_DEFAULT_COUNT);
TestAttrConInit(REQUIRED_ATTR_DEFAULT_COUNT);
TestConInit(REQUIRED_ATTR_DEFAULT_COUNT);
TestConSignal(REQUIRED_ATTR_DEFAULT_COUNT);
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_CONDITION_TEST_H
#define HC_CONDITION_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcSemCondition(void);
void TestHcPthreadCondition(void);
#ifdef __cplusplus
}
#endif
enum {
WAIT_IN_SEM_SECONDS_MIN = 1,
WAIT_IN_SEM_SECONDS_MAX = 2,
};
#endif // HC_CONDITION_TEST_H
\ No newline at end of file
/*
* Copyright (C) 2021 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_dev_info_test.h"
#include <hctest.h>
#include <parameter.h>
#include <pthread.h>
#include <securec.h>
#include <stdlib.h>
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_INPUT_UDID_LEN 200
enum {
INPUT_UDID_LEN = 65,
TEST_UDID_TIMES = 20,
};
static void TestGetDevUdid(char *udid, int len)
{
int res;
int zeroCount = 0;
res = memset_s(udid, MAX_INPUT_UDID_LEN, 0, MAX_INPUT_UDID_LEN);
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, GetDevUdid(udid, len));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < len; ++i) {
if (udid[i] == 0) {
++zeroCount;
}
}
TEST_ASSERT_NOT_EQUAL_MESSAGE(len, zeroCount, "invalid all zero udid");
}
void TestHcGetUdid(void)
{
int res;
char udid[MAX_INPUT_UDID_LEN] = {0};
int udidLen;
LOGI("check rand size udid");
for (int i = 0; i < TEST_UDID_TIMES; ++i) {
udidLen = rand() % (MAX_INPUT_UDID_LEN - INPUT_UDID_LEN) + INPUT_UDID_LEN;
LOGI("rand udidLen = %d", udidLen);
res = memset_s(udid, sizeof(udid), 0, sizeof(udid));
TEST_ASSERT_EQUAL(0, res);
TestGetDevUdid(udid, udidLen);
LOGI("udid = \"%s\"", udid);
}
LOGI("check min size udid");
res = memset_s(udid, sizeof(udid), 0, sizeof(udid));
TEST_ASSERT_EQUAL(0, res);
TestGetDevUdid(udid, INPUT_UDID_LEN);
LOGI("udid = \"%s\"", udid);
LOGI("check max size udid");
res = memset_s(udid, sizeof(udid), 0, sizeof(udid));
TEST_ASSERT_EQUAL(0, res);
TestGetDevUdid(udid, MAX_INPUT_UDID_LEN);
LOGI("udid = \"%s\"", udid);
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_DEV_INFO_TEST_H
#define HC_DEV_INFO_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcGetUdid(void);
#ifdef __cplusplus
}
#endif
#endif // HC_DEV_INFO_TEST_H
/*
* Copyright (C) 2021 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_file_common.h"
#include <hctest.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
char *GenerateTestingText(int size)
{
TEST_ASSERT_LESS_OR_EQUAL(TEN_KILOBYTE, size);
char *text = (char *)malloc(size + 1);
if (text == NULL) {
return NULL;
}
for (int i = 0; i < size; i++) {
text[i] = rand() % (ASCII_END - ASCII_START) + ASCII_START;
}
text[size] = 0;
return text;
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_FILE_COMMON_H
#define HC_FILE_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif
#define TEST_FILE_NAME "test"
#define TEST_FILE_DIR "/testDir"
#define DEFAULT_FILE_PERMISSION 0644
#define ASCII_START 32
#define ASCII_END 126
#define ONE_BYTE 1
#define HALF_KILOBYTE 512
#define ONE_KILOBYTE 1024
#define TEN_KILOBYTE 10240
#define TEST_FILE_SIZE_LIST_LEN 4
static const int testFileSizeList[TEST_FILE_SIZE_LIST_LEN] = {
ONE_BYTE,
HALF_KILOBYTE,
ONE_KILOBYTE,
TEN_KILOBYTE
};
char *GenerateTestingText(int size);
#ifdef __cplusplus
}
#endif
#endif // HC_FILE_COMMON_H
\ No newline at end of file
/*
* Copyright (C) 2021 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_file_f_test.h"
#include <errno.h>
#include <fcntl.h>
#include <hctest.h>
#include <securec.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "hc_file_common.h"
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#if F_API_IMPLEMENTED
static void ReadFile(char *buffer, FILE *file, int size)
{
int total = 0;
while (total < size) {
int readCount = fread(buffer + total, 1, size - total, file);
TEST_ASSERT_GREATER_OR_EQUAL(0, readCount);
TEST_ASSERT_LESS_OR_EQUAL(size - total, readCount);
total += readCount;
if (readCount <= 0) {
break;
}
}
}
static void WriteFile(const char *buffer, FILE *file, int size)
{
int total = 0;
while (total < size) {
int writeCount = fwrite(buffer + total, 1, size - total, file);
TEST_ASSERT_GREATER_OR_EQUAL(0, writeCount);
TEST_ASSERT_LESS_OR_EQUAL(size - total, writeCount);
total += writeCount;
if (writeCount <= 0) {
break;
}
}
}
static void TestHcFileFopenAndFclose(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to open file: %s", fileName);
FILE *file;
RUN_AND_PRINT_ELAPSED_TIME(file, fopen(fileName, "w+"));
TEST_ASSERT_NOT_NULL(file);
int ret;
LOGI("begin to close file: %s", fileName);
RUN_AND_PRINT_ELAPSED_TIME(ret, fclose(file));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileFreadAndFwrite(void)
{
const char *fileName = TEST_FILE_NAME;
for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
FILE *file = fopen(fileName, "w+");
TEST_ASSERT_NOT_NULL(file);
int size = testFileSizeList[i];
LOGI("begin to write file [%s] for size: %d", fileName, size);
char *writeBuffer = GenerateTestingText(size);
TEST_ASSERT_NOT_NULL(writeBuffer);
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(WriteFile(writeBuffer, file, size));
TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_END));
const int fileSize = ftell(file);
TEST_ASSERT_EQUAL(size, fileSize);
TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_SET));
LOGI("begin to read file [%s] for size: %d", fileName, fileSize);
TEST_ASSERT_LESS_OR_EQUAL(TEN_KILOBYTE, fileSize);
char *readBuffer = (char *)malloc(fileSize + 1);
TEST_ASSERT_NOT_NULL(readBuffer);
TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(ReadFile(readBuffer, file, fileSize));
TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
free(readBuffer);
free(writeBuffer);
TEST_ASSERT_EQUAL(0, fclose(file));
sleep(1);
}
}
static void TestHcFileFseekAndFtell(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to count file size");
FILE *file = fopen(fileName, "rb");
TEST_ASSERT_NOT_NULL(file);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, fseek(file, 0, SEEK_END));
TEST_ASSERT_EQUAL(0, ret);
int size;
RUN_AND_PRINT_ELAPSED_TIME(size, ftell(file));
TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_SET));
LOGI("the file size is: %d", size);
TEST_ASSERT_EQUAL(0, fclose(file));
RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(fileName));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileMkdir(void)
{
#if MKDIR_IMPLEMENTED
const char *dir = TEST_FILE_DIR;
LOGI("begin to make directory: %s", dir);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, mkdir(dir, DEFAULT_FILE_PERMISSION));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(-1, mkdir(dir, DEFAULT_FILE_PERMISSION));
TEST_ASSERT_EQUAL(0, rmdir(dir));
#endif
}
static void TestHcFileStat(void)
{
#if STAT_IMPLEMENTED
const char *fileName = TEST_FILE_NAME;
LOGI("begin to check [%s] state", fileName);
FILE *file = fopen(fileName, "w+");
TEST_ASSERT_NOT_NULL(file);
TEST_ASSERT_EQUAL(0, fclose(file));
struct stat fileStat;
int ret;
ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
TEST_ASSERT_EQUAL(0, ret);
RUN_AND_PRINT_ELAPSED_TIME(ret, stat(fileName, &fileStat));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(0, unlink(fileName));
ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(-1, stat(fileName, &fileStat));
TEST_ASSERT_EQUAL(ENOENT, errno);
#endif
}
static void TestHcFileRemove(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to remove file: %s", fileName);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(fileName));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileAccess(void)
{
#if ACCESS_IMPLEMENTED
const char *fileName = TEST_FILE_NAME;
LOGI("begin to check access of [%s]", fileName);
FILE *file = fopen(fileName, "w+");
TEST_ASSERT_NOT_NULL(file);
TEST_ASSERT_EQUAL(0, fclose(file));
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, access(fileName, F_OK));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(0, unlink(fileName));
TEST_ASSERT_EQUAL(-1, access(fileName, F_OK));
#endif
}
void TestHcFileFApi(void)
{
LOGI("test opening and closing file");
TestHcFileFopenAndFclose();
LOGI("test removing file");
TestHcFileRemove();
LOGI("test reading and writing file");
TestHcFileFreadAndFwrite();
LOGI("test counting file size");
TestHcFileFseekAndFtell();
LOGI("test making directory");
TestHcFileMkdir();
LOGI("test getting file state");
TestHcFileStat();
LOGI("test accessing a file");
TestHcFileAccess();
}
#else // F_API_IMPLEMENTED
void TestHcFileFApi(void)
{
LOGE("no F_API_IMPLEMENTED, do not test fopen series!");
}
#endif // F_API_IMPLEMENTED
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_FILE_F_TEST_H
#define HC_FILE_F_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcFileFApi(void);
#ifdef __cplusplus
}
#endif
#endif // HC_FILE_F_TEST_H
/*
* Copyright (C) 2021 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_file_iot_flash_test.h"
#include <hctest.h>
#include <iot_errno.h>
#include <iot_flash.h>
#include <securec.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#if FLASH_START_ADDRESS_HICHAIN
#define FLASH_SIZE_4_K 4096
#define TOTAL_STORAGE_SIZE FLASH_SIZE_4_K // 4K Bytes
#define GROUP_OFFSET_ADDR 0x0100 // sizeof(struct FlashHeader)
#define TCIS_OFFSET_ADDR 0x0BC0
#define KEY_OFFSET_ADDR 0x0DC0
#define PART2_OFFSET_ADDR 0x0EC0
#define PART3_OFFSET_ADDR 0x0F00
#define SALT_OFFSET_ADDR 0x0F40
#define MK_OFFSET_ADDR 0x0F80
#define MAGIC_NUM 0x07DECADE
#define DEFAULT_VERSION 0x01
typedef enum FileIdEnumT {
FILE_ID_GROUP = 0,
FILE_ID_TCIS_DATA,
FILE_ID_KEYCONTENT,
FILE_ID_PART2,
FILE_ID_PART3,
FILE_ID_SALT,
FILE_ID_MK,
FILE_ID_LAST,
} FileIdEnum;
static uint32_t g_startAddr = FLASH_START_ADDRESS_HICHAIN;
static const struct FlashHeader {
struct VersionHeader {
uint64_t magic;
uint16_t version;
uint8_t reserved[54]; // 54: Reserved bytes for expansion
} versionHeader;
struct FileHeader {
uint32_t start;
uint32_t size;
uint32_t end;
} fileHeaders[FILE_ID_LAST];
uint8_t reserved[108]; // 108: Reserved bytes for expansion
} g_flashHeader = {
.versionHeader = {
.magic = MAGIC_NUM,
.version = DEFAULT_VERSION,
},
.fileHeaders[FILE_ID_GROUP] = {
.start = GROUP_OFFSET_ADDR,
.size = TCIS_OFFSET_ADDR - GROUP_OFFSET_ADDR,
.end = TCIS_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_TCIS_DATA] = {
.start = TCIS_OFFSET_ADDR,
.size = KEY_OFFSET_ADDR - TCIS_OFFSET_ADDR,
.end = KEY_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_KEYCONTENT] = {
.start = KEY_OFFSET_ADDR,
.size = PART2_OFFSET_ADDR - KEY_OFFSET_ADDR,
.end = PART2_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_PART2] = {
.start = PART2_OFFSET_ADDR,
.size = PART3_OFFSET_ADDR - PART2_OFFSET_ADDR,
.end = PART3_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_PART3] = {
.start = PART3_OFFSET_ADDR,
.size = SALT_OFFSET_ADDR - PART3_OFFSET_ADDR,
.end = SALT_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_SALT] = {
.start = SALT_OFFSET_ADDR,
.size = MK_OFFSET_ADDR - SALT_OFFSET_ADDR,
.end = MK_OFFSET_ADDR,
},
.fileHeaders[FILE_ID_MK] = {
.start = MK_OFFSET_ADDR,
.size = TOTAL_STORAGE_SIZE - MK_OFFSET_ADDR,
.end = TOTAL_STORAGE_SIZE,
},
};
enum {
RANDOM_READ_TIMES = 20,
};
static void ReadFlash(uint32_t offset, uint8_t *buffer, uint32_t size)
{
int res;
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashInit());
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashRead(g_startAddr + offset, size, buffer));
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashDeinit());
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
}
static void WriteFlash(uint32_t offset, const uint8_t *buffer, uint32_t size)
{
int res;
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashInit());
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
LOGI("begin write, g_startAddr + offset = %lu, size = %lu, buffer = %p",
(unsigned long)(g_startAddr + offset), (unsigned long)(size), buffer);
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashWrite(g_startAddr + offset, size, buffer, true));
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
RUN_AND_PRINT_ELAPSED_TIME(res, IoTFlashDeinit());
TEST_ASSERT_EQUAL(IOT_SUCCESS, res);
}
static void TestIotFlashWrite(const uint8_t *totalDataContent, uint32_t sz)
{
LOGI("begin to test writing flash");
WriteFlash(0, totalDataContent, sz);
LOGI("test writing flash done");
}
static void TestIotFlashReadWholeBlock(
uint8_t *totalData, uint32_t sz, const uint8_t *totalDataContent, uint32_t contentSz)
{
int res = memset_s(totalData, sz, 0, sz);
TEST_ASSERT_EQUAL(EOK, res);
LOGI("begin to test read whole flash");
ReadFlash(0, totalData, sz);
LOGI("test reading whole flash done");
TEST_ASSERT_EQUAL(sz, contentSz);
TEST_ASSERT_EQUAL_HEX8_ARRAY(totalData, totalDataContent, sz);
}
static void TestIotFlashReadFlashHeader(
struct FlashHeader *flashHeader, const struct FlashHeader *flashHeaderCompare)
{
int res = memset_s(flashHeader, sizeof(*flashHeader), 0, sizeof(*flashHeader));
TEST_ASSERT_EQUAL(EOK, res);
LOGI("begin reading header");
ReadFlash(0, (uint8_t *)flashHeader, sizeof(struct FlashHeader));
LOGI("end reading header");
TEST_ASSERT_EQUAL_HEX8_ARRAY((const uint8_t *)flashHeader, (const uint8_t *)flashHeaderCompare,
sizeof(struct FlashHeader));
}
static void TestIotFlashReadSmallFiles(
uint8_t *totalData, uint32_t sz, const uint8_t *totalDataContent, uint32_t contentSz)
{
LOGI("begin reading small files");
TEST_ASSERT_EQUAL((uint32_t)(TOTAL_STORAGE_SIZE), contentSz);
int res;
for (int i = 0; i < FILE_ID_LAST; ++i) {
LOGI("test flash file %d/%d", i, FILE_ID_LAST);
res = memset_s(totalData, sz, 0, sz);
TEST_ASSERT_EQUAL(EOK, res);
LOGI("test read from %lu, size %lu", (unsigned long)(g_flashHeader.fileHeaders[i].start),
(unsigned long)(g_flashHeader.fileHeaders[i].size));
ReadFlash(g_flashHeader.fileHeaders[i].start, totalData, g_flashHeader.fileHeaders[i].size);
TEST_ASSERT_EQUAL_HEX8_ARRAY(totalData, totalDataContent + g_flashHeader.fileHeaders[i].start,
g_flashHeader.fileHeaders[i].size);
}
LOGI("end reading small files");
}
static void TestIotFlashReadRandom(uint8_t *totalData, uint32_t sz, const uint8_t *totalDataContent, uint32_t contentSz)
{
LOGI("begin reading random bytes");
TEST_ASSERT_EQUAL((uint32_t)(TOTAL_STORAGE_SIZE), contentSz);
int res;
for (int i = 0; i < RANDOM_READ_TIMES; ++i) {
uint32_t startAddr = rand() % TOTAL_STORAGE_SIZE;
uint32_t size = rand() % (TOTAL_STORAGE_SIZE - startAddr);
LOGI("test random read flash %d/%d, startAddr = %lu, size = %lu", i,
RANDOM_READ_TIMES, (unsigned long)(startAddr), (unsigned long)(size));
res = memset_s(totalData, sz, 0, sz);
TEST_ASSERT_EQUAL(EOK, res);
ReadFlash(startAddr, totalData, size);
TEST_ASSERT_EQUAL_HEX8_ARRAY(totalData, totalDataContent + startAddr, size);
}
LOGI("end reading random bytes");
LOGI("begin reading small bytes");
for (int i = 0; i < RANDOM_READ_TIMES; ++i) {
uint32_t size = i + 1;
uint32_t startAddr = rand() % (TOTAL_STORAGE_SIZE - size);
LOGI("test random read flash %d/%d, startAddr = %lu, size = %lu", i,
RANDOM_READ_TIMES, (unsigned long)(startAddr), (unsigned long)(size));
res = memset_s(totalData, sz, 0, sz);
TEST_ASSERT_EQUAL(EOK, res);
ReadFlash(startAddr, totalData, size);
TEST_ASSERT_EQUAL_HEX8_ARRAY(totalData, totalDataContent + startAddr, size);
}
LOGI("end reading small bytes");
}
void TestHcFileIotFlash(void)
{
uint8_t *totalData = (uint8_t *)malloc(TOTAL_STORAGE_SIZE);
TEST_ASSERT_NOT_NULL(totalData);
uint8_t *totalDataContent = (uint8_t *)malloc(TOTAL_STORAGE_SIZE);
TEST_ASSERT_NOT_NULL(totalDataContent);
LOGI("malloc memory succeed");
int res, zeroCount = 0;
struct FlashHeader flashHeader;
res = memset_s(&flashHeader, sizeof(flashHeader), 0, sizeof(flashHeader));
TEST_ASSERT_EQUAL(EOK, res);
for (uint32_t i = 0; i < TOTAL_STORAGE_SIZE; ++i) {
totalDataContent[i] = rand() % UINT8_MAX;
if (totalDataContent[i] == 0) {
++zeroCount;
}
}
TEST_ASSERT_NOT_EQUAL(TOTAL_STORAGE_SIZE, zeroCount);
TestIotFlashWrite(totalDataContent, TOTAL_STORAGE_SIZE);
TestIotFlashReadWholeBlock(totalData, TOTAL_STORAGE_SIZE, totalDataContent, TOTAL_STORAGE_SIZE);
TestIotFlashReadFlashHeader(&flashHeader, (struct FlashHeader *)totalDataContent);
TestIotFlashReadSmallFiles(totalData, TOTAL_STORAGE_SIZE, totalDataContent, TOTAL_STORAGE_SIZE);
TestIotFlashReadRandom(totalData, TOTAL_STORAGE_SIZE, totalDataContent, TOTAL_STORAGE_SIZE);
free(totalData);
free(totalDataContent);
}
#else // FLASH_START_ADDRESS_HICHAIN
void TestHcFileIotFlash(void)
{
LOGE("no FLASH_START_ADDRESS_HICHAIN, do not test iot flash");
}
#endif // FLASH_START_ADDRESS_HICHAIN
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_FILE_IOT_FLASH_TEST_H
#define HC_FILE_IOT_FLASH_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcFileIotFlash(void);
#ifdef __cplusplus
}
#endif
#endif // HC_FILE_IOT_FLASH_TEST_H
/*
* Copyright (C) 2021 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_file_test.h"
#include <errno.h>
#include <fcntl.h>
#include <hctest.h>
#include <securec.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "hc_file_common.h"
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#if TEST_HC_FILE_OPEN_SERIES
static void ReadFile(char *buffer, int fd, int size)
{
int total = 0;
while (total < size) {
int readCount = read(fd, buffer + total, size - total);
TEST_ASSERT_GREATER_OR_EQUAL(0, readCount);
TEST_ASSERT_LESS_OR_EQUAL(size - total, readCount);
total += readCount;
if (readCount <= 0) {
break;
}
}
}
static void WriteFile(char *buffer, int fd, int size)
{
int total = 0;
while (total < size) {
int writeCount = write(fd, buffer + total, size - total);
TEST_ASSERT_GREATER_OR_EQUAL(0, writeCount);
TEST_ASSERT_LESS_OR_EQUAL(size - total, writeCount);
total += writeCount;
if (writeCount <= 0) {
break;
}
}
}
static void TestHcFileOpenAndClose(void)
{
const char *file = TEST_FILE_NAME;
LOGI("begin to open file: %s", file);
int fd;
RUN_AND_PRINT_ELAPSED_TIME(fd, open(file, O_RDWR | O_CREAT));
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
int ret;
LOGI("begin to close file: %s", file);
RUN_AND_PRINT_ELAPSED_TIME(ret, close(fd));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileReadAndWrite(void)
{
const char *file = TEST_FILE_NAME;
for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
int fd = open(file, O_RDWR | O_CREAT);
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
int size = testFileSizeList[i];
LOGI("begin to write file [%s] for size: %d", file, size);
char *writeBuffer = GenerateTestingText(size);
TEST_ASSERT_NOT_NULL(writeBuffer);
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(WriteFile(writeBuffer, fd, size));
const int fileSize = lseek(fd, 0, SEEK_END);
TEST_ASSERT_EQUAL(size, fileSize);
TEST_ASSERT_EQUAL(0, lseek(fd, 0, SEEK_SET));
LOGI("begin to read file [%s] for size: %d", file, fileSize);
TEST_ASSERT_LESS_OR_EQUAL(TEN_KILOBYTE, fileSize);
char *readBuffer = (char *)malloc(fileSize + 1);
TEST_ASSERT_NOT_NULL(readBuffer);
TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(ReadFile(readBuffer, fd, fileSize));
TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
free(readBuffer);
free(writeBuffer);
TEST_ASSERT_EQUAL(0, close(fd));
sleep(1);
}
}
static void TestHcFileSize(void)
{
const char *file = TEST_FILE_NAME;
LOGI("begin to count file size");
int fd = open(file, O_RDWR);
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
int size;
RUN_AND_PRINT_ELAPSED_TIME(size, lseek(fd, 0, SEEK_END));
TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
TEST_ASSERT_EQUAL(0, lseek(fd, 0, SEEK_SET));
LOGI("the file size is: %d", size);
TEST_ASSERT_EQUAL(0, close(fd));
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(file));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileMkdir(void)
{
#if MKDIR_IMPLEMENTED
const char *dir = TEST_FILE_DIR;
LOGI("begin to make directory: %s", dir);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, mkdir(dir, DEFAULT_FILE_PERMISSION));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(-1, mkdir(dir, DEFAULT_FILE_PERMISSION));
TEST_ASSERT_EQUAL(0, rmdir(dir));
#else
LOGE("no MKDIR_IMPLEMENTED, do not test mkdir() !");
#endif
}
static void TestHcFileStat(void)
{
#if STAT_IMPLEMENTED
const char *file = TEST_FILE_NAME;
LOGI("begin to check [%s] state", file);
TEST_ASSERT_GREATER_OR_EQUAL(0, open(file, O_RDWR | O_CREAT));
struct stat fileStat;
int ret;
ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
TEST_ASSERT_EQUAL(0, ret);
RUN_AND_PRINT_ELAPSED_TIME(ret, stat(file, &fileStat));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(0, unlink(file));
ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(-1, stat(file, &fileStat));
TEST_ASSERT_EQUAL(ENOENT, errno);
#else
LOGE("no STAT_IMPLEMENTED, do not test stat() !");
#endif
}
static void TestHcFileRemove(void)
{
const char *file = TEST_FILE_NAME;
LOGI("begin to remove file: %s", file);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(file));
TEST_ASSERT_EQUAL(0, ret);
}
void TestHcFile(void)
{
LOGI("test opening and closing file");
TestHcFileOpenAndClose();
LOGI("test removing file");
TestHcFileRemove();
LOGI("test reading and writing file");
TestHcFileReadAndWrite();
LOGI("test counting file size");
TestHcFileSize();
LOGI("test making directory");
TestHcFileMkdir();
LOGI("test getting file state");
TestHcFileStat();
}
#else // TEST_HC_FILE_OPEN_SERIES
void TestHcFile(void)
{
LOGE("no TEST_HC_FILE_OPEN_SERIES, do not test hc_file open series!");
}
#endif // TEST_HC_FILE_OPEN_SERIES
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_LINUX_FILE_TEST_H
#define HC_LINUX_FILE_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcFile(void);
#ifdef __cplusplus
}
#endif
#endif // HC_LINUX_FILE_TEST_H
/*
* Copyright (C) 2021 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_file_utils_test.h"
#include <hctest.h>
#include <securec.h>
#include <stdlib.h>
#include <unistd.h>
#include "hc_file_common.h"
#include "print_log.h"
#include "test_timer.h"
#include "utils_file.h"
#ifdef __cplusplus
extern "C" {
#endif
#if UTILS_FILE_IMPLEMENTED
static void TestHcFileUtilsFileOpenAndUtilsFileClose(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to open file: %s", fileName);
int fd;
TEST_ASSERT_EQUAL(-1, UtilsFileOpen(fileName, O_RDONLY_FS, 0));
RUN_AND_PRINT_ELAPSED_TIME(fd, UtilsFileOpen(fileName, O_RDONLY_FS | O_CREAT_FS, 0));
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
int ret;
LOGI("begin to close file: %s", fileName);
RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileClose(fd));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileUtilsFileReadAndFileWrite(void)
{
const char *fileName = TEST_FILE_NAME;
for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS, 0);
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
unsigned int size = testFileSizeList[i];
LOGI("begin to write file [%s] for size: %d", fileName, size);
char *writeBuffer = GenerateTestingText(size);
TEST_ASSERT_NOT_NULL(writeBuffer);
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(UtilsFileWrite(fd, writeBuffer, size));
TEST_ASSERT_EQUAL(0, UtilsFileClose(fd));
fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS, 0);
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
unsigned int fileSize;
TEST_ASSERT_EQUAL(0, UtilsFileStat(fileName, &fileSize));
TEST_ASSERT_EQUAL(size, fileSize);
LOGI("begin to read file [%s] for size: %d", fileName, fileSize);
char *readBuffer = (char *)malloc(fileSize + 1);
TEST_ASSERT_NOT_NULL(readBuffer);
TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(UtilsFileRead(fd, readBuffer, fileSize));
TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
free(readBuffer);
free(writeBuffer);
TEST_ASSERT_EQUAL(0, UtilsFileClose(fd));
sleep(1);
}
}
static void TestHcFileUtilsFileSize(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to count file size");
unsigned int size;
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileStat(fileName, &size));
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
LOGI("the file size is: %d", size);
RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileDelete(fileName));
TEST_ASSERT_EQUAL(0, ret);
}
static void TestHcFileUtilsFileDelete(void)
{
const char *fileName = TEST_FILE_NAME;
LOGI("begin to remove file: %s", fileName);
int ret;
RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileDelete(fileName));
TEST_ASSERT_EQUAL(0, ret);
}
void TestHcFileUtilsFile(void)
{
LOGI("test opening and closing file");
TestHcFileUtilsFileOpenAndUtilsFileClose();
LOGI("test removing file");
TestHcFileUtilsFileDelete();
LOGI("test reading and writing file");
TestHcFileUtilsFileReadAndFileWrite();
LOGI("test counting file size");
TestHcFileUtilsFileSize();
}
#else // UTILS_FILE_IMPLEMENTED
void TestHcFileUtilsFile(void)
{
LOGE("no UTILS_FILE_IMPLEMENTED, do not test utils file!");
}
#endif // UTILS_FILE_IMPLEMENTED
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_FILE_UTILS_TEST_H
#define HC_FILE_UTILS_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcFileUtilsFile(void);
#ifdef __cplusplus
}
#endif
#endif // HC_FILE_UTILS_TEST_H
/*
* Copyright (C) 2021 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_mutex_test.h"
#include <hctest.h>
#include <pthread.h>
#include <securec.h>
#include <unistd.h>
#include "hc_thread_mutex_share.h"
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#define REQUIRED_MUTEX_COUNT 20
static void TestSpecifiedCount(int count)
{
pthread_mutex_t mutexes[REQUIRED_MUTEX_COUNT];
int res;
TEST_ASSERT_GREATER_THAN(0, count);
TEST_ASSERT_LESS_OR_EQUAL(REQUIRED_MUTEX_COUNT, count);
res = memset_s(mutexes, sizeof(mutexes), 0, sizeof(mutexes));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_init(&mutexes[i], NULL));
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_lock(&mutexes[i]));
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_unlock(&mutexes[i]));
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < count; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_destroy(&mutexes[i]));
TEST_ASSERT_EQUAL(0, res);
}
}
static void *MutexThreadRoutine(void *args)
{
pthread_mutex_t *mtx = (pthread_mutex_t *)args;
TEST_ASSERT_NOT_NULL(mtx);
unsigned long long timeConsumingResult;
int res;
int sleepSeconds = rand() % (WAIT_IN_MUTEX_SECONDS_MAX - WAIT_IN_MUTEX_SECONDS_MIN) + WAIT_IN_MUTEX_SECONDS_MIN;
LOGI("in thread begin phase 1 work");
RUN_AND_PRINT_ELAPSED_TIME(timeConsumingResult, TimeConsumingOperation());
LOGI("in thread begin wait for mutex");
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_lock(mtx));
TEST_ASSERT_EQUAL(0, res);
LOGI("in thread wait for mutex succeed");
LOGI("in thread begin wait for %d seconds", sleepSeconds);
sleep(sleepSeconds);
LOGI("in thread wait for %d seconds succeed", sleepSeconds);
LOGI("in thread begin phase 2 work");
RUN_AND_PRINT_ELAPSED_TIME(timeConsumingResult, TimeConsumingOperation());
LOGI("in thread begin release mutex");
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_unlock(mtx));
TEST_ASSERT_EQUAL(0, res);
LOGI("in thread release mutex successfully");
LOGI("in thread begin phase 3 work");
RUN_AND_PRINT_ELAPSED_TIME(timeConsumingResult, TimeConsumingOperation());
(void)(timeConsumingResult);
LOGI("in thread all work done");
return mtx;
}
static void TestMutexInThreads(void)
{
pthread_mutex_t mtx;
pthread_attr_t attr;
int res;
pthread_t threads[REQUIRED_THREAD_COUNT];
res = memset_s(&mtx, sizeof(mtx), 0, sizeof(mtx));
TEST_ASSERT_EQUAL(0, res);
res = memset_s(&attr, sizeof(attr), 0, sizeof(attr));
TEST_ASSERT_EQUAL(0, res);
res = memset_s(&threads, sizeof(threads), 0, sizeof(threads));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_mutex_init(&mtx, NULL));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_init(&attr));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setstacksize(&attr, MAX_THREAD_STACK_SIZE));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < REQUIRED_THREAD_COUNT; ++i) {
LOGI("test threads %d/%d", i, REQUIRED_THREAD_COUNT);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &attr, MutexThreadRoutine, &mtx));
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < REQUIRED_THREAD_COUNT; ++i) {
LOGI("test threads %d/%d", i, REQUIRED_THREAD_COUNT);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_join(threads[i], NULL));
LOGI("pthread_join result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_destroy(&attr));
TEST_ASSERT_EQUAL(0, res);
}
void TestHcMutex(void)
{
LOGI("test one mutex");
TestSpecifiedCount(1);
LOGI("test %d mutexes", REQUIRED_MUTEX_COUNT);
TestSpecifiedCount(REQUIRED_MUTEX_COUNT);
LOGI("test mutexes in threads");
TestMutexInThreads();
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_MUTEX_TEST_H
#define HC_MUTEX_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcMutex(void);
#ifdef __cplusplus
}
#endif
#endif // HC_MUTEX_TEST_H
/*
* Copyright (C) 2021 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 HC_THREAD_MUTEX_SHARE_H
#define HC_THREAD_MUTEX_SHARE_H
#define REQUIRED_THREAD_COUNT 5
#define MAX_THREAD_STACK_SIZE 4096
#define MIN_THREAD_STACK_SIZE 4096
enum {
WAIT_IN_MUTEX_SECONDS_MIN = 3,
WAIT_IN_MUTEX_SECONDS_MAX = 5,
};
#endif // HC_THREAD_MUTEX_SHARE_H
/*
* Copyright (C) 2021 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_thread_test.h"
#include <hctest.h>
#include <pthread.h>
#include <securec.h>
#include <stdlib.h>
#include <unistd.h>
#include "hc_thread_mutex_share.h"
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
#define REQUIRED_PTHREAD_ATTR_COUNT 20
#define TEST_STRING "hello"
typedef struct {
void *(*function)(void *);
size_t sz;
int i;
char *str;
} ThreadTestArgs;
static void *ThreadRountine(void *args)
{
TEST_ASSERT_NOT_NULL(args);
ThreadTestArgs *testArgs = (ThreadTestArgs *)args;
int sleepSeconds = rand() % (WAIT_IN_MUTEX_SECONDS_MAX - WAIT_IN_MUTEX_SECONDS_MIN) + WAIT_IN_MUTEX_SECONDS_MIN;
TEST_ASSERT_EQUAL((void *)ThreadRountine, (void *)testArgs->function);
TEST_ASSERT_EQUAL((size_t)MAX_THREAD_STACK_SIZE, testArgs->sz);
TEST_ASSERT_GREATER_OR_EQUAL(MIN_THREAD_STACK_SIZE, testArgs->i);
TEST_ASSERT_LESS_OR_EQUAL(MAX_THREAD_STACK_SIZE, testArgs->i);
TEST_ASSERT_EQUAL_STRING(TEST_STRING, testArgs->str);
LOGI("test thread begin wait for %d seconds", sleepSeconds);
sleep(sleepSeconds);
LOGI("test thread wait for %d seconds successfully", sleepSeconds);
LOGI("begin time consuming operation");
unsigned long long res;
RUN_AND_PRINT_ELAPSED_TIME(res, TimeConsumingOperation());
LOGI("end time consuming operation, res = %llu", res);
return args;
}
static int GetStackSize(void)
{
// use macro instead of enum to avoid error: division by zero [-Werror=div-by-zero]
#if (MAX_THREAD_STACK_SIZE == MIN_THREAD_STACK_SIZE)
return MIN_THREAD_STACK_SIZE;
#else
int stackSize = rand() % (MAX_THREAD_STACK_SIZE - MIN_THREAD_STACK_SIZE) + MIN_THREAD_STACK_SIZE;
LOGI("rand stack size = %d", stackSize);
return stackSize;
#endif
}
static void TestSpecifiedCount(int count, int detachState)
{
TEST_ASSERT_GREATER_THAN(0, count);
TEST_ASSERT_LESS_OR_EQUAL(REQUIRED_THREAD_COUNT, count);
TEST_ASSERT_TRUE_MESSAGE(detachState == PTHREAD_CREATE_JOINABLE || detachState == PTHREAD_CREATE_DETACHED,
"invalid detach state");
pthread_attr_t attr;
pthread_t threads[REQUIRED_THREAD_COUNT];
int res, stackSize;
static ThreadTestArgs args = {
.function = ThreadRountine,
.sz = MAX_THREAD_STACK_SIZE,
.i = -1,
.str = (char *)TEST_STRING,
};
res = memset_s(threads, sizeof(threads), 0, sizeof(threads));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_init(&attr));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setdetachstate(&attr, detachState));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < count; ++i) {
LOGI("test thread %d/%d", i, count);
stackSize = GetStackSize();
LOGI("rand stack size = %d", stackSize);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setstacksize(&attr, stackSize));
TEST_ASSERT_EQUAL(0, res);
args.i = stackSize;
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_create(&threads[i], &attr, ThreadRountine, &args));
TEST_ASSERT_EQUAL(0, res);
}
if (detachState == PTHREAD_CREATE_JOINABLE) {
for (int i = 0; i < count; ++i) {
LOGI("test thread %d/%d", i, count);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_join(threads[i], NULL));
LOGI("pthread_join result = %d", res);
TEST_ASSERT_EQUAL(0, res);
}
} else {
LOGI("detachState is PTHREAD_CREATE_DETACHED, do not test pthread_join");
}
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_destroy(&attr));
TEST_ASSERT_EQUAL(0, res);
}
static void TestPthreadAttr(int detachState)
{
TEST_ASSERT_TRUE_MESSAGE(detachState == PTHREAD_CREATE_JOINABLE || detachState == PTHREAD_CREATE_DETACHED,
"invalid detach state");
int res, stackSize;
pthread_attr_t attr[REQUIRED_PTHREAD_ATTR_COUNT];
res = memset_s(attr, sizeof(attr), 0, sizeof(attr));
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < REQUIRED_PTHREAD_ATTR_COUNT; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_init(&attr[i]));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setdetachstate(&attr[i], detachState));
TEST_ASSERT_EQUAL(0, res);
stackSize = GetStackSize();
LOGI("rand stack size = %d", stackSize);
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_setstacksize(&attr[i], stackSize));
TEST_ASSERT_EQUAL(0, res);
}
for (int i = 0; i < REQUIRED_PTHREAD_ATTR_COUNT; ++i) {
RUN_AND_PRINT_ELAPSED_TIME(res, pthread_attr_destroy(&attr[i]));
TEST_ASSERT_EQUAL(0, res);
}
}
static void TestState(int detachState)
{
TEST_ASSERT_TRUE_MESSAGE(detachState == PTHREAD_CREATE_JOINABLE || detachState == PTHREAD_CREATE_DETACHED,
"invalid detach state");
TestPthreadAttr(detachState);
LOGI("test one thread");
TestSpecifiedCount(1, detachState);
LOGI("test %d threads", REQUIRED_THREAD_COUNT);
TestSpecifiedCount(REQUIRED_THREAD_COUNT, detachState);
}
void TestHcThread(void)
{
#if TEST_PTHREAD_CREATE_DETACHED
LOGI("test state PTHREAD_CREATE_DETACHED");
TestState(PTHREAD_CREATE_DETACHED);
#else // TEST_PTHREAD_CREATE_DETACHED
LOGI("test state PTHREAD_CREATE_JOINABLE");
TestState(PTHREAD_CREATE_JOINABLE);
#endif // TEST_PTHREAD_CREATE_DETACHED
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_THREAD_TEST_H
#define HC_THREAD_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcThread(void);
#ifdef __cplusplus
}
#endif
#endif // HC_THREAD_TEST_H
/*
* Copyright (C) 2021 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_time_test.h"
// math.h must be included before hctest.h -> unity.h -> unity_internals.h
// otherwise isinf and isnan macro will be redefined
#include <math.h>
#include <hctest.h>
#include <securec.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "print_log.h"
#include "test_timer.h"
#ifdef __cplusplus
extern "C" {
#endif
enum {
SLEEP_SECONDS_MIN = 3,
SLEEP_SECONDS_MAX = 5,
TEST_TIMES = 5,
};
#define MILLISECONDS_DEVIATION 1.e2
static void TestTime(void)
{
struct timespec start, end;
int res, sleepSeconds = rand() % (SLEEP_SECONDS_MAX - SLEEP_SECONDS_MIN) + SLEEP_SECONDS_MIN;
res = memset_s(&start, sizeof(start), 0, sizeof(start));
TEST_ASSERT_EQUAL(0, res);
res = memset_s(&end, sizeof(end), 0, sizeof(end));
TEST_ASSERT_EQUAL(0, res);
LOGI("test correctness");
RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &start));
TEST_ASSERT_EQUAL(0, res);
LOGI("begin sleeping for %d seconds", sleepSeconds);
sleep(sleepSeconds);
RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &end));
TEST_ASSERT_EQUAL(0, res);
TEST_ASSERT_LESS_THAN(MILLISECONDS_DEVIATION, fabs(CalcTimeSpecDiff(start, end) - sleepSeconds * 1000.0f));
LOGI("test precision");
res = memset_s(&start, sizeof(start), 0, sizeof(start));
TEST_ASSERT_EQUAL(0, res);
res = memset_s(&end, sizeof(end), 0, sizeof(end));
TEST_ASSERT_EQUAL(0, res);
RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &start));
TEST_ASSERT_EQUAL(0, res);
LOGI("in testing precision");
RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &end));
TEST_ASSERT_EQUAL(0, res);
TEST_ASSERT_LESS_THAN(MILLISECONDS_DEVIATION, fabs(CalcTimeSpecDiff(start, end)));
}
static void TestPrintTime(void)
{
for (int i = 0; i < TEST_TIMES; ++i) {
struct timespec start;
int res, sleepSeconds;
res = memset_s(&start, sizeof(start), 0, sizeof(start));
TEST_ASSERT_EQUAL(0, res);
sleepSeconds = rand() % (SLEEP_SECONDS_MAX - SLEEP_SECONDS_MIN) + SLEEP_SECONDS_MIN;
res = clock_gettime(CLOCK_MONOTONIC, &start);
TEST_ASSERT_EQUAL(0, res);
double rawTime = (start.tv_nsec / 1000.0 / 1000.0 / 1000.0) + start.tv_sec;
time_t timeTime = (time_t)rawTime;
struct tm *pts = gmtime(&timeTime);
TEST_ASSERT_NOT_NULL(pts);
LOGI("time = %d-%d-%d %d:%d:%d, %d day of week, %d days in year, isdst = %d",
pts->tm_year,
pts->tm_mon + 1,
pts->tm_mday,
pts->tm_hour,
pts->tm_min,
pts->tm_sec,
pts->tm_wday + 1,
pts->tm_yday + 1,
pts->tm_isdst);
LOGI("begin sleeping for %d seconds", sleepSeconds);
sleep(sleepSeconds);
}
}
void TestHcTime(void)
{
for (int i = 0; i < TEST_TIMES; ++i) {
TestTime();
}
TestPrintTime();
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 HC_TIME_TEST_H
#define HC_TIME_TEST_H
#ifdef __cplusplus
extern "C" {
#endif
void TestHcTime(void);
#ifdef __cplusplus
}
#endif
#endif // HC_TIME_TEST_H
\ No newline at end of file
/*
* Copyright (C) 2021 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 "print_log.h"
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LINE_BYTE_COUNT 16
#define FORMAT_PRINT_BUFFER LOGD
#define CHAR_ZERO '0'
#define CHAR_A 'A'
#define NUM_ZERO 0
#define NUM_ONE 1
#define NUM_TWO 2
#define NUM_THREE 3
#define NUM_FOUR 4
#define NUM_NINE 9
#define NUM_TEN 10
#define NUM_FIFTEEN 15
#define HALF_BYTE_MASK 0x0F
#define NUM_PRINTABLE_BEGIN 0x20
#define NUM_PRINTABLE_END 0x7E
#define BITS_PER_BYTE 8
#define BITS_PER_HEX 4
static char Int2char(uint8_t c)
{
// c >= NUM_ZERO comparison is always true due to limited range of data type
if (c <= NUM_NINE) {
return (char)(c + CHAR_ZERO);
} else if (c >= NUM_TEN && c <= NUM_FIFTEEN) {
return (char)(c - NUM_TEN + CHAR_A);
} else {
return 0;
}
}
static void PrintLine(const uint8_t *ptr, int32_t len, uint32_t offset)
{
int i;
char x1;
char x2;
char str[NUM_NINE + LINE_BYTE_COUNT * NUM_FOUR + NUM_ONE] = {0};
char *out = str + NUM_NINE;
uint32_t t;
for (i = 0; i < BITS_PER_BYTE; i++) {
t = offset << (i * BITS_PER_HEX);
t = t >> ((BITS_PER_BYTE - 1) * BITS_PER_HEX);
str[i] = Int2char(t);
}
str[BITS_PER_BYTE] = ' ';
for (i = 0; i < len; i++) {
uint8_t c = ptr[i];
x1 = Int2char(c >> NUM_FOUR);
x2 = Int2char(c & HALF_BYTE_MASK);
out[i * NUM_THREE] = x1;
out[i * NUM_THREE + NUM_ONE] = x2;
out[i * NUM_THREE + NUM_TWO] = ' ';
}
for (i = len; i < LINE_BYTE_COUNT; i++) {
out[i * NUM_THREE] = out[i * NUM_THREE + NUM_ONE] = out[i * NUM_THREE + NUM_TWO] = ' ';
}
for (i = 0; i < len; i++) {
if (ptr[i] >= NUM_PRINTABLE_BEGIN && ptr[i] <= NUM_PRINTABLE_END) {
out[LINE_BYTE_COUNT * NUM_THREE + i] = ptr[i];
} else {
out[LINE_BYTE_COUNT * NUM_THREE + i] = '.';
}
}
FORMAT_PRINT_BUFFER("%s", str);
}
void PrintBuffer(const char *tag, const uint8_t *ptr, int32_t len)
{
int i = 0;
const uint8_t *linePtr = ptr;
uint32_t offset = 0;
FORMAT_PRINT_BUFFER("PrintBuffer tag = %s, len = %d, 0x%x", tag, len, len);
while (i < len) {
int currentLineByteCount = (int)(len - i);
if (currentLineByteCount >= LINE_BYTE_COUNT) {
PrintLine(linePtr, LINE_BYTE_COUNT, offset);
i += LINE_BYTE_COUNT;
} else {
PrintLine(linePtr, currentLineByteCount, offset);
i += currentLineByteCount;
}
linePtr += LINE_BYTE_COUNT;
offset += LINE_BYTE_COUNT;
}
}
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2021 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 PRINT_LOG_H
#define PRINT_LOG_H
#include <stdint.h>
#include <stdio.h>
#define LOGI(fmt, ...) printf("[I][%s][%d]" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOGD(fmt, ...) printf("[D][%s][%d]" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOGE(fmt, ...) printf("[E][%s][%d]" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
void PrintBuffer(const char *tag, const uint8_t *ptr, int32_t len);
#ifdef __cplusplus
}
#endif
#endif // PRINT_LOG_H
\ No newline at end of file
此差异已折叠。
/*
* Copyright (C) 2021 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 "test_timer.h"
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
// 3861 platform limits that the running time cannot exceed 16 seconds.
// If the running time exceeds 16 seconds, crash occurs.
// The following comments are from at91SAM9_wdt.c in directory
// device/hisilicon/hispark_pegasus/sdk_liteos/third_party/u-boot-v2019.07/u-boot-v2019.07/drivers/watchdog
/*
* All counting occurs at SLOW_CLOCK / 128 = 256 Hz
*
* Since WDV is a 12-bit counter, the maximum period is
* 4096 / 256 = 16 seconds.
*/
enum {
MAX_FIBONACCI_OPTION = 0xFFFFFF,
MIN_FIBONACCI_OPTION = 0xFFFFF,
};
double CalcTimeSpecDiff(struct timespec before, struct timespec after)
{
double mseconds = (((double)after.tv_sec - (double)before.tv_sec) * 1000000.0f +
((double)after.tv_nsec - (double)before.tv_nsec) / 1000.0f) / 1000.0f;
return mseconds;
}
unsigned long long TimeConsumingOperation(void)
{
unsigned long long i, n, t1 = 0, t2 = 1, nextTerm = 0;
n = rand() % (MAX_FIBONACCI_OPTION - MIN_FIBONACCI_OPTION) + MIN_FIBONACCI_OPTION;
for (i = 1; i <= n; ++i) {
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return nextTerm;
}
#ifdef __cplusplus
}
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册