1 /*
2 * Copyright (c) 2023 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 <sys/stat.h>
18 #include <sys/types.h>
19
20 #include <string>
21
22 #include "common.h"
23 #include "relational_store.h"
24 #include "relational_store_error_code.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::NativeRdb;
28
29 class RdbNativeAssetTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
InitRdbConfig()35 static void InitRdbConfig()
36 {
37 config_.dataBaseDir = RDB_TEST_PATH;
38 config_.storeName = "rdb_asset_test.db";
39 config_.bundleName = "com.ohos.example.distributedndk";
40 config_.moduleName = "";
41 config_.securityLevel = OH_Rdb_SecurityLevel::S1;
42 config_.isEncrypt = false;
43 config_.area = Rdb_SecurityArea::RDB_SECURITY_AREA_EL1;
44 config_.selfSize = sizeof(OH_Rdb_Config);
45 }
46 static void CreateAssetTable();
47 static OH_Rdb_Config config_;
48 };
49
50 OH_Rdb_Store *assetTestRdbStore_;
51 OH_Rdb_Config RdbNativeAssetTest::config_ = { 0 };
SetUpTestCase(void)52 void RdbNativeAssetTest::SetUpTestCase(void)
53 {
54 InitRdbConfig();
55 //0770表示文件拥有者及其所在群组成员有对该文件读写的权限
56 mkdir(config_.dataBaseDir, 0770);
57 int errCode = 0;
58 assetTestRdbStore_ = OH_Rdb_GetOrOpen(&config_, &errCode);
59 EXPECT_NE(assetTestRdbStore_, NULL);
60 CreateAssetTable();
61 }
62
TearDownTestCase(void)63 void RdbNativeAssetTest::TearDownTestCase(void)
64 {
65 char dropTableSql[] = "DROP TABLE IF EXISTS asset_table";
66 int errCode = OH_Rdb_Execute(assetTestRdbStore_, dropTableSql);
67 EXPECT_EQ(errCode, 0);
68 errCode = OH_Rdb_CloseStore(assetTestRdbStore_);
69 EXPECT_EQ(errCode, 0);
70 errCode = OH_Rdb_DeleteStore(&config_);
71 EXPECT_EQ(errCode, 0);
72 }
73
SetUp(void)74 void RdbNativeAssetTest::SetUp(void)
75 {
76 }
77
TearDown(void)78 void RdbNativeAssetTest::TearDown(void)
79 {
80 }
81
CreateAssetTable()82 void RdbNativeAssetTest::CreateAssetTable()
83 {
84 char createTableSql[] = "CREATE TABLE IF NOT EXISTS asset_table (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 "
85 "asset, data2 assets );";
86 int errCode = OH_Rdb_Execute(assetTestRdbStore_, createTableSql);
87 EXPECT_EQ(errCode, RDB_OK);
88 }
89
90 /**
91 * @tc.number: RDB_Native_asset_test_001
92 * @tc.name: Abnormal testCase of asset for setName.
93 * @tc.desc: 1.Create asset
94 * 2.Execute SetName (nullptr, nullptr)
95 * 3.Execute SetName (asset, nullptr)
96 * 4.Execute SetName (nullptr, name.c_str())
97 * 5.Destroy asset
98 * @tc.type: FUNC
99 */
100 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_setName, TestSize.Level1)
101 {
102 Data_Asset *asset = OH_Data_Asset_CreateOne();
103 int errCode = OH_Data_Asset_SetName(nullptr, nullptr);
104 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
105 errCode = OH_Data_Asset_SetName(asset, nullptr);
106 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
107 errCode = OH_Data_Asset_SetName(nullptr, "name");
108 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
109 OH_Data_Asset_DestroyOne(asset);
110 }
111
112 /**
113 * @tc.number: RDB_Native_asset_test_002
114 * @tc.name: Abnormal testCase of asset for setUri.
115 * @tc.desc: 1.Create asset
116 * 2.Execute SetUri (nullptr, nullptr)
117 * 3.Execute SetUri (asset, nullptr)
118 * 4.Execute SetUri (nullptr, uri.c_str())
119 * 5.Destroy asset
120 * @tc.type: FUNC
121 */
122 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_setUri, TestSize.Level1)
123 {
124 Data_Asset *asset = OH_Data_Asset_CreateOne();
125 int errCode = OH_Data_Asset_SetUri(nullptr, nullptr);
126 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
127 errCode = OH_Data_Asset_SetUri(asset, nullptr);
128 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
129 errCode = OH_Data_Asset_SetUri(nullptr, "uri");
130 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
131 OH_Data_Asset_DestroyOne(asset);
132 }
133
134 /**
135 * @tc.number: RDB_Native_asset_test_003
136 * @tc.name: Abnormal testCase of asset for setPath.
137 * @tc.desc: 1.Create asset
138 * 2.Execute SetPath (nullptr, nullptr)
139 * 3.Execute SetPath (asset, nullptr)
140 * 4.Execute SetPath (nullptr, path.c_str())
141 * 5.Destroy asset
142 * @tc.type: FUNC
143 */
144 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_setPath, TestSize.Level1)
145 {
146 Data_Asset *asset = OH_Data_Asset_CreateOne();
147 int errCode = OH_Data_Asset_SetPath(nullptr, nullptr);
148 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
149 errCode = OH_Data_Asset_SetPath(asset, nullptr);
150 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
151 errCode = OH_Data_Asset_SetPath(nullptr, "path");
152 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
153 OH_Data_Asset_DestroyOne(asset);
154 }
155
156 /**
157 * @tc.number: RDB_Native_asset_test_004
158 * @tc.name: Abnormal testCase of asset for setCreateTime, setModifyTime, setSize, setStatus.
159 * @tc.desc: 1.Execute SetCreateTime (nullptr, 1)
160 * 2.Execute SetModifyTime (nullptr, 1)
161 * 3.Execute SetSize (nullptr, 1)
162 * 4.Execute SetStatus (nullptr, Data_AssetStatus::ASSET_NORMAL)
163 * @tc.type: FUNC
164 */
165 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_for_setCreateTime_setModifyTime_setSize_setStatus, TestSize.Level1)
166 {
167 int errCode = OH_Data_Asset_SetCreateTime(nullptr, 1);
168 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
169 errCode = OH_Data_Asset_SetModifyTime(nullptr, 1);
170 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
171 errCode = OH_Data_Asset_SetSize(nullptr, 1);
172 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
173 errCode = OH_Data_Asset_SetStatus(nullptr, Data_AssetStatus::ASSET_NORMAL);
174 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
175 }
176
177 /**
178 * @tc.number: RDB_Native_asset_test_005
179 * @tc.name: Abnormal testCase of asset for getName.
180 * @tc.desc: 1.Create asset
181 * 2.Execute GetName (asset == nullptr)
182 * 3.Execute GetName (nameLength >= *length)
183 * 4.Destroy asset
184 * @tc.type: FUNC
185 */
186 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getName, TestSize.Level1)
187 {
188 Data_Asset *asset = OH_Data_Asset_CreateOne();
189 int errCode = OH_Data_Asset_SetName(asset, "name");
190 EXPECT_EQ(errCode, RDB_OK);
191 char name[10] = "";
192 size_t nameLength = 10;
193 errCode = OH_Data_Asset_GetName(nullptr, name, &nameLength);
194 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
195
196 errCode = OH_Data_Asset_SetName(asset, "0123456789");
197 EXPECT_EQ(errCode, RDB_OK);
198 errCode = OH_Data_Asset_GetName(asset, name, &nameLength);
199 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
200 OH_Data_Asset_DestroyOne(asset);
201 }
202
203 /**
204 * @tc.number: RDB_Native_asset_test_006
205 * @tc.name: Abnormal testCase of asset for getUri.
206 * @tc.desc: 1.Create asset
207 * 2.Execute GetUri (asset == nullptr)
208 * 3.Execute GetUri (uriLength >= *length)
209 * 4.Destroy asset
210 * @tc.type: FUNC
211 */
212 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getUri, TestSize.Level1)
213 {
214 Data_Asset *asset = OH_Data_Asset_CreateOne();
215 int errCode = OH_Data_Asset_SetUri(asset, "uri");
216 EXPECT_EQ(errCode, RDB_OK);
217 char uri[10] = "";
218 size_t uriLength = 10;
219 errCode = OH_Data_Asset_GetUri(nullptr, uri, &uriLength);
220 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
221
222 errCode = OH_Data_Asset_SetUri(asset, "0123456789");
223 EXPECT_EQ(errCode, RDB_OK);
224 errCode = OH_Data_Asset_GetUri(asset, uri, &uriLength);
225 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
226 OH_Data_Asset_DestroyOne(asset);
227 }
228
229 /**
230 * @tc.number: RDB_Native_asset_test_007
231 * @tc.name: Abnormal testCase of asset for getPath.
232 * @tc.desc: 1.Create asset
233 * 2.Execute GetPath (asset == nullptr)
234 * 3.Execute GetPath (pathLength >= *length)
235 * 4.Destroy asset
236 * @tc.type: FUNC
237 */
238 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getPath, TestSize.Level1)
239 {
240 Data_Asset *asset = OH_Data_Asset_CreateOne();
241 int errCode = OH_Data_Asset_SetPath(asset, "path");
242 EXPECT_EQ(errCode, RDB_OK);
243 char path[10] = "";
244 size_t pathLength = 10;
245 errCode = OH_Data_Asset_GetPath(nullptr, path, &pathLength);
246 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
247
248 errCode = OH_Data_Asset_SetPath(asset, "0123456789");
249 EXPECT_EQ(errCode, RDB_OK);
250 errCode = OH_Data_Asset_GetPath(asset, path, &pathLength);
251 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
252 OH_Data_Asset_DestroyOne(asset);
253 }
254
255 /**
256 * @tc.number: RDB_Native_asset_test_008
257 * @tc.name: Abnormal testCase of asset for getCreatTime.
258 * @tc.desc: Execute GetCreateTime (asset == nullptr)
259 * @tc.type: FUNC
260 */
261 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getCreatTime, TestSize.Level1)
262 {
263 int64_t createTime = 0;
264 int errCode = OH_Data_Asset_GetCreateTime(nullptr, &createTime);
265 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
266 }
267
268 /**
269 * @tc.number: RDB_Native_asset_test_009
270 * @tc.name: Abnormal testCase of asset for getModifyTime.
271 * @tc.desc: Execute GetModifyTime (asset == nullptr)
272 * @tc.type: FUNC
273 */
274 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getModifyTime, TestSize.Level1)
275 {
276 int64_t modifyTime = 0;
277 int errCode = OH_Data_Asset_GetModifyTime(nullptr, &modifyTime);
278 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
279 }
280
281 /**
282 * @tc.number: RDB_Native_asset_test_0010
283 * @tc.name: Abnormal testCase of asset for getSize.
284 * @tc.desc: Execute GetSize (asset == nullptr)
285 * @tc.type: FUNC
286 */
287 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getSize, TestSize.Level1)
288 {
289 size_t size = 0;
290 int errCode = OH_Data_Asset_GetSize(nullptr, &size);
291 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
292 }
293
294 /**
295 * @tc.number: RDB_Native_asset_test_0011
296 * @tc.name: Abnormal testCase of asset for getStatus.
297 * @tc.desc: Execute GetStatus (asset == nullptr)
298 * @tc.type: FUNC
299 */
300 HWTEST_F(RdbNativeAssetTest, Abnormal_testCase_of_asset_for_getStatus, TestSize.Level1)
301 {
302 Data_AssetStatus status = Data_AssetStatus::ASSET_NORMAL;
303 int errCode = OH_Data_Asset_GetStatus(nullptr, &status);
304 EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
305 }