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 "user_idm_session_controller_test.h"
17 
18 #include "iam_common_defines.h"
19 #include "mock_iuser_auth_interface.h"
20 #include "user_idm_session_controller.h"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 using namespace testing;
26 using namespace testing::ext;
27 
SetUpTestCase()28 void UserIdmSessionControllerTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void UserIdmSessionControllerTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void UserIdmSessionControllerTest::SetUp()
37 {
38 }
39 
TearDown()40 void UserIdmSessionControllerTest::TearDown()
41 {
42     MockIUserAuthInterface::Holder::GetInstance().Reset();
43     UserIdmSessionController::Instance().ForceReset();
44 }
45 
46 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceOpenSessionSuccess, TestSize.Level0)
47 {
48     int32_t userId = 100;
49     std::vector<uint8_t> challenge;
50 
51     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
52     EXPECT_CALL(*mock, OpenSession(userId, _)).WillRepeatedly(Return(SUCCESS));
53 
54     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId, challenge));
55 }
56 
57 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceOpenSessionTwice, TestSize.Level0)
58 {
59     int32_t userId1 = 100;
60     int32_t userId2 = 100;
61     int32_t userId3 = 200;
62     std::vector<uint8_t> challenge;
63 
64     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
65     EXPECT_CALL(*mock, OpenSession(_, _)).WillRepeatedly(Return(SUCCESS));
66 
67     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId1, challenge));
68     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId2, challenge));
69     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId3, challenge));
70 }
71 
72 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceOpenSessionFailed, TestSize.Level0)
73 {
74     int32_t userId = 100;
75     std::vector<uint8_t> challenge;
76 
77     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
78     EXPECT_CALL(*mock, OpenSession(userId, _)).WillRepeatedly(Return(GENERAL_ERROR));
79 
80     EXPECT_EQ(false, UserIdmSessionController::Instance().OpenSession(userId, challenge));
81 }
82 
83 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceIsSessionOpened, TestSize.Level0)
84 {
85     int32_t userId = 100;
86     std::vector<uint8_t> nonce = {1, 3, 2, 5, 7};
87 
88     std::vector<uint8_t> challenge;
89 
__anonad6838d20102(std::vector<uint8_t> &challenge) 90     auto fillUpChallenge = [&nonce](std::vector<uint8_t> &challenge) { challenge = nonce; };
91 
92     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
93     EXPECT_CALL(*mock, OpenSession(userId, _)).WillRepeatedly(DoAll(WithArg<1>(fillUpChallenge), Return(SUCCESS)));
94 
95     EXPECT_EQ(false, UserIdmSessionController::Instance().IsSessionOpened(userId));
96 
97     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId, challenge));
98     EXPECT_THAT(challenge, ElementsAreArray(nonce));
99 
100     EXPECT_EQ(true, UserIdmSessionController::Instance().IsSessionOpened(userId));
101 }
102 
103 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceIsSessionClosedByUserId, TestSize.Level0)
104 {
105     int32_t userId = 100;
106     std::vector<uint8_t> nonce = {1, 3, 2, 5, 7};
107 
108     std::vector<uint8_t> challenge;
109 
__anonad6838d20202(std::vector<uint8_t> &challenge) 110     auto fillUpChallenge = [&nonce](std::vector<uint8_t> &challenge) { challenge = nonce; };
111 
112     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
113     EXPECT_CALL(*mock, OpenSession(userId, _)).WillRepeatedly(DoAll(WithArg<1>(fillUpChallenge), Return(SUCCESS)));
114     EXPECT_CALL(*mock, CloseSession(userId))
115         .WillOnce(Return(HDF_SUCCESS))
116         .WillOnce(Return(HDF_FAILURE));
117 
118     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId, challenge));
119 
120     EXPECT_TRUE(UserIdmSessionController::Instance().CloseSession(userId));
121     EXPECT_FALSE(UserIdmSessionController::Instance().CloseSession(userId));
122 }
123 
124 HWTEST_F(UserIdmSessionControllerTest, UserIdmServiceIsSessionClosedByChallenge, TestSize.Level0)
125 {
126     int32_t userId = 100;
127     std::vector<uint8_t> nonce = {1, 3, 2, 5, 7};
128 
129     std::vector<uint8_t> challenge;
130 
__anonad6838d20302(std::vector<uint8_t> &challenge) 131     auto fillUpChallenge = [&nonce](std::vector<uint8_t> &challenge) { challenge = nonce; };
132 
133     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
134     EXPECT_CALL(*mock, OpenSession(userId, _)).WillRepeatedly(DoAll(WithArg<1>(fillUpChallenge), Return(SUCCESS)));
135     EXPECT_CALL(*mock, CloseSession(userId)).WillRepeatedly(Return(SUCCESS));
136 
137     EXPECT_EQ(true, UserIdmSessionController::Instance().OpenSession(userId, challenge));
138 
139     EXPECT_EQ(true, UserIdmSessionController::Instance().CloseSession(userId));
140 }
141 } // namespace UserAuth
142 } // namespace UserIam
143 } // namespace OHOS