1 /*
2 * Copyright (C) 2024 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 "crypto_common.h"
18 #include "crypto_digest.h"
19 #include "log.h"
20 #include "memory.h"
21 #include "memory_mock.h"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 constexpr uint32_t SHA1_LEN = 20;
27
28 namespace {
29 class NativeDigestTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase()37 void NativeDigestTest::SetUpTestCase() {}
TearDownTestCase()38 void NativeDigestTest::TearDownTestCase() {}
39
SetUp()40 void NativeDigestTest::SetUp() // add init here, this will be called before test.
41 {
42 }
43
TearDown()44 void NativeDigestTest::TearDown() // add destroy here, this will be called when test case done.
45 {
46 }
47
48 HWTEST_F(NativeDigestTest, NativeDigestTest001, TestSize.Level0)
49 {
50 OH_CryptoDigest *mdObj = nullptr;
51 OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj);
52 ASSERT_EQ(ret, CRYPTO_SUCCESS);
53 // set input and output buf
54 uint8_t testData[] = "My test data";
55 // define input and output data in blob form
56 Crypto_DataBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
57 Crypto_DataBlob outBlob = { .data = nullptr, .len = 0 };
58 // test api functions
59 ret = OH_CryptoDigest_Update(mdObj, &inBlob);
60 EXPECT_EQ(ret, CRYPTO_SUCCESS);
61 ret = OH_CryptoDigest_Final(mdObj, &outBlob);
62 EXPECT_EQ(ret, CRYPTO_SUCCESS);
63 // destroy the API obj and blob data
64 OH_Crypto_FreeDataBlob(&outBlob);
65 OH_DigestCrypto_Destroy(mdObj);
66 }
67
68 HWTEST_F(NativeDigestTest, NativeDigestTest002, TestSize.Level0)
69 {
70 // create a API obj with SHA1
71 OH_CryptoDigest *mdObj = nullptr;
72 OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj);
73 ASSERT_EQ(ret, CRYPTO_SUCCESS);
74 // test api functions
75 uint32_t len = OH_CryptoDigest_GetLength(mdObj);
76 EXPECT_EQ(len, SHA1_LEN);
77 OH_DigestCrypto_Destroy(mdObj);
78 }
79
80 HWTEST_F(NativeDigestTest, NativeDigestTest003, TestSize.Level0)
81 {
82 // create a SHA1 obj
83 OH_CryptoDigest *mdObj = nullptr;
84 OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj);
85 ASSERT_EQ(ret, CRYPTO_SUCCESS);
86 ASSERT_NE(mdObj, nullptr);
87 // test api functions
88 const char *algoName = OH_CryptoDigest_GetAlgoName(mdObj);
89 int32_t cmpRes = strcmp(algoName, "SHA1");
90 EXPECT_EQ(cmpRes, CRYPTO_SUCCESS);
91 OH_DigestCrypto_Destroy(mdObj);
92 }
93
94 }