• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 
18 #include "context_callback_impl.h"
19 #include "iam_ptr.h"
20 #include "mock_user_auth_callback.h"
21 #include "mock_user_idm_callback.h"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 using namespace std;
27 using namespace testing;
28 using namespace testing::ext;
29 
30 class ContextCallbackImplTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33 
34     static void TearDownTestCase();
35 
36     void SetUp() override;
37 
38     void TearDown() override;
39 };
40 
SetUpTestCase()41 void ContextCallbackImplTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void ContextCallbackImplTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void ContextCallbackImplTest::SetUp()
50 {
51 }
52 
TearDown()53 void ContextCallbackImplTest::TearDown()
54 {
55 }
56 
57 HWTEST_F(ContextCallbackImplTest, ContextCallbackImplUserAuthNull, TestSize.Level0)
58 {
59     sptr<UserAuthCallbackInterface> callback = nullptr;
60     auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
61     ASSERT_EQ(contextCallback, nullptr);
62 }
63 
64 HWTEST_F(ContextCallbackImplTest, ContextCallbackImplUserIdmNull, TestSize.Level0)
65 {
66     sptr<IdmCallbackInterface> callback = nullptr;
67     auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
68     ASSERT_EQ(contextCallback, nullptr);
69 }
70 
71 HWTEST_F(ContextCallbackImplTest, ContextCallbackImplUserAuth, TestSize.Level0)
72 {
73     int32_t testResult = 66;
74     auto testAttr = Common::MakeShared<Attributes>();
75     ASSERT_TRUE(testAttr != nullptr);
76 
77     sptr<MockUserAuthCallback> mockCallback = new (nothrow) MockUserAuthCallback();
78     ASSERT_TRUE(mockCallback != nullptr);
79     EXPECT_CALL(*mockCallback, OnResult(_, _))
80         .Times(Exactly(1))
__anon3528a01e0102(int32_t result, const Attributes &reqRet) 81         .WillOnce([&testResult, &testAttr](int32_t result, const Attributes &reqRet) {
82             EXPECT_TRUE(testResult == result);
83             EXPECT_TRUE(&reqRet == testAttr.get());
84         });
85     sptr<UserAuthCallbackInterface> callback = mockCallback;
86     auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
87     ASSERT_NE(contextCallback, nullptr);
88     contextCallback->OnAcquireInfo(static_cast<ExecutorRole>(0), 0, {});
89     contextCallback->OnResult(testResult, *testAttr);
90 }
91 
92 HWTEST_F(ContextCallbackImplTest, ContextCallbackImplUserIdmOnResult, TestSize.Level0)
93 {
94     int32_t testResult = 66;
95     std::vector<uint8_t> testMsg = {1, 2, 3, 4};
96     auto testAttr = Common::MakeShared<Attributes>();
97     ASSERT_TRUE(testAttr != nullptr);
98     EXPECT_TRUE(testAttr->SetInt32Value(Attributes::ATTR_REMAIN_TIMES, 2));
99     EXPECT_TRUE(testAttr->SetInt32Value(Attributes::ATTR_FREEZING_TIME, 40));
100 
__anon3528a01e0202(const ContextCallbackNotifyListener::MetaData &metaData) 101     auto notify = [](const ContextCallbackNotifyListener::MetaData &metaData) { return; };
102     ContextCallbackNotifyListener::GetInstance().AddNotifier(notify);
103     ContextCallbackNotifyListener::GetInstance().AddNotifier(nullptr);
104 
105     sptr<MockIdmCallback> mockCallback = new (nothrow) MockIdmCallback();
106     ASSERT_TRUE(mockCallback != nullptr);
107     EXPECT_CALL(*mockCallback, OnResult(_, _)).Times(1);
108     EXPECT_CALL(*mockCallback, OnAcquireInfo(_, _, _)).Times(1);
109     sptr<IdmCallbackInterface> callback = mockCallback;
110     auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
111     ASSERT_NE(contextCallback, nullptr);
112     contextCallback->OnAcquireInfo(static_cast<ExecutorRole>(0), 0, testMsg);
113     contextCallback->OnResult(testResult, *testAttr);
114 }
115 } // namespace UserAuth
116 } // namespace UserIam
117 } // namespace OHOS
118