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 #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 (nothrow) MockUserAuthCallback();
60 ASSERT_NE(callback, nullptr);
61 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_AUTH_USER);
62 ContextFactory::AuthContextPara 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);
77 ContextFactory::AuthContextPara 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 (nothrow) MockUserAuthCallback();
88 ASSERT_NE(callback, nullptr);
89 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_IDENTIFY);
90 ContextFactory::IdentifyContextPara 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 ContextFactory::IdentifyContextPara 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 (nothrow) MockIdmCallback();
116 ASSERT_NE(callback, nullptr);
117 auto contextCallback = ContextCallback::NewInstance(callback, TRACE_ADD_CREDENTIAL);
118 ContextFactory::EnrollContextPara 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 ContextFactory::EnrollContextPara para = {};
134 auto context = factory->CreateEnrollContext(para, contextCallback);
135 ASSERT_EQ(context, nullptr);
136 }
137 } // namespace UserAuth
138 } // namespace UserIam
139 } // namespace OHOS
140