1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <gtest/gtest.h>
17
18#include "patterns_matcher.h"
19
20using namespace testing::ext;
21using namespace OHOS::AAFwk;
22using OHOS::Parcel;
23
24namespace OHOS {
25namespace AAFwk {
26class PatternsMatcherBaseTest : public testing::Test {
27public:
28    PatternsMatcherBaseTest()
29    {}
30    ~PatternsMatcherBaseTest()
31    {}
32    static void SetUpTestCase(void);
33    static void TearDownTestCase(void);
34    void SetUp();
35    void TearDown();
36    std::shared_ptr<PatternsMatcher> PatternsMatcherIn_ = nullptr;
37    std::shared_ptr<PatternsMatcher> PatternsMatcherOut_ = nullptr;
38};
39
40void PatternsMatcherBaseTest::SetUpTestCase(void)
41{}
42
43void PatternsMatcherBaseTest::TearDownTestCase(void)
44{}
45
46void PatternsMatcherBaseTest::SetUp(void)
47{
48    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>();
49    PatternsMatcherOut_ = std::make_shared<PatternsMatcher>();
50}
51
52void PatternsMatcherBaseTest::TearDown(void)
53{}
54
55/**
56 * @tc.number: AaFwk_PatternsMatcher_Parcelable_0100
57 * @tc.name: Marshalling/Unmarshalling
58 * @tc.desc: marshalling PatternMatcher, and then check result.
59 */
60HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0100, Function | MediumTest | Level1)
61{
62    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("1234", MatchType::DEFAULT);
63    if (PatternsMatcherIn_ != nullptr) {
64        Parcel in;
65        PatternsMatcherIn_->Marshalling(in);
66        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
67        if (PatternsMatcherOut_ != nullptr) {
68            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
69            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
70        }
71    }
72}
73
74/**
75 * @tc.number: AaFwk_PatternsMatcher_Parcelable_0200
76 * @tc.name: Marshalling/Unmarshalling
77 * @tc.desc: marshalling PatternMatcher, and then check result.
78 */
79HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0200, Function | MediumTest | Level1)
80{
81    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("@#¥#3243adsafdf_中文", MatchType::PREFIX);
82    if (PatternsMatcherIn_ != nullptr) {
83        Parcel in;
84        PatternsMatcherIn_->Marshalling(in);
85        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
86        if (PatternsMatcherOut_ != nullptr) {
87            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
88            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
89        }
90    }
91}
92
93/**
94 * @tc.number: AaFwk_PatternsMatcher_Parcelable_0300
95 * @tc.name: Marshalling/Unmarshalling
96 * @tc.desc: marshalling PatternMatcher, and then check result.
97 */
98HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0300, Function | MediumTest | Level1)
99{
100    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("", MatchType::GLOBAL);
101    if (PatternsMatcherIn_ != nullptr) {
102        Parcel in;
103        PatternsMatcherIn_->Marshalling(in);
104        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
105        if (PatternsMatcherOut_ != nullptr) {
106            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
107            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
108        }
109    }
110}
111
112/**
113 * @tc.number: AaFwk_PatternsMatcher_Match_0100
114 * @tc.name: Match
115 * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
116 */
117HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0100, Function | MediumTest | Level1)
118{
119    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abcdefg", MatchType::DEFAULT);
120    if (PatternsMatcherIn_ != nullptr) {
121        EXPECT_EQ(PatternsMatcherIn_->match("abcdefg"), true);
122        EXPECT_EQ(PatternsMatcherIn_->match("Abcdefg"), false);
123    }
124}
125
126/**
127 * @tc.number: AaFwk_PatternsMatcher_Match_0200
128 * @tc.name: Match
129 * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
130 */
131HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0200, Function | MediumTest | Level1)
132{
133    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abcdefg", MatchType::PREFIX);
134    if (PatternsMatcherIn_ != nullptr) {
135        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgABCDEFG"), true);
136        EXPECT_EQ(PatternsMatcherIn_->match("AbcdefgABCDEFG"), false);
137    }
138}
139
140/**
141 * @tc.number: AaFwk_PatternsMatcher_Match_0300
142 * @tc.name: Match
143 * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
144 */
145HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0300, Function | MediumTest | Level1)
146{
147    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abc*defg.", MatchType::PATTERN);
148    if (PatternsMatcherIn_ != nullptr) {
149        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgG"), true);
150        EXPECT_EQ(PatternsMatcherIn_->match("abcccccdefgG"), true);
151        EXPECT_EQ(PatternsMatcherIn_->match("abcdefg"), false);
152        EXPECT_EQ(PatternsMatcherIn_->match("ABCDEFG"), false);
153    }
154}
155
156/**
157 * @tc.number: AaFwk_PatternsMatcher_Match_0400
158 * @tc.name: Match
159 * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
160 */
161HWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0400, Function | MediumTest | Level1)
162{
163    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abc*ABC*123", MatchType::GLOBAL);
164    if (PatternsMatcherIn_ != nullptr) {
165        EXPECT_EQ(PatternsMatcherIn_->match("abcABC123"), true);
166        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgABCDEFG123"), true);
167        EXPECT_EQ(PatternsMatcherIn_->match("000abcdefg000ABCDEFG000123"), true);
168        EXPECT_EQ(PatternsMatcherIn_->match("aBc123"), false);
169        EXPECT_EQ(PatternsMatcherIn_->match("AbC123"), false);
170        EXPECT_EQ(PatternsMatcherIn_->match("abcABC12345"), false);
171    }
172}
173}  // namespace AAFwk
174}  // namespace OHOS
175