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 #include <gtest/gtest.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <string>
19 #include "relational_store_error_code.h"
20 #include "common.h"
21 #include "oh_rdb_crypto_param.h"
22 #include "oh_data_define.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::NativeRdb;
26
27 class RdbCryptoParamTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
InitRdbConfig()33 static void InitRdbConfig()
34 {
35 }
36 };
37
SetUpTestCase(void)38 void RdbCryptoParamTest::SetUpTestCase(void)
39 {
40 }
41
TearDownTestCase(void)42 void RdbCryptoParamTest::TearDownTestCase(void)
43 {
44 }
45
SetUp(void)46 void RdbCryptoParamTest::SetUp(void)
47 {
48 }
49
TearDown(void)50 void RdbCryptoParamTest::TearDown(void)
51 {
52 }
53
54 /**
55 * @tc.name: RDB_Crypto_Param_test_001
56 * @tc.desc: Normal testCase of OH_Rdb_CreateCryptoParam and OH_Rdb_DestroyCryptoParam.
57 * @tc.type: FUNC
58 */
59 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_001, TestSize.Level1)
60 {
61 auto obj = OH_Rdb_CreateCryptoParam();
62 EXPECT_NE(obj, NULL);
63
64 auto ret = OH_Rdb_DestroyCryptoParam(nullptr);
65 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
66
67 OH_Rdb_CryptoParam wrong;
68 wrong.id = 1;
69 ret = OH_Rdb_DestroyCryptoParam(&wrong);
70 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
71
72 ret = OH_Rdb_DestroyCryptoParam(obj);
73 EXPECT_EQ(ret, RDB_OK);
74 }
75
76 /**
77 * @tc.name: RDB_Crypto_Param_test_002
78 * @tc.desc: Normal testCase of OH_Crypto_SetEncryptionKey.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_002, TestSize.Level1)
82 {
83 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
84 EXPECT_NE(obj, NULL);
85
86 const uint8_t key[] = "12345678";
87 auto ret = OH_Crypto_SetEncryptionKey(nullptr, key, sizeof(key) - 1);
88 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
89
90 ret = OH_Crypto_SetEncryptionKey(obj, key, sizeof(key) - 1);
91 EXPECT_EQ(ret, RDB_OK);
92 EXPECT_EQ(obj->cryptoParam.encryptKey_.size(), sizeof(key) - 1);
93 ret = OH_Rdb_DestroyCryptoParam(obj);
94 EXPECT_EQ(ret, RDB_OK);
95 }
96
97 /**
98 * @tc.name: RDB_Crypto_Param_test_003
99 * @tc.desc: Normal testCase of OH_Crypto_SetIteration.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_003, TestSize.Level1)
103 {
104 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
105 EXPECT_NE(obj, NULL);
106
107 // -1 is iteration times
108 auto ret = OH_Crypto_SetIteration(obj, -1);
109 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
110
111 // 1000 is iteration times
112 ret = OH_Crypto_SetIteration(obj, 1000);
113 EXPECT_EQ(ret, RDB_OK);
114 EXPECT_EQ(obj->cryptoParam.iterNum, 1000);
115 ret = OH_Rdb_DestroyCryptoParam(obj);
116 EXPECT_EQ(ret, RDB_OK);
117 }
118
119 /**
120 * @tc.name: RDB_Crypto_Param_test_004
121 * @tc.desc: Normal testCase of OH_Crypto_SetEncryptionAlgo.
122 * @tc.type: FUNC
123 */
124 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_004, TestSize.Level1)
125 {
126 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
127 EXPECT_NE(obj, NULL);
128
129 // 2 is invalid encryption
130 auto ret = OH_Crypto_SetEncryptionAlgo(obj, 2);
131 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
132
133 ret = OH_Crypto_SetEncryptionAlgo(obj, RDB_AES_256_CBC);
134 EXPECT_EQ(ret, RDB_OK);
135 EXPECT_EQ(obj->cryptoParam.encryptAlgo, RDB_AES_256_CBC);
136 ret = OH_Rdb_DestroyCryptoParam(obj);
137 EXPECT_EQ(ret, RDB_OK);
138 }
139
140 /**
141 * @tc.name: RDB_Crypto_Param_test_005
142 * @tc.desc: Normal testCase of OH_Crypto_SetHmacAlgo.
143 * @tc.type: FUNC
144 */
145 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_005, TestSize.Level1)
146 {
147 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
148 EXPECT_NE(obj, NULL);
149
150 // 3 is invalid hdc
151 auto ret = OH_Crypto_SetHmacAlgo(obj, 3);
152 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
153
154 ret = OH_Crypto_SetHmacAlgo(obj, RDB_HMAC_SHA512);
155 EXPECT_EQ(ret, RDB_OK);
156 EXPECT_EQ(obj->cryptoParam.hmacAlgo, RDB_HMAC_SHA512);
157
158 ret = OH_Rdb_DestroyCryptoParam(obj);
159 EXPECT_EQ(ret, RDB_OK);
160 }
161
162 /**
163 * @tc.name: RDB_Crypto_Param_test_006
164 * @tc.desc: Normal testCase of OH_Crypto_SetKdfAlgo.
165 * @tc.type: FUNC
166 */
167 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_006, TestSize.Level1)
168 {
169 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
170 EXPECT_NE(obj, NULL);
171
172 // 3 is invalid kdf
173 auto ret = OH_Crypto_SetKdfAlgo(obj, 3);
174 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
175
176 ret = OH_Crypto_SetKdfAlgo(obj, RDB_KDF_SHA512);
177 EXPECT_EQ(ret, RDB_OK);
178 EXPECT_EQ(obj->cryptoParam.kdfAlgo, RDB_KDF_SHA512);
179 ret = OH_Rdb_DestroyCryptoParam(obj);
180 EXPECT_EQ(ret, RDB_OK);
181 }
182
183 /**
184 * @tc.name: RDB_Crypto_Param_test_007
185 * @tc.desc: Normal testCase of OH_Crypto_SetCryptoPageSize.
186 * @tc.type: FUNC
187 */
188 HWTEST_F(RdbCryptoParamTest, RDB_Crypto_Param_test_007, TestSize.Level1)
189 {
190 OH_Rdb_CryptoParam *obj = OH_Rdb_CreateCryptoParam();
191 EXPECT_NE(obj, NULL);
192
193 int64_t pageSize = -1;
194 auto ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
195 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
196
197 pageSize = 1023;
198 ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
199 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
200
201 pageSize = 0;
202 ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
203 EXPECT_EQ(ret, RDB_E_INVALID_ARGS);
204
205 EXPECT_EQ(OH_Crypto_SetCryptoPageSize(obj, 512), RDB_E_INVALID_ARGS);
206 EXPECT_EQ(OH_Crypto_SetCryptoPageSize(obj, 131072), RDB_E_INVALID_ARGS);
207
208 pageSize = 1024;
209 ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
210 EXPECT_EQ(ret, RDB_OK);
211 EXPECT_EQ(obj->cryptoParam.cryptoPageSize, pageSize);
212
213 pageSize = 4096;
214 ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
215 EXPECT_EQ(ret, RDB_OK);
216 EXPECT_EQ(obj->cryptoParam.cryptoPageSize, pageSize);
217
218 pageSize = 65536;
219 ret = OH_Crypto_SetCryptoPageSize(obj, pageSize);
220 EXPECT_EQ(ret, RDB_OK);
221 EXPECT_EQ(obj->cryptoParam.cryptoPageSize, pageSize);
222 ret = OH_Rdb_DestroyCryptoParam(obj);
223 EXPECT_EQ(ret, RDB_OK);
224 }