• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "hdc_huks_ut.h"
16 #include "securec.h"
17 
18 using namespace testing::ext;
19 namespace Hdc {
20     #define TEST_HUKS_ALIAS "test_huks_alias"
21     #define TEST_DATA_LEN 10
SetUpTestCase()22     void HdcHuksTest::SetUpTestCase() {}
TearDownTestCase()23     void HdcHuksTest::TearDownTestCase() {}
SetUp()24     void HdcHuksTest::SetUp() {}
TearDown()25     void HdcHuksTest::TearDown() {}
26 
27     HWTEST_F(HdcHuksTest, TestResetHuksKey, TestSize.Level0)
28     {
29         HdcHuks huks(TEST_HUKS_ALIAS);
30         ASSERT_EQ(huks.ResetHuksKey(), true);
31     }
32 
33     HWTEST_F(HdcHuksTest, TestAesGcmEncrypt, TestSize.Level0)
34     {
35         HdcHuks huks(TEST_HUKS_ALIAS);
36         std::vector<uint8_t> encryptData;
37         uint8_t testData[TEST_DATA_LEN];
38         memset_s(testData, TEST_DATA_LEN, '1', TEST_DATA_LEN);
39         bool ret = huks.AesGcmEncrypt(testData, TEST_DATA_LEN, encryptData);
40         ASSERT_EQ(ret, true);
41     }
42 
43     HWTEST_F(HdcHuksTest, TestAesGcmEncryptLen, TestSize.Level0)
44     {
45         HdcHuks huks(TEST_HUKS_ALIAS);
46         int len = huks.CaculateGcmEncryptLen(TEST_DATA_LEN);
47         ASSERT_EQ(len, 38); // 38 = 12 + 10 + 16
48     }
49 
50 
51     HWTEST_F(HdcHuksTest, TestAesGcmEncryptAndDecrypt, TestSize.Level0)
52     {
53         HdcHuks huks(TEST_HUKS_ALIAS);
54         std::vector<uint8_t> encryptData;
55         uint8_t testData[TEST_DATA_LEN];
56         memset_s(testData, TEST_DATA_LEN, '1', TEST_DATA_LEN);
57         bool ret = huks.AesGcmEncrypt(testData, TEST_DATA_LEN, encryptData);
58         ASSERT_EQ(ret, true);
59 
60         std::string encryptStr;
61         encryptStr.assign(encryptData.begin(), encryptData.end());
62         std::pair<uint8_t*, int> plainStr = huks.AesGcmDecrypt(encryptStr);
63         ASSERT_EQ(plainStr.second, 10); // 10 is testData length
64         ASSERT_EQ(memcmp(plainStr.first, testData, plainStr.second), 0);
65     }
66 } // namespace Hdc