• 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 "data_share_test.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "data_publisher.h"
22 #include "data_publisher_sys_event_callback.h"
23 #include "data_share_common.h"
24 #include "data_share_dao.h"
25 #include "data_share_store.h"
26 #include "data_share_util.h"
27 #include "file_util.h"
28 #include "ret_code.h"
29 #include "sys_event.h"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace {
34 const char TEST_FILE_PATH[] = "/data/test/data_share_test/";
35 constexpr int DB_SUCC = 0;
36 constexpr int DB_FAILED = -1;
37 constexpr int INDEX_0 = 0;
38 constexpr int INDEX_1 = 1;
39 
GetTestDir(std::string & testCaseName)40 std::string GetTestDir(std::string& testCaseName)
41 {
42     std::string workPath = std::string(TEST_FILE_PATH);
43     if (workPath.back() != '/') {
44         workPath = workPath + "/";
45     }
46     workPath.append(testCaseName);
47     workPath.append("/");
48     std::string testDir = workPath;
49     if (!FileUtil::FileExists(testDir)) {
50         FileUtil::ForceCreateDirectory(testDir, FileUtil::FILE_PERM_770);
51     }
52     return testDir;
53 }
54 
GenerateTestFileName(std::string & testCaseName,int index)55 std::string GenerateTestFileName(std::string& testCaseName, int index)
56 {
57     return GetTestDir(testCaseName) + "testFile" + std::to_string(index);
58 }
59 }
60 
SetUpTestCase()61 void DataShareTest::SetUpTestCase() {}
62 
TearDownTestCase()63 void DataShareTest::TearDownTestCase() {}
64 
SetUp()65 void DataShareTest::SetUp() {}
66 
TearDown()67 void DataShareTest::TearDown() {}
68 
69 /**
70  * @tc.name: TestDataShareStore001
71  * @tc.desc: test dbStore api
72  * @tc.type: FUNC
73  * @tc.require: SR000I1G43
74  */
75 HWTEST_F(DataShareTest, TestDataShareStore001, testing::ext::TestSize.Level3)
76 {
77     std::string emptyTestDir = "";
78     auto dataShareStore = std::make_shared<DataShareStore>(emptyTestDir);
79     auto dbstore = dataShareStore->GetDbStore();
80     EXPECT_EQ(dbstore, nullptr);
81     std::string tableName = "subscribe_events";
82     auto ret = dataShareStore->DropTable(tableName);
83     EXPECT_EQ(ret, DB_FAILED);
84     ret = dataShareStore->DestroyDbStore();
85     EXPECT_EQ(ret, DB_SUCC);
86     std::string dataBaseTestDir = "/data/log/hiview/system_event_db/subscriber_test_tmp/";
87     dataShareStore = std::make_shared<DataShareStore>(dataBaseTestDir);
88     dbstore = dataShareStore->GetDbStore();
89     EXPECT_NE(dbstore, nullptr);
90     ret = dataShareStore->DropTable(tableName);
91     EXPECT_EQ(ret, DB_SUCC);
92     ret = dataShareStore->DestroyDbStore();
93     EXPECT_EQ(ret, DB_SUCC);
94 }
95 
96 /**
97  * @tc.name: TestDataShareDao001
98  * @tc.desc: test method defined in namespace DataShareDao
99  * @tc.type: FUNC
100  * @tc.require: SR000I1G43
101  */
102 HWTEST_F(DataShareTest, TestDataShareDao001, testing::ext::TestSize.Level3)
103 {
104     std::string dataBaseTestDir = "/data/log/hiview/system_event_db/subscriber_test/";
105     auto dataShareDao = std::make_shared<DataShareDao>(std::make_shared<DataShareStore>(dataBaseTestDir));
106     int32_t uid = 20010039;
107     std::string events = "event1;event2";
108     auto result = dataShareDao->SaveSubscriberInfo(uid, events);
109     EXPECT_EQ(result, DB_SUCC);
110     auto res = dataShareDao->IsUidExists(uid);
111     ASSERT_TRUE(res);
112     std::string eventList;
113     result = dataShareDao->GetEventListByUid(uid, eventList);
114     EXPECT_EQ(result, DB_SUCC);
115     std::string bundleName;
116     result = dataShareDao->GetUidByBundleName(bundleName, uid);
117     EXPECT_EQ(result, DB_SUCC);
118     std::map<int, std::string> uidToEventsMap;
119     result = dataShareDao->GetTotalSubscriberInfo(uidToEventsMap);
120     EXPECT_EQ(result, DB_SUCC);
121     events = "event1;event3";
122     result = dataShareDao->SaveSubscriberInfo(uid, events);
123     EXPECT_EQ(result, DB_SUCC);
124     result = dataShareDao->DeleteSubscriberInfo(uid);
125     EXPECT_EQ(result, DB_SUCC);
126 }
127 
128 /**
129  * @tc.name: DataShareUtilTest001
130  * @tc.desc: Test method defined in namespace DataShareUtil
131  * @tc.type: FUNC
132  * @tc.require: SR000I1G43
133  */
134 HWTEST_F(DataShareTest, DataShareUtilTest001, testing::ext::TestSize.Level3)
135 {
136     std::string testCaseName("DataShareUtilTest001");
137     int expectedFailedRet = -1;
138     auto ret = DataShareUtil::CopyFile("//......./invalid_dest_file",
139         GenerateTestFileName(testCaseName, INDEX_1).c_str());
140     ASSERT_EQ(expectedFailedRet, ret);
141     (void)FileUtil::SaveStringToFile(GenerateTestFileName(testCaseName, INDEX_0).c_str(), "test0");
142     ret = DataShareUtil::CopyFile(GenerateTestFileName(testCaseName, INDEX_0).c_str(), "//...../invalid_dest_file");
143     ASSERT_EQ(expectedFailedRet, ret);
144     (void)FileUtil::SaveStringToFile(GenerateTestFileName(testCaseName, INDEX_1).c_str(), "test1");
145     ret = DataShareUtil::CopyFile(GenerateTestFileName(testCaseName, INDEX_0).c_str(),
146         GenerateTestFileName(testCaseName, INDEX_1).c_str());
147     int expectedSuccessRet = 0;
148     ASSERT_EQ(expectedSuccessRet, ret);
149     int32_t uid = 20010039;
150     std::string sandbox = DataShareUtil::GetSandBoxPathByUid(uid);
151     ASSERT_FALSE(sandbox.empty());
152     ASSERT_GE(DataShareUtil::GetBundleNameById(uid).size(), 0);
153     ASSERT_TRUE(DataShareUtil::GetBundleNameById(-1).empty());
154     uid = DataShareUtil::GetUidByBundleName("hiview");
155     ASSERT_GE(uid, -1); // -1 is expected value
156 }
157 
158 /**
159  * @tc.name: DataPublisherTest001
160  * @tc.desc: Test method defined in DataPublisher
161  * @tc.type: FUNC
162  * @tc.require: SR000I1G43
163  */
164 HWTEST_F(DataShareTest, DataPublisherTest001, testing::ext::TestSize.Level3)
165 {
166     auto dataPublisher = std::make_shared<DataPublisher>();
167     std::vector<std::string> events { "TEST_EVENT_NAME1", "TEST_EVENT_NAME2" };
168     int32_t uid = 20010039;
169     auto ret = dataPublisher->AddSubscriber(uid, events);
170     int expectedSuccessRet = 0;
171     ASSERT_EQ(expectedSuccessRet, ret);
172     ret = dataPublisher->RemoveSubscriber(uid);
173     ASSERT_EQ(expectedSuccessRet, ret);
174     ret = dataPublisher->RemoveSubscriber(uid);
175     ASSERT_EQ(ERR_REMOVE_SUBSCRIBE, ret);
176     SysEventCreator sysEventCreator("TEST_DOMAIN", "TEST_EVENT_NAME1", SysEventCreator::FAULT);
177     auto sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
178     dataPublisher->OnSysEvent(sysEvent);
179     ASSERT_TRUE(true);
180     ret = dataPublisher->AddSubscriber(uid, events);
181     ASSERT_EQ(expectedSuccessRet, ret);
182     dataPublisher->OnSysEvent(sysEvent);
183     ASSERT_TRUE(true);
184     sysEventCreator = SysEventCreator("BUNDLE_MANAGER", "BUNDLE_UNINSTALL", SysEventCreator::FAULT);
185     sysEventCreator.SetKeyValue("BUNDLE_NAME", "com.test.demo");
186     sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
187     dataPublisher->OnSysEvent(sysEvent);
188     ASSERT_TRUE(true);
189     dataPublisher->SetWorkLoop(nullptr);
190 }
191 
192 /**
193  * @tc.name: DataPublisherTest002
194  * @tc.desc: Test method defined in DataPublisher
195  * @tc.type: FUNC
196  * @tc.require: issueICJ952
197  */
198 HWTEST_F(DataShareTest, DataPublisherTest002, testing::ext::TestSize.Level3)
199 {
200     auto dataPublisher = std::make_shared<DataPublisher>();
201     ASSERT_GE(dataPublisher->GetTimeStampByUid(0), 0); // 0 is expected value to compare
202 }
203 
204 /**
205  * @tc.name: DataPublisherSysEventCallbackTest001
206  * @tc.desc: Test method defined in DataPublisherSysEventCallback
207  * @tc.type: FUNC
208  * @tc.require: SR000I1G43
209  */
210 HWTEST_F(DataShareTest, DataPublisherSysEventCallbackTest001, testing::ext::TestSize.Level3)
211 {
212     DataPublisherSysEventCallback callback1("/data/log/hiview/system_event_db/events/testSrc1.evt",
213         "/data/log/hiview/system_event_db/events/DomainTest1", 0, 0);
214     std::vector<std::u16string> eventList1 = { u"test event" };
215     std::vector<int64_t> emptySeqList;
216     callback1.OnQuery(eventList1, emptySeqList);
217     callback1.OnComplete(0, 10, 12345);
218     ASSERT_TRUE(true);
219     DataPublisherSysEventCallback callback2("/data/log/hiview/system_event_db/events/testSrc2.evt",
220         "/data/log/hiview/system_event_db/events/DomainTest2", 0, MAXIMUM_FILE_SIZE);
221     std::vector<std::u16string> eventList2 = { u"test event2" };
222     callback2.OnQuery(eventList2, emptySeqList);
223     callback2.OnComplete(0, 10, 12345);
224     ASSERT_TRUE(true);
225     DataPublisherSysEventCallback callback3("//......./invalid_dest_file",
226         "/data/log/hiview/system_event_db/events/DomainTest3", 0, 0);
227     callback3.HandleEventFile("//......./invalid_src_file", "/data/log/hiview/system_event_db/events/Domain3");
228     ASSERT_TRUE(true);
229 }
230 }
231 }