• 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_hash_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 
31 class HksHashTest : 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 HksHashTest::SetUpTestCase(void)
43 {
44 }
45 
TearDownTestCase(void)46 void HksHashTest::TearDownTestCase(void)
47 {
48 }
49 
SetUp()50 void HksHashTest::SetUp()
51 {
52     EXPECT_EQ(HksInitialize(), 0);
53 }
54 
TearDown()55 void HksHashTest::TearDown()
56 {
57 }
58 
59 const int DEFAULT_SRC_DATA_SIZE = 200;
60 const int DIGEST_SHA256_HASH_SIZE = 32;
61 
62 const struct HksTestHashParams g_testHashParams[] = {
63     /* normal case */
64     { 0, HKS_SUCCESS,
65         { true, true, HKS_DIGEST_SHA256 },
66         { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
67         { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE }
68     },
69 };
70 
71 /**
72  * @tc.name: HksHashTest.HksHashTest001
73  * @tc.desc: The static function will return true;
74  * @tc.type: FUNC
75  */
76 HWTEST_F(HksHashTest, HksHashTest001, TestSize.Level1)
77 {
78     struct HksParamSet *paramSet = NULL;
79     struct HksBlob *srcData = NULL;
80     struct HksBlob *hash = NULL;
81 
82     int32_t ret = TestConstructHashParamSet(&paramSet,
83         g_testHashParams[0].paramSetParams.paramSetExist,
84         g_testHashParams[0].paramSetParams.setDigest, g_testHashParams[0].paramSetParams.digest);
85     HKS_TEST_ASSERT(ret == 0);
86 
87     ret = TestConstuctBlob(&srcData,
88         g_testHashParams[0].srcDataParams.blobExist,
89         g_testHashParams[0].srcDataParams.blobSize,
90         g_testHashParams[0].srcDataParams.blobDataExist,
91         g_testHashParams[0].srcDataParams.blobDataSize);
92     HKS_TEST_ASSERT(ret == 0);
93 
94     ret = TestConstructBlobOut(&hash,
95         g_testHashParams[0].hashParams.blobExist,
96         g_testHashParams[0].hashParams.blobSize,
97         g_testHashParams[0].hashParams.blobDataExist,
98         g_testHashParams[0].hashParams.blobDataSize);
99     HKS_TEST_ASSERT(ret == 0);
100 
101     ret = HksHashRun(paramSet, srcData, hash, 1);
102     if (ret != g_testHashParams[0].expectResult) {
103         HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[0].testId, ret);
104     }
105     HKS_TEST_ASSERT(ret == g_testHashParams[0].expectResult);
106 
107     HksFreeParamSet(&paramSet);
108     TestFreeBlob(&srcData);
109     TestFreeBlob(&hash);
110     HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[0].testId);
111     ASSERT_TRUE(ret == 0);
112 }
113 #endif /* _CUT_AUTHENTICATE_ */
114 }
115