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