diff --git a/security/deviceauth/deviceauth_basic_deps/BUILD.gn b/security/deviceauth/deviceauth_basic_deps/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b17894f8eb173f46bba92e30c651cfb441f761e0 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/BUILD.gn @@ -0,0 +1,88 @@ +# 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/build/suite.gni") + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + ] +} + +DEVICEAUTH_BASIC_DEPS_CPP_DIR = + "//test/xts/acts/security/deviceauth/deviceauth_basic_deps" + +DEVICEAUTH_BASIC_DEPS_CPP_SOURCE = [ + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/print_log.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/test_timer.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_mutex_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_f_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_thread_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_dev_info_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_condition_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_utils_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_iot_flash_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_time_test.cpp", + "${DEVICEAUTH_BASIC_DEPS_CPP_DIR}/hc_file_common.cpp", +] + +ohos_moduletest_suite("DeviceAuthBasicDepsTest") { + configs = [ ":module_private_config" ] + + cflags_cc = [ + "-DHILOG_ENABLE", + "-Werror", + ] + + sources = [ "device_auth_test.cpp" ] + + sources += DEVICEAUTH_BASIC_DEPS_CPP_SOURCE + + include_dirs = [ + "//third_party/googletest/googlemock/include", + + # hctest.h compatible with c++ + "//test/xts/acts/security/deviceauth/deviceauth_basic_deps", + + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + "//base/security/deviceauth/deps_adapter/os_adapter/interfaces/liteos", + "//base/startup/syspara_lite/interfaces/kits", + "//test/xts/acts/security_lite/deviceauth_basic_deps", + ] + + include_dirs += DEVICEAUTH_BASIC_DEPS_INC + + deps = [ + "//base/security/deviceauth/deps_adapter:deviceauth_hal_linux", + "//third_party/bounds_checking_function:libsec_static", + "//utils/native/base:utils", + ] + + cflags = [ "-Werror" ] + + defines = [ + "MKDIR_IMPLEMENTED=1", + "STAT_IMPLEMENTED=1", + "ACCESS_IMPLEMENTED=1", + "F_API_IMPLEMENTED=1", + "FLASH_START_ADDRESS_HICHAIN=0", + "UTILS_FILE_IMPLEMENTED=0", + "TEST_PTHREAD_CREATE_DETACHED=1", + "TEST_HC_FILE_OPEN_SERIES=1", + ] +} diff --git a/security/deviceauth/deviceauth_basic_deps/Test.json b/security/deviceauth/deviceauth_basic_deps/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..a27e19696a5393b809a3a5d116b10dedc4c7907c --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/Test.json @@ -0,0 +1,18 @@ +{ + "kits": [ + { + "push": [ + "DeviceAuthBasicDepsTest->/data/local/tmp/DeviceAuthBasicDepsTest" + ], + "type": "PushKit" + } + ], + "driver": { + "native-test-timeout": "120000", + "type": "CppTest", + "module-name": "DeviceAuthBasicDepsTest", + "runtime-hint": "1s", + "native-test-device-path": "/data/local/tmp" + }, + "description": "Configuration for DeviceAuthBasicDepsTest Tests" +} diff --git a/security/deviceauth/deviceauth_basic_deps/device_auth_test.cpp b/security/deviceauth/deviceauth_basic_deps/device_auth_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c48b48b7ed26cf6ee6e6d97f3887c1b307f9d7fd --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/device_auth_test.cpp @@ -0,0 +1,189 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#include "hc_condition_test.h" +#include "hc_dev_info_test.h" +#include "hc_file_f_test.h" +#include "hc_file_iot_flash_test.h" +#include "hc_file_test.h" +#include "hc_file_utils_test.h" +#include "hc_mutex_test.h" +#include "hc_thread_test.h" +#include "hc_time_test.h" +#include "print_log.h" + +using namespace testing::ext; +namespace { +class DeviceAuthTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void DeviceAuthTest::SetUpTestCase(void) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +void DeviceAuthTest::TearDownTestCase(void) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +void DeviceAuthTest::SetUp() +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + (void)(srand(time(nullptr))); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +void DeviceAuthTest::TearDown() +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcMutex + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcMutex, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcMutex(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcThread + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcThread, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcThread(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcGetUdid + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcGetUdid, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcGetUdid(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcFile + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcFile, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFile(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcFileFApi + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcFileFApi, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileFApi(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcFileUtilsFile + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcFileUtilsFile, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileUtilsFile(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseSemCondition + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseSemCondition, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcSemCondition(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCasePthreadCondition + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCasePthreadCondition, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcPthreadCondition(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcFileIotFlash + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcFileIotFlash, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileIotFlash(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthTest.TestCaseHcTime + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(DeviceAuthTest, TestCaseHcTime, TestSize.Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcTime(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} +} // namespace diff --git a/security/deviceauth/deviceauth_basic_deps/hc_condition_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_condition_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..be8b5aae869e2031804bef7faa2e1fe7a86b6bc8 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_condition_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_condition_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_dev_info_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_dev_info_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..41ce0b58ba300edf0920700eb83e86c6f41bced7 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_dev_info_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_dev_info_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_file_common.cpp b/security/deviceauth/deviceauth_basic_deps/hc_file_common.cpp new file mode 120000 index 0000000000000000000000000000000000000000..2b0664acd0e87f7e646eb825ffd790c8fe01c180 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_file_common.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_file_common.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_file_f_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_file_f_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..91f342969689570fa8608d2a62cb78efa0a34719 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_file_f_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_file_f_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_file_iot_flash_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_file_iot_flash_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..f7cef42ab5e20c18be33a79bb74ccbb5e1b232db --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_file_iot_flash_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_file_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_file_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..e789e922d32fbee8a10bc7ecf93408a2b077fbbb --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_file_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_file_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_file_utils_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_file_utils_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..2ab4711c899c8ab7760e1e9c05d5e1d94e115edb --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_file_utils_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_file_utils_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_mutex_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_mutex_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..0bae17762e3bc54bb9f73fcf63bfb58c6245670f --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_mutex_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_mutex_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_thread_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_thread_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..c3aabc1455a6832622fa2627093a99080746f83f --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_thread_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_thread_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hc_time_test.cpp b/security/deviceauth/deviceauth_basic_deps/hc_time_test.cpp new file mode 120000 index 0000000000000000000000000000000000000000..99de63286247bd485f3e0bcd525e561fc76be860 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hc_time_test.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/hc_time_test.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/hctest.h b/security/deviceauth/deviceauth_basic_deps/hctest.h new file mode 100644 index 0000000000000000000000000000000000000000..abb242fa15ab9cb716e15348815af1c0a9cdb3e9 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/hctest.h @@ -0,0 +1,41 @@ +/* + * 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_COMPATIBLE_GTEST_TEST_H +#define HC_COMPATIBLE_GTEST_TEST_H + +#include +#include +#include +#include + +#define TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements) \ + EXPECT_THAT(std::vector((actual), (actual) + (num_elements)), \ + ::testing::ElementsAreArray((expected), (num_elements))) +#define TEST_ASSERT_EQUAL(expected, actual) EXPECT_EQ((actual), (expected)) +#define TEST_ASSERT_EQUAL_MESSAGE(expected, actual, message) EXPECT_EQ((actual), (expected)) << (message) +#define TEST_ASSERT_EQUAL_STRING(expected, actual) EXPECT_STREQ((actual), (expected)) +#define TEST_ASSERT_GREATER_OR_EQUAL(threshold, actual) EXPECT_GE((actual), (threshold)) +#define TEST_ASSERT_GREATER_THAN(threshold, actual) EXPECT_GT((actual), (threshold)) +#define TEST_ASSERT_LESS_OR_EQUAL(threshold, actual) EXPECT_LE((actual), (threshold)) +#define TEST_ASSERT_LESS_THAN(threshold, actual) EXPECT_LT((actual), (threshold)) +#define TEST_ASSERT_MESSAGE(condition, message) EXPECT_TRUE((condition)) << (message) +#define TEST_ASSERT_NOT_EQUAL(expected, actual) EXPECT_NE((actual), (expected)) +#define TEST_ASSERT_NOT_EQUAL_MESSAGE(expected, actual, message) EXPECT_NE((actual), (expected)) << (message) +#define TEST_ASSERT_NOT_NULL(pointer) EXPECT_NE((pointer), nullptr) +#define TEST_ASSERT_TRUE(condition) EXPECT_TRUE((condition)) +#define TEST_ASSERT_TRUE_MESSAGE(condition, message) EXPECT_TRUE((condition)) << (message) + +#endif // HC_COMPATIBLE_GTEST_TEST_H diff --git a/security/deviceauth/deviceauth_basic_deps/print_log.cpp b/security/deviceauth/deviceauth_basic_deps/print_log.cpp new file mode 120000 index 0000000000000000000000000000000000000000..df93622d9f66557d75f291a01bc3ddbd73851c05 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/print_log.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/print_log.c \ No newline at end of file diff --git a/security/deviceauth/deviceauth_basic_deps/test_timer.cpp b/security/deviceauth/deviceauth_basic_deps/test_timer.cpp new file mode 120000 index 0000000000000000000000000000000000000000..321f266c7cdb6ac80ab2a662b0fc284042a09662 --- /dev/null +++ b/security/deviceauth/deviceauth_basic_deps/test_timer.cpp @@ -0,0 +1 @@ +../../../security_lite/deviceauth_basic_deps/test_timer.c \ No newline at end of file diff --git a/security_lite/deviceauth_basic_deps/BUILD.gn b/security_lite/deviceauth_basic_deps/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..08c306cf6661a106195efa71045434449f575a43 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/BUILD.gn @@ -0,0 +1,93 @@ +# 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 = [ + "//utils/native/base/include", + "//utils/native/lite/include", + "//test/xts/tools/lite/hctest/include", + "//third_party/unity/src", + "//foundation/distributedschedule/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", + ] + + if (defined(ohos_lite)) { + if (ohos_kernel_type == "liteos_m") { + # liteos m + include_dirs += [ "//base/startup/syspara_lite/interfaces/kits" ] + deps += + [ "//base/startup/syspara_lite/frameworks/parameter/src:sysparam" ] + 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", + ] + defines += [ + "FLASH_START_ADDRESS_HICHAIN=0x1F6000", + "TEST_HC_FILE_OPEN_SERIES=0", + ] + } + if (board_name == "hispark_pegasus") { + # 3861 + } + } else { + #liteos a or L1 linux + include_dirs += [ "//base/startup/syspara_lite/interfaces/kits" ] + deps += + [ "//base/startup/syspara_lite/frameworks/parameter/src:sysparam" ] + } + } else { + # linux + include_dirs += [ + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", + + "//base/security/deviceauth/deps_adapter/os_adapter/interfaces/linux", + ] + deps += [ + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + ] + } +} diff --git a/security_lite/deviceauth_basic_deps/Test.tmpl b/security_lite/deviceauth_basic_deps/Test.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..be8407e1286041a5af2bdc75a15633337376eb9d --- /dev/null +++ b/security_lite/deviceauth_basic_deps/Test.tmpl @@ -0,0 +1,19 @@ +{ + "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 diff --git a/security_lite/deviceauth_basic_deps/deviceauth.gni b/security_lite/deviceauth_basic_deps/deviceauth.gni new file mode 100644 index 0000000000000000000000000000000000000000..b150059a0d901fd3f2273ec450fa6f888ec510f7 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/deviceauth.gni @@ -0,0 +1,35 @@ +# 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_INC = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "//utils/native/lite/include", # utils_file.h +] diff --git a/security_lite/deviceauth_basic_deps/hc_condition_test.c b/security_lite/deviceauth_basic_deps/hc_condition_test.c new file mode 100644 index 0000000000000000000000000000000000000000..5d431ac65adb09e1041ae90f83df8c6c8e16fe73 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_condition_test.c @@ -0,0 +1,312 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_condition_test.h b/security_lite/deviceauth_basic_deps/hc_condition_test.h new file mode 100644 index 0000000000000000000000000000000000000000..58a46f7dd5b14bcc2a5e61af9664f04e0a983536 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_condition_test.h @@ -0,0 +1,36 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_dev_info_test.c b/security_lite/deviceauth_basic_deps/hc_dev_info_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b513b26502326d016fa9be3fd23aec8e4351bff7 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_dev_info_test.c @@ -0,0 +1,86 @@ +/* + * 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 +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_dev_info_test.h b/security_lite/deviceauth_basic_deps/hc_dev_info_test.h new file mode 100644 index 0000000000000000000000000000000000000000..4810276931255ab9f3fecb46616e571873bd5861 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_dev_info_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_common.c b/security_lite/deviceauth_basic_deps/hc_file_common.c new file mode 100644 index 0000000000000000000000000000000000000000..2ea1dd9151fa94f913f4c527b88947335984e6be --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_common.c @@ -0,0 +1,41 @@ +/* + * 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 + +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_common.h b/security_lite/deviceauth_basic_deps/hc_file_common.h new file mode 100644 index 0000000000000000000000000000000000000000..f0f63855c2bb035928b765044ee88bbc76286c0b --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_common.h @@ -0,0 +1,46 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_f_test.c b/security_lite/deviceauth_basic_deps/hc_file_f_test.c new file mode 100644 index 0000000000000000000000000000000000000000..48845f199967ce4694ca903f46d6da428360599f --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_f_test.c @@ -0,0 +1,237 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_f_test.h b/security_lite/deviceauth_basic_deps/hc_file_f_test.h new file mode 100644 index 0000000000000000000000000000000000000000..5303acdd6459a9f0178150aba83faff30c4948f5 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_f_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.c b/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.c new file mode 100644 index 0000000000000000000000000000000000000000..7b82d9c62217298d9ac3fc0567561f090e1be191 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.c @@ -0,0 +1,277 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.h b/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.h new file mode 100644 index 0000000000000000000000000000000000000000..214ee34ae3bbdd9370dacefcf08d236cc4efdcb1 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_iot_flash_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_test.c b/security_lite/deviceauth_basic_deps/hc_file_test.c new file mode 100644 index 0000000000000000000000000000000000000000..a88597567ad76f76684e44ade696d469f6488b82 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_test.c @@ -0,0 +1,212 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_test.h b/security_lite/deviceauth_basic_deps/hc_file_test.h new file mode 100644 index 0000000000000000000000000000000000000000..603027385455d379e58a943d8e0af238a5e87844 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_utils_test.c b/security_lite/deviceauth_basic_deps/hc_file_utils_test.c new file mode 100644 index 0000000000000000000000000000000000000000..d75ff77fe6ddeaee037d441c9cdd1878149f1897 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_utils_test.c @@ -0,0 +1,134 @@ +/* + * 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 +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_file_utils_test.h b/security_lite/deviceauth_basic_deps/hc_file_utils_test.h new file mode 100644 index 0000000000000000000000000000000000000000..1a10f6babef13c7cdde6a105553387f294c91ac2 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_file_utils_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_mutex_test.c b/security_lite/deviceauth_basic_deps/hc_mutex_test.c new file mode 100644 index 0000000000000000000000000000000000000000..1a45ab3e8d66457020cf24e189db6dac7a481275 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_mutex_test.c @@ -0,0 +1,158 @@ +/* + * 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 +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_mutex_test.h b/security_lite/deviceauth_basic_deps/hc_mutex_test.h new file mode 100644 index 0000000000000000000000000000000000000000..990b1124653abde1d06b9d9f80e782a66591b8dd --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_mutex_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_thread_mutex_share.h b/security_lite/deviceauth_basic_deps/hc_thread_mutex_share.h new file mode 100644 index 0000000000000000000000000000000000000000..c51e6759721dd0c8db6ba9619cb6869efa589d81 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_thread_mutex_share.h @@ -0,0 +1,28 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_thread_test.c b/security_lite/deviceauth_basic_deps/hc_thread_test.c new file mode 100644 index 0000000000000000000000000000000000000000..c5091a94da72c8f356c771b5ec9e23f0ac785e2b --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_thread_test.c @@ -0,0 +1,183 @@ +/* + * 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 +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_thread_test.h b/security_lite/deviceauth_basic_deps/hc_thread_test.h new file mode 100644 index 0000000000000000000000000000000000000000..b1dd6e1862eb58995b31e79f873d44839ac620ee --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_thread_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/hc_time_test.c b/security_lite/deviceauth_basic_deps/hc_time_test.c new file mode 100644 index 0000000000000000000000000000000000000000..dd040d747d399e701a77e88edd94dbf449e0621a --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_time_test.c @@ -0,0 +1,126 @@ +/* + * 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 + +#include +#include +#include +#include +#include +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/hc_time_test.h b/security_lite/deviceauth_basic_deps/hc_time_test.h new file mode 100644 index 0000000000000000000000000000000000000000..c89a7352e995201076f559d2405ee3a5cd588ee1 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/hc_time_test.h @@ -0,0 +1,29 @@ +/* + * 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 diff --git a/security_lite/deviceauth_basic_deps/print_log.c b/security_lite/deviceauth_basic_deps/print_log.c new file mode 100644 index 0000000000000000000000000000000000000000..22e6362a5a7a46ac95faf3addef746fa62d67ae7 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/print_log.c @@ -0,0 +1,115 @@ +/* + * 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 +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/print_log.h b/security_lite/deviceauth_basic_deps/print_log.h new file mode 100644 index 0000000000000000000000000000000000000000..518c1f1e87951d519248efb7470ffc0a095d8ee2 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/print_log.h @@ -0,0 +1,36 @@ +/* + * 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 +#include + +#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 diff --git a/security_lite/deviceauth_basic_deps/test.c b/security_lite/deviceauth_basic_deps/test.c new file mode 100644 index 0000000000000000000000000000000000000000..f0f9d618455a551f0e3abefcfad9a7f789b5975e --- /dev/null +++ b/security_lite/deviceauth_basic_deps/test.c @@ -0,0 +1,172 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include "hc_condition_test.h" +#include "hc_dev_info_test.h" +#include "hc_file_f_test.h" +#include "hc_file_iot_flash_test.h" +#include "hc_file_test.h" +#include "hc_file_utils_test.h" +#include "hc_mutex_test.h" +#include "hc_thread_test.h" +#include "hc_time_test.h" +#include "print_log.h" + +LITE_TEST_SUIT(security, deviceAuth, DeviceAuthBasicDepsTestSuite) + +static BOOL DeviceAuthBasicDepsTestSuiteSetUp(void) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + (void)(srand(time(NULL))); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + return TRUE; +} + +static BOOL DeviceAuthBasicDepsTestSuiteTearDown(void) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + return TRUE; +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcMutex + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcMutex, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcMutex(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcThread + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcThread, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcThread(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcGetUdid + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcGetUdid, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcGetUdid(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcFile + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcFile, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFile(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcFileFApi + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcFileFApi, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileFApi(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcFileUtilsFile + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcFileUtilsFile, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileUtilsFile(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseSemCondition + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseSemCondition, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcSemCondition(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCasePthreadCondition + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCasePthreadCondition, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcPthreadCondition(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcFileIotFlash + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcFileIotFlash, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcFileIotFlash(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +/** + * @tc.name: DeviceAuthBasicDepsTestSuite.TestCaseHcTime + * @tc.desc: + * @tc.type: FUNC + */ +LITE_TEST_CASE(DeviceAuthBasicDepsTestSuite, TestCaseHcTime, Function | SmallTest | Level1) +{ + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); + TestHcTime(); + LOGI("++++++++++++++++++++++++++++++++++++++++\n"); +} + +RUN_TEST_SUITE(DeviceAuthBasicDepsTestSuite) diff --git a/security_lite/deviceauth_basic_deps/test_timer.c b/security_lite/deviceauth_basic_deps/test_timer.c new file mode 100644 index 0000000000000000000000000000000000000000..91bcfde09a0ed74af5df4d908734b6f9010bf140 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/test_timer.c @@ -0,0 +1,62 @@ +/* + * 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 + +#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 diff --git a/security_lite/deviceauth_basic_deps/test_timer.h b/security_lite/deviceauth_basic_deps/test_timer.h new file mode 100644 index 0000000000000000000000000000000000000000..84f9a2da9f8c793a6a27caa9840cad74b1d54a46 --- /dev/null +++ b/security_lite/deviceauth_basic_deps/test_timer.h @@ -0,0 +1,58 @@ +/* + * 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 TEST_TIMER_H +#define TEST_TIMER_H + +#include "print_log.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +double CalcTimeSpecDiff(struct timespec before, struct timespec after); + +unsigned long long TimeConsumingOperation(void); + +#ifdef __cplusplus +} +#endif + +#define RUN_AND_PRINT_ELAPSED_TIME(out_result, operation) \ + do { \ + struct timespec __before, __after; \ + int __beforeRes = clock_gettime(CLOCK_MONOTONIC, &__before); \ + out_result = operation; \ + int __afterRes = clock_gettime(CLOCK_MONOTONIC, &__after); \ + TEST_ASSERT_TRUE(__beforeRes == 0 && __afterRes == 0); \ + double __mseconds = CalcTimeSpecDiff(__before, __after); \ + LOGI("operation %s took %f ms", #operation, __mseconds); \ + } while (0) + +#define RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(operation) \ + do { \ + struct timespec __before, __after; \ + int __beforeRes = clock_gettime(CLOCK_MONOTONIC, &__before); \ + (void)(operation); \ + int __afterRes = clock_gettime(CLOCK_MONOTONIC, &__after); \ + TEST_ASSERT_TRUE(__beforeRes == 0 && __afterRes == 0); \ + double __mseconds = CalcTimeSpecDiff(__before, __after); \ + LOGI("operation %s took %f ms", #operation, __mseconds); \ + } while (0) + +#endif // TEST_TIMER_H