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_service.h"
22 #include "domain_account_plugin_stub.h"
23 #undef private
24 #include "parcel.h"
25 #include "want.h"
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AccountSA;
30 
31 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.accountfwk.IDomainAccountPlugin");
32 
33 namespace {
34 } // namespace
35 
36 class DomainPluginStubModuleTest : public testing::Test {
37 public:
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
40     void SetUp();
41     void TearDown();
42     sptr<DomainAccountPluginService> pluginServie_ = nullptr;
43 };
44 
SetUpTestCase(void)45 void DomainPluginStubModuleTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void DomainPluginStubModuleTest::TearDownTestCase(void)
49 {}
50 
SetUp(void)51 void DomainPluginStubModuleTest::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     pluginServie_ = new (std::nothrow) DomainAccountPluginService(nullptr);
61     ASSERT_NE(pluginServie_, nullptr);
62 }
63 
TearDown(void)64 void DomainPluginStubModuleTest::TearDown(void)
65 {}
66 
67 /**
68  * @tc.name: DomainPluginStubModuleTest_OnRemoteRequest_001
69  * @tc.desc: OnRemoteRequest with invalid code.
70  * @tc.type: FUNC
71  * @tc.require:
72  */
73 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_OnRemoteRequest_001, TestSize.Level0)
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option;
78     data.WriteInterfaceToken(GetDescriptor());
79     EXPECT_NE(pluginServie_->OnRemoteRequest(-1, data, reply, option), ERR_NONE);
80 }
81 
82 /**
83  * @tc.name: DomainPluginStubModuleTest_ProcAuthCommonInterface_001
84  * @tc.desc: ProcAuthCommonInterface with invalid info.
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcAuthCommonInterface_001, TestSize.Level0)
89 {
90     MessageParcel data;
91     MessageParcel reply;
92     data.WriteInterfaceToken(GetDescriptor());
93     EXPECT_EQ(pluginServie_->ProcAuthCommonInterface(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
94 }
95 
96 /**
97  * @tc.name: DomainPluginStubModuleTest_ProcAuthCommonInterface_002
98  * @tc.desc: ProcAuthCommonInterface with AUTH_WITH_TOKEN_MODE.
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcAuthCommonInterface_002, TestSize.Level0)
103 {
104     MessageParcel data;
105     MessageParcel reply;
106     DomainAccountInfo info;
107     info.accountName_ = "test";
108     info.domain_ = "china";
109     EXPECT_EQ(data.WriteParcelable(&info), true);
110     std::vector<uint8_t> authData;
111     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
112     std::shared_ptr<DomainAccountCallback> callback = nullptr;
113     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
114     ASSERT_NE(callbackService, nullptr);
115     EXPECT_EQ(data.WriteRemoteObject(callbackService->AsObject()), true);
116     EXPECT_EQ(data.WriteInt32(AUTH_WITH_TOKEN_MODE), true);
117     EXPECT_EQ(pluginServie_->ProcAuthCommonInterface(data, reply), ERR_NONE);
118 }
119 
120 /**
121  * @tc.name: DomainPluginStubModuleTest_ProcGetAuthStatusInfo_001
122  * @tc.desc: ProcGetAuthStatusInfo success.
123  * @tc.type: FUNC
124  * @tc.require:
125  */
126 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcGetAuthStatusInfo_001, TestSize.Level0)
127 {
128     MessageParcel data;
129     MessageParcel reply;
130     DomainAccountInfo info;
131     info.accountName_ = "test";
132     info.domain_ = "china";
133     EXPECT_EQ(data.WriteParcelable(&info), true);
134     std::shared_ptr<DomainAccountCallback> callback = nullptr;
135     sptr<DomainAccountCallbackService> callbackService = new (std::nothrow) DomainAccountCallbackService(callback);
136     ASSERT_NE(callbackService, nullptr);
137     EXPECT_EQ(data.WriteRemoteObject(callbackService->AsObject()), true);
138     EXPECT_EQ(pluginServie_->ProcGetAuthStatusInfo(data, reply), ERR_NONE);
139     int32_t result = -1;
140     EXPECT_EQ(reply.WriteInt32(result), true);
141 }
142 
143 /**
144  * @tc.name: DomainPluginStubModuleTest_ProcGetAuthStatusInfo_002
145  * @tc.desc: ProcGetAuthStatusInfo with invalid info.
146  * @tc.type: FUNC
147  * @tc.require:
148  */
149 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcGetAuthStatusInfo_002, TestSize.Level0)
150 {
151     MessageParcel data;
152     MessageParcel reply;
153     DomainAccountInfo info;
154     std::vector<uint8_t> authData;
155     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
156     EXPECT_EQ(pluginServie_->ProcGetAuthStatusInfo(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
157 }
158 
159 /**
160  * @tc.name: DomainPluginStubModuleTest_ProcIsAccountTokenValid_001
161  * @tc.desc: ProcIsAccountTokenValid with invalid info.
162  * @tc.type: FUNC
163  * @tc.require:
164  */
165 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcIsAccountTokenValid_001, TestSize.Level0)
166 {
167     MessageParcel data;
168     MessageParcel reply;
169     std::vector<uint8_t> authData;
170     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
171     EXPECT_EQ(pluginServie_->ProcIsAccountTokenValid(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
172 }
173 
174 /**
175  * @tc.name: DomainPluginStubModuleTest_ProcIsAccountTokenValid_002
176  * @tc.desc: ProcGetAccessToken with invalid info.
177  * @tc.type: FUNC
178  * @tc.require:
179  */
180 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcGetAccessToken_001, TestSize.Level0)
181 {
182     MessageParcel data;
183     MessageParcel reply;
184     std::vector<uint8_t> authData;
185     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
186     EXPECT_EQ(pluginServie_->ProcGetAccessToken(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
187 }
188 
189 /**
190  * @tc.name: DomainPluginStubModuleTest_ProcGetAccessToken_001
191  * @tc.desc: ProcGetAccessToken with invalid GetAccessTokenOptions.
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcGetAccessToken_002, TestSize.Level0)
196 {
197     MessageParcel data;
198     MessageParcel reply;
199     DomainAccountInfo info;
200     info.accountName_ = "test";
201     info.domain_ = "china";
202     EXPECT_EQ(data.WriteParcelable(&info), true);
203     std::vector<uint8_t> authData;
204     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
205     std::string name = "test";
206     EXPECT_EQ(data.WriteString(name), true);
207     EXPECT_EQ(pluginServie_->ProcIsAccountTokenValid(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
208 }
209 
210 /**
211  * @tc.name: DomainPluginStubModuleTest_ProcOnAccountBound_001
212  * @tc.desc: ProcOnAccountBound with invalid info.
213  * @tc.type: FUNC
214  * @tc.require:
215  */
216 HWTEST_F(DomainPluginStubModuleTest, DomainPluginStubModuleTest_ProcOnAccountBound_001, TestSize.Level0)
217 {
218     MessageParcel data;
219     MessageParcel reply;
220     std::vector<uint8_t> authData;
221     EXPECT_EQ(data.WriteUInt8Vector(authData), true);
222     EXPECT_EQ(pluginServie_->ProcOnAccountBound(data, reply), ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
223 }