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 <memory>
18 #include <string>
19 #include "mock_verify_agent.h"
20 #include "app_domain_verify_agent_service_proxy.h"
21 #include "refbase.h"
22 #include "mock_constant.h"
23 #include "mock_verify_mgr.h"
24 #include "sa_interface/app_domain_verify_mgr_interface_code.h"
25 #define private public
26 #define protected public
27 #include "app_domain_verify_mgr_client.h"
28 #include "app_domain_verify_agent_client.h"
29 #include "sa_interface/app_domain_verify_mgr_service.h"
30 #undef private
31 #undef protected
32 #include "mock_access_token.h"
33
34 namespace OHOS::AppDomainVerify {
35 using ::testing::_;
36 using ::testing::Invoke;
37 using ::testing::Mock;
38 using namespace testing;
39 using namespace testing::ext;
40 class AppDomainVerifyMgrModuleTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46 };
47 auto appDomainVerifyMgrService_ = std::make_shared<AppDomainVerifyMgrService>();
48 auto appDomainVerifyAgentStubMock_ = std::make_shared<AppDomainVerifyAgentRemoteStubMock>();
InvokeSingleVerifyOK(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int InvokeSingleVerifyOK(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MODULE_BUTT, "call end");
52 std::string bundleName = BUNDLE_NAME;
53 VerifyResultInfo verifyResultInfo;
54 verifyResultInfo.hostVerifyStatusMap.insert_or_assign(
55 "https://" + HOST, std::make_tuple(InnerVerifyStatus::STATE_SUCCESS, std::string(), 0));
56 appDomainVerifyMgrService_->SaveDomainVerifyStatus(bundleName, verifyResultInfo);
57 return 0;
58 }
59
InvokeSingleVerifyFail(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)60 int InvokeSingleVerifyFail(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
61 {
62 APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MODULE_BUTT, "call end");
63 std::string bundleName = BUNDLE_NAME;
64 VerifyResultInfo verifyResultInfo;
65 verifyResultInfo.hostVerifyStatusMap.insert_or_assign(
66 "https://" + HOST, std::make_tuple(InnerVerifyStatus::STATE_FAIL, std::string(), 0));
67 appDomainVerifyMgrService_->SaveDomainVerifyStatus(bundleName, verifyResultInfo);
68 return UNKNOWN_ERROR;
69 }
70
SetUpTestCase(void)71 void AppDomainVerifyMgrModuleTest::SetUpTestCase(void)
72 {
73 }
74
TearDownTestCase(void)75 void AppDomainVerifyMgrModuleTest::TearDownTestCase(void)
76 {
77 printf("TearDownTestCase 1\n");
78 AppDomainVerifyAgentClient::agentServiceProxy_.ForceSetRefPtr(nullptr);
79 printf("TearDownTestCase 2\n");
80 AppDomainVerifyAgentClient::DestroyInstance();
81 printf("TearDownTestCase 3\n");
82 appDomainVerifyMgrService_->appDetailsDataMgr_ = nullptr;
83 appDomainVerifyMgrService_->Stop();
84 appDomainVerifyAgentStubMock_.reset();
85 printf("TearDownTestCase \n");
86 }
87
SetUp(void)88 void AppDomainVerifyMgrModuleTest::SetUp(void)
89 {
90 MockAccessToken::mockSA();
91 AppDomainVerifyAgentClient::staticDestoryMonitor_.destoryed_ = true;
92 }
93
TearDown(void)94 void AppDomainVerifyMgrModuleTest::TearDown(void)
95 {
96 }
97 /**
98 * @tc.name: AppDomainVerifyMgrModuleTest001
99 * @tc.desc: VerifyDomain ok test.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(AppDomainVerifyMgrModuleTest, AppDomainVerifyMgrModuleTest001, TestSize.Level1)
103 {
104 EXPECT_CALL(*appDomainVerifyAgentStubMock_, SendRequest(_, _, _, _))
105 .Times(1)
106 .WillOnce(::testing::Invoke(InvokeSingleVerifyOK));
107 AppDomainVerifyAgentClient::agentServiceProxy_ = sptr<AppDomainVerifyAgentServiceProxy>::MakeSptr(
108 appDomainVerifyAgentStubMock_.get());
109 std::vector<SkillUri> skillUris;
110 SkillUri skillUri;
111 skillUri.scheme = "https";
112 skillUri.host = HOST;
113 skillUris.emplace_back(skillUri);
114 appDomainVerifyMgrService_->VerifyDomain(APP_IDENTIFIER, BUNDLE_NAME, FINGERPRINT, skillUris);
115 DomainVerifyStatus domainVerificationState;
116 auto queryRes = appDomainVerifyMgrService_->QueryDomainVerifyStatus(BUNDLE_NAME, domainVerificationState);
117 ASSERT_TRUE(queryRes);
118 ASSERT_TRUE(domainVerificationState == DomainVerifyStatus::STATE_VERIFIED);
119 }
120
121 /**
122 * @tc.name: AppDomainVerifyMgrModuleTest002
123 * @tc.desc: VerifyDomain fail test.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(AppDomainVerifyMgrModuleTest, AppDomainVerifyMgrModuleTest002, TestSize.Level1)
127 {
128 EXPECT_CALL(*appDomainVerifyAgentStubMock_, SendRequest(_, _, _, _))
129 .Times(1)
130 .WillOnce(::testing::Invoke(InvokeSingleVerifyFail));
131 AppDomainVerifyAgentClient::agentServiceProxy_ = sptr<AppDomainVerifyAgentServiceProxy>::MakeSptr(
132 appDomainVerifyAgentStubMock_.get());
133
134 std::vector<SkillUri> skillUris;
135 SkillUri skillUri;
136 skillUri.scheme = "https";
137 skillUri.host = HOST;
138 skillUris.emplace_back(skillUri);
139 appDomainVerifyMgrService_->VerifyDomain(APP_IDENTIFIER, BUNDLE_NAME, FINGERPRINT, skillUris);
140 DomainVerifyStatus domainVerificationState;
141 auto queryRes = appDomainVerifyMgrService_->QueryDomainVerifyStatus(BUNDLE_NAME, domainVerificationState);
142 ASSERT_TRUE(queryRes);
143 ASSERT_TRUE(domainVerificationState == DomainVerifyStatus::STATE_NONE);
144 }
145
146 /**
147 * @tc.name: AppDomainVerifyMgrModuleTest006
148 * @tc.desc: QueryDomainVerifyStatus test.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(AppDomainVerifyMgrModuleTest, AppDomainVerifyMgrModuleTest006, TestSize.Level1)
152 {
153 DomainVerifyStatus domainVerificationState;
154 auto queryRes = appDomainVerifyMgrService_->QueryDomainVerifyStatus(BUNDLE_NAME, domainVerificationState);
155 ASSERT_TRUE(queryRes);
156 ASSERT_TRUE(domainVerificationState == DomainVerifyStatus::STATE_NONE);
157 }
158 }
159