1 /*
2 * Copyright (C) 2022 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 "huks_attest_test.h"
19 #include "huks_attest_test_common.h"
20
21 using namespace testing::ext;
22 namespace Unittest::AttestKey {
23 static struct OH_Huks_Blob g_secInfo = { sizeof(SEC_INFO_DATA), (uint8_t *)SEC_INFO_DATA };
24 static struct OH_Huks_Blob g_challenge = { sizeof(CHALLENGE_DATA), (uint8_t *)CHALLENGE_DATA };
25 static struct OH_Huks_Blob g_version = { sizeof(VERSION_DATA), (uint8_t *)VERSION_DATA };
26 bool useSoftware = true;
27
28 class HuksAttestKeyNoIdsTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void HuksAttestKeyNoIdsTest::SetUpTestCase(void)
40 {
41 useSoftware = checkUseSoftware();
42 }
43
TearDownTestCase(void)44 void HuksAttestKeyNoIdsTest::TearDownTestCase(void)
45 {
46 }
47
SetUp()48 void HuksAttestKeyNoIdsTest::SetUp()
49 {
50 }
51
TearDown()52 void HuksAttestKeyNoIdsTest::TearDown()
53 {
54 }
55
56 static const struct OH_Huks_Blob g_keyAlias = { sizeof(ALIAS), (uint8_t *)ALIAS };
57
58 static const struct OH_Huks_Param g_commonParams[] = {
59 { .tag = OH_HUKS_TAG_ATTESTATION_ID_SEC_LEVEL_INFO, .blob = g_secInfo },
60 { .tag = OH_HUKS_TAG_ATTESTATION_CHALLENGE, .blob = g_challenge },
61 { .tag = OH_HUKS_TAG_ATTESTATION_ID_VERSION_INFO, .blob = g_version },
62 { .tag = OH_HUKS_TAG_ATTESTATION_ID_ALIAS, .blob = g_keyAlias },
63 };
64
65 /**
66 * @tc.name: HuksAttestKeyNoIdsTest.Security_HUKS_NAPI_Attest_0100
67 * @tc.desc: attest with right params and validate success.
68 * @tc.type: FUNC
69 * @tc.require: issueI5NY0L
70 */
71 HWTEST_F(HuksAttestKeyNoIdsTest, Security_HUKS_NAPI_Attest_0100, TestSize.Level0)
72 {
73 if (useSoftware)
74 {
75 OH_Huks_Result ret = TestGenerateKey(&g_keyAlias);
76 ASSERT_TRUE(ret.errorCode == (int32_t)OH_HUKS_SUCCESS);
77 struct OH_Huks_ParamSet *paramSet = NULL;
78 GenerateParamSet(¶mSet, g_commonParams, sizeof(g_commonParams) / sizeof(g_commonParams[0]));
79 OH_Huks_CertChain *certChain = NULL;
80 const struct HksTestCertChain certParam = { true, true, true, g_size };
81 (void)ConstructDataToCertChain(&certChain, &certParam);
82 ret = OH_Huks_AttestKeyItem(&g_keyAlias, paramSet, certChain);
83 ASSERT_TRUE(ret.errorCode == (int32_t)OH_HUKS_SUCCESS);
84 ret = ValidateCertChainTest(certChain, g_commonParams, NON_IDS_PARAM);
85 ASSERT_TRUE(ret.errorCode == (int32_t)OH_HUKS_SUCCESS);
86 FreeCertChain(&certChain, certChain->certsCount);
87 certChain = NULL;
88
89 OH_Huks_FreeParamSet(¶mSet);
90
91 OH_Huks_Result ret1 = OH_Huks_DeleteKeyItem(&g_keyAlias, NULL);
92 ASSERT_TRUE(ret1.errorCode == (int32_t)OH_HUKS_SUCCESS);
93 }
94 ASSERT_TRUE(0 == 0);
95 }
96 }
97