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