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