1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "hks_exist_test.h"
19
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25 #include "hks_type.h"
26
27 using namespace testing::ext;
28 namespace {
29 #ifndef _CUT_AUTHENTICATE_
30 class HksExistTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33
34 static void TearDownTestCase(void);
35
36 void SetUp();
37
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void HksExistTest::SetUpTestCase(void)
42 {
43 }
44
TearDownTestCase(void)45 void HksExistTest::TearDownTestCase(void)
46 {
47 }
48
SetUp()49 void HksExistTest::SetUp()
50 {
51 EXPECT_EQ(HksInitialize(), 0);
52 }
53
TearDown()54 void HksExistTest::TearDown()
55 {
56 }
57
58 const struct HksTestKeyExistParams g_testKeyExistParams[] = {
59 /* normal case */
60 { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } },
61 };
62
63 /**
64 * @tc.name: HksExistTest.HksExistTest001
65 * @tc.desc: The static function will return true;
66 * @tc.type: FUNC
67 */
HWTEST_F(HksExistTest, HksExistTest001, TestSize.Level1)68 HWTEST_F(HksExistTest, HksExistTest001, TestSize.Level1)
69 {
70 int32_t ret;
71 struct HksBlob *keyAlias = NULL;
72 if (g_testKeyExistParams[0].isGenKey) {
73 HKS_TEST_ASSERT(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0);
74 ret = HksKeyExistRun(keyAlias, 1);
75 HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult);
76 HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == HKS_SUCCESS);
77 } else {
78 ret = TestConstuctBlob(&keyAlias,
79 g_testKeyExistParams[0].keyAliasParams.blobExist,
80 g_testKeyExistParams[0].keyAliasParams.blobSize,
81 g_testKeyExistParams[0].keyAliasParams.blobDataExist,
82 g_testKeyExistParams[0].keyAliasParams.blobDataSize);
83 HKS_TEST_ASSERT(ret == 0);
84 ret = HksKeyExistRun(keyAlias, 1);
85 if (ret != g_testKeyExistParams[0].expectResult) {
86 HKS_TEST_LOG_I("HksKeyExistRun 2 failed, ret[%u] = %d", g_testKeyExistParams[0].testId, ret);
87 }
88 HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult);
89 }
90 TestFreeBlob(&keyAlias);
91 ASSERT_TRUE(ret == 0);
92 }
93 #endif /* _CUT_AUTHENTICATE_ */
94 }