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 <cerrno>
17 #include <gtest/gtest.h>
18 #include "account_log_wrapper.h"
19 #define private public
20 #include "domain_account_callback_service.h"
21 #include "domain_account_plugin_proxy.h"
22 #include "domain_account_plugin_service.h"
23 #include "domain_account_callback_service.h"
24 #include "domain_has_domain_info_callback.h"
25 #undef private
26 #include "parcel.h"
27 #include "want.h"
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::AccountSA;
32 
33 namespace {
34 } // namespace
35 
36 class DomainAccountPluginProxyTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     sptr<DomainAccountPluginProxy> pluginProxy_ = nullptr;
43 };
44 
SetUpTestCase(void)45 void DomainAccountPluginProxyTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void DomainAccountPluginProxyTest::TearDownTestCase(void)
49 {}
50 
SetUp(void)51 void DomainAccountPluginProxyTest::SetUp(void) __attribute__((no_sanitize("cfi")))
52 {
53     testing::UnitTest *test = testing::UnitTest::GetInstance();
54     ASSERT_NE(test, nullptr);
55     const testing::TestInfo *testinfo = test->current_test_info();
56     ASSERT_NE(testinfo, nullptr);
57     string testCaseName = string(testinfo->name());
58     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
59 
60     sptr<DomainAccountPluginService> pluginServie_ = new (std::nothrow) DomainAccountPluginService(nullptr);
61     ASSERT_NE(pluginServie_, nullptr);
62     pluginProxy_ = new (std::nothrow) DomainAccountPluginProxy(pluginServie_);
63     ASSERT_NE(pluginProxy_, nullptr);
64 }
65 
TearDown(void)66 void DomainAccountPluginProxyTest::TearDown(void)
67 {}
68 
69 /**
70  * @tc.name: DomainAccountPluginProxyTest_AuthCommonInterface_001
71  * @tc.desc: AuthCommonInterface callback is nullptr.
72  * @tc.type: FUNC
73  * @tc.require:
74  */
75 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_AuthCommonInterface_001, TestSize.Level0)
76 {
77     DomainAccountInfo info;
78     std::vector<uint8_t> authData;
79     EXPECT_EQ(pluginProxy_->AuthCommonInterface(info, authData, nullptr, AUTH_WITH_TOKEN_MODE),
80         ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
81 }
82 
83 /**
84  * @tc.name: DomainAccountPluginProxyTest_IsAccountTokenValid_001
85  * @tc.desc: IsAccountTokenValid callback is nullptr.
86  * @tc.type: FUNC
87  * @tc.require:
88  */
89 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_IsAccountTokenValid_001, TestSize.Level0)
90 {
91     DomainAccountInfo info;
92     std::vector<uint8_t> authData;
93     EXPECT_EQ(pluginProxy_->IsAccountTokenValid(info, authData, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
94 }
95 
96 /**
97  * @tc.name: DomainAccountPluginProxyTest_GetAccessToken_001
98  * @tc.desc: GetAccessToken callback is nullptr.
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_GetAccessToken_001, TestSize.Level0)
103 {
104     DomainAccountInfo info;
105     std::vector<uint8_t> authData;
106     GetAccessTokenOptions option;
107     EXPECT_EQ(pluginProxy_->GetAccessToken(info, authData, option, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
108 }
109 
110 /**
111  * @tc.name: DomainAccountPluginProxyTest_GetAuthStatusInfo_001
112  * @tc.desc: GetAuthStatusInfo callback is nullptr.
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_GetAuthStatusInfo_001, TestSize.Level0)
117 {
118     DomainAccountInfo info;
119     EXPECT_EQ(pluginProxy_->GetAuthStatusInfo(info, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
120 }
121 
122 /**
123  * @tc.name: DomainAccountPluginProxyTest_GetAuthStatusInfo_002
124  * @tc.desc: GetAuthStatusInfo success.
125  * @tc.type: FUNC
126  * @tc.require:
127  */
128 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_GetAuthStatusInfo_002, TestSize.Level0)
129 {
130     DomainAccountInfo info;
131     std::shared_ptr<DomainAccountCallback> callback = nullptr;
132     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
133     EXPECT_NE(callbackService, nullptr);
134     EXPECT_NE(pluginProxy_->GetAuthStatusInfo(info, callbackService), ERR_OK);
135 }
136 
137 /**
138  * @tc.name: DomainAccountPluginProxyTest_AuthWithToken_001
139  * @tc.desc: GetAuthStatusInfo callback is nullptr.
140  * @tc.type: FUNC
141  * @tc.require:
142  */
143 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_AuthWithToken_001, TestSize.Level0)
144 {
145     DomainAccountInfo info;
146     std::vector<uint8_t> token;
147     EXPECT_EQ(pluginProxy_->AuthWithToken(info, token, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
148 }
149 
150 /**
151  * @tc.name: DomainAccountPluginProxyTest_AuthWithToken_002
152  * @tc.desc: GetAuthStatusInfo success.
153  * @tc.type: FUNC
154  * @tc.require:
155  */
156 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_AuthWithToken_002, TestSize.Level0)
157 {
158     DomainAccountInfo info;
159     std::shared_ptr<DomainAccountCallback> callback = nullptr;
160     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
161     EXPECT_NE(callbackService, nullptr);
162     std::vector<uint8_t> token;
163     EXPECT_NE(pluginProxy_->AuthWithToken(info, token, callbackService), ERR_OK);
164 }
165 
166 /**
167  * @tc.name: DomainAccountPluginProxyTest_GetDomainAccountInfo_001
168  * @tc.desc: GetDomainAccountInfo callback is nullptr.
169  * @tc.type: FUNC
170  * @tc.require:
171  */
172 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_GetDomainAccountInfo_001, TestSize.Level0)
173 {
174     GetDomainAccountInfoOptions options;
175     EXPECT_EQ(pluginProxy_->GetDomainAccountInfo(options, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
176 }
177 
178 /**
179  * @tc.name: DomainAccountPluginProxyTest_OnAccountBound_001
180  * @tc.desc: OnAccountBound callback is nullptr.
181  * @tc.type: FUNC
182  * @tc.require:
183  */
184 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_OnAccountBound_001, TestSize.Level0)
185 {
186     DomainAccountInfo info;
187     EXPECT_EQ(pluginProxy_->OnAccountBound(info, 0, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
188 }
189 
190 /**
191  * @tc.name: DomainAccountPluginProxyTest_OnAccountUnBound_001
192  * @tc.desc: OnAccountUnBound callback is nullptr.
193  * @tc.type: FUNC
194  * @tc.require:
195  */
196 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_OnAccountUnBound_001, TestSize.Level0)
197 {
198     DomainAccountInfo info;
199     EXPECT_EQ(pluginProxy_->OnAccountUnBound(info, nullptr), ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR);
200 }
201 
202 /**
203  * @tc.name: DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_001
204  * @tc.desc: OnResult callback is nullptr.
205  * @tc.type: FUNC
206  * @tc.require:
207  */
208 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_001, TestSize.Level0)
209 {
210     auto callbackWrapper = std::make_shared<DomainHasDomainInfoCallback>(nullptr, "test", "name");
211     Parcel parcel;
212     callbackWrapper->OnResult(0, parcel);
213     EXPECT_EQ(callbackWrapper->innerCallback_, nullptr);
214 }
215 
216 /**
217  * @tc.name: DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_002
218  * @tc.desc: OnResult parcel is enpty.
219  * @tc.type: FUNC
220  * @tc.require:
221  */
222 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_002, TestSize.Level0)
223 {
224     std::shared_ptr<DomainAccountCallback> callback = nullptr;
225     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
226     EXPECT_NE(callbackService, nullptr);
227     auto callbackWrapper = std::make_shared<DomainHasDomainInfoCallback>(callbackService, "test", "name");
228     Parcel parcel;
229     int32_t result = 0;
230     EXPECT_EQ(parcel.WriteInt32(result), true);
231     callbackWrapper->OnResult(0, parcel);
232     EXPECT_NE(callbackWrapper->innerCallback_, nullptr);
233 }
234 
235 /**
236  * @tc.name: DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_003
237  * @tc.desc: OnResult domian is invalid and accountName is invalid.
238  * @tc.type: FUNC
239  * @tc.require:
240  */
241 HWTEST_F(DomainAccountPluginProxyTest, DomainAccountPluginProxyTest_DomainHasDomainInfoCallback_003, TestSize.Level0)
242 {
243     std::shared_ptr<DomainAccountCallback> callback = nullptr;
244     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
245     EXPECT_NE(callbackService, nullptr);
246     auto callbackWrapper = std::make_shared<DomainHasDomainInfoCallback>(callbackService, "test", "test");
247     Parcel parcel;
248     DomainAccountInfo info1("111", "test");
249     EXPECT_EQ(info1.Marshalling(parcel), true);
250     callbackWrapper->OnResult(0, parcel);
251     EXPECT_NE(callbackWrapper->innerCallback_, nullptr);
252     Parcel parcel2;
253     DomainAccountInfo info2("test", "111");
254     EXPECT_EQ(info2.Marshalling(parcel2), true);
255     callbackWrapper->OnResult(0, parcel2);
256     EXPECT_NE(callbackWrapper->innerCallback_, nullptr);
257 }