• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "securec.h"
18 
19 #include "md.h"
20 #include "md_openssl.h"
21 
22 #include "log.h"
23 #include "memory.h"
24 
25 using namespace std;
26 using namespace testing::ext;
27 
28 namespace {
29 class CryptoMdSM3Test : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 constexpr uint32_t SM3_LEN = 32;
37 
SetUpTestCase()38 void CryptoMdSM3Test::SetUpTestCase() {}
TearDownTestCase()39 void CryptoMdSM3Test::TearDownTestCase() {}
40 
SetUp()41 void CryptoMdSM3Test::SetUp() // add init here, this will be called before test.
42 {
43 }
44 
TearDown()45 void CryptoMdSM3Test::TearDown() // add destroy here, this will be called when test case done.
46 {
47 }
48 
49 
50 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3CreateTest001, TestSize.Level0)
51 {
52     // create a SM3 obj
53     HcfResult ret = HcfMdCreate("SM3", nullptr);
54     EXPECT_NE(ret, HCF_SUCCESS);
55 }
56 
57 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoSuppTest001, TestSize.Level0)
58 {
59     // create a SM3 obj
60     HcfMd *mdObj = nullptr;
61     HcfResult ret = HcfMdCreate("SM3", &mdObj);
62     ASSERT_EQ(ret, HCF_SUCCESS);
63     ASSERT_NE(mdObj, nullptr);
64     HcfObjDestroy(mdObj);
65 }
66 
67 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoNameTest001, TestSize.Level0)
68 {
69     // create a SM3 obj
70     HcfMd *mdObj = nullptr;
71     HcfResult ret = HcfMdCreate("SM3", &mdObj);
72     ASSERT_EQ(ret, HCF_SUCCESS);
73     ASSERT_NE(mdObj, nullptr);
74     // test api functions
75     const char *algoName =  mdObj->getAlgoName(mdObj);
76     int32_t cmpRes = strcmp(algoName, "SM3");
77     EXPECT_EQ(cmpRes, HCF_SUCCESS);
78     HcfObjDestroy(mdObj);
79 }
80 
81 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3UpdateTest001, TestSize.Level0)
82 {
83     // create a SM3 obj
84     HcfMd *mdObj = nullptr;
85     HcfResult ret = HcfMdCreate("SM3", &mdObj);
86     ASSERT_EQ(ret, HCF_SUCCESS);
87     // define input and output data in blob form
88     HcfBlob *inBlob = nullptr;
89     // test api functions
90     ret = mdObj->update(mdObj, inBlob);
91     EXPECT_NE(ret, HCF_SUCCESS);
92     HcfObjDestroy(mdObj);
93 }
94 
95 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3UpdateTest002, TestSize.Level0)
96 {
97     // create a SM3 obj
98     HcfMd *mdObj = nullptr;
99     HcfResult ret = HcfMdCreate("SM3", &mdObj);
100     ASSERT_EQ(ret, HCF_SUCCESS);
101     // set input and output buf
102     uint8_t testData[] = "My test data";
103     // define input and output data in blob form
104     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
105     // test api functions
106     ret = mdObj->update(mdObj, &inBlob);
107     EXPECT_EQ(ret, HCF_SUCCESS);
108     HcfObjDestroy(mdObj);
109 }
110 
111 
112 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3DoFinalTest001, TestSize.Level0)
113 {
114     // create a SM3 obj
115     HcfMd *mdObj = nullptr;
116     HcfResult ret = HcfMdCreate("SM3", &mdObj);
117     ASSERT_EQ(ret, HCF_SUCCESS);
118     // set input and output buf
119     HcfBlob outBlob = { .data = nullptr, .len = 0 };
120     // test api functions
121     ret = mdObj->doFinal(mdObj, &outBlob);
122     EXPECT_EQ(ret, HCF_SUCCESS);
123     // destroy the API obj and blob data
124     HcfBlobDataClearAndFree(&outBlob);
125     HcfObjDestroy(mdObj);
126 }
127 
128 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3DoFinalTest002, TestSize.Level0)
129 {
130     // create a SM3 obj
131     HcfMd *mdObj = nullptr;
132     HcfResult ret = HcfMdCreate("SM3", &mdObj);
133     ASSERT_EQ(ret, HCF_SUCCESS);
134     // set input and output buf
135     uint8_t testData[] = "My test data";
136     // define input and output data in blob form
137     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
138     HcfBlob outBlob = { .data = nullptr, .len = 0 };
139     // test api functions
140     ret = mdObj->update(mdObj, &inBlob);
141     EXPECT_EQ(ret, HCF_SUCCESS);
142     ret = mdObj->doFinal(mdObj, &outBlob);
143     EXPECT_EQ(ret, HCF_SUCCESS);
144     // destroy the API obj and blob data
145     HcfBlobDataClearAndFree(&outBlob);
146     HcfObjDestroy(mdObj);
147 }
148 
149 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3LenTest001, TestSize.Level0)
150 {
151     // create a API obj with SM3
152     HcfMd *mdObj = nullptr;
153     HcfResult ret = HcfMdCreate("SM3", &mdObj);
154     ASSERT_EQ(ret, HCF_SUCCESS);
155     // test api functions
156     uint32_t len = mdObj->getMdLength(mdObj);
157     EXPECT_EQ(len, SM3_LEN);
158     HcfObjDestroy(mdObj);
159 }
160 
161 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoTest002, TestSize.Level0)
162 {
163     // create a API obj with SM3
164     HcfMd *mdObj = nullptr;
165     HcfResult ret = HcfMdCreate("SM3", &mdObj);
166     ASSERT_EQ(ret, HCF_SUCCESS);
167     // set input and output buf
168     uint8_t testData[] = "My test data";
169     // define input and output data in blob form
170     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
171     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
172     // test api functions
173     ret = mdObj->update(mdObj, &inBlob);
174     EXPECT_EQ(ret, HCF_SUCCESS);
175     ret = mdObj->doFinal(mdObj, &outBlob);
176     EXPECT_EQ(ret, HCF_SUCCESS);
177     uint32_t len = mdObj->getMdLength(mdObj);
178     EXPECT_EQ(len, SM3_LEN);
179     // destroy the API obj and blob data
180     HcfBlobDataClearAndFree(&outBlob);
181     HcfObjDestroy(mdObj);
182 }
183 
184 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoTest003, TestSize.Level0)
185 {
186     HcfMdSpi *spiObj = nullptr;
187     HcfResult ret = OpensslMdSpiCreate("SM3", &spiObj);
188     EXPECT_EQ(ret, HCF_SUCCESS);
189 }
190 
191 HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoTest004, TestSize.Level0)
192 {
193     HcfMdSpi *spiObj = nullptr;
194     HcfResult ret = OpensslMdSpiCreate("SM2", &spiObj);
195     EXPECT_NE(ret, HCF_SUCCESS);
196 }
197 }