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(GetExecutorSensorHint, uint64_t());
35     MOCK_CONST_METHOD0(GetExecutorMatcher, uint64_t());
36     MOCK_CONST_METHOD0(GetExecutorEsl, ExecutorSecureLevel());
37     MOCK_CONST_METHOD0(GetExecutorPublicKey, std::vector<uint8_t>());
38     MOCK_CONST_METHOD0(GetExecutorDeviceUdid, std::string());
39 
40     MOCK_METHOD3(BeginExecute,
41         int32_t(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command));
42     MOCK_METHOD2(EndExecute, int32_t(uint64_t scheduleId, const Attributes &command));
43     MOCK_METHOD1(SetProperty, int32_t(const Attributes &properties));
44     MOCK_METHOD2(GetProperty, int32_t(const Attributes &condition, Attributes &values));
45     MOCK_METHOD2(SendData, int32_t(uint64_t scheduleId, const Attributes &data));
46     MOCK_METHOD0(DeleteFromDriver, void());
47     MOCK_METHOD0(DetachFromDriver, void());
48 
49     static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, bool detach = false)
50     {
51         using namespace testing;
52         auto node = std::make_shared<MockResourceNode>();
53         EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId));
54         EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(PIN));
55         EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(COLLECTOR));
56         EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0));
57         EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0));
58         EXPECT_CALL(*node, DetachFromDriver()).Times(detach ? 1 : 0);
59         return node;
60     }
61 
CreateWithExecuteIndex(uint64_t executorId,AuthType authType,ExecutorRole executorRole,ExecutorCallbackInterface & callback)62     static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, AuthType authType,
63         ExecutorRole executorRole, ExecutorCallbackInterface &callback)
64     {
65         using namespace testing;
66         auto node = std::make_shared<MockResourceNode>();
67         std::vector<uint8_t> key;
68         EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId));
69         EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(authType));
70         EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(executorRole));
71         EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0));
72         EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0));
73         EXPECT_CALL(*node, GetExecutorPublicKey()).WillRepeatedly(Return(key));
74         EXPECT_CALL(*node, BeginExecute(_, _, _)).Times(AnyNumber());
75         EXPECT_CALL(*node, EndExecute(_, _)).Times(AnyNumber());
76 
77         ON_CALL(*node, BeginExecute)
78             .WillByDefault(
79                 [&callback](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
80                     return callback.OnBeginExecute(scheduleId, publicKey, command);
81                 });
82         ON_CALL(*node, EndExecute).WillByDefault([&callback](uint64_t scheduleId, const Attributes &command) {
83             return callback.OnEndExecute(scheduleId, command);
84         });
85 
86         return node;
87     }
88 
CreateWithExecuteIndex(uint64_t executorId,AuthType authType,ExecutorRole executorRole)89     static std::shared_ptr<ResourceNode> CreateWithExecuteIndex(uint64_t executorId, AuthType authType,
90         ExecutorRole executorRole)
91     {
92         using namespace testing;
93         auto node = std::make_shared<MockResourceNode>();
94         std::vector<uint8_t> key;
95         EXPECT_CALL(*node, GetExecutorIndex()).WillRepeatedly(Return(executorId));
96         EXPECT_CALL(*node, GetAuthType()).WillRepeatedly(Return(authType));
97         EXPECT_CALL(*node, GetExecutorRole()).WillRepeatedly(Return(executorRole));
98         EXPECT_CALL(*node, GetExecutorMatcher()).WillRepeatedly(Return(0));
99         EXPECT_CALL(*node, GetExecutorSensorHint()).WillRepeatedly(Return(0));
100         EXPECT_CALL(*node, GetExecutorPublicKey()).WillRepeatedly(Return(key));
101         EXPECT_CALL(*node, BeginExecute(_, _, _)).Times(AnyNumber());
102         EXPECT_CALL(*node, EndExecute(_, _)).Times(AnyNumber());
103         return node;
104     }
105 };
106 } // namespace UserAuth
107 } // namespace UserIam
108 } // namespace OHOS
109 #endif // IAM_MOCK_RESOURCE_NODE_H