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 <cstdio>
17 #include <thread>
18 #include <unistd.h>
19
20 #include <gtest/gtest.h>
21 #include <nlohmann/json.hpp>
22
23 #include "accesstoken_kit.h"
24 #include "context_impl.h"
25 #include "file_access_framework_errno.h"
26 #include "file_access_observer_common.h"
27 #include "iservice_registry.h"
28 #include "iobserver_callback.h"
29 #include "nativetoken_kit.h"
30 #include "observer_callback_stub.h"
31 #include "token_setproc.h"
32
33 #define private public
34 #include "file_access_helper.h"
35 #undef private
36
37 namespace {
38 using namespace std;
39 using namespace OHOS;
40 using namespace FileAccessFwk;
41 using json = nlohmann::json;
42 const int ABILITY_ID = 5003;
43 const int INIT_THREADS_NUMBER = 4;
44 shared_ptr<FileAccessHelper> g_fah = nullptr;
45 int g_notifyEvent = -1;
46 int g_notifyFlag = -1;
47 string g_notifyUri = "";
48 const int SLEEP_TIME = 100 * 1000;
49 const int UID_TRANSFORM_TMP = 20000000;
50 const int UID_DEFAULT = 0;
51 shared_ptr<OHOS::AbilityRuntime::Context> g_context = nullptr;
52
SetNativeToken()53 void SetNativeToken()
54 {
55 uint64_t tokenId;
56 const char **perms = new const char *[1];
57 perms[0] = "ohos.permission.FILE_ACCESS_MANAGER";
58 NativeTokenInfoParams infoInstance = {
59 .dcapsNum = 0,
60 .permsNum = 1,
61 .aclsNum = 0,
62 .dcaps = nullptr,
63 .perms = perms,
64 .acls = nullptr,
65 .aplStr = "system_core",
66 };
67
68 infoInstance.processName = "SetUpTestCase";
69 tokenId = GetAccessTokenId(&infoInstance);
70 const uint64_t systemAppMask = (static_cast<uint64_t>(1) << 32);
71 tokenId |= systemAppMask;
72 SetSelfTokenID(tokenId);
73 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
74 delete[] perms;
75 }
76
77 class FileExtensionNotifyTest : public testing::Test {
78 public:
SetUpTestCase(void)79 static void SetUpTestCase(void)
80 {
81 cout << "FileExtensionNotifyTest code test" << endl;
82 SetNativeToken();
83 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
84 auto remoteObj = saManager->GetSystemAbility(ABILITY_ID);
85 g_context = make_shared<OHOS::AbilityRuntime::ContextImpl>();
86 g_context->SetToken(remoteObj);
87 AAFwk::Want want;
88 vector<AAFwk::Want> wantVec;
89 setuid(UID_TRANSFORM_TMP);
90 int ret = FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wantVec);
91 EXPECT_EQ(ret, OHOS::FileAccessFwk::ERR_OK);
92 bool sus = false;
93 for (size_t i = 0; i < wantVec.size(); i++) {
94 auto element = wantVec[i].GetElement();
95 if (element.GetBundleName() == "com.ohos.UserFile.ExternalFileManager" &&
96 element.GetAbilityName() == "FileExtensionAbility") {
97 want = wantVec[i];
98 sus = true;
99 break;
100 }
101 }
102 EXPECT_TRUE(sus);
103 vector<AAFwk::Want> wants{want};
104 g_fah = FileAccessHelper::Creator(remoteObj, wants);
105 if (g_fah == nullptr) {
106 GTEST_LOG_(ERROR) << "external_file_access_test g_fah is nullptr";
107 exit(1);
108 }
109 setuid(UID_DEFAULT);
110 }
TearDownTestCase()111 static void TearDownTestCase()
112 {
113 g_fah->Release();
114 g_fah = nullptr;
115 };
SetUp()116 void SetUp(){};
TearDown()117 void TearDown(){};
118 };
119
120 class MyObserver : public ObserverCallbackStub {
121 public:
MyObserver()122 MyObserver() {};
123 virtual ~MyObserver() = default;
124 void OnChange(NotifyMessage ¬ifyMessage) override;
125 };
126
OnChange(NotifyMessage & notifyMessage)127 void MyObserver::OnChange (NotifyMessage ¬ifyMessage)
128 {
129 g_notifyEvent = static_cast<int>(notifyMessage.notifyType_);
130 std::string notifyUri = notifyMessage.uris_[0];
131 g_notifyUri = notifyUri;
132 GTEST_LOG_(INFO) << "enter notify uri =" + notifyUri + "type =" + std::to_string(g_notifyEvent);
133 }
134
135 class TestObserver : public ObserverCallbackStub {
136 public:
TestObserver()137 TestObserver() {};
138 virtual ~TestObserver() = default;
139 void OnChange(NotifyMessage ¬ifyMessage) override;
140 };
141
OnChange(NotifyMessage & notifyMessage)142 void TestObserver::OnChange (NotifyMessage ¬ifyMessage)
143 {
144 g_notifyFlag = static_cast<int>(notifyMessage.notifyType_);
145 std::string notifyUri = notifyMessage.uris_[0];
146 GTEST_LOG_(INFO) << "enter TestObserver uri =" + notifyUri + "type =" + std::to_string(g_notifyFlag);
147 }
148
149 /**
150 * @tc.number: user_file_service_external_file_access_notify_0000
151 * @tc.name: external_file_access_notify_0000
152 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
153 * @tc.size: MEDIUM
154 * @tc.type: FUNC
155 * @tc.level Level 1
156 * @tc.require: SR000H0386
157 */
158 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0000, testing::ext::TestSize.Level1)
159 {
160 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0000";
161 try {
162 vector<RootInfo> info;
163 int result = g_fah->GetRoots(info);
164 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
165 bool notifyForDescendants = false;
166 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
167 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
168 sptr<IFileAccessObserver> myObserver3 = new (std::nothrow) MyObserver();
169 sptr<IFileAccessObserver> myObserver4 = new (std::nothrow) MyObserver();
170 Uri parentUri(info[1].uri);
171 Uri newDirUriTest1("");
172 result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
173 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
174 Uri newDirUriTest2("");
175 result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2);
176 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
177 Uri newFileUri1("");
178 result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
179 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
180 GTEST_LOG_(INFO) << newFileUri1.ToString();
181 Uri newFileUri2("");
182 result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
183 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
184
185 result = g_fah->RegisterNotify(newDirUriTest1, true, myObserver1);
186 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
187 result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver2);
188 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
189 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver3);
190 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
191 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver4);
192 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
193
194 Uri testUri("");
195 result = g_fah->CreateFile(newDirUriTest1, "testUri", testUri);
196 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
197 usleep(SLEEP_TIME);
198 EXPECT_EQ(g_notifyEvent, ADD_EVENT);
199 EXPECT_EQ(g_notifyUri, testUri.ToString());
200 Uri renameDirUri1("");
201 result = g_fah->Rename(newDirUriTest1, "testDir", renameDirUri1);
202 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
203 usleep(SLEEP_TIME);
204 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
205 EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString());
206 result = g_fah->Delete(newDirUriTest2);
207 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
208 usleep(SLEEP_TIME);
209 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
210 EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString());
211
212 Uri renameUri1("");
213 result = g_fah->Rename(newFileUri1, "renameUri1", renameUri1);
214 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
215 usleep(SLEEP_TIME);
216 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
217 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
218 result = g_fah->Delete(newFileUri2);
219 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
220 usleep(SLEEP_TIME);
221 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
222 EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
223
224 sleep(1);
225 result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1);
226 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
227 result = g_fah->UnregisterNotify(newDirUriTest2, myObserver2);
228 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
229 result = g_fah->UnregisterNotify(newFileUri1, myObserver3);
230 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
231 result = g_fah->UnregisterNotify(newFileUri2, myObserver4);
232 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
233 result = g_fah->Delete(renameDirUri1);
234 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
235 result = g_fah->Delete(renameUri1);
236 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
237 } catch (...) {
238 GTEST_LOG_(ERROR) << "external_file_access_notify_0000 occurs an exception.";
239 }
240 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0000";
241 }
242
243 /**
244 * @tc.number: user_file_service_external_file_access_notify_0001
245 * @tc.name: external_file_access_notify_0001
246 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
247 * @tc.size: MEDIUM
248 * @tc.type: FUNC
249 * @tc.level Level 1
250 * @tc.require: SR000H0386
251 */
252 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext::TestSize.Level1)
253 {
254 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0001";
255 try {
256 vector<RootInfo> info;
257 int result = g_fah->GetRoots(info);
258 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
259 bool notifyForDescendants1 = true;
260 bool notifyForDescendants2 = false;
261 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
262 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
263 sptr<IFileAccessObserver> myObserver3 = new (std::nothrow) MyObserver();
264 for (size_t i = 0; i < info.size(); i++) {
265 Uri parentUri(info[i].uri);
266 Uri newFileUri1("");
267 result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
268 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
269 Uri newFileUri2("");
270 result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
271 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
272 Uri newFileUri3("");
273 result = g_fah->CreateFile(parentUri, "uri_file3", newFileUri3);
274 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
275
276 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver1);
277 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
278 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants1, myObserver2);
279 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
280 result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants2, myObserver3);
281 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
282
283 Uri renameFileUri1("");
284 result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1);
285 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
286 usleep(SLEEP_TIME);
287 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
288 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
289
290 result = g_fah->Delete(newFileUri2);
291 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
292 usleep(SLEEP_TIME);
293 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
294 EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
295 result = g_fah->Delete(newFileUri3);
296 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
297 usleep(SLEEP_TIME);
298 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
299 EXPECT_EQ(g_notifyUri, newFileUri3.ToString());
300
301 sleep(1);
302 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
303 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
304 result = g_fah->UnregisterNotify(newFileUri2, myObserver2);
305 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
306 result = g_fah->UnregisterNotify(newFileUri3, myObserver3);
307 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
308 result = g_fah->Delete(renameFileUri1);
309 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
310 }
311 } catch (...) {
312 GTEST_LOG_(ERROR) << "external_file_access_notify_0001 occurs an exception.";
313 }
314 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0001";
315 }
316
317 /**
318 * @tc.number: user_file_service_external_file_access_notify_0002
319 * @tc.name: external_file_access_notify_0002
320 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
321 * @tc.size: MEDIUM
322 * @tc.type: FUNC
323 * @tc.level Level 1
324 * @tc.require: SR000H0386
325 */
326 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0002, testing::ext::TestSize.Level1)
327 {
328 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0002";
329 try {
330 vector<RootInfo> info;
331 int result = g_fah->GetRoots(info);
332 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
333 bool notifyForDescendants = true;
334 sptr<IFileAccessObserver> myObserverDir11 = new (std::nothrow) MyObserver();
335 sptr<IFileAccessObserver> myObserverDir12 = new (std::nothrow) MyObserver();
336 sptr<IFileAccessObserver> myObserverDir13 = new (std::nothrow) MyObserver();
337 sptr<IFileAccessObserver> myObserverDir21 = new (std::nothrow) MyObserver();
338 sptr<IFileAccessObserver> myObserverDir22 = new (std::nothrow) MyObserver();
339 sptr<IFileAccessObserver> myObserverDir23 = new (std::nothrow) MyObserver();
340 sptr<IFileAccessObserver> myObserverFile11 = new (std::nothrow) MyObserver();
341 sptr<IFileAccessObserver> myObserverFile12 = new (std::nothrow) MyObserver();
342 sptr<IFileAccessObserver> myObserverFile13 = new (std::nothrow) MyObserver();
343 sptr<IFileAccessObserver> myObserverFile21 = new (std::nothrow) MyObserver();
344 sptr<IFileAccessObserver> myObserverFile22 = new (std::nothrow) MyObserver();
345 sptr<IFileAccessObserver> myObserverFile23 = new (std::nothrow) MyObserver();
346 Uri parentUri(info[1].uri);
347 Uri newDirUriTest1("");
348 result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
349 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
350 Uri newDirUriTest2("");
351 result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2);
352 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
353 Uri newFileUri1("");
354 result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
355 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
356 Uri newFileUri2("");
357 result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
358 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
359
360 result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir11);
361 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
362 result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir12);
363 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
364 result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserverDir13);
365 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
366
367 result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir21);
368 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
369 result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir22);
370 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
371 result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserverDir23);
372 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
373
374 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile11);
375 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
376 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile12);
377 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
378 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserverFile13);
379 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
380
381 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile21);
382 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
383 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile22);
384 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
385 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserverFile23);
386 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
387
388 Uri testFile("");
389 result = g_fah->CreateFile(newDirUriTest1, "test_file", testFile);
390 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
391 Uri renameDirUri1("");
392 result = g_fah->Rename(newDirUriTest1, "renameDir", renameDirUri1);
393 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
394 usleep(SLEEP_TIME);
395 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
396 EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString());
397 result = g_fah->Delete(newDirUriTest2);
398 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
399 usleep(SLEEP_TIME);
400 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
401 EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString());
402 Uri renameFileUri1("");
403 result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1);
404 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
405 usleep(SLEEP_TIME);
406 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
407 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
408 result = g_fah->Delete(newFileUri2);
409 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
410 usleep(SLEEP_TIME);
411 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
412 EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
413 sleep(2);
414 result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir11);
415 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
416 result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir12);
417 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
418 result = g_fah->UnregisterNotify(newDirUriTest1, myObserverDir13);
419 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
420 result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir21);
421 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
422 result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir22);
423 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
424 result = g_fah->UnregisterNotify(newDirUriTest2, myObserverDir23);
425 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
426 result = g_fah->UnregisterNotify(newFileUri1, myObserverFile11);
427 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
428 result = g_fah->UnregisterNotify(newFileUri1, myObserverFile12);
429 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
430 result = g_fah->UnregisterNotify(newFileUri1, myObserverFile13);
431 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
432 result = g_fah->UnregisterNotify(newFileUri2, myObserverFile21);
433 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
434 result = g_fah->UnregisterNotify(newFileUri2, myObserverFile22);
435 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
436 result = g_fah->UnregisterNotify(newFileUri2, myObserverFile23);
437 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
438
439 result = g_fah->Delete(renameDirUri1);
440 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
441 result = g_fah->Delete(renameFileUri1);
442 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
443 } catch (...) {
444 GTEST_LOG_(ERROR) << "external_file_access_notify_0002 occurs an exception.";
445 }
446 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0002";
447 }
448
449 /**
450 * @tc.number: user_file_service_external_file_access_notify_0003
451 * @tc.name: external_file_access_notify_0003
452 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
453 * @tc.size: MEDIUM
454 * @tc.type: FUNC
455 * @tc.level Level 1
456 * @tc.require: SR000H0386
457 */
458 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0003, testing::ext::TestSize.Level1)
459 {
460 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0003";
461 try {
462 vector<RootInfo> info;
463 int result = g_fah->GetRoots(info);
464 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
465 bool notifyForDescendants = true;
466 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
467 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
468 for (size_t i = 0; i < info.size(); i++) {
469 Uri parentUri(info[i].uri);
470 Uri newDirUriTest1("");
471 result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
472 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
473 Uri newFileUri1("");
474 result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
475 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
476 result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1);
477 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
478 result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1);
479 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
480
481 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2);
482 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
483 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2);
484 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
485 result = g_fah->UnregisterNotify(newDirUriTest1);
486 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
487 result = g_fah->UnregisterNotify(newFileUri1);
488 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
489 result = g_fah->Delete(newDirUriTest1);
490 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
491 result = g_fah->Delete(newFileUri1);
492 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
493 }
494 } catch (...) {
495 GTEST_LOG_(ERROR) << "external_file_access_notify_0003 occurs an exception.";
496 }
497 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0003";
498 }
499
500 /**
501 * @tc.number: user_file_service_external_file_access_notify_0005
502 * @tc.name: external_file_access_notify_0005
503 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
504 * @tc.size: MEDIUM
505 * @tc.type: FUNC
506 * @tc.level Level 1
507 * @tc.require: SR000H0386
508 */
509 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0005, testing::ext::TestSize.Level1)
510 {
511 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0005";
512 try {
513 vector<RootInfo> info;
514 int result = g_fah->GetRoots(info);
515 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
516 bool notifyForDescendants = true;
517 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
518 for (size_t i = 0; i < info.size(); i++) {
519 Uri parentUri(info[i].uri);
520 Uri uri_dir("");
521 result = g_fah->Mkdir(parentUri, "uri_dir123", uri_dir);
522 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
523 Uri uri_dirSub1("");
524 result = g_fah->Mkdir(uri_dir, "uri_dirSub1", uri_dirSub1);
525 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
526 result = g_fah->RegisterNotify(uri_dir, notifyForDescendants, myObserver1);
527 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
528 result = g_fah->RegisterNotify(uri_dirSub1, notifyForDescendants, myObserver1);
529 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
530 Uri uri_dirSub2("");
531 result = g_fah->Mkdir(uri_dir, "uri_dirSub2", uri_dirSub2);
532 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
533 usleep(SLEEP_TIME);
534 EXPECT_EQ(g_notifyEvent, ADD_EVENT);
535 EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
536 result = g_fah->RegisterNotify(uri_dirSub2, notifyForDescendants, myObserver1);
537 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
538 Uri renameDirUri1("");
539 result = g_fah->Rename(uri_dirSub2, "renameDir1", renameDirUri1);
540 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
541 usleep(SLEEP_TIME);
542 if (g_notifyEvent != MOVED_TO) {
543 if (g_notifyEvent != MOVED_SELF) {
544 EXPECT_EQ(g_notifyEvent, MOVED_FROM);
545 EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
546 } else {
547 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
548 EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
549 }
550 } else {
551 EXPECT_EQ(g_notifyEvent, MOVED_TO);
552 EXPECT_EQ(g_notifyUri, renameDirUri1.ToString());
553 }
554 result = g_fah->Delete(uri_dirSub1);
555 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
556 usleep(SLEEP_TIME);
557 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
558 EXPECT_EQ(g_notifyUri, uri_dirSub1.ToString());
559
560 sleep(2);
561 result = g_fah->UnregisterNotify(uri_dir, myObserver1);
562 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
563 result = g_fah->Delete(renameDirUri1);
564 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
565 result = g_fah->Delete(uri_dir);
566 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
567 }
568 } catch (...) {
569 GTEST_LOG_(ERROR) << "external_file_access_notify_0005 occurs an exception.";
570 }
571 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0005";
572 }
573
574 /**
575 * @tc.number: user_file_service_external_file_access_notify_0006
576 * @tc.name: external_file_access_notify_0006
577 * @tc.desc: Duplicate UnregisterNotify failed.
578 * @tc.size: MEDIUM
579 * @tc.type: FUNC
580 * @tc.level Level 1
581 * @tc.require: SR000H0386
582 */
583 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0006, testing::ext::TestSize.Level1)
584 {
585 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0006";
586 try {
587 vector<RootInfo> info;
588 int result = g_fah->GetRoots(info);
589 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
590 bool notifyForDescendants = true;
591 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
592 for (size_t i = 0; i < info.size(); i++) {
593 Uri parentUri(info[i].uri);
594 Uri newFileUri1("");
595 result = g_fah->CreateFile(parentUri, "uri_file", newFileUri1);
596 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
597 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
598 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
599 result = g_fah->Delete(newFileUri1);
600 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
601 usleep(SLEEP_TIME);
602 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
603 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
604 sleep(1);
605 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
606 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
607 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
608 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
609 }
610 } catch (...) {
611 GTEST_LOG_(ERROR) << "external_file_access_notify_0006 occurs an exception.";
612 }
613 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0006";
614 }
615
616 /**
617 * @tc.number: user_file_service_external_file_access_notify_0007
618 * @tc.name: external_file_access_notify_0007
619 * @tc.desc: Test function of RegisterNotify and UnregisterNotify failed when callback is Invalid.
620 * @tc.size: MEDIUM
621 * @tc.type: FUNC
622 * @tc.level Level 1
623 * @tc.require: SR000H0386
624 */
625 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0007, testing::ext::TestSize.Level1)
626 {
627 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0007";
628 try {
629 vector<RootInfo> info;
630 int result = g_fah->GetRoots(info);
631 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
632 sptr<IFileAccessObserver> myObserver1 = nullptr;
633 bool notifyForDescendants = true;
634 for (size_t i = 0; i < info.size(); i++) {
635 Uri parentUri(info[i].uri);
636 Uri newFileUri1("");
637 result = g_fah->CreateFile(parentUri, "uri_file007", newFileUri1);
638 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
639 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
640 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
641 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
642 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
643 result = g_fah->Delete(newFileUri1);
644 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
645 }
646 } catch (...) {
647 GTEST_LOG_(ERROR) << "external_file_access_notify_0007 occurs an exception.";
648 }
649 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0007";
650 }
651
652 /**
653 * @tc.number: user_file_service_external_file_access_notify_0008
654 * @tc.name: external_file_access_notify_0008
655 * @tc.desc: Test function of UnregisterNotify failed when uri is Invalid.
656 * @tc.size: MEDIUM
657 * @tc.type: FUNC
658 * @tc.level Level 1
659 * @tc.require: SR000H0386
660 */
661 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0008, testing::ext::TestSize.Level1)
662 {
663 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0008";
664 try {
665 vector<RootInfo> info;
666 int result = g_fah->GetRoots(info);
667 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
668 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
669 Uri newFileUri1("*/&%");
670 bool notifyForDescendants = true;
671 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
672 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
673 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
674 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
675 } catch (...) {
676 GTEST_LOG_(ERROR) << "external_file_access_notify_0008 occurs an exception.";
677 }
678 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0008";
679 }
680
681 /**
682 * @tc.number: user_file_service_external_file_access_notify_0009
683 * @tc.name: external_file_access_notify_0009
684 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS when the file is Chinese name.
685 * @tc.size: MEDIUM
686 * @tc.type: FUNC
687 * @tc.level Level 1
688 * @tc.require: SR000H0386
689 */
690 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0009, testing::ext::TestSize.Level1)
691 {
692 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0009";
693 try {
694 vector<RootInfo> info;
695 int result = g_fah->GetRoots(info);
696 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
697 bool notifyForDescendants = true;
698 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
699 for (size_t i = 0; i < info.size(); i++) {
700 Uri parentUri(info[i].uri);
701 Uri newFileUri1("");
702 result = g_fah->CreateFile(parentUri, "测试文件", newFileUri1);
703 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
704 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
705 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
706 result = g_fah->Delete(newFileUri1);
707 usleep(SLEEP_TIME);
708 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
709 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
710 sleep(1);
711 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
712 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
713 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
714 }
715 } catch (...) {
716 GTEST_LOG_(ERROR) << "external_file_access_notify_0009 occurs an exception.";
717 }
718 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0009";
719 }
720
721 /**
722 * @tc.number: user_file_service_external_file_access_notify_0010
723 * @tc.name: external_file_access_notify_0010
724 * @tc.desc: Test function of UnregisterNotify interface for failed when the Uri and callback do not match.
725 * @tc.size: MEDIUM
726 * @tc.type: FUNC
727 * @tc.level Level 1
728 * @tc.require: SR000H0386
729 */
730 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0010, testing::ext::TestSize.Level1)
731 {
732 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0010";
733 try {
734 vector<RootInfo> info;
735 int result = g_fah->GetRoots(info);
736 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
737 bool notifyForDescendants = true;
738 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
739 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
740 for (size_t i = 0; i < info.size(); i++) {
741 Uri parentUri(info[i].uri);
742 Uri newFileUri1("");
743 result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
744 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
745 Uri newFileUri2("");
746 result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
747 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
748 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
749 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
750 result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver2);
751 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
752
753 result = g_fah->UnregisterNotify(newFileUri1, myObserver2);
754 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
755 result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
756 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
757 result = g_fah->UnregisterNotify(newFileUri2, myObserver2);
758 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
759 result = g_fah->Delete(newFileUri1);
760 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
761 result = g_fah->Delete(newFileUri2);
762 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
763 }
764 } catch (...) {
765 GTEST_LOG_(ERROR) << "external_file_access_notify_0010 occurs an exception.";
766 }
767 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0010";
768 }
769
770 /**
771 * @tc.number: user_file_service_external_file_access_notify_0011
772 * @tc.name: external_file_access_notify_0011
773 * @tc.desc: Test function of RegisterNotify interface for SUCCESS when set the notifyForDescendants to true and
774 * @tc.desc: then set to false
775 * @tc.size: MEDIUM
776 * @tc.type: FUNC
777 * @tc.level Level 1
778 * @tc.require: SR000H0386
779 */
780 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0011, testing::ext::TestSize.Level1)
781 {
782 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0011";
783 try {
784 vector<RootInfo> info;
785 int result = g_fah->GetRoots(info);
786 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
787 bool notifyForDescendants1 = true;
788 bool notifyForDescendants2 = false;
789 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) TestObserver();
790 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
791 for (size_t i = 0; i < info.size(); i++) {
792 Uri parentUri(info[i].uri);
793 Uri newFileDir1("");
794 result = g_fah->Mkdir(parentUri, "uri_dir11", newFileDir1);
795 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
796 Uri newFileUri1("");
797 result = g_fah->CreateFile(newFileDir1, "uri_file11", newFileUri1);
798 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
799 sleep(1);
800 result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants1, myObserver1);
801 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
802 result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver2);
803 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
804 Uri renameFileUri1("");
805 result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1);
806 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
807 usleep(SLEEP_TIME * 2);
808 if (g_notifyEvent != MOVED_TO) {
809 if (g_notifyEvent != MOVED_SELF) {
810 EXPECT_EQ(g_notifyEvent, MOVED_FROM);
811 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
812 } else {
813 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
814 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
815 }
816 } else {
817 EXPECT_EQ(g_notifyEvent, MOVED_TO);
818 EXPECT_EQ(g_notifyUri, renameFileUri1.ToString());
819 }
820 result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants2, myObserver1);
821 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
822 sleep(2);
823 result = g_fah->Delete(renameFileUri1);
824 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
825 usleep(SLEEP_TIME);
826 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
827 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
828 EXPECT_NE(g_notifyFlag, DELETE_EVENT);
829
830 sleep(1);
831 result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
832 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
833 result = g_fah->UnregisterNotify(newFileUri1, myObserver2);
834 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
835 result = g_fah->Delete(newFileDir1);
836 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
837 }
838 } catch (...) {
839 GTEST_LOG_(ERROR) << "external_file_access_notify_0011 occurs an exception.";
840 }
841 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0011";
842 }
843
RegisterDirNotify(Uri parentUri,std::string dirName,IFileAccessObserver * observer)844 static void RegisterDirNotify(Uri parentUri, std::string dirName, IFileAccessObserver *observer)
845 {
846 Uri newDirUriTest("");
847 bool notifyForDescendants = true;
848 int result = g_fah->Mkdir(parentUri, dirName, newDirUriTest);
849 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
850 sptr<IFileAccessObserver> myObserver(observer);
851 result = g_fah->RegisterNotify(newDirUriTest, notifyForDescendants, myObserver);
852 EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
853 result = g_fah->Delete(newDirUriTest);
854 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
855 usleep(SLEEP_TIME);
856 EXPECT_EQ(g_notifyEvent, DELETE_EVENT);
857 EXPECT_EQ(g_notifyUri, newDirUriTest.ToString());
858 sleep(2);
859 result = g_fah->UnregisterNotify(newDirUriTest, myObserver);
860 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
861 }
862
863 /**
864 * @tc.number: user_file_service_external_file_access_notify_0012
865 * @tc.name: external_file_access_notify_0012
866 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
867 * @tc.size: MEDIUM
868 * @tc.type: FUNC
869 * @tc.level Level 1
870 * @tc.require: SR000H0386
871 */
872 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00012, testing::ext::TestSize.Level1)
873 {
874 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00012";
875 try {
876 vector<RootInfo> info;
877 int result = g_fah->GetRoots(info);
878 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
879 Uri parentUri(info[1].uri);
880 Uri newFileUri1("");
881 GTEST_LOG_(INFO) << parentUri.ToString();
882 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
883 for (int i = 0; i < INIT_THREADS_NUMBER; i++) {
884 std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr());
885 execthread1.join();
886 }
887 } catch (...) {
888 GTEST_LOG_(ERROR) << "external_file_access_notify_00012 occurs an exception.";
889 }
890 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00012";
891 }
892
893 /**
894 * @tc.number: user_file_service_external_file_access_notify_0013
895 * @tc.name: external_file_access_notify_0013
896 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
897 * @tc.size: MEDIUM
898 * @tc.type: FUNC
899 * @tc.level Level 1
900 * @tc.require: SR000H0386
901 */
902 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00013, testing::ext::TestSize.Level1)
903 {
904 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00013";
905 try {
906 vector<RootInfo> info;
907 int result = g_fah->GetRoots(info);
908 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
909 Uri parentUri(info[1].uri);
910 GTEST_LOG_(INFO) << parentUri.ToString();
911 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
912 std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr());
913 execthread1.join();
914 std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver1.GetRefPtr());
915 execthread2.join();
916 std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver1.GetRefPtr());
917 execthread3.join();
918 } catch (...) {
919 GTEST_LOG_(ERROR) << "external_file_access_notify_00013 occurs an exception.";
920 }
921 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00013";
922 }
923
924 /**
925 * @tc.number: user_file_service_external_file_access_notify_0014
926 * @tc.name: external_file_access_notify_0014
927 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
928 * @tc.size: MEDIUM
929 * @tc.type: FUNC
930 * @tc.level Level 1
931 * @tc.require: SR000H0386
932 */
933 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00014, testing::ext::TestSize.Level1)
934 {
935 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00014";
936 try {
937 vector<RootInfo> info;
938 int result = g_fah->GetRoots(info);
939 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
940 Uri parentUri(info[1].uri);
941 GTEST_LOG_(INFO) << parentUri.ToString();
942 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
943 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
944 sptr<IFileAccessObserver> myObserver3 = new (std::nothrow) MyObserver();
945
946 std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr());
947 execthread1.join();
948 sleep(1);
949 std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest", myObserver2.GetRefPtr());
950 execthread2.join();
951 sleep(1);
952 std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest", myObserver3.GetRefPtr());
953 execthread3.join();
954 } catch (...) {
955 GTEST_LOG_(ERROR) << "external_file_access_notify_00014 occurs an exception.";
956 }
957 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00014";
958 }
959
960 /**
961 * @tc.number: user_file_service_external_file_access_notify_0015
962 * @tc.name: external_file_access_notify_0015
963 * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
964 * @tc.size: MEDIUM
965 * @tc.type: FUNC
966 * @tc.level Level 1
967 * @tc.require: SR000H0386
968 */
969 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00015, testing::ext::TestSize.Level1)
970 {
971 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00015";
972 try {
973 vector<RootInfo> info;
974 int result = g_fah->GetRoots(info);
975 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
976 Uri parentUri(info[1].uri);
977 GTEST_LOG_(INFO) << parentUri.ToString();
978 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
979 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
980 sptr<IFileAccessObserver> myObserver3 = new (std::nothrow) MyObserver();
981
982 std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr());
983 execthread1.join();
984 std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver2.GetRefPtr());
985 execthread2.join();
986 std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver3.GetRefPtr());
987 execthread3.join();
988 } catch (...) {
989 GTEST_LOG_(ERROR) << "external_file_access_notify_00015 occurs an exception.";
990 }
991 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00015";
992 }
993
994 /**
995 * @tc.number: user_file_service_external_file_access_notify_0016
996 * @tc.name: external_file_access_notify_0016
997 * @tc.desc: Test UnregisterNotify all callbacks related to the current uri
998 * @tc.size: MEDIUM
999 * @tc.type: FUNC
1000 * @tc.level Level 1
1001 * @tc.require: SR000H0386
1002 */
1003 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0016, testing::ext::TestSize.Level1)
1004 {
1005 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0016";
1006 try {
1007 vector<RootInfo> info;
1008 int result = g_fah->GetRoots(info);
1009 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1010 bool notifyForDescendants = true;
1011 sptr<IFileAccessObserver> myObserver1 = new (std::nothrow) MyObserver();
1012 sptr<IFileAccessObserver> myObserver2 = new (std::nothrow) MyObserver();
1013 sptr<IFileAccessObserver> myObserver3 = new (std::nothrow) MyObserver();
1014 for (size_t i = 0; i < info.size(); i++) {
1015 Uri parentUri(info[i].uri);
1016 Uri newFileDir1("");
1017 result = g_fah->Mkdir(parentUri, "uri_dir0016", newFileDir1);
1018 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1019 result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1020 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1021 result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver2);
1022 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1023 result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver3);
1024 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1025
1026 result = g_fah->UnregisterNotify(newFileDir1);
1027 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1028 result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1029 EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
1030
1031 result = g_fah->Delete(newFileDir1);
1032 EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1033 }
1034 } catch (...) {
1035 GTEST_LOG_(ERROR) << "external_file_access_notify_0016 occurs an exception.";
1036 }
1037 GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0016";
1038 }
1039 } // namespace
1040