1 /*
2 * Copyright (c) 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 "accesstoken_kit.h"
17 #define private public
18 #include "account_iam_callback_service.h"
19 #undef private
20 #include "account_iam_client.h"
21 #include "account_iam_client_test_callback.h"
22 #include "account_log_wrapper.h"
23 #include "token_setproc.h"
24
25 namespace OHOS {
26 namespace AccountTest {
27 namespace {
28 const int32_t TEST_USER_ID = 200;
29 }
30
31 using namespace testing;
32 using namespace testing::ext;
33 using namespace OHOS::AccountSA;
34 using namespace OHOS::Security::AccessToken;
35
36 class MockIInputer : public OHOS::AccountSA::IInputer {
37 public:
~MockIInputer()38 virtual ~MockIInputer() {}
OnGetData(int32_t authSubType,std::shared_ptr<IInputerData> inputerData)39 void OnGetData(int32_t authSubType, std::shared_ptr<IInputerData> inputerData) override
40 {
41 return;
42 }
43 };
44
45 class AccountIAMCallbackServiceTest : public testing::Test {
46 public:
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 void SetUp(void) override;
50 void TearDown(void) override;
51 };
52
SetUpTestCase(void)53 void AccountIAMCallbackServiceTest::SetUpTestCase(void)
54 {
55 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.settings", 0);
56 SetSelfTokenID(tokenId);
57 }
58
TearDownTestCase(void)59 void AccountIAMCallbackServiceTest::TearDownTestCase(void)
60 {}
61
SetUp(void)62 void AccountIAMCallbackServiceTest::SetUp(void)
63 {}
64
TearDown(void)65 void AccountIAMCallbackServiceTest::TearDown(void)
66 {}
67
68 /**
69 * @tc.name: IDMCallbackService_OnAcquireInfo_0100
70 * @tc.desc: OnAcquireInfo with nullptr.
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(AccountIAMCallbackServiceTest, IDMCallbackService_OnAcquireInfo_0100, TestSize.Level0)
75 {
76 sptr<IDMCallbackService> wrapper = new (std::nothrow) IDMCallbackService(TEST_USER_ID, nullptr);
77 EXPECT_TRUE(wrapper->callback_ == nullptr);
78 Attributes extraInfo;
79 wrapper->OnAcquireInfo(0, 0, extraInfo);
80 }
81
82 /**
83 * @tc.name: IDMCallbackService_OnAcquireInfo_0200
84 * @tc.desc: OnAcquireInfo with not nullptr.
85 * @tc.type: FUNC
86 * @tc.require:
87 */
88 HWTEST_F(AccountIAMCallbackServiceTest, IDMCallbackService_OnAcquireInfo_0200, TestSize.Level0)
89 {
90 auto testCallback = std::make_shared<MockIDMCallback>();
91 EXPECT_NE(testCallback, nullptr);
92 EXPECT_CALL(*testCallback, OnAcquireInfo(_, _, _)).Times(Exactly(1));
93 sptr<IDMCallbackService> wrapper = new (std::nothrow) IDMCallbackService(TEST_USER_ID, testCallback);
94 EXPECT_TRUE(wrapper->callback_ != nullptr);
95 Attributes extraInfo;
96 wrapper->OnAcquireInfo(0, 0, extraInfo);
97 }
98
99 /**
100 * @tc.name: IDMCallbackService_OnResult_0100
101 * @tc.desc: OnResult with nullptr.
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(AccountIAMCallbackServiceTest, IDMCallbackService_OnResult_0100, TestSize.Level0)
106 {
107 sptr<IDMCallbackService> wrapper = new (std::nothrow) IDMCallbackService(TEST_USER_ID, nullptr);
108 EXPECT_TRUE(wrapper->callback_ == nullptr);
109 Attributes extraInfo;
110 wrapper->OnResult(0, extraInfo);
111 }
112
113 /**
114 * @tc.name: GetCredInfoCallbackService_OnCredentialInfo_0100
115 * @tc.desc: OnCredentialInfo with nullptr.
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(AccountIAMCallbackServiceTest, GetCredInfoCallbackService_OnCredentialInfo_0100, TestSize.Level0)
120 {
121 sptr<GetCredInfoCallbackService> wrapper = new (std::nothrow) GetCredInfoCallbackService(nullptr);
122 EXPECT_TRUE(wrapper->callback_ == nullptr);
123 std::vector<CredentialInfo> infoList;
124 wrapper->OnCredentialInfo(infoList);
125 }
126
127 /**
128 * @tc.name: GetSetPropCallbackService_OnResult_0100
129 * @tc.desc: OnResult with nullptr.
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(AccountIAMCallbackServiceTest, GetSetPropCallbackService_OnResult_0100, TestSize.Level0)
134 {
135 sptr<GetSetPropCallbackService> wrapper = new (std::nothrow) GetSetPropCallbackService(nullptr);
136 EXPECT_TRUE(wrapper->callback_ == nullptr);
137 Attributes extraInfo;
138 wrapper->OnResult(0, extraInfo);
139 }
140
141 /**
142 * @tc.name: IAMInputer_OnGetData_0100
143 * @tc.desc: OnGetData with inputerData_ nullptr.
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 HWTEST_F(AccountIAMCallbackServiceTest, IAMInputer_OnGetData_0100, TestSize.Level0)
148 {
149 std::shared_ptr<MockIInputer> inputer = std::make_shared<MockIInputer>();
150 auto iamInputer = std::make_shared<IAMInputer>(TEST_USER_ID, inputer);
151 ASSERT_TRUE(iamInputer != nullptr);
152 int32_t authSubType = 0;
153 auto iamInputerData = std::make_shared<IAMInputerData>(TEST_USER_ID, nullptr);
154 EXPECT_TRUE(iamInputerData != nullptr);
155 iamInputer->inputerData_ = nullptr;
156 iamInputer->OnGetData(authSubType, iamInputerData);
157 }
158
159 /**
160 * @tc.name: IAMInputer_OnGetData_0200
161 * @tc.desc: OnGetData.
162 * @tc.type: FUNC
163 * @tc.require:
164 */
165 HWTEST_F(AccountIAMCallbackServiceTest, IAMInputer_OnGetData_0200, TestSize.Level0)
166 {
167 std::shared_ptr<MockIInputer> inputer = std::make_shared<MockIInputer>();
168 auto iamInputer = std::make_shared<IAMInputer>(TEST_USER_ID, inputer);
169 ASSERT_TRUE(iamInputer != nullptr);
170 int32_t authSubType = 0;
171 auto iamInputerData = std::make_shared<IAMInputerData>(TEST_USER_ID, nullptr);
172 EXPECT_TRUE(iamInputerData != nullptr);
173 iamInputer->OnGetData(authSubType, iamInputerData);
174 }
175 } // namespace AccountTest
176 } // namespace OHOS