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<IamCallbackInterface> 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_METHOD2(ProcessAuthResult, void(int32_t tip, const std::vector<uint8_t> &extraInfo)); 54 MOCK_METHOD0(GetIamCallback, sptr<IamCallbackInterface>()); 55 MOCK_METHOD0(GetCallerName, std::string()); 56 MOCK_METHOD1(SetTraceCallerType, void(int32_t callerType)); 57 MOCK_METHOD1(SetTraceIsRemoteAuth, void(bool isRemoteAuth)); 58 MOCK_METHOD1(SetTraceRemoteUdid, void(const std::string &remoteUdid)); 59 MOCK_METHOD1(SetTraceLocalUdid, void(const std::string &LocalUdid)); 60 MOCK_METHOD1(SetTraceConnectionName, void(const std::string &connectionName)); 61 MOCK_METHOD1(SetTraceAuthFinishReason, void(const std::string &authFinishReason)); 62 MOCK_METHOD1(SetTraceIsBackgroundApplication, void(const bool isBackgroundApplication)); 63 }; 64 65 class MockContext final : public Context { 66 public: 67 MOCK_METHOD0(Start, bool()); 68 MOCK_METHOD0(Stop, bool()); 69 MOCK_CONST_METHOD0(GetContextId, uint64_t()); 70 MOCK_CONST_METHOD0(GetContextType, ContextType()); 71 MOCK_CONST_METHOD1(GetScheduleNode, std::shared_ptr<ScheduleNode>(uint64_t scheduleId)); 72 MOCK_CONST_METHOD0(GetScheduleNodes, std::vector<std::shared_ptr<ScheduleNode>> ()); 73 MOCK_CONST_METHOD0(GetLatestError, int32_t()); 74 MOCK_CONST_METHOD0(GetTokenId, uint32_t()); 75 MOCK_CONST_METHOD0(GetUserId, int32_t()); 76 CreateWithContextId(uint64_t contextId)77 static std::shared_ptr<Context> CreateWithContextId(uint64_t contextId) 78 { 79 using namespace testing; 80 auto context = Common::MakeShared<MockContext>(); 81 if (context == nullptr) { 82 EXPECT_NE(context, nullptr); 83 return nullptr; 84 }; 85 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 86 return context; 87 } 88 CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList)89 static std::shared_ptr<Context> CreateContextWithScheduleNode(uint64_t contextId, std::set<uint64_t> scheduleIdList) 90 { 91 using namespace testing; 92 auto context = Common::MakeShared<MockContext>(); 93 if (context == nullptr) { 94 EXPECT_NE(context, nullptr); 95 return nullptr; 96 }; 97 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 98 EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber()); 99 100 ON_CALL(*context, GetScheduleNode) 101 .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> { 102 auto iter = scheduleIdList.find(id); 103 if (iter != scheduleIdList.end()) { 104 return MockScheduleNode::CreateWithScheduleId(id); 105 } 106 return nullptr; 107 }); 108 return context; 109 } 110 CreateContextWithScheduleNode( uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList)111 static std::shared_ptr<Context> CreateContextWithScheduleNode( 112 uint64_t contextId, const std::set<std::shared_ptr<ScheduleNode>> &scheduleIdList) 113 { 114 using namespace testing; 115 auto context = Common::MakeShared<MockContext>(); 116 if (context == nullptr) { 117 EXPECT_NE(context, nullptr); 118 return nullptr; 119 }; 120 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId)); 121 EXPECT_CALL(*context, GetScheduleNode(_)).Times(AnyNumber()); 122 123 ON_CALL(*context, GetScheduleNode) 124 .WillByDefault([scheduleIdList](uint64_t id) -> std::shared_ptr<ScheduleNode> { 125 for (auto const &node : scheduleIdList) { 126 if (node->GetScheduleId() == id) { 127 return node; 128 } 129 } 130 return nullptr; 131 }); 132 return context; 133 } 134 135 protected: 136 MOCK_METHOD1(SetLatestError, void(int32_t error)); 137 }; 138 } // namespace UserAuth 139 } // namespace UserIam 140 } // namespace OHOS 141 #endif // IAM_MOCK_CONTEXT_H