• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef IAM_MOCK_CONTEXT_H
16 #define IAM_MOCK_CONTEXT_H
17 
18 #include <memory>
19 
20 #include <gmock/gmock.h>
21 
22 #include "app_mgr_interface.h"
23 #include "context.h"
24 #include "context_callback.h"
25 #include "iam_ptr.h"
26 #include "mock_schedule_node.h"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 using namespace OHOS::AppExecFwk;
32 class MockContextCallback : public ContextCallback {
33 public:
34     virtual ~MockContextCallback() = default;
35     MOCK_METHOD2(NewInstance, std::shared_ptr<ContextCallback>(sptr<IIamCallback> iamCallback,
36         OperationType operationType));
37     MOCK_METHOD2(OnResult, void(int32_t resultCode, const Attributes &finalResult));
38     MOCK_METHOD3(
39         OnAcquireInfo, void(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg));
40     MOCK_METHOD1(SetTraceCallerName, void(const std::string &callerName));
41     MOCK_METHOD1(SetTraceRequestContextId, void(uint64_t requestContextId));
42     MOCK_METHOD1(SetTraceAuthContextId, void(uint64_t authContextId));
43     MOCK_METHOD1(SetTraceUserId, void(int32_t userId));
44     MOCK_METHOD1(SetTraceRemainTime, void(int32_t remainTime));
45     MOCK_METHOD1(SetTraceFreezingTime, void(int32_t freezingTime));
46     MOCK_METHOD1(SetTraceSdkVersion, void(int32_t version));
47     MOCK_METHOD1(SetTraceAuthType, void(int32_t authType));
48     MOCK_METHOD1(SetTraceAuthWidgetType, void(uint32_t authWidgetType));
49     MOCK_METHOD1(SetTraceAuthTrustLevel, void(AuthTrustLevel atl));
50     MOCK_METHOD1(SetTraceReuseUnlockResultMode, void(uint32_t reuseUnlockResultMode));
51     MOCK_METHOD1(SetTraceReuseUnlockResultDuration, void(uint64_t reuseUnlockResultDuration));
52     MOCK_METHOD1(SetCleaner, void(Context::ContextStopCallback callback));
53     MOCK_METHOD4(ParseAuthTipInfo, int32_t(int32_t tip, const std::vector<uint8_t> &extraInfo,
54         int32_t &authResult, int32_t &freezingTime));
55     MOCK_METHOD2(ProcessAuthResult, void(int32_t tip, const std::vector<uint8_t> &extraInfo));
56     MOCK_METHOD0(GetIamCallback, sptr<IIamCallback>());
57     MOCK_METHOD0(GetCallerName, std::string());
58     MOCK_METHOD1(SetTraceCallerType, void(int32_t callerType));
59     MOCK_METHOD1(SetTraceIsRemoteAuth, void(bool isRemoteAuth));
60     MOCK_METHOD1(SetTraceRemoteUdid, void(const std::string &remoteUdid));
61     MOCK_METHOD1(SetTraceLocalUdid, void(const std::string &LocalUdid));
62     MOCK_METHOD1(SetTraceConnectionName, void(const std::string &connectionName));
63     MOCK_METHOD1(SetTraceAuthFinishReason, void(const std::string &authFinishReason));
64     MOCK_METHOD1(SetTraceIsBackgroundApplication, void(const bool isBackgroundApplication));
65 };
66 
67 class MockContext final : public Context {
68 public:
69     MOCK_METHOD0(Start, bool());
70     MOCK_METHOD0(Stop, bool());
71     MOCK_CONST_METHOD0(GetContextId, uint64_t());
72     MOCK_CONST_METHOD0(GetContextType, ContextType());
73     MOCK_CONST_METHOD1(GetScheduleNode, std::shared_ptr<ScheduleNode>(uint64_t scheduleId));
74     MOCK_CONST_METHOD0(GetScheduleNodes, std::vector<std::shared_ptr<ScheduleNode>> ());
75     MOCK_CONST_METHOD0(GetLatestError, int32_t());
76     MOCK_CONST_METHOD0(GetTokenId, uint32_t());
77     MOCK_CONST_METHOD0(GetUserId, int32_t());
78     MOCK_CONST_METHOD0(GetAuthType, int32_t());
79     MOCK_CONST_METHOD0(GetCallerName, std::string());
80 
CreateWithContextId(uint64_t contextId)81     static std::shared_ptr<Context> CreateWithContextId(uint64_t contextId)
82     {
83         using namespace testing;
84         auto context = Common::MakeShared<MockContext>();
85         if (context == nullptr) {
86             EXPECT_NE(context, nullptr);
87             return nullptr;
88         };
89         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
90         return context;
91     }
92 
CreateContextWithScheduleNode(uint64_t contextId,std::set<uint64_t> scheduleIdList)93     static std::shared_ptr<Context> CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList)
94     {
95         using namespace testing;
96         auto context = Common::MakeShared<MockContext>();
97         if (context == nullptr) {
98             EXPECT_NE(context, nullptr);
99             return nullptr;
100         };
101         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
102         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
103 
104         ON_CALL(*context, GetScheduleNode)
105             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
106                 auto iter = scheduleIdList.find(id);
107                 if (iter != scheduleIdList.end()) {
108                     return MockScheduleNode::CreateWithScheduleId(id);
109                 }
110                 return nullptr;
111             });
112         return context;
113     }
114 
CreateContextWithScheduleNode(uint64_t contextId,const std::set<std::shared_ptr<ScheduleNode>> & scheduleIdList)115     static std::shared_ptr<Context> CreateContextWithScheduleNode(
116         uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList)
117     {
118         using namespace testing;
119         auto context = Common::MakeShared<MockContext>();
120         if (context == nullptr) {
121             EXPECT_NE(context, nullptr);
122             return nullptr;
123         };
124         EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
125         EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber());
126 
127         ON_CALL(*context, GetScheduleNode)
128             .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> {
129                 for (auto const &node : scheduleIdList) {
130                     if (node->GetScheduleId() == id) {
131                         return node;
132                     }
133                 }
134                 return nullptr;
135             });
136         return context;
137     }
138 
139 protected:
140     MOCK_METHOD1(SetLatestError, void(int32_t error));
141 };
142 } // namespace UserAuth
143 } // namespace UserIam
144 } // namespace OHOS
145 #endif // IAM_MOCK_CONTEXT_H
146