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 "crypto_manager.h"
17 
18 #include <random>
19 
20 #include "gtest/gtest.h"
21 #include "types.h"
22 using namespace testing::ext;
23 using namespace OHOS::DistributedData;
24 class CryptoManagerTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
SetUp()28     void SetUp(){};
TearDown()29     void TearDown(){};
30 
31 protected:
32     static std::vector<uint8_t> randomKey;
33     static std::vector<uint8_t> Random(uint32_t len);
34 };
35 
36 static const uint32_t KEY_LENGTH = 32;
37 static const uint32_t ENCRYPT_KEY_LENGTH = 48;
38 std::vector<uint8_t> CryptoManagerTest::randomKey;
SetUpTestCase(void)39 void CryptoManagerTest::SetUpTestCase(void)
40 {
41     randomKey = Random(KEY_LENGTH);
42 }
43 
TearDownTestCase(void)44 void CryptoManagerTest::TearDownTestCase(void)
45 {
46     randomKey.assign(randomKey.size(), 0);
47 }
48 
Random(uint32_t len)49 std::vector<uint8_t> CryptoManagerTest::Random(uint32_t len)
50 {
51     std::random_device randomDevice;
52     std::uniform_int_distribution<int> distribution(0, std::numeric_limits<uint8_t>::max());
53     std::vector<uint8_t> key(len);
54     for (uint32_t i = 0; i < len; i++) {
55         key[i] = static_cast<uint8_t>(distribution(randomDevice));
56     }
57     return key;
58 }
59 
60 /**
61 * @tc.name: GenerateRootKey
62 * @tc.desc: generate the root key
63 * @tc.type: FUNC
64 * @tc.require:
65 * @tc.author: zuojiangjiang
66 */
HWTEST_F(CryptoManagerTest, GenerateRootKey, TestSize.Level0)67 HWTEST_F(CryptoManagerTest, GenerateRootKey, TestSize.Level0)
68 {
69     auto errCode = CryptoManager::GetInstance().GenerateRootKey();
70     EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS);
71 }
72 
73 /**
74 * @tc.name: CheckRootKey
75 * @tc.desc: check root key exist;
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author: zuojiangjiang
79 */
HWTEST_F(CryptoManagerTest, CheckRootKey, TestSize.Level0)80 HWTEST_F(CryptoManagerTest, CheckRootKey, TestSize.Level0)
81 {
82     auto errCode = CryptoManager::GetInstance().CheckRootKey();
83     EXPECT_EQ(errCode, CryptoManager::ErrCode::SUCCESS);
84 }
85 
86 /**
87 * @tc.name: Encrypt001
88 * @tc.desc: encrypt random key;
89 * @tc.type: FUNC
90 * @tc.require:
91 * @tc.author: zuojiangjiang
92 */
HWTEST_F(CryptoManagerTest, Encrypt001, TestSize.Level0)93 HWTEST_F(CryptoManagerTest, Encrypt001, TestSize.Level0)
94 {
95     auto encryptKey = CryptoManager::GetInstance().Encrypt(randomKey);
96     EXPECT_EQ(encryptKey.size(), ENCRYPT_KEY_LENGTH);
97     encryptKey.assign(encryptKey.size(), 0);
98 }
99 
100 /**
101 * @tc.name: Encrypt002
102 * @tc.desc: encrypt empty key;
103 * @tc.type: FUNC
104 * @tc.require:
105 * @tc.author: zuojiangjiang
106 */
HWTEST_F(CryptoManagerTest, Encrypt002, TestSize.Level0)107 HWTEST_F(CryptoManagerTest, Encrypt002, TestSize.Level0)
108 {
109     auto encryptKey = CryptoManager::GetInstance().Encrypt({ });
110     EXPECT_TRUE(encryptKey.empty());
111 }
112 
113 /**
114 * @tc.name: DecryptKey001
115 * @tc.desc: decrypt the encrypt key;
116 * @tc.type: FUNC
117 * @tc.require:
118 * @tc.author: zuojiangjiang
119 */
HWTEST_F(CryptoManagerTest, DecryptKey001, TestSize.Level0)120 HWTEST_F(CryptoManagerTest, DecryptKey001, TestSize.Level0)
121 {
122     auto encryptKey = CryptoManager::GetInstance().Encrypt(randomKey);
123     std::vector<uint8_t> key;
124     auto result = CryptoManager::GetInstance().Decrypt(encryptKey, key);
125     EXPECT_TRUE(result);
126     EXPECT_EQ(key.size(), KEY_LENGTH);
127     encryptKey.assign(encryptKey.size(), 0);
128 }
129 
130 /**
131 * @tc.name: DecryptKey002
132 * @tc.desc: decrypt the key, the source key is not encrypt;
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: zuojiangjiang
136 */
HWTEST_F(CryptoManagerTest, DecryptKey002, TestSize.Level0)137 HWTEST_F(CryptoManagerTest, DecryptKey002, TestSize.Level0)
138 {
139     std::vector<uint8_t> key;
140     auto result = CryptoManager::GetInstance().Decrypt(randomKey, key);
141     EXPECT_FALSE(result);
142     EXPECT_TRUE(key.empty());
143 }
144 
145 /**
146 * @tc.name: DecryptKey003
147 * @tc.desc: decrypt the key, the source key is empty;
148 * @tc.type: FUNC
149 * @tc.require:
150 * @tc.author: zuojiangjiang
151 */
HWTEST_F(CryptoManagerTest, DecryptKey003, TestSize.Level0)152 HWTEST_F(CryptoManagerTest, DecryptKey003, TestSize.Level0)
153 {
154     std::vector<uint8_t> srcKey {};
155     std::vector<uint8_t> key;
156     auto result = CryptoManager::GetInstance().Decrypt(srcKey, key);
157     EXPECT_FALSE(result);
158     EXPECT_TRUE(key.empty());
159 }