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_sym_key.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 namespace {
27 class NativeSymKeyTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void NativeSymKeyTest::SetUpTestCase() {}
TearDownTestCase()36 void NativeSymKeyTest::TearDownTestCase() {}
37
SetUp()38 void NativeSymKeyTest::SetUp() // add init here, this will be called before test.
39 {
40 }
41
TearDown()42 void NativeSymKeyTest::TearDown() // add destroy here, this will be called when test case done.
43 {
44 }
45
46 HWTEST_F(NativeSymKeyTest, NativeSymKeyTest001, TestSize.Level0)
47 {
48 OH_CryptoSymKeyGenerator *ctx = nullptr;
49 OH_CryptoSymKey *convertKey = nullptr;
50 uint8_t testKey[] = "abcdefghijklmnop";
51 uint32_t testKeyLen = 16;
52 Crypto_DataBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
53
54 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &ctx);
55 EXPECT_EQ(ret, CRYPTO_SUCCESS);
56 ret = OH_CryptoSymKeyGenerator_Convert(ctx, &keyMaterialBlob, &convertKey);
57 EXPECT_EQ(ret, CRYPTO_SUCCESS);
58 const char *algoName = OH_CryptoSymKeyGenerator_GetAlgoName(ctx);
59 ASSERT_NE(algoName, nullptr);
60
61 OH_CryptoSymKey_Destroy(convertKey);
62 OH_CryptoSymKeyGenerator_Destroy(ctx);
63 }
64
65 HWTEST_F(NativeSymKeyTest, NativeSymKeyTest002, TestSize.Level0)
66 {
67 OH_CryptoSymKeyGenerator *ctx = nullptr;
68 OH_CryptoSymKey *symKey = nullptr;
69
70 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &ctx);
71 EXPECT_EQ(ret, CRYPTO_SUCCESS);
72 ret = OH_CryptoSymKeyGenerator_Generate(ctx, &symKey);
73 EXPECT_EQ(ret, CRYPTO_SUCCESS);
74 Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 };
75 ret = OH_CryptoSymKey_GetKeyData(symKey, &dataBlob);
76
77 EXPECT_EQ(ret, CRYPTO_SUCCESS);
78 const char *algoName = OH_CryptoSymKeyGenerator_GetAlgoName(ctx);
79 ASSERT_NE(algoName, nullptr);
80
81 OH_CryptoSymKey_Destroy(symKey);
82 OH_CryptoSymKeyGenerator_Destroy(ctx);
83 }
84 }