• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "user_access_ctrl_client_test.h"
17 
18 #include "iam_ptr.h"
19 #include "user_access_ctrl_client.h"
20 #include "user_access_ctrl_client_impl.h"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 using namespace testing;
26 using namespace testing::ext;
27 
SetUpTestCase()28 void UserAccessCtrlClientTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void UserAccessCtrlClientTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void UserAccessCtrlClientTest::SetUp()
37 {
38 }
39 
TearDown()40 void UserAccessCtrlClientTest::TearDown()
41 {
42 }
43 
44 HWTEST_F(UserAccessCtrlClientTest, UserAccessCtrlClientVerifyAuthToken001, TestSize.Level0)
45 {
46     std::vector<uint8_t> tokenIn;
47     uint64_t allowableDuration = 0;
48     std::shared_ptr<MockVerifyTokenCallback> testCallback = Common::MakeShared<MockVerifyTokenCallback>();
49     EXPECT_NE(testCallback, nullptr);
50     UserAccessCtrlClientImpl::Instance().VerifyAuthToken(tokenIn, allowableDuration, testCallback);
51 }
52 
53 HWTEST_F(UserAccessCtrlClientTest, UserAccessCtrlClientVerifyAuthToken002, TestSize.Level0)
54 {
55     std::vector<uint8_t> testTokenIn;
56     uint64_t testAllowableDuration = 0;
57     auto testCallback = Common::MakeShared<MockVerifyTokenCallback>();
58     EXPECT_NE(testCallback, nullptr);
59 
60     auto service = Common::MakeShared<MockUserAuthService>();
61     EXPECT_NE(service, nullptr);
62     EXPECT_CALL(*service, VerifyAuthToken(_, _, _)).Times(Exactly(1));
63     ON_CALL(*service, VerifyAuthToken)
64         .WillByDefault(
65             [&testTokenIn, &testAllowableDuration, &testCallback]
66             (const std::vector<uint8_t> &tokenIn, uint64_t allowableDuration,
__anon63be17af0102(const std::vector<uint8_t> &tokenIn, uint64_t allowableDuration, const sptr<VerifyTokenCallbackInterface> &callback) 67                 const sptr<VerifyTokenCallbackInterface> &callback) {
68                 EXPECT_EQ(testTokenIn, tokenIn);
69                 EXPECT_EQ(testAllowableDuration, allowableDuration);
70             }
71         );
72 
73     sptr<MockRemoteObject> obj = new MockRemoteObject();
74     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
75     CallRemoteObject(service, obj, dr);
76     UserAccessCtrlClientImpl::Instance().VerifyAuthToken(testTokenIn, testAllowableDuration, testCallback);
77     dr->OnRemoteDied(obj);
78     IpcClientUtils::ResetObj();
79 }
80 
CallRemoteObject(const std::shared_ptr<MockUserAuthService> service,const sptr<MockRemoteObject> & obj,sptr<IRemoteObject::DeathRecipient> & dr)81 void UserAccessCtrlClientTest::CallRemoteObject(const std::shared_ptr<MockUserAuthService> service,
82     const sptr<MockRemoteObject> &obj, sptr<IRemoteObject::DeathRecipient> &dr)
83 {
84     EXPECT_NE(obj, nullptr);
85     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
86     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
87     EXPECT_CALL(*obj, AddDeathRecipient(_))
88         .WillRepeatedly([&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
89             dr = recipient;
90             return true;
91         });
92 
93     IpcClientUtils::SetObj(obj);
94     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
95     ON_CALL(*obj, SendRequest)
96         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
97             service->OnRemoteRequest(code, data, reply, option);
98             return OHOS::NO_ERROR;
99         });
100 }
101 } // namespace UserAuth
102 } // namespace UserIam
103 } // namespace OHOS