1 /*
2 * Copyright (C) 2022-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 #include <gtest/gtest.h>
16
17 #include "context_factory.h"
18 #include "mock_user_auth_callback.h"
19 #include "mock_user_idm_callback.h"
20
21 namespace OHOS {
22 namespace UserIam {
23 namespace UserAuth {
24 using namespace std;
25 using namespace testing;
26 using namespace testing::ext;
27 class ContextFactoryTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30
31 static void TearDownTestCase();
32
33 void SetUp() override;
34
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void ContextFactoryTest::SetUpTestCase()
39 {
40 }
41
TearDownTestCase()42 void ContextFactoryTest::TearDownTestCase()
43 {
44 }
45
SetUp()46 void ContextFactoryTest::SetUp()
47 {
48 }
49
TearDown()50 void ContextFactoryTest::TearDown()
51 {
52 }
53
54 HWTEST_F(ContextFactoryTest, ContextFactoryCreateSimpleAuth_001, TestSize.Level0)
55 {
56 auto factory = ContextFactory::GetInstance();
57 ASSERT_NE(factory, nullptr);
58 std::vector<uint8_t> challenge;
59 sptr<UserAuthCallbackInterface> callback(new (std::nothrow) MockUserAuthCallback());
60 ASSERT_NE(callback, nullptr);
61 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_AUTH_USER_ALL);
62 Authentication::AuthenticationPara para = {};
63 auto context = factory->CreateSimpleAuthContext(para, contextCallback);
64 ASSERT_NE(context, nullptr);
65 EXPECT_NE(context->GetContextId(), 0U);
66 ASSERT_EQ(context->GetContextType(), CONTEXT_SIMPLE_AUTH);
67 }
68
69 HWTEST_F(ContextFactoryTest, ContextFactoryCreateSimpleAuth_002, TestSize.Level0)
70 {
71 auto factory = ContextFactory::GetInstance();
72 ASSERT_NE(factory, nullptr);
73 std::vector<uint8_t> challenge;
74 // Error: callback is null
75 sptr<UserAuthCallbackInterface> callback(nullptr);
76 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_AUTH_USER_ALL);
77 Authentication::AuthenticationPara para = {};
78 auto context = factory->CreateSimpleAuthContext(para, contextCallback);
79 ASSERT_EQ(context, nullptr);
80 }
81
82 HWTEST_F(ContextFactoryTest, ContextFactoryCreateIdentify_001, TestSize.Level0)
83 {
84 auto factory = ContextFactory::GetInstance();
85 ASSERT_NE(factory, nullptr);
86 std::vector<uint8_t> challenge;
87 sptr<UserAuthCallbackInterface> callback(new (std::nothrow) MockUserAuthCallback());
88 ASSERT_NE(callback, nullptr);
89 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_IDENTIFY);
90 Identification::IdentificationPara para = {};
91 auto context = factory->CreateIdentifyContext(para, contextCallback);
92 ASSERT_NE(context, nullptr);
93 EXPECT_NE(context->GetContextId(), 0U);
94 ASSERT_EQ(context->GetContextType(), CONTEXT_IDENTIFY);
95 }
96
97 HWTEST_F(ContextFactoryTest, ContextFactoryCreateIdentify_002, TestSize.Level0)
98 {
99 auto factory = ContextFactory::GetInstance();
100 ASSERT_NE(factory, nullptr);
101 std::vector<uint8_t> challenge;
102 // Error: callback is null
103 sptr<UserAuthCallbackInterface> callback(nullptr);
104 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_IDENTIFY);
105 Identification::IdentificationPara para = {};
106 auto context = factory->CreateIdentifyContext(para, contextCallback);
107 ASSERT_EQ(context, nullptr);
108 }
109
110 HWTEST_F(ContextFactoryTest, ContextFactoryCreateEnrollContext_001, TestSize.Level0)
111 {
112 auto factory = ContextFactory::GetInstance();
113 ASSERT_NE(factory, nullptr);
114 std::vector<uint8_t> token;
115 sptr<IdmCallbackInterface> callback(new (std::nothrow) MockIdmCallback());
116 ASSERT_NE(callback, nullptr);
117 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
118 Enrollment::EnrollmentPara para = {};
119 auto context = factory->CreateEnrollContext(para, contextCallback);
120 ASSERT_NE(context, nullptr);
121 EXPECT_NE(context->GetContextId(), 0U);
122 ASSERT_EQ(context->GetContextType(), CONTEXT_ENROLL);
123 }
124
125 HWTEST_F(ContextFactoryTest, ContextFactoryCreateEnrollContext_002, TestSize.Level0)
126 {
127 auto factory = ContextFactory::GetInstance();
128 ASSERT_NE(factory, nullptr);
129 std::vector<uint8_t> token;
130 // Error: callback is null
131 sptr<IdmCallbackInterface> callback(nullptr);
132 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
133 Enrollment::EnrollmentPara para = {};
134 auto context = factory->CreateEnrollContext(para, contextCallback);
135 ASSERT_EQ(context, nullptr);
136 }
137
138 HWTEST_F(ContextFactoryTest, ContextFactoryCreateWidgetAuthContext_001, TestSize.Level0)
139 {
140 auto factory = ContextFactory::GetInstance();
141 ASSERT_NE(factory, nullptr);
142 std::vector<uint8_t> token;
143 sptr<IdmCallbackInterface> callback(nullptr);
144 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
145 EXPECT_EQ(ContextFactory::CreateWidgetAuthContext(contextCallback), nullptr);
146 }
147
148 HWTEST_F(ContextFactoryTest, ContextFactoryCreateWidgetContext_001, TestSize.Level0)
149 {
150 auto factory = ContextFactory::GetInstance();
151 ASSERT_NE(factory, nullptr);
152 std::vector<uint8_t> token;
153 sptr<IdmCallbackInterface> callback(new (std::nothrow) MockIdmCallback());
154 ASSERT_NE(callback, nullptr);
155 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
156 ContextFactory::AuthWidgetContextPara para = {};
157 auto context = factory->CreateWidgetContext(para, contextCallback);
158 ASSERT_NE(context, nullptr);
159 }
160
161 HWTEST_F(ContextFactoryTest, ContextFactoryCreateWidgetContext_002, TestSize.Level0)
162 {
163 auto factory = ContextFactory::GetInstance();
164 ASSERT_NE(factory, nullptr);
165 std::vector<uint8_t> token;
166 sptr<IdmCallbackInterface> callback(nullptr);
167 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
168 ContextFactory::AuthWidgetContextPara para = {};
169 auto context = factory->CreateWidgetContext(para, contextCallback);
170 ASSERT_EQ(context, nullptr);
171 }
172
173 } // namespace UserAuth
174 } // namespace UserIam
175 } // namespace OHOS
176