• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 
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 "ifile_access_observer.h"
29 #include "nativetoken_kit.h"
30 #include "file_access_observer_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 vector<string> g_notifyUris;
49 const int SLEEP_TIME = 600 * 1000;
50 const int UID_TRANSFORM_TMP = 20000000;
51 const int UID_DEFAULT = 0;
52 const uint64_t SYSTEM_APP_MASK =  (static_cast<uint64_t>(1) << 32); // 1: Base number, 32: Left shifted bit numbers
53 
54 shared_ptr<OHOS::AbilityRuntime::Context> g_context = nullptr;
55 
SetNativeToken()56 void SetNativeToken()
57 {
58     uint64_t tokenId;
59     const char *perms[] = {
60         "ohos.permission.FILE_ACCESS_MANAGER",
61         "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
62         "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION"
63     };
64     NativeTokenInfoParams infoInstance = {
65         .dcapsNum = 0,
66         .permsNum = 3,
67         .aclsNum = 0,
68         .dcaps = nullptr,
69         .perms = perms,
70         .acls = nullptr,
71         .aplStr = "system_core",
72     };
73 
74     infoInstance.processName = "SetUpTestCase";
75     tokenId = GetAccessTokenId(&infoInstance);
76     const uint64_t systemAppMask = (static_cast<uint64_t>(1) << 32);
77     tokenId |= systemAppMask;
78     SetSelfTokenID(tokenId);
79     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
80 }
81 
SetNativeToken(const char * processName,const char * perms[],int32_t permsNum)82 void SetNativeToken(const char* processName, const char*perms[], int32_t permsNum)
83 {
84     uint64_t tokenId;
85     NativeTokenInfoParams infoInstance = {
86         .dcapsNum = 0,
87         .permsNum = permsNum,
88         .aclsNum = 0,
89         .dcaps = nullptr,
90         .perms = perms,
91         .acls = nullptr,
92         .aplStr = "system_core",
93     };
94     tokenId = GetAccessTokenId(&infoInstance);
95     const uint64_t systemAppMask = SYSTEM_APP_MASK;
96     tokenId |= systemAppMask;
97     SetSelfTokenID(tokenId);
98     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
99 }
100 
101 class FileExtensionNotifyTest : public testing::Test {
102 public:
SetUpTestCase(void)103     static void SetUpTestCase(void)
104     {
105         cout << "FileExtensionNotifyTest code test" << endl;
106         SetNativeToken();
107         auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108         if (saManager == nullptr) {
109             return;
110         }
111         auto remoteObj = saManager->GetSystemAbility(ABILITY_ID);
112         g_context = make_shared<OHOS::AbilityRuntime::ContextImpl>();
113         g_context->SetToken(remoteObj);
114         AAFwk::Want want;
115         vector<AAFwk::Want> wantVec;
116         setuid(UID_TRANSFORM_TMP);
117         int ret = FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wantVec);
118         EXPECT_EQ(ret, OHOS::FileAccessFwk::ERR_OK);
119         bool sus = false;
120         for (size_t i = 0; i < wantVec.size(); i++) {
121             auto element = wantVec[i].GetElement();
122             if (element.GetBundleName() == "com.ohos.UserFile.ExternalFileManager" &&
123                 element.GetAbilityName() == "FileExtensionAbility") {
124                 want = wantVec[i];
125                 sus = true;
126                 break;
127             }
128         }
129         EXPECT_TRUE(sus);
130         vector<AAFwk::Want> wants{want};
131         g_fah = FileAccessHelper::Creator(remoteObj, wants);
132         if (g_fah == nullptr) {
133             GTEST_LOG_(ERROR) << "external_file_access_test g_fah is nullptr";
134             exit(1);
135         }
136         setuid(UID_DEFAULT);
137     }
TearDownTestCase()138     static void TearDownTestCase()
139     {
140         if (g_fah) {
141             g_fah->Release();
142         }
143         g_fah = nullptr;
144     };
SetUp()145     void SetUp(){};
TearDown()146     void TearDown(){};
147 };
148 
149 class MyObserver : public FileAccessObserverStub {
150 public:
MyObserver()151     MyObserver() {};
152     virtual ~MyObserver() = default;
153     int OnChange(const NotifyMessage &notifyMessage) override;
154 };
155 
OnChange(const NotifyMessage & notifyMessage)156 int MyObserver::OnChange(const NotifyMessage &notifyMessage)
157 {
158     g_notifyEvent = static_cast<int>(notifyMessage.notifyType_);
159     std::string notifyUri = notifyMessage.uris_[0];
160     g_notifyUri = notifyUri;
161     GTEST_LOG_(INFO) << "enter notify uri =" + notifyUri + " type =" + std::to_string(g_notifyEvent) +" uri size" +
162         std::to_string(notifyMessage.uris_.size());
163 
164     return 1;
165 }
166 
167 class TestObserver : public FileAccessObserverStub {
168 public:
TestObserver()169     TestObserver() {};
170     virtual ~TestObserver() = default;
171     int OnChange(const NotifyMessage &notifyMessage) override;
172 };
173 
OnChange(const NotifyMessage & notifyMessage)174 int TestObserver::OnChange(const NotifyMessage &notifyMessage)
175 {
176     g_notifyFlag = static_cast<int>(notifyMessage.notifyType_);
177     std::string notifyUri = notifyMessage.uris_[0];
178     g_notifyUris = notifyMessage.uris_;
179     GTEST_LOG_(INFO) << "enter TestObserver uri =" + notifyUri + "type =" + std::to_string(g_notifyFlag);
180 
181     return 1;
182 }
183 
ReadyRegisterNotify00(Uri & parentUri,sptr<IFileAccessObserver> & myObserver1,sptr<IFileAccessObserver> & myObserver2,sptr<IFileAccessObserver> & myObserver3,sptr<IFileAccessObserver> & myObserver4)184 static tuple<Uri, Uri, Uri, Uri> ReadyRegisterNotify00(Uri& parentUri,
185                                                        sptr<IFileAccessObserver>& myObserver1,
186                                                        sptr<IFileAccessObserver>& myObserver2,
187                                                        sptr<IFileAccessObserver>& myObserver3,
188                                                        sptr<IFileAccessObserver>& myObserver4)
189 {
190     bool notifyForDescendants = false;
191     Uri newDirUriTest1("");
192     EXPECT_NE(g_fah, nullptr);
193     int result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
194     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
195     Uri newDirUriTest2("");
196     result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2);
197     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
198     Uri newFileUri1("");
199     result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
200     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
201     GTEST_LOG_(INFO) <<  newFileUri1.ToString();
202     Uri newFileUri2("");
203     result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
204     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
205 
206     result = g_fah->RegisterNotify(newDirUriTest1, true, myObserver1);
207     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
208     result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver2);
209     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
210     result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver3);
211     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
212     result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver4);
213     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
214 
215     return {newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2};
216 }
217 
TriggerNotify00(Uri & newDirUriTest1,Uri & newDirUriTest2,Uri & newFileUri1,Uri & newFileUri2)218 static tuple<Uri, Uri> TriggerNotify00(Uri& newDirUriTest1, Uri& newDirUriTest2, Uri& newFileUri1, Uri& newFileUri2)
219 {
220     Uri testUri("");
221     EXPECT_NE(g_fah, nullptr);
222     int result = g_fah->CreateFile(newDirUriTest1, "testUri", testUri);
223     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
224     usleep(SLEEP_TIME);
225     EXPECT_EQ(g_notifyEvent, ADD_EVENT);
226     EXPECT_EQ(g_notifyUri, testUri.ToString());
227     Uri renameDirUri1("");
228     result = g_fah->Rename(newDirUriTest1, "testDir", renameDirUri1);
229     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
230     usleep(SLEEP_TIME);
231     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
232     EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString());
233     result = g_fah->Delete(newDirUriTest2);
234     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
235     usleep(SLEEP_TIME);
236     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
237     EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString());
238 
239     Uri renameUri1("");
240     result = g_fah->Rename(newFileUri1, "renameUri1", renameUri1);
241     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
242     usleep(SLEEP_TIME);
243     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
244     EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
245     result = g_fah->Delete(newFileUri2);
246     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
247     usleep(SLEEP_TIME);
248     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
249     EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
250 
251     return {renameDirUri1, renameUri1};
252 }
253 
254 /**
255  * @tc.number: user_file_service_external_file_access_notify_0000
256  * @tc.name: external_file_access_notify_0000
257  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
258  * @tc.size: MEDIUM
259  * @tc.type: FUNC
260  * @tc.level Level 1
261  * @tc.require: SR000H0386
262  */
263 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0000, testing::ext::TestSize.Level1)
264 {
265     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0000";
266     try {
267         vector<RootInfo> info;
268         EXPECT_NE(g_fah, nullptr);
269         int result = g_fah->GetRoots(info);
270         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
271         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
272         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
273         sptr<IFileAccessObserver> myObserver3 = sptr(new (std::nothrow) MyObserver());
274         sptr<IFileAccessObserver> myObserver4 = sptr(new (std::nothrow) MyObserver());
275         Uri parentUri(info[0].uri);
276         auto [newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2] =
277             ReadyRegisterNotify00(parentUri, myObserver1, myObserver2, myObserver3, myObserver4);
278 
279         auto [renameDirUri1, renameUri1] = TriggerNotify00(newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2);
280 
281         sleep(1);
282         result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1);
283         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
284         result = g_fah->UnregisterNotify(newDirUriTest2, myObserver2);
285         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
286         result = g_fah->UnregisterNotify(newFileUri1, myObserver3);
287         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
288         result = g_fah->UnregisterNotify(newFileUri2, myObserver4);
289         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
290         result = g_fah->Delete(renameDirUri1);
291         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
292         result = g_fah->Delete(renameUri1);
293         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
294     } catch (...) {
295         GTEST_LOG_(ERROR) << "external_file_access_notify_0000 occurs an exception.";
296     }
297     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0000";
298 }
299 
ReadyRegisterNotify01(Uri & parentUri,sptr<IFileAccessObserver> myObserver1,sptr<IFileAccessObserver> myObserver2,sptr<IFileAccessObserver> myObserver3)300 static tuple<Uri, Uri, Uri> ReadyRegisterNotify01(Uri& parentUri, sptr<IFileAccessObserver> myObserver1,
301     sptr<IFileAccessObserver> myObserver2, sptr<IFileAccessObserver> myObserver3)
302 {
303     bool notifyForDescendants1 = true;
304     bool notifyForDescendants2 = false;
305     Uri newFileUri1("");
306     EXPECT_NE(g_fah, nullptr);
307     int result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
308     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
309     Uri newFileUri2("");
310     result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
311     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
312     Uri newFileUri3("");
313     result = g_fah->CreateFile(parentUri, "uri_file3", newFileUri3);
314     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
315 
316     result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver1);
317     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
318     result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants1, myObserver2);
319     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
320     result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants2, myObserver3);
321     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
322     return {newFileUri1, newFileUri2, newFileUri3};
323 }
324 
325 /**
326  * @tc.number: user_file_service_external_file_access_notify_0001
327  * @tc.name: external_file_access_notify_0001
328  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
329  * @tc.size: MEDIUM
330  * @tc.type: FUNC
331  * @tc.level Level 1
332  * @tc.require: SR000H0386
333  */
334 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0001, testing::ext::TestSize.Level1)
335 {
336     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0001";
337     try {
338         vector<RootInfo> info;
339         EXPECT_NE(g_fah, nullptr);
340         int result = g_fah->GetRoots(info);
341         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
342 
343         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
344         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
345         sptr<IFileAccessObserver> myObserver3 = sptr(new (std::nothrow) MyObserver());
346         Uri parentUri(info[0].uri);
347         auto [newFileUri1, newFileUri2, newFileUri3] =
348             ReadyRegisterNotify01(parentUri, myObserver1, myObserver2, myObserver3);
349 
350         Uri renameFileUri1("");
351         result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1);
352         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
353         usleep(SLEEP_TIME);
354         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
355         EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
356 
357         result = g_fah->Delete(newFileUri2);
358         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
359         usleep(SLEEP_TIME);
360         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
361         EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
362         result = g_fah->Delete(newFileUri3);
363         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
364         usleep(SLEEP_TIME);
365         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
366         EXPECT_EQ(g_notifyUri, newFileUri3.ToString());
367 
368         sleep(1);
369         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
370         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
371         result = g_fah->UnregisterNotify(newFileUri2, myObserver2);
372         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
373         result = g_fah->UnregisterNotify(newFileUri3, myObserver3);
374         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
375         result = g_fah->Delete(renameFileUri1);
376         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
377     } catch (...) {
378         GTEST_LOG_(ERROR) << "external_file_access_notify_0001 occurs an exception.";
379     }
380     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0001";
381 }
382 
ReadyRegisterNotify02(Uri & parentUri)383 static tuple<Uri, Uri, Uri, Uri> ReadyRegisterNotify02(Uri& parentUri)
384 {
385     Uri newDirUriTest1("");
386     EXPECT_NE(g_fah, nullptr);
387     int result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
388     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
389     Uri newDirUriTest2("");
390     result = g_fah->Mkdir(parentUri, "uri_dir2", newDirUriTest2);
391     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
392     Uri newFileUri1("");
393     result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
394     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
395     Uri newFileUri2("");
396     result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
397     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
398 
399     return {newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2};
400 }
401 
TriggerNotify02(Uri & newDirUriTest1,Uri & newDirUriTest2,Uri & newFileUri1,Uri & newFileUri2)402 static tuple<Uri, Uri> TriggerNotify02(Uri& newDirUriTest1, Uri& newDirUriTest2, Uri& newFileUri1, Uri& newFileUri2)
403 {
404     const int tm = 2;
405     Uri testFile("");
406     EXPECT_NE(g_fah, nullptr);
407     int result = g_fah->CreateFile(newDirUriTest1, "test_file", testFile);
408     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
409     usleep(SLEEP_TIME);
410     Uri renameDirUri1("");
411     result = g_fah->Rename(newDirUriTest1, "renameDir", renameDirUri1);
412     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
413     usleep(SLEEP_TIME);
414     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
415     EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString());
416     result = g_fah->Delete(newDirUriTest2);
417     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
418     usleep(SLEEP_TIME);
419     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
420     EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString());
421     Uri renameFileUri1("");
422     result = g_fah->Rename(newFileUri1, "renamefile", renameFileUri1);
423     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
424     usleep(SLEEP_TIME);
425     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
426     EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
427     result = g_fah->Delete(newFileUri2);
428     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
429     usleep(SLEEP_TIME);
430     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
431     EXPECT_EQ(g_notifyUri, newFileUri2.ToString());
432     sleep(tm);
433     return {renameDirUri1, renameFileUri1};
434 }
435 /**
436  * @tc.number: user_file_service_external_file_access_notify_0002
437  * @tc.name: external_file_access_notify_0002
438  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
439  * @tc.size: MEDIUM
440  * @tc.type: FUNC
441  * @tc.level Level 1
442  * @tc.require: SR000H0386
443  */
444 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0002, testing::ext::TestSize.Level1)
445 {
446     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0002";
447     try {
448         vector<RootInfo> info;
449         EXPECT_NE(g_fah, nullptr);
450         int result = g_fah->GetRoots(info);
451         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
452         bool notifyForDescendants = true;
453         vector<sptr<IFileAccessObserver>> observers;
454         const int len = 12;
455         const int group = 3;
456         for (int i = 0; i < len; i++) {
457             observers.emplace_back(new (std::nothrow) MyObserver());
458         }
459         Uri parentUri(info[0].uri);
460         auto [newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2] = ReadyRegisterNotify02(parentUri);
461 
462         for (int i = 0; i < group; i++) {
463             result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, observers[i]);
464             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
465             result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, observers[3 + i]);
466             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
467             result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, observers[6 + i]);
468             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
469             result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, observers[9 + i]);
470             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
471         }
472 
473         auto [renameDirUri1, renameFileUri1] =
474             TriggerNotify02(newDirUriTest1, newDirUriTest2, newFileUri1, newFileUri2);
475 
476         for (int i = 0; i < group; i++) {
477             result = g_fah->UnregisterNotify(newDirUriTest1, observers[i]);
478             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
479             result = g_fah->UnregisterNotify(newDirUriTest2, observers[3 + i]);
480             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
481             result = g_fah->UnregisterNotify(newFileUri1, observers[6 + i]);
482             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
483             result = g_fah->UnregisterNotify(newFileUri2, observers[9 + i]);
484             EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
485         }
486         result = g_fah->Delete(renameDirUri1);
487         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
488         result = g_fah->Delete(renameFileUri1);
489         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
490     } catch (...) {
491         GTEST_LOG_(ERROR) << "external_file_access_notify_0002 occurs an exception.";
492     }
493     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0002";
494 }
495 
496 /**
497  * @tc.number: user_file_service_external_file_access_notify_0003
498  * @tc.name: external_file_access_notify_0003
499  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
500  * @tc.size: MEDIUM
501  * @tc.type: FUNC
502  * @tc.level Level 1
503  * @tc.require: SR000H0386
504  */
505 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0003, testing::ext::TestSize.Level1)
506 {
507     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0003";
508     try {
509         vector<RootInfo> info;
510         EXPECT_NE(g_fah, nullptr);
511         int result = g_fah->GetRoots(info);
512         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
513         bool notifyForDescendants = true;
514         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
515         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
516         Uri parentUri(info[0].uri);
517         Uri newDirUriTest1("");
518         result = g_fah->Mkdir(parentUri, "uri_dir1", newDirUriTest1);
519         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
520         Uri newFileUri1("");
521         result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
522         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
523         result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1);
524         EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
525         result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1);
526         EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
527 
528         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2);
529         EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
530         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2);
531         EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
532         result = g_fah->UnregisterNotify(newDirUriTest1);
533         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
534         result = g_fah->UnregisterNotify(newFileUri1);
535         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
536         result = g_fah->Delete(newDirUriTest1);
537         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
538         result = g_fah->Delete(newFileUri1);
539         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
540     } catch (...) {
541         GTEST_LOG_(ERROR) << "external_file_access_notify_0003 occurs an exception.";
542     }
543     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0003";
544 }
545 
ReadyRegisterNotify05(Uri & parentUri,sptr<IFileAccessObserver> & myObserver1)546 static tuple<Uri, Uri, Uri, Uri> ReadyRegisterNotify05(Uri& parentUri, sptr<IFileAccessObserver>& myObserver1)
547 {
548     bool notifyForDescendants = true;
549     Uri uri_dir("");
550     EXPECT_NE(g_fah, nullptr);
551     int result = g_fah->Mkdir(parentUri, "uri_dir123", uri_dir);
552     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
553     Uri uri_dirSub1("");
554     result = g_fah->Mkdir(uri_dir, "uri_dirSub1", uri_dirSub1);
555     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
556     result = g_fah->RegisterNotify(uri_dir, notifyForDescendants, myObserver1);
557     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
558     result = g_fah->RegisterNotify(uri_dirSub1, notifyForDescendants, myObserver1);
559     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
560     Uri uri_dirSub2("");
561     result = g_fah->Mkdir(uri_dir, "uri_dirSub2", uri_dirSub2);
562     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
563     usleep(SLEEP_TIME);
564     EXPECT_EQ(g_notifyEvent, ADD_EVENT);
565     EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
566     result = g_fah->RegisterNotify(uri_dirSub2, notifyForDescendants, myObserver1);
567     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
568     Uri renameDirUri1("");
569     result = g_fah->Rename(uri_dirSub2, "renameDir1", renameDirUri1);
570     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
571     usleep(SLEEP_TIME);
572     return {uri_dir, uri_dirSub1, uri_dirSub2, renameDirUri1};
573 }
574 
575 /**
576  * @tc.number: user_file_service_external_file_access_notify_0005
577  * @tc.name: external_file_access_notify_0005
578  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS.
579  * @tc.size: MEDIUM
580  * @tc.type: FUNC
581  * @tc.level Level 1
582  * @tc.require: SR000H0386
583  */
584 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0005, testing::ext::TestSize.Level1)
585 {
586     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0005";
587     try {
588         vector<RootInfo> info;
589         EXPECT_NE(g_fah, nullptr);
590         int result = g_fah->GetRoots(info);
591         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
592         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
593         Uri parentUri(info[0].uri);
594         auto [uri_dir, uri_dirSub1, uri_dirSub2, renameDirUri1] = ReadyRegisterNotify05(parentUri, myObserver1);
595         if (g_notifyEvent != MOVED_TO) {
596             if (g_notifyEvent != MOVED_SELF) {
597                 EXPECT_EQ(g_notifyEvent, MOVED_FROM);
598                 EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
599             } else {
600                 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
601                 EXPECT_EQ(g_notifyUri, uri_dirSub2.ToString());
602             }
603         } else {
604             EXPECT_EQ(g_notifyEvent, MOVED_TO);
605             EXPECT_EQ(g_notifyUri, renameDirUri1.ToString());
606         }
607         result = g_fah->Delete(uri_dirSub1);
608         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
609         usleep(SLEEP_TIME);
610         EXPECT_TRUE(g_notifyEvent == MOVED_FROM || g_notifyEvent == MOVED_SELF);
611         EXPECT_EQ(g_notifyUri, uri_dirSub1.ToString());
612 
613         sleep(2);
614         result = g_fah->UnregisterNotify(uri_dir, myObserver1);
615         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
616         result = g_fah->Delete(renameDirUri1);
617         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
618         result = g_fah->Delete(uri_dir);
619         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
620         sleep(1);
621     } catch (...) {
622         GTEST_LOG_(ERROR) << "external_file_access_notify_0005 occurs an exception.";
623     }
624     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0005";
625 }
626 
627 /**
628  * @tc.number: user_file_service_external_file_access_notify_0006
629  * @tc.name: external_file_access_notify_0006
630  * @tc.desc: Duplicate UnregisterNotify failed.
631  * @tc.size: MEDIUM
632  * @tc.type: FUNC
633  * @tc.level Level 1
634  * @tc.require: SR000H0386
635  */
636 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0006, testing::ext::TestSize.Level1)
637 {
638     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0006";
639     try {
640         vector<RootInfo> info;
641         EXPECT_NE(g_fah, nullptr);
642         int result = g_fah->GetRoots(info);
643         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
644         bool notifyForDescendants = true;
645         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
646         Uri parentUri(info[0].uri);
647         Uri newFileUri1("");
648         result = g_fah->CreateFile(parentUri, "uri_file", newFileUri1);
649         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
650         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
651         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
652         result = g_fah->Delete(newFileUri1);
653         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
654         usleep(SLEEP_TIME);
655         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
656         EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
657         sleep(1);
658         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
659         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
660         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
661         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
662     } catch (...) {
663         GTEST_LOG_(ERROR) << "external_file_access_notify_0006 occurs an exception.";
664     }
665     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0006";
666 }
667 
668 /**
669  * @tc.number: user_file_service_external_file_access_notify_0007
670  * @tc.name: external_file_access_notify_0007
671  * @tc.desc: Test function of RegisterNotify and UnregisterNotify failed when callback is Invalid.
672  * @tc.size: MEDIUM
673  * @tc.type: FUNC
674  * @tc.level Level 1
675  * @tc.require: SR000H0386
676  */
677 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0007, testing::ext::TestSize.Level1)
678 {
679     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0007";
680     try {
681         vector<RootInfo> info;
682         EXPECT_NE(g_fah, nullptr);
683         int result = g_fah->GetRoots(info);
684         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
685         sptr<IFileAccessObserver> myObserver1 = nullptr;
686         bool notifyForDescendants = true;
687         Uri parentUri(info[0].uri);
688         Uri newFileUri1("");
689         result = g_fah->CreateFile(parentUri, "uri_file007", newFileUri1);
690         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
691         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
692         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
693         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
694         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
695         result = g_fah->Delete(newFileUri1);
696         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
697     } catch (...) {
698         GTEST_LOG_(ERROR) << "external_file_access_notify_0007 occurs an exception.";
699     }
700     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0007";
701 }
702 
703 /**
704  * @tc.number: user_file_service_external_file_access_notify_0008
705  * @tc.name: external_file_access_notify_0008
706  * @tc.desc: Test function of UnregisterNotify failed when uri is Invalid.
707  * @tc.size: MEDIUM
708  * @tc.type: FUNC
709  * @tc.level Level 1
710  * @tc.require: SR000H0386
711  */
712 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0008, testing::ext::TestSize.Level1)
713 {
714     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0008";
715     try {
716         vector<RootInfo> info;
717         EXPECT_NE(g_fah, nullptr);
718         int result = g_fah->GetRoots(info);
719         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
720         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
721         Uri newFileUri1("*/&%");
722         bool notifyForDescendants = true;
723         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
724         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
725         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
726         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
727     } catch (...) {
728         GTEST_LOG_(ERROR) << "external_file_access_notify_0008 occurs an exception.";
729     }
730     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0008";
731 }
732 
733 /**
734  * @tc.number: user_file_service_external_file_access_notify_0009
735  * @tc.name: external_file_access_notify_0009
736  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS when the file is Chinese name.
737  * @tc.size: MEDIUM
738  * @tc.type: FUNC
739  * @tc.level Level 1
740  * @tc.require: SR000H0386
741  */
742 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0009, testing::ext::TestSize.Level1)
743 {
744     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0009";
745     try {
746         vector<RootInfo> info;
747         EXPECT_NE(g_fah, nullptr);
748         int result = g_fah->GetRoots(info);
749         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
750         bool notifyForDescendants = true;
751         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
752         Uri parentUri(info[0].uri);
753         Uri newFileUri1("");
754         result = g_fah->CreateFile(parentUri, "测试文件", newFileUri1);
755         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
756         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
757         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
758         result = g_fah->Delete(newFileUri1);
759         usleep(SLEEP_TIME);
760         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
761         EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
762         sleep(1);
763         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
764         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
765         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
766     } catch (...) {
767         GTEST_LOG_(ERROR) << "external_file_access_notify_0009 occurs an exception.";
768     }
769     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0009";
770 }
771 
772 /**
773  * @tc.number: user_file_service_external_file_access_notify_0010
774  * @tc.name: external_file_access_notify_0010
775  * @tc.desc: Test function of UnregisterNotify interface for failed when the Uri and callback do not match.
776  * @tc.size: MEDIUM
777  * @tc.type: FUNC
778  * @tc.level Level 1
779  * @tc.require: SR000H0386
780  */
781 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0010, testing::ext::TestSize.Level1)
782 {
783     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0010";
784     try {
785         vector<RootInfo> info;
786         EXPECT_NE(g_fah, nullptr);
787         int result = g_fah->GetRoots(info);
788         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
789         bool notifyForDescendants = true;
790         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
791         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
792         Uri parentUri(info[0].uri);
793         Uri newFileUri1("");
794         result = g_fah->CreateFile(parentUri, "uri_file1", newFileUri1);
795         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
796         Uri newFileUri2("");
797         result = g_fah->CreateFile(parentUri, "uri_file2", newFileUri2);
798         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
799         result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver1);
800         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
801         result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver2);
802         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
803 
804         result = g_fah->UnregisterNotify(newFileUri1, myObserver2);
805         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
806         result = g_fah->UnregisterNotify(newFileUri1, myObserver1);
807         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
808         result = g_fah->UnregisterNotify(newFileUri2, myObserver2);
809         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
810         result = g_fah->Delete(newFileUri1);
811         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
812         result = g_fah->Delete(newFileUri2);
813         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
814     } catch (...) {
815         GTEST_LOG_(ERROR) << "external_file_access_notify_0010 occurs an exception.";
816     }
817     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0010";
818 }
819 
ReadyRegisterNotify11(Uri & parentUri,Uri & newFileDir1,sptr<IFileAccessObserver> & myObserver1,sptr<IFileAccessObserver> & myObserver2)820 static tuple<Uri, Uri> ReadyRegisterNotify11(Uri& parentUri, Uri &newFileDir1, sptr<IFileAccessObserver>& myObserver1,
821     sptr<IFileAccessObserver>& myObserver2)
822 {
823     const int tm = SLEEP_TIME * 2;
824     bool notifyForDescendants1 = true;
825     EXPECT_NE(g_fah, nullptr);
826     int result = g_fah->Mkdir(parentUri, "uri_dir11", newFileDir1);
827     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
828     Uri newFileUri1("");
829     result = g_fah->CreateFile(newFileDir1, "uri_file11", newFileUri1);
830     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
831     sleep(1);
832     result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants1, myObserver1);
833     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
834     result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants1, myObserver2);
835     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
836     Uri renameFileUri1("");
837     result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1);
838     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
839     usleep(tm);
840 
841     return {newFileUri1, renameFileUri1};
842 }
843 
844 /**
845  * @tc.number: user_file_service_external_file_access_notify_0011
846  * @tc.name: external_file_access_notify_0011
847  * @tc.desc: Test function of RegisterNotify interface for SUCCESS when set the notifyForDescendants to true and
848  * @tc.desc: then set to false
849  * @tc.size: MEDIUM
850  * @tc.type: FUNC
851  * @tc.level Level 1
852  * @tc.require: SR000H0386
853  */
854 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0011, testing::ext::TestSize.Level1)
855 {
856     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0011";
857     try {
858         vector<RootInfo> info;
859         EXPECT_NE(g_fah, nullptr);
860         int result = g_fah->GetRoots(info);
861         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
862         bool notifyForDescendants2 = false;
863         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) TestObserver());
864         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
865         Uri parentUri(info[0].uri);
866         Uri newFileDir1("");
867         auto [newFileUri1, renameFileUri1] =
868             ReadyRegisterNotify11(parentUri, newFileDir1, myObserver1, myObserver2);
869         usleep(SLEEP_TIME * 2);
870         if (g_notifyEvent != MOVED_TO) {
871             if (g_notifyEvent != MOVED_SELF) {
872                 EXPECT_EQ(g_notifyEvent, MOVED_FROM);
873                 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
874             } else {
875                 EXPECT_EQ(g_notifyEvent, MOVED_SELF);
876                 EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
877             }
878         } else {
879             EXPECT_EQ(g_notifyEvent, MOVED_TO);
880             EXPECT_EQ(g_notifyUri, renameFileUri1.ToString());
881         }
882         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants2, myObserver1);
883         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
884         sleep(2);
885         result = g_fah->Delete(renameFileUri1);
886         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
887         usleep(SLEEP_TIME);
888         EXPECT_EQ(g_notifyEvent, MOVED_SELF);
889         EXPECT_EQ(g_notifyUri, newFileUri1.ToString());
890         EXPECT_NE(g_notifyFlag, MOVED_SELF);
891 
892         sleep(1);
893         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
894         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
895         result = g_fah->UnregisterNotify(newFileUri1, myObserver2);
896         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
897         result = g_fah->Delete(newFileDir1);
898         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
899     } catch (...) {
900         GTEST_LOG_(ERROR) << "external_file_access_notify_0011 occurs an exception.";
901     }
902     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0011";
903 }
904 
RegisterDirNotify(Uri parentUri,std::string dirName,IFileAccessObserver * observer)905 static void RegisterDirNotify(Uri parentUri, std::string dirName, IFileAccessObserver *observer)
906 {
907     Uri newDirUriTest("");
908     bool notifyForDescendants = true;
909     EXPECT_NE(g_fah, nullptr);
910     int result = g_fah->Mkdir(parentUri, dirName, newDirUriTest);
911     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
912     sptr<IFileAccessObserver> myObserver(observer);
913     result = g_fah->RegisterNotify(newDirUriTest, notifyForDescendants, myObserver);
914     EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK);
915     result = g_fah->Delete(newDirUriTest);
916     EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
917     usleep(SLEEP_TIME);
918     EXPECT_EQ(g_notifyEvent, MOVED_SELF);
919     EXPECT_EQ(g_notifyUri, newDirUriTest.ToString());
920     sleep(2);
921     g_fah->UnregisterNotify(newDirUriTest, myObserver);
922 }
923 
924 /**
925  * @tc.number: user_file_service_external_file_access_notify_0012
926  * @tc.name: external_file_access_notify_0012
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_00012, testing::ext::TestSize.Level1)
934 {
935     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00012";
936     try {
937         vector<RootInfo> info;
938         EXPECT_NE(g_fah, nullptr);
939         int result = g_fah->GetRoots(info);
940         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
941         Uri parentUri(info[0].uri);
942         Uri newFileUri1("");
943         GTEST_LOG_(INFO) << parentUri.ToString();
944         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
945         for (int i = 0; i < INIT_THREADS_NUMBER; i++) {
946             std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr());
947             execthread1.join();
948         }
949     } catch (...) {
950         GTEST_LOG_(ERROR) << "external_file_access_notify_00012 occurs an exception.";
951     }
952     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00012";
953 }
954 
955 /**
956  * @tc.number: user_file_service_external_file_access_notify_0013
957  * @tc.name: external_file_access_notify_0013
958  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
959  * @tc.size: MEDIUM
960  * @tc.type: FUNC
961  * @tc.level Level 1
962  * @tc.require: SR000H0386
963  */
964 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00013, testing::ext::TestSize.Level1)
965 {
966     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00013";
967     try {
968         vector<RootInfo> info;
969         EXPECT_NE(g_fah, nullptr);
970         int result = g_fah->GetRoots(info);
971         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
972         Uri parentUri(info[0].uri);
973         GTEST_LOG_(INFO) << parentUri.ToString();
974         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
975         std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr());
976         execthread1.join();
977         std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver1.GetRefPtr());
978         execthread2.join();
979         std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver1.GetRefPtr());
980         execthread3.join();
981     } catch (...) {
982         GTEST_LOG_(ERROR) << "external_file_access_notify_00013 occurs an exception.";
983     }
984     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00013";
985 }
986 
987 /**
988  * @tc.number: user_file_service_external_file_access_notify_0014
989  * @tc.name: external_file_access_notify_0014
990  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
991  * @tc.size: MEDIUM
992  * @tc.type: FUNC
993  * @tc.level Level 1
994  * @tc.require: SR000H0386
995  */
996 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00014, testing::ext::TestSize.Level1)
997 {
998     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00014";
999     try {
1000         vector<RootInfo> info;
1001         EXPECT_NE(g_fah, nullptr);
1002         int result = g_fah->GetRoots(info);
1003         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1004         Uri parentUri(info[0].uri);
1005         GTEST_LOG_(INFO) << parentUri.ToString();
1006         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
1007         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
1008         sptr<IFileAccessObserver> myObserver3 = sptr(new (std::nothrow) MyObserver());
1009 
1010         std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest", myObserver1.GetRefPtr());
1011         execthread1.join();
1012         sleep(1);
1013         std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest", myObserver2.GetRefPtr());
1014         execthread2.join();
1015         sleep(1);
1016         std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest", myObserver3.GetRefPtr());
1017         execthread3.join();
1018     } catch (...) {
1019         GTEST_LOG_(ERROR) << "external_file_access_notify_00014 occurs an exception.";
1020     }
1021     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00014";
1022 }
1023 
1024 /**
1025  * @tc.number: user_file_service_external_file_access_notify_0015
1026  * @tc.name: external_file_access_notify_0015
1027  * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS which Concurrent.
1028  * @tc.size: MEDIUM
1029  * @tc.type: FUNC
1030  * @tc.level Level 1
1031  * @tc.require: SR000H0386
1032  */
1033 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_00015, testing::ext::TestSize.Level1)
1034 {
1035     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_00015";
1036     try {
1037         vector<RootInfo> info;
1038         EXPECT_NE(g_fah, nullptr);
1039         int result = g_fah->GetRoots(info);
1040         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1041         Uri parentUri(info[0].uri);
1042         GTEST_LOG_(INFO) << parentUri.ToString();
1043         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
1044         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
1045         sptr<IFileAccessObserver> myObserver3 = sptr(new (std::nothrow) MyObserver());
1046 
1047         std::thread execthread1(RegisterDirNotify, parentUri, "WatcherTest1", myObserver1.GetRefPtr());
1048         execthread1.join();
1049         std::thread execthread2(RegisterDirNotify, parentUri, "WatcherTest2", myObserver2.GetRefPtr());
1050         execthread2.join();
1051         std::thread execthread3(RegisterDirNotify, parentUri, "WatcherTest3", myObserver3.GetRefPtr());
1052         execthread3.join();
1053     } catch (...) {
1054         GTEST_LOG_(ERROR) << "external_file_access_notify_00015 occurs an exception.";
1055     }
1056     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_00015";
1057 }
1058 
1059 /**
1060  * @tc.number: user_file_service_external_file_access_notify_0016
1061  * @tc.name: external_file_access_notify_0016
1062  * @tc.desc: Test UnregisterNotify all callbacks related to the current uri
1063  * @tc.size: MEDIUM
1064  * @tc.type: FUNC
1065  * @tc.level Level 1
1066  * @tc.require: SR000H0386
1067  */
1068 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0016, testing::ext::TestSize.Level1)
1069 {
1070     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0016";
1071     try {
1072         vector<RootInfo> info;
1073         EXPECT_NE(g_fah, nullptr);
1074         int result = g_fah->GetRoots(info);
1075         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1076         bool notifyForDescendants = true;
1077         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
1078         sptr<IFileAccessObserver> myObserver2 = sptr(new (std::nothrow) MyObserver());
1079         sptr<IFileAccessObserver> myObserver3 = sptr(new (std::nothrow) MyObserver());
1080         Uri parentUri(info[0].uri);
1081         Uri newFileDir1("");
1082         result = g_fah->Mkdir(parentUri, "uri_dir0016", newFileDir1);
1083         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1084         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1085         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1086         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver2);
1087         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1088         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver3);
1089         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1090 
1091         result = g_fah->UnregisterNotify(newFileDir1);
1092         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1093         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1094         EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK);
1095 
1096         result = g_fah->Delete(newFileDir1);
1097         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1098     } catch (...) {
1099         GTEST_LOG_(ERROR) << "external_file_access_notify_0016 occurs an exception.";
1100     }
1101     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0016";
1102 }
1103 
1104 /**
1105  * @tc.number: user_file_service_external_file_access_notify_0017
1106  * @tc.name: external_file_access_notify_0017
1107  * @tc.desc: Test event changes exceeding 500ms trigger callback
1108  * @tc.size: MEDIUM
1109  * @tc.type: FUNC
1110  * @tc.level Level 1
1111  * @tc.require: SR000H0386
1112  */
1113 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0017, testing::ext::TestSize.Level1)
1114 {
1115     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0017";
1116     try {
1117         vector<RootInfo> info;
1118         EXPECT_NE(g_fah, nullptr);
1119         int result = g_fah->GetRoots(info);
1120         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1121         bool notifyForDescendants = true;
1122         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
1123         Uri parentUri(info[0].uri);
1124         Uri newFileDir1("");
1125         result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1);
1126         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1127         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1128         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1129         Uri fileUri("");
1130         result = g_fah->CreateFile(newFileDir1, "uri_file", fileUri);
1131         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1132         usleep(SLEEP_TIME);
1133         EXPECT_EQ(g_notifyEvent, ADD_EVENT);
1134 
1135         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1136         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1137 
1138         result = g_fah->Delete(newFileDir1);
1139         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1140     } catch (...) {
1141         GTEST_LOG_(ERROR) << "external_file_access_notify_0017 occurs an exception.";
1142     }
1143     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0017";
1144 }
1145 
1146 /**
1147  * @tc.number: user_file_service_external_file_access_notify_0018
1148  * @tc.name: external_file_access_notify_0018
1149  * @tc.desc: Test event changes More than 32 notifications callback
1150  * @tc.size: MEDIUM
1151  * @tc.type: FUNC
1152  * @tc.level Level 1
1153  * @tc.require: SR000H0386
1154  */
1155 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0018, testing::ext::TestSize.Level1)
1156 {
1157     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0018";
1158     try {
1159         vector<RootInfo> info;
1160         EXPECT_NE(g_fah, nullptr);
1161         int result = g_fah->GetRoots(info);
1162         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1163         bool notifyForDescendants = true;
1164         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) TestObserver());
1165         Uri parentUri(info[0].uri);
1166         Uri newFileDir1("");
1167         result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1);
1168         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1169         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1170         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1171         Uri fileUri("");
1172         for (int i = 0; i < 64; i++) {
1173             result = g_fah->CreateFile(newFileDir1, "uri_file" + to_string(i), fileUri);
1174             EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1175         }
1176         sleep(1);
1177         EXPECT_LE(g_notifyUris.size(), 32);
1178         g_notifyUris.clear();
1179         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1180         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1181 
1182         result = g_fah->Delete(newFileDir1);
1183         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1184     } catch (...) {
1185         GTEST_LOG_(ERROR) << "external_file_access_notify_0018 occurs an exception.";
1186     }
1187     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0018";
1188 }
1189 
1190 /**
1191  * @tc.number: user_file_service_external_file_access_notify_0019
1192  * @tc.name: external_file_access_notify_0019
1193  * @tc.desc: Test event changes More than one notifications callback
1194  * @tc.size: MEDIUM
1195  * @tc.type: FUNC
1196  * @tc.level Level 1
1197  * @tc.require: SR000H0386
1198  */
1199 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0019, testing::ext::TestSize.Level1)
1200 {
1201     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0019";
1202     try {
1203         vector<RootInfo> info;
1204         EXPECT_NE(g_fah, nullptr);
1205         int result = g_fah->GetRoots(info);
1206         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1207         bool notifyForDescendants = true;
1208         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) TestObserver());
1209         Uri parentUri(info[0].uri);
1210         Uri newFileDir1("");
1211         result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1);
1212         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1213         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1214         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1215         Uri fileUri("");
1216         for (int i = 0; i < 10; i++) {
1217             result = g_fah->CreateFile(newFileDir1, "uri_file" + to_string(i), fileUri);
1218             EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1219         }
1220         usleep(SLEEP_TIME);
1221         EXPECT_GT(g_notifyUris.size(), 1);
1222         g_notifyUris.clear();
1223         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1224         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1225 
1226         result = g_fah->Delete(newFileDir1);
1227         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1228     } catch (...) {
1229         GTEST_LOG_(ERROR) << "external_file_access_notify_0019 occurs an exception.";
1230     }
1231     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0019";
1232 }
1233 
1234 HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0020, testing::ext::TestSize.Level1)
1235 {
1236     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0020";
1237     try {
1238         g_notifyEvent = -1;
1239         vector<RootInfo> info;
1240         const char* perms[] = {
1241             "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"
1242         };
1243         SetNativeToken("SetUpTestCase", perms, sizeof(perms) / sizeof(perms[0]));
1244         EXPECT_NE(g_fah, nullptr);
1245         int result = g_fah->GetRoots(info);
1246         EXPECT_EQ(result, OHOS::FileAccessFwk::E_PERMISSION);
1247         SetNativeToken();
1248         result = g_fah->GetRoots(info);
1249         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1250 
1251         bool notifyForDescendants = true;
1252         sptr<IFileAccessObserver> myObserver1 = sptr(new (std::nothrow) MyObserver());
1253         Uri parentUri(info[0].uri);
1254         Uri newFileDir1("");
1255         SetNativeToken("SetUpTestCase", perms, sizeof(perms) / sizeof(perms[0]));
1256         result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1);
1257         EXPECT_EQ(result, OHOS::FileAccessFwk::E_PERMISSION);
1258         SetNativeToken();
1259         result = g_fah->Mkdir(parentUri, "uri_dir", newFileDir1);
1260         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1261 
1262         SetNativeToken("SetUpTestCase", perms, sizeof(perms) / sizeof(perms[0]));
1263         result = g_fah->RegisterNotify(newFileDir1, notifyForDescendants, myObserver1);
1264         EXPECT_EQ(result, OHOS::FileAccessFwk::E_PERMISSION);
1265 
1266         result = g_fah->UnregisterNotify(newFileDir1, myObserver1);
1267         EXPECT_EQ(result, OHOS::FileAccessFwk::E_PERMISSION);
1268 
1269         result = g_fah->Delete(newFileDir1);
1270         EXPECT_EQ(result, OHOS::FileAccessFwk::E_PERMISSION);
1271 
1272         SetNativeToken();
1273         result = g_fah->Delete(newFileDir1);
1274         EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
1275 
1276     } catch (...) {
1277         GTEST_LOG_(ERROR) << "external_file_access_notify_0020 occurs an exception.";
1278     }
1279     GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0020";
1280 }
1281 } // namespace
1282