1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include "mock_constant.h"
18 #define private public
19 #define protected public
20 #include "skill_uri.h"
21 #include "app_verify_base_info.h"
22 #include "bundle_verify_status_info.h"
23 #include "domain_url_util.h"
24 #undef private
25 #undef protected
26 #include "common_utils.h"
27 
28 namespace OHOS::AppDomainVerify {
29 using namespace testing;
30 using namespace testing::ext;
31 
32 class AppDomainVerifyBeanTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 };
SetUpTestCase(void)39 void AppDomainVerifyBeanTest::SetUpTestCase(void)
40 {
41 }
42 
TearDownTestCase(void)43 void AppDomainVerifyBeanTest::TearDownTestCase(void)
44 {
45 }
46 
SetUp(void)47 void AppDomainVerifyBeanTest::SetUp(void)
48 {
49 }
50 
TearDown(void)51 void AppDomainVerifyBeanTest::TearDown(void)
52 {
53 }
54 /**
55  * @tc.name: AppDomainVerifySkillUriTest001
56  * @tc.desc: SkillUri test.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifySkillUriTest001, TestSize.Level0)
60 {
61     SkillUri skillUri;
62     skillUri.scheme = "scheme";
63     skillUri.host = "host";
64     skillUri.port = "port";
65     skillUri.path = "path";
66     skillUri.pathStartWith = "pathStartWith";
67     skillUri.pathRegex = "pathRegex";
68     skillUri.type = "type";
69     Parcel parcel;
70     skillUri.Marshalling(parcel);
71     auto unmarshalling = SkillUri::Unmarshalling(parcel);
72     ASSERT_TRUE(unmarshalling->scheme == "scheme");
73     ASSERT_TRUE(unmarshalling->host == "host");
74     ASSERT_TRUE(unmarshalling->port == "port");
75     ASSERT_TRUE(unmarshalling->path == "path");
76     ASSERT_TRUE(unmarshalling->pathStartWith == "pathStartWith");
77     ASSERT_TRUE(unmarshalling->pathRegex == "pathRegex");
78     ASSERT_TRUE(unmarshalling->type == "type");
79 
80     Parcel parcel1;
81     unmarshalling = SkillUri::Unmarshalling(parcel1);
82     ASSERT_TRUE(unmarshalling == nullptr);
83 
84     parcel1.WriteString("https");
85     unmarshalling = SkillUri::Unmarshalling(parcel1);
86     ASSERT_TRUE(unmarshalling == nullptr);
87 }
88 /**
89  * @tc.name: AppDomainVerifyAppVerifyBaseInfoTest001
90  * @tc.desc: AppVerifyBaseInfo test.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyAppVerifyBaseInfoTest001, TestSize.Level0)
94 {
95     AppVerifyBaseInfo appVerifyBaseInfo;
96     appVerifyBaseInfo.appIdentifier = "appIdentifier";
97     appVerifyBaseInfo.bundleName = "bundleName";
98     appVerifyBaseInfo.fingerprint = "fingerprint";
99 
100     Parcel parcel;
101     appVerifyBaseInfo.Marshalling(parcel);
102     auto unmarshalling = AppVerifyBaseInfo::Unmarshalling(parcel);
103     ASSERT_TRUE(unmarshalling->appIdentifier == "appIdentifier");
104     ASSERT_TRUE(unmarshalling->bundleName == "bundleName");
105     ASSERT_TRUE(unmarshalling->fingerprint == "fingerprint");
106 
107     Parcel parcel1;
108     unmarshalling = AppVerifyBaseInfo::Unmarshalling(parcel1);
109     ASSERT_TRUE(unmarshalling == nullptr);
110     parcel1.WriteString(APP_IDENTIFIER);
111     unmarshalling = AppVerifyBaseInfo::Unmarshalling(parcel1);
112     ASSERT_TRUE(unmarshalling == nullptr);
113     parcel1.WriteString(BUNDLE_NAME);
114     unmarshalling = AppVerifyBaseInfo::Unmarshalling(parcel1);
115     ASSERT_TRUE(unmarshalling == nullptr);
116 }
117 /**
118  * @tc.name: AppDomainVerifyBundleVerifyStatusInfoTest001
119  * @tc.desc: BundleVerifyStatusInfo test.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyBundleVerifyStatusInfoTest001, TestSize.Level0)
123 {
124     BundleVerifyStatusInfo bundleVerifyStatusInfo;
125     VerifyResultInfo verifyResultInfo;
126     verifyResultInfo.hostVerifyStatusMap.insert_or_assign(
127         "https://" + HOST, std::make_tuple(InnerVerifyStatus::STATE_SUCCESS, "", 0));
128     bundleVerifyStatusInfo.bundleVerifyStatusInfoMap_.insert_or_assign(BUNDLE_NAME, verifyResultInfo);
129 
130     Parcel parcel;
131     bundleVerifyStatusInfo.Marshalling(parcel);
132     auto unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel);
133 
134     ASSERT_TRUE(unmarshalling->bundleVerifyStatusInfoMap_.size() == 1);
135     ASSERT_TRUE(unmarshalling->bundleVerifyStatusInfoMap_.begin()->first == BUNDLE_NAME);
136     ASSERT_TRUE(unmarshalling->bundleVerifyStatusInfoMap_.begin()->second.hostVerifyStatusMap.size() == 1);
137     ASSERT_TRUE(unmarshalling->bundleVerifyStatusInfoMap_.begin()->second.hostVerifyStatusMap.begin()->first ==
138         "https://" + HOST);
139     auto [status, verifyTime, cnt] =
140         unmarshalling->bundleVerifyStatusInfoMap_.begin()->second.hostVerifyStatusMap.begin()->second;
141     ASSERT_TRUE(status == InnerVerifyStatus::STATE_SUCCESS);
142 
143     Parcel parcel1;
144     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
145     ASSERT_TRUE(unmarshalling == nullptr);
146     parcel1.WriteUint32(1);
147     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
148     ASSERT_TRUE(unmarshalling == nullptr);
149     parcel1.WriteString(BUNDLE_NAME);
150     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
151     ASSERT_TRUE(unmarshalling == nullptr);
152     parcel1.WriteString(APP_IDENTIFIER);
153     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
154     ASSERT_TRUE(unmarshalling == nullptr);
155     parcel1.WriteUint32(1);
156     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
157     ASSERT_TRUE(unmarshalling == nullptr);
158     parcel1.WriteString("https://" + HOST);
159     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
160     ASSERT_TRUE(unmarshalling == nullptr);
161 }
162 /**
163  * @tc.name: AppDomainVerifyBundleVerifyStatusInfoTest002
164  * @tc.desc: BundleVerifyStatusInfo test.
165  * @tc.type: FUNC
166  */
167 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyBundleVerifyStatusInfoTest002, TestSize.Level0)
168 {
169     Parcel parcel1;
170     auto unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
171     ASSERT_TRUE(unmarshalling == nullptr);
172     parcel1.WriteUint32(1);
173     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
174     ASSERT_TRUE(unmarshalling == nullptr);
175     parcel1.WriteString(BUNDLE_NAME);
176     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
177     ASSERT_TRUE(unmarshalling == nullptr);
178     parcel1.WriteString(APP_IDENTIFIER);
179     unmarshalling = BundleVerifyStatusInfo::Unmarshalling(parcel1);
180     ASSERT_TRUE(unmarshalling == nullptr);
181     parcel1.WriteUint32(100);
182 }
183 /**
184  * @tc.name: AppDomainVerifyVerifyResultInfoTest001
185  * @tc.desc: VerifyResultInfo test.
186  * @tc.type: FUNC
187  */
188 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyVerifyResultInfoTest001, TestSize.Level0)
189 {
190     VerifyResultInfo verifyResultInfo;
191     verifyResultInfo.appIdentifier = APP_IDENTIFIER;
192     verifyResultInfo.hostVerifyStatusMap.insert_or_assign(
193         "https://" + HOST, std::make_tuple(InnerVerifyStatus::STATE_SUCCESS, "", 0));
194 
195     Parcel parcel;
196     verifyResultInfo.Marshalling(parcel);
197     auto unmarshalling = VerifyResultInfo::Unmarshalling(parcel);
198     ASSERT_TRUE(unmarshalling->appIdentifier == APP_IDENTIFIER);
199     ASSERT_TRUE(unmarshalling->hostVerifyStatusMap.size() == 1);
200     ASSERT_TRUE(unmarshalling->hostVerifyStatusMap.begin()->first == "https://" + HOST);
201     auto [status, verifyTime, cnt] = unmarshalling->hostVerifyStatusMap.begin()->second;
202     ASSERT_TRUE(status == InnerVerifyStatus::STATE_SUCCESS);
203 
204     Parcel parcel1;
205     unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
206     ASSERT_TRUE(unmarshalling == nullptr);
207     parcel1.WriteString(APP_IDENTIFIER);
208     unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
209     ASSERT_TRUE(unmarshalling == nullptr);
210     parcel1.WriteUint32(1);
211     unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
212     ASSERT_TRUE(unmarshalling == nullptr);
213 }
214 /**
215  * @tc.name: AppDomainVerifyVerifyResultInfoTest002
216  * @tc.desc: VerifyResultInfo test.
217  * @tc.type: FUNC
218  */
219 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyVerifyResultInfoTest002, TestSize.Level0)
220 {
221     Parcel parcel1;
222     auto unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
223     ASSERT_TRUE(unmarshalling == nullptr);
224     parcel1.WriteString(APP_IDENTIFIER);
225     unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
226     ASSERT_TRUE(unmarshalling == nullptr);
227     parcel1.WriteUint32(100);
228     unmarshalling = VerifyResultInfo::Unmarshalling(parcel1);
229     ASSERT_TRUE(unmarshalling == nullptr);
230 }
231 /**
232  * @tc.name: AppDomainVerifyUrlUtilTest001
233  * @tc.desc: UrlUtil test.
234  * @tc.type: FUNC
235  */
236 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyUrlUtilTest001, TestSize.Level0)
237 {
238     ASSERT_TRUE(UrlUtil::IsValidAppDomainVerifyHost("https://" + HOST));
239     ASSERT_TRUE(UrlUtil::IsValidUrl("https://" + HOST));
240     ASSERT_TRUE(UrlUtil::GetScheme(HOST) == "");
241 }
242 
243 /**
244  * @tc.name: AppDomainVerifyCommonUtilTest001
245  * @tc.desc: CommonUtil test.
246  * @tc.type: FUNC
247  */
248 HWTEST_F(AppDomainVerifyBeanTest, AppDomainVerifyCommonUtilTest001, TestSize.Level0)
249 {
250     ASSERT_TRUE(MaskStr("").empty());
251     std::string ONE_TEST = "a";
252     ASSERT_TRUE(MaskStr(ONE_TEST) == ONE_TEST);
253     std::string NORMAL_TEST = "https://www.openharmony.cn/res";
254     std::string NORMAL_TEST_EXPECT = "h**p**/**w**o**n**r**n**c**r**";
255     ASSERT_TRUE(MaskStr(NORMAL_TEST) == NORMAL_TEST_EXPECT);
256 }
257 
258 }
259