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 #ifndef IAM_MOCK_RESOURCE_NODE_H 16 #define IAM_MOCK_RESOURCE_NODE_H 17 18 #include <memory> 19 20 #include <gmock/gmock.h> 21 22 #include "resource_node.h" 23 24 namespace OHOS { 25 namespace UserIam { 26 namespace UserAuth { 27 class MockResourceNode final : public ResourceNode, public std::enable_shared_from_this<MockResourceNode> { 28 public: 29 MOCK_CONST_METHOD0(GetExecutorIndex, uint64_t()); 30 MOCK_CONST_METHOD0(GetOwnerDeviceId, std::string()); 31 MOCK_CONST_METHOD0(GetOwnerPid, uint32_t()); 32 MOCK_CONST_METHOD0(GetAuthType, AuthType()); 33 MOCK_CONST_METHOD0(GetExecutorRole, ExecutorRole()); 34 MOCK_CONST_METHOD0(GetExecutorMatcher, uint64_t()); 35 MOCK_CONST_METHOD0(GetExecutorSensorHint, uint64_t()); 36 MOCK_CONST_METHOD0(GetExecutorEsl, ExecutorSecureLevel()); 37 MOCK_CONST_METHOD0(GetExecutorPublicKey, std::vector<uint8_t>()); 38 39 MOCK_METHOD3(BeginExecute, 40 int32_t(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command)); 41 MOCK_METHOD2(EndExecute, int32_t(uint64_t scheduleId, const Attributes &command)); 42 MOCK_METHOD1(SetProperty, int32_t(const Attributes &properties)); 43 MOCK_METHOD2(GetProperty, int32_t(const Attributes &condition, Attributes &values)); 44 MOCK_METHOD0(Detach, void()); 45 46 static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, bool detach = false) 47 { 48 using namespace testing; 49 auto node = std::make_shared<MockResourceNode>(); 50 EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId)); 51 EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(PIN)); 52 EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(COLLECTOR)); 53 EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0)); 54 EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0)); 55 EXPECT_CALL(*node, Detach()).Times(detach ? 1 : 0); 56 return node; 57 } 58 CreateWithExecuteIndex(uint64_t executorId,AuthType authType,ExecutorRole executorRole,ExecutorCallbackInterface & callback)59 static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, AuthType authType, 60 ExecutorRole executorRole, ExecutorCallbackInterface &callback) 61 { 62 using namespace testing; 63 auto node = std::make_shared<MockResourceNode>(); 64 std::vector<uint8_t> key; 65 EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId)); 66 EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(authType)); 67 EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(executorRole)); 68 EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0)); 69 EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0)); 70 EXPECT_CALL(*node, GetExecutorPublicKey()).WillRepeatedly(Return(key)); 71 EXPECT_CALL(*node, BeginExecute(_, _, _)).Times(AnyNumber()); 72 EXPECT_CALL(*node, EndExecute(_, _)).Times(AnyNumber()); 73 74 ON_CALL(*node, BeginExecute) 75 .WillByDefault( 76 [&callback](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) { 77 return callback.OnBeginExecute(scheduleId, publicKey, command); 78 }); 79 ON_CALL(*node, EndExecute).WillByDefault([&callback](uint64_t scheduleId, const Attributes &command) { 80 return callback.OnEndExecute(scheduleId, command); 81 }); 82 83 return node; 84 } 85 CreateWithExecuteIndex(uint64_t executorId,AuthType authType,ExecutorRole executorRole)86 static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, AuthType authType, 87 ExecutorRole executorRole) 88 { 89 using namespace testing; 90 auto node = std::make_shared<MockResourceNode>(); 91 std::vector<uint8_t> key; 92 EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId)); 93 EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(authType)); 94 EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(executorRole)); 95 EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0)); 96 EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0)); 97 EXPECT_CALL(*node, GetExecutorPublicKey()).WillRepeatedly(Return(key)); 98 EXPECT_CALL(*node, BeginExecute(_, _, _)).Times(AnyNumber()); 99 EXPECT_CALL(*node, EndExecute(_, _)).Times(AnyNumber()); 100 return node; 101 } 102 }; 103 } // namespace UserAuth 104 } // namespace UserIam 105 } // namespace OHOS 106 #endif // IAM_MOCK_RESOURCE_NODE_H