• 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 
16 #include <gtest/gtest.h>
17 #include "crypto_common.h"
18 #include "crypto_asym_key.h"
19 #include "crypto_key_agreement.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "memory_mock.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class NativeKeyAgreementTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase()36 void NativeKeyAgreementTest::SetUpTestCase() {}
TearDownTestCase()37 void NativeKeyAgreementTest::TearDownTestCase() {}
38 
SetUp()39 void NativeKeyAgreementTest::SetUp() // add init here, this will be called before test.
40 {
41 }
42 
TearDown()43 void NativeKeyAgreementTest::TearDown() // add destroy here, this will be called when test case done.
44 {
45 }
46 
47 HWTEST_F(NativeKeyAgreementTest, NativeKeyAgreementTest001, TestSize.Level0)
48 {
49     OH_CryptoAsymKeyGenerator* generator = nullptr;
50     OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("X25519", &generator);
51     EXPECT_EQ(ret, CRYPTO_SUCCESS);
52     OH_CryptoKeyPair *keyPairC = nullptr;
53     ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPairC);
54     EXPECT_EQ(ret, CRYPTO_SUCCESS);
55     OH_CryptoKeyPair *keyPairS = nullptr;
56     ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPairS);
57     EXPECT_EQ(ret, CRYPTO_SUCCESS);
58     OH_CryptoPrivKey *privkey = OH_CryptoKeyPair_GetPrivKey(keyPairC);
59     ASSERT_NE(privkey, nullptr);
60     OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPairS);
61     ASSERT_NE(pubkey, nullptr);
62 
63     OH_CryptoKeyAgreement *ctx = nullptr;
64     ret = OH_CryptoKeyAgreement_Create("X25519", &ctx);
65     EXPECT_EQ(ret, CRYPTO_SUCCESS);
66 
67     Crypto_DataBlob key = {0};
68     ret = OH_CryptoKeyAgreement_GenerateSecret(nullptr, privkey, pubkey, &key);
69     EXPECT_NE(ret, CRYPTO_SUCCESS);
70     ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, nullptr, pubkey, &key);
71     EXPECT_NE(ret, CRYPTO_SUCCESS);
72     ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, nullptr, &key);
73     EXPECT_NE(ret, CRYPTO_SUCCESS);
74     ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, pubkey, nullptr);
75     EXPECT_NE(ret, CRYPTO_SUCCESS);
76     ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, pubkey, &key);
77     EXPECT_EQ(ret, CRYPTO_SUCCESS);
78 
79     OH_Crypto_FreeDataBlob(&key);
80     OH_CryptoKeyAgreement_Destroy(ctx);
81     OH_CryptoKeyPair_Destroy(keyPairC);
82     OH_CryptoKeyPair_Destroy(keyPairS);
83     OH_CryptoAsymKeyGenerator_Destroy(generator);
84 }
85 
86 HWTEST_F(NativeKeyAgreementTest, NativeKeyAgreementTest002, TestSize.Level0)
87 {
88     OH_Crypto_ErrCode ret = OH_CryptoKeyAgreement_Create("X25519", nullptr);
89     EXPECT_NE(ret, CRYPTO_SUCCESS);
90 }
91 }