• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "pasteboard_client.h"
17 #include "unistd.h"
18 #include <gtest/gtest.h>
19 
20 namespace OHOS::MiscServices {
21 using namespace testing::ext;
22 using namespace testing;
23 using namespace OHOS::Media;
24 constexpr const uid_t EDM_UID = 3057;
25 using Patterns = std::set<Pattern>;
26 class PasteboardClientTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp() override;
31     void TearDown() override;
32 };
33 
SetUpTestCase(void)34 void PasteboardClientTest::SetUpTestCase(void)
35 {
36     setuid(EDM_UID);
37 }
38 
TearDownTestCase(void)39 void PasteboardClientTest::TearDownTestCase(void)
40 {
41     setuid(0);
42 }
43 
SetUp(void)44 void PasteboardClientTest::SetUp(void)
45 {
46 }
47 
TearDown(void)48 void PasteboardClientTest::TearDown(void)
49 {
50 }
51 
52 /**
53 * @tc.name: IsRemoteData001
54 * @tc.desc: pasteData is local data.
55 * @tc.type: FUNC
56 * @tc.require:
57 * @tc.author:
58 */
59 HWTEST_F(PasteboardClientTest, IsRemoteData001, TestSize.Level0)
60 {
61     std::string plainText = "plain text";
62     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
63     PasteboardClient::GetInstance()->SetPasteData(*pasteData);
64     bool ret = PasteboardClient::GetInstance()->IsRemoteData();
65     ASSERT_FALSE(ret);
66 }
67 
68 /**
69 * @tc.name: IsRemoteData002
70 * @tc.desc: pasteData is remote data.
71 * @tc.type: FUNC
72 * @tc.require:
73 * @tc.author:
74 */
75 HWTEST_F(PasteboardClientTest, IsRemoteData002, TestSize.Level0)
76 {
77     std::string plainText = "plain text";
78     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
79     pasteData->SetRemote(true);
80     PasteboardClient::GetInstance()->SetPasteData(*pasteData);
81     bool ret = PasteboardClient::GetInstance()->IsRemoteData();
82     ASSERT_TRUE(ret);
83 }
84 
85 /**
86 * @tc.name: HasDataType001
87 * @tc.desc: data type is MIMETYPE_TEXT_PLAIN.
88 * @tc.type: FUNC
89 * @tc.require:
90 * @tc.author:
91 */
92 HWTEST_F(PasteboardClientTest, HasDataType001, TestSize.Level0)
93 {
94     std::string plainText = "helloWorld";
95     auto newData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
96     PasteboardClient::GetInstance()->SetPasteData(*newData);
97     auto ret = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_PLAIN);
98     ASSERT_TRUE(ret);
99     auto result = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_URI);
100     ASSERT_FALSE(result);
101 }
102 
103 /**
104 * @tc.name: HasDataType002
105 * @tc.desc: data type is MIMETYPE_TEXT_HTML.
106 * @tc.type: FUNC
107 * @tc.require:
108 * @tc.author:
109 */
110 HWTEST_F(PasteboardClientTest, HasDataType002, TestSize.Level0)
111 {
112     std::string htmlText = "<div class='disable'>helloWorld</div>";
113     auto newPasteData = PasteboardClient::GetInstance()->CreateHtmlData(htmlText);
114     PasteboardClient::GetInstance()->SetPasteData(*newPasteData);
115     auto ret = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_HTML);
116     ASSERT_TRUE(ret);
117     auto result = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_PLAIN);
118     ASSERT_FALSE(result);
119 }
120 
121 /**
122 * @tc.name: HasDataType003
123 * @tc.desc: data type is MIMETYPE_TEXT_URI
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author:
127 */
128 HWTEST_F(PasteboardClientTest, HasDataType003, TestSize.Level0)
129 {
130     OHOS::Uri uri("uri");
131     auto newPasteData = PasteboardClient::GetInstance()->CreateUriData(uri);
132     PasteboardClient::GetInstance()->SetPasteData(*newPasteData);
133     auto ret = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_URI);
134     ASSERT_TRUE(ret);
135     auto result = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_PLAIN);
136     ASSERT_FALSE(result);
137 }
138 
139 /**
140 * @tc.name: HasDataType004
141 * @tc.desc: data type is MIMETYPE_PIXELMAP
142 * @tc.type: FUNC
143 * @tc.require:
144 * @tc.author:
145 */
146 HWTEST_F(PasteboardClientTest, HasDataType004, TestSize.Level0)
147 {
148     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
149     InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888 };
150     std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
151     std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
152     auto newPasteData = PasteboardClient::GetInstance()->CreatePixelMapData(pixelMapIn);
153     PasteboardClient::GetInstance()->SetPasteData(*newPasteData);
154     auto ret = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_PIXELMAP);
155     ASSERT_TRUE(ret);
156     auto result = PasteboardClient::GetInstance()->HasDataType(MIMETYPE_TEXT_URI);
157     ASSERT_FALSE(result);
158 }
159 
160 /**
161 * @tc.name: GetDataSource001
162 * @tc.desc: Get the source of the data.
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author:
166 */
167 HWTEST_F(PasteboardClientTest, GetDataSource001, TestSize.Level0)
168 {
169     std::string plainText = "helloWorld";
170     auto newData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
171     PasteboardClient::GetInstance()->SetPasteData(*newData);
172     std::string bundleName;
173     PasteboardClient::GetInstance()->GetDataSource(bundleName);
174     EXPECT_FALSE(bundleName.empty());
175 }
176 
177 /**
178 * @tc.name: SetGlobalShareOption
179 * @tc.desc: Set global shareOption
180 * @tc.type: FUNC
181 * @tc.require:
182 * @tc.author:
183 */
184 HWTEST_F(PasteboardClientTest, SetGlobalShareOption, TestSize.Level0)
185 {
186     std::map<uint32_t, ShareOption> settings = {
187         {100, ShareOption::InApp},
188         {200, ShareOption::LocalDevice},
189         {300, ShareOption::CrossDevice}};
190     PasteboardClient::GetInstance()->SetGlobalShareOption(settings);
191     auto result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
192     EXPECT_TRUE(result.size() == 3);
193     EXPECT_EQ(result[100], ShareOption::InApp);
194     EXPECT_EQ(result[200], ShareOption::LocalDevice);
195     EXPECT_EQ(result[300], ShareOption::CrossDevice);
196     std::map<uint32_t, ShareOption> modify = {{100, ShareOption::CrossDevice},
197                                               {400, ShareOption::InApp}};
198     PasteboardClient::GetInstance()->SetGlobalShareOption(modify);
199     result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
200     EXPECT_TRUE(result.size() == 4);
201     EXPECT_EQ(result[100], ShareOption::CrossDevice);
202     EXPECT_EQ(result[400], ShareOption::InApp);
203     PasteboardClient::GetInstance()->RemoveGlobalShareOption({100, 200, 300, 400});
204 }
205 
206 /**
207 * @tc.name: GetGlobalShareOption
208 * @tc.desc: Get global shareOption
209 * @tc.type: FUNC
210 * @tc.require:
211 * @tc.author:
212 */
213 HWTEST_F(PasteboardClientTest, GetGlobalShareOption, TestSize.Level0)
214 {
215     std::map<uint32_t, ShareOption> settings = {
216         {100, ShareOption::InApp},
217         {200, ShareOption::LocalDevice},
218         {300, ShareOption::CrossDevice}};
219     PasteboardClient::GetInstance()->SetGlobalShareOption(settings);
220     auto result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
221     EXPECT_TRUE(result.size() == 3);
222     EXPECT_EQ(result[100], ShareOption::InApp);
223     EXPECT_EQ(result[200], ShareOption::LocalDevice);
224     EXPECT_EQ(result[300], ShareOption::CrossDevice);
225     result = PasteboardClient::GetInstance()->GetGlobalShareOption({100, 400});
226     EXPECT_TRUE(result.size() == 1);
227     EXPECT_EQ(result[100], ShareOption::InApp);
228     EXPECT_TRUE(result.find(400) == result.end());
229     PasteboardClient::GetInstance()->RemoveGlobalShareOption({100, 200, 300});
230 }
231 
232 /**
233 * @tc.name: RemoveGlobalShareOption
234 * @tc.desc: Remove global shareOption
235 * @tc.type: FUNC
236 * @tc.require:
237 * @tc.author:
238 */
239 HWTEST_F(PasteboardClientTest, RemoveGlobalShareOption, TestSize.Level0)
240 {
241     std::map<uint32_t, ShareOption> settings = {
242         {100, ShareOption::InApp},
243         {200, ShareOption::LocalDevice},
244         {300, ShareOption::CrossDevice}};
245     PasteboardClient::GetInstance()->SetGlobalShareOption(settings);
246     auto result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
247     EXPECT_TRUE(result.size() == 3);
248     EXPECT_EQ(result[100], ShareOption::InApp);
249     EXPECT_EQ(result[200], ShareOption::LocalDevice);
250     EXPECT_EQ(result[300], ShareOption::CrossDevice);
251     PasteboardClient::GetInstance()->RemoveGlobalShareOption({});
252     result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
253     EXPECT_TRUE(result.size() == 3);
254     PasteboardClient::GetInstance()->RemoveGlobalShareOption({100, 400});
255     result = PasteboardClient::GetInstance()->GetGlobalShareOption({});
256     EXPECT_TRUE(result.size() == 2);
257     EXPECT_TRUE(result.find(100) == result.end());
258     PasteboardClient::GetInstance()->RemoveGlobalShareOption({200, 300});
259 }
260 
261 /**
262 * @tc.name: DetectPatterns001
263 * @tc.desc: Cover Permutation
264 * @tc.type: FUNC
265 * @tc.require:
266 * @tc.author:
267 */
268 HWTEST_F(PasteboardClientTest, DetectPatterns001, TestSize.Level0)
269 {
270     std::string plainText("r法塔赫已经,速tdghf!】qd rqdswww.comsski,.sjopwe"
271     "ihhtpsdhttp我也带过去给他№のjioijhhu");
272     std::string plainText0("https://giedqwrtheeeeeefub.cerm/meeeelkove/obaklo_tjokl"
273     "psetkjdttk/bkkjob/mwjweww.md)");
274     std::string plainText1("2我就破888芙蓉王82h7");
275     std::string plainText2("uhiyqydueuw@kahqw.oisko.sji");
276 
277     std::vector<std::string> plainTextVec{
278         plainText, plainText+plainText0, plainText+plainText1, plainText+plainText2,
279         plainText+plainText0+plainText1, plainText0+plainText2+plainText, plainText1+plainText+plainText2,
280         plainText0+plainText1+plainText+plainText2
281     };
282     std::vector<Patterns> patternsVec{
283         {}, {Pattern::URL}, {Pattern::Number}, {Pattern::EmailAddress},
284         {Pattern::URL, Pattern::Number}, {Pattern::URL, Pattern::EmailAddress},
285         {Pattern::Number, Pattern::EmailAddress}, {Pattern::URL, Pattern::Number, Pattern::EmailAddress}
286     };
287     std::vector<std::vector<int>> patternsRightIndexVec{
288         {0, 0, 0, 0, 0, 0, 0, 0},
289         {0, 1, 0, 0, 1, 1, 0, 1},
290         {0, 0, 2, 0, 2, 0, 2, 2},
291         {0, 0, 0, 3, 0, 3, 3, 3},
292         {0, 1, 2, 0, 4, 1, 2, 4},
293         {0, 1, 0, 3, 1, 5, 3, 5},
294         {0, 0, 2, 3, 2, 3, 6, 6},
295         {0, 1, 2, 3, 4, 5, 6, 7}
296     };
297     for (int i = 0; i != 8; ++i) {
298         for (int j = 0; j != 8; ++j) {
299             auto newData = PasteboardClient::GetInstance()->CreatePlainTextData(
300                 plainTextVec[i]);
301             PasteboardClient::GetInstance()->SetPasteData(*newData);
302             auto ret = PasteboardClient::GetInstance()->DetectPatterns(
303                 patternsVec[j]);
304             int rightIndex = patternsRightIndexVec[i][j];
305             ASSERT_EQ(ret, patternsVec[rightIndex]);
306         }
307     }
308 }
309 
310 /**
311 * @tc.name: DetectPatterns002
312 * @tc.desc: check HTML
313 * @tc.type: FUNC
314 * @tc.require:
315 * @tc.author:
316 */
317 HWTEST_F(PasteboardClientTest, DetectPatterns002, TestSize.Level0)
318 {
319     std::string htmlText1 = "<!DOCTYPE html><html><head><title>"
320     "超链案头研究。,封为啊啊</title></head><body><h2>发高热</h2>"
321     "<p>隔热隔热的氛围<a href=\"https://exq23amwerwqple.com\">"
322     "个人网站https://ex24t33tamp65hhle.com</a>。</p></body></html>";
323     auto newData1 = PasteboardClient::GetInstance()->CreateHtmlData(htmlText1);
324     PasteboardClient::GetInstance()->SetPasteData(*newData1);
325     Patterns patternsToCheck1{Pattern::URL, Pattern::EmailAddress};
326     auto ret1 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck1);
327     Patterns expected1{Pattern::URL};
328     ASSERT_EQ(ret1, expected1);
329 
330     std::string htmlText2 = "<!DOCTYPE html><html><head><title>"
331     "各个环节</title></head><body><h2>妈妈那边的</h2>"
332     "<p>啊啊分,凤凰方法,环境https://examjjuyewple.com问我的<a href=\"https://ehhgxametgeple.com\">"
333     "阿婆吗weqkqo@exaetmple.com</a>。????打法</p></body></html>";
334     auto newData2 = PasteboardClient::GetInstance()->CreateHtmlData(htmlText2);
335     PasteboardClient::GetInstance()->SetPasteData(*newData2);
336     Patterns patternsToCheck2{Pattern::URL, Pattern::EmailAddress, Pattern::Number};
337     auto ret2 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck2);
338     Patterns expected2{Pattern::URL, Pattern::EmailAddress};
339     ASSERT_EQ(ret2, expected2);
340 }
341 
342 /**
343 * @tc.name: DetectPatterns003
344 * @tc.desc: Outlier force cast uint32_t to unsurportted Pattern
345 * @tc.type: FUNC
346 * @tc.require:
347 * @tc.author:
348 */
349 HWTEST_F(PasteboardClientTest, DetectPatterns003, TestSize.Level0)
350 {
351     std::string plainText1 = "部分人的十点半:\n"
352     "「而飞过海」\n"
353     "方法:\n"
354     "https://pr5yyye-drseyive.u54yk.cwerfe/s/42e1ewed77f3dab4"
355     "网gest加尔文iqru发的我ui哦计划任务i文化人:\n"
356     "~b0043fg3423tddj~";
357     auto newData1 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText1);
358     PasteboardClient::GetInstance()->SetPasteData(*newData1);
359     Patterns patternsToCheck{
360         Pattern::Number, Pattern::URL, Pattern::EmailAddress, static_cast<Pattern>(1023)};
361     auto ret1 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
362     Patterns expected1{};
363     ASSERT_EQ(ret1, expected1);
364     std::string plainText2 = "【撒迪化,等我i却很难,无穷花的!】"
365     "额外i卡号!念佛为?,为单位打开陪我。而奋斗,我去二队去,威威:trfwrtg"
366     "(¥¥软骨素用人员为bdfdgse https://tgrthwerrwt.com/marrkerrerlorrve/ usrdq12_22swe@16rtgre3.com)";
367     auto newData2 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText2);
368     PasteboardClient::GetInstance()->SetPasteData(*newData2);
369     auto ret2 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
370     Patterns expected2{};
371     ASSERT_EQ(ret2, expected2);
372     std::string plainText3 = "【撒迪化,等我i却很难,无穷花的!】"
373     "额外i卡号!念佛为?,为单位打开陪我。而奋斗,我去二队去,威威:trfwrtg";
374     auto newData3 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText3);
375     PasteboardClient::GetInstance()->SetPasteData(*newData3);
376     auto ret3 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
377     ASSERT_EQ(ret3, Patterns{});
378 }
379 
380 /**
381 * @tc.name: DetectPatterns004
382 * @tc.desc: Outlier force cast uint32_t 0xffffffff to unsurportted Pattern
383 * @tc.type: FUNC
384 * @tc.require:
385 * @tc.author:
386 */
387 HWTEST_F(PasteboardClientTest, DetectPatterns004, TestSize.Level0)
388 {
389     std::string plainText1 = "部分人的十点半:\n"
390     "「而飞过海」\n"
391     "方法:\n"
392     "https://pr5yyye-drseyive.u54yk.cwerfe/s/42e1ewed77f3dab4"
393     "网gest加尔文iqru发的我ui哦计划任务i文化人:\n"
394     "~b0043fg3423tddj~";
395     auto newData1 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText1);
396     PasteboardClient::GetInstance()->SetPasteData(*newData1);
397     std::set<Pattern> patternsToCheck{
398         Pattern::Number, Pattern::URL, Pattern::EmailAddress,
399         static_cast<Pattern>(0xffffffff), static_cast<Pattern>(0xffffff1a)};
400     auto ret1 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
401     std::set<Pattern> expected1{};
402     ASSERT_EQ(ret1, expected1);
403     std::string plainText2 = "【撒迪化,等我i却很难,无穷花的!】"
404     "额外i卡号!念佛为?,为单位打开陪我。而奋斗,我去二队去,威威:trfwrtg"
405     "(¥¥软骨素用人员为bdfdgse https://tgrthwerrwt.com/marrkerrerlorrve/ usrdq12_22swe@16rtgre3.com)";
406     auto newData2 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText2);
407     PasteboardClient::GetInstance()->SetPasteData(*newData2);
408     auto ret2 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
409     std::set<Pattern> expected2{};
410     ASSERT_EQ(ret2, expected2);
411     std::string plainText3 = "【撒迪化,等我i却很难,无穷花的!】"
412     "额外i卡号!念佛为?,为单位打开陪我。而奋斗,我去二队去,威威:trfwrtg";
413     auto newData3 = PasteboardClient::GetInstance()->CreatePlainTextData(plainText3);
414     PasteboardClient::GetInstance()->SetPasteData(*newData3);
415     auto ret3 = PasteboardClient::GetInstance()->DetectPatterns(patternsToCheck);
416     ASSERT_EQ(ret3, std::set<Pattern>{});
417 }
418 
419 } // namespace OHOS::MiscServices