• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_safe_cipher_key_test.h"
19 
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_curve25519.h"
23 #include "hks_test_file_operator.h"
24 #include "hks_test_mem.h"
25 
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 namespace {
30 #ifndef _CUT_AUTHENTICATE_
31 class HksSafeCipherKeyTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34 
35     static void TearDownTestCase(void);
36 
37     void SetUp();
38 
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void HksSafeCipherKeyTest::SetUpTestCase(void)
43 {
44 }
45 
TearDownTestCase(void)46 void HksSafeCipherKeyTest::TearDownTestCase(void)
47 {
48 }
49 
SetUp()50 void HksSafeCipherKeyTest::SetUp()
51 {
52     EXPECT_EQ(HksInitialize(), 0);
53 }
54 
TearDown()55 void HksSafeCipherKeyTest::TearDown()
56 {
57 }
58 
59 const char *g_storePath = "/storage/maindata/hks_client/key";
60 const char *g_testEd25519 = "test_ed25519";
61 
CompareTwoKey(const struct HksBlob * keyAliasOne,const struct HksBlob * keyAliasTwo)62 static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo)
63 {
64     uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data);
65     uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
66     if (bufOne == NULL) {
67         return HKS_ERROR_MALLOC_FAIL;
68     }
69     uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne);
70 
71     uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data);
72     uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo);
73     if (bufTwo == NULL) {
74         HksTestFree(bufOne);
75         return HKS_ERROR_MALLOC_FAIL;
76     }
77     sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne);
78     int32_t ret = memcmp(bufOne, bufTwo, sizeRead);
79     HksTestFree(bufOne);
80     HksTestFree(bufTwo);
81     return ret;
82 }
83 
84 /**
85  * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
86  * @tc.desc: The static function will return true;
87  * @tc.type: FUNC
88  */
89 HWTEST_F(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, TestSize.Level1)
90 {
91     struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
92     int32_t ret = TestGenerateEd25519Key(ed25519Alias);
93     uint8_t pubKey[32] = {0};
94     uint32_t pubKeyLen = 32;
95     struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
96     ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
97     EXPECT_EQ(ret, 0);
98     ret = HksDeleteKey(&ed25519Alias, NULL);
99     EXPECT_EQ(ret, 0);
100 
101     struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
102     ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
103     EXPECT_EQ(ret, 0);
104 
105     struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
106     ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
107     EXPECT_EQ(ret, 0);
108 
109     ret = CompareTwoKey(&newAliasOne, &newAliasTwo);
110     EXPECT_NE(ret, 0);
111 
112     ret = HksDeleteKey(&newAliasOne, NULL);
113     EXPECT_EQ(ret, 0);
114     ret = HksDeleteKey(&newAliasTwo, NULL);
115     EXPECT_EQ(ret, 0);
116 }
117 #endif /* _CUT_AUTHENTICATE_ */
118 }