• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef OHOS_MOCK_DBINDER_BASE_INVOKER_H
17 #define OHOS_MOCK_DBINDER_BASE_INVOKER_H
18 
19 #include "gtest/gtest.h"
20 #include "gmock/gmock.h"
21 #include "dbinder_base_invoker.h"
22 
23 namespace OHOS {
24 class MockDBinderBaseInvoker : public DBinderBaseInvoker<DBinderSessionObject> {
25 public:
26     MockDBinderBaseInvoker();
27     MOCK_METHOD2(CheckAndSetCallerInfo, int(int32_t socketId, uint64_t stubIndex));
28     MOCK_METHOD1(AcquireHandle, bool(int32_t handle));
29     MOCK_METHOD1(ReleaseHandle, bool(int32_t handle));
30     MOCK_METHOD1(JoinThread, void(bool initiative));
31     MOCK_METHOD1(JoinProcessThread, void(bool initiative));
32     MOCK_METHOD0(StopWorkThread, void());
33     MOCK_CONST_METHOD0(GetCallerSid, std::string());
34     MOCK_CONST_METHOD0(GetCallerPid, pid_t());
35     MOCK_CONST_METHOD0(GetCallerRealPid, pid_t());
36     MOCK_CONST_METHOD0(GetCallerUid, uid_t());
37     MOCK_CONST_METHOD0(GetCallerTokenID, uint64_t());
38     MOCK_CONST_METHOD0(GetFirstCallerTokenID, uint64_t());
39     MOCK_CONST_METHOD0(GetSelfTokenID, uint64_t());
40     MOCK_CONST_METHOD0(GetSelfFirstCallerTokenID, uint64_t());
41     MOCK_METHOD0(IsLocalCalling, bool());
42     MOCK_METHOD0(GetLocalDeviceID, std::string());
43     MOCK_CONST_METHOD0(GetCallerDeviceID, std::string());
44     MOCK_CONST_METHOD2(FlattenObject, bool(Parcel &parcel, const IRemoteObject *object));
45     MOCK_METHOD1(UnflattenObject, sptr<IRemoteObject>(Parcel &parcel));
46     MOCK_METHOD1(ReadFileDescriptor, int(Parcel &parcel));
47     MOCK_METHOD3(WriteFileDescriptor, bool(Parcel &parcel, int fd, bool takeOwnership));
48     MOCK_METHOD1(FlushCommands, int(IRemoteObject *object));
49     MOCK_METHOD0(ResetCallingIdentity, std::string());
50     MOCK_METHOD2(SetCallingIdentity, bool(std::string &identity, bool flag));
51     MOCK_METHOD0(TriggerSystemIPCThreadReclaim, bool());
52     MOCK_METHOD1(EnableIPCThreadReclaim, bool(bool enable));
53     MOCK_METHOD1(QueryServerSessionObject, std::shared_ptr<DBinderSessionObject>(uint32_t handle));
54     MOCK_METHOD1(UpdateClientSession, bool(std::shared_ptr<DBinderSessionObject> sessionObject));
55     MOCK_METHOD1(QueryHandleBySession, uint32_t(std::shared_ptr<DBinderSessionObject> session));
56     MOCK_METHOD1(QueryClientSessionObject, std::shared_ptr<DBinderSessionObject>(uint32_t databusHandle));
57     MOCK_METHOD2(NewSessionOfBinderProxy, std::shared_ptr<DBinderSessionObject>(uint32_t handle,
58         std::shared_ptr<DBinderSessionObject> session));
59     MOCK_METHOD2(QuerySessionOfBinderProxy, std::shared_ptr<DBinderSessionObject>(uint32_t handle,
60         std::shared_ptr<DBinderSessionObject> session));
61     MOCK_METHOD2(CreateServerSessionObject, std::shared_ptr<DBinderSessionObject>(binder_uintptr_t binder,
62         std::shared_ptr<DBinderSessionObject> sessionObject));
63     MOCK_METHOD3(FlattenSession, uint32_t(unsigned char *sessionOffset,
64         const std::shared_ptr<DBinderSessionObject> connectSession, uint32_t binderVersion));
65     MOCK_METHOD2(UnFlattenSession, std::shared_ptr<DBinderSessionObject>(unsigned char *sessionOffset,
66         uint32_t binderVersion));
67     MOCK_METHOD1(OnSendMessage, int(std::shared_ptr<DBinderSessionObject> sessionOfPeer));
68     MOCK_METHOD0(CreateProcessThread, bool());
69     MOCK_CONST_METHOD0(GetSeqNum, uint64_t());
70     MOCK_CONST_METHOD0(GetClientFd, int32_t());
71     MOCK_METHOD1(SetClientFd, void(int32_t fd));
72     MOCK_METHOD1(SetCallerPid, void(pid_t pid));
73     MOCK_METHOD1(SetCallerUid, void(pid_t uid));
74     MOCK_METHOD1(SetCallerDeviceID, void(const std::string &deviceId));
75     MOCK_METHOD1(SetCallerTokenID, void(const uint32_t tokerId));
76     MOCK_METHOD3(OnSendRawData, int(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size));
77 
78     uint32_t GetStatus() override;
79     void GetCallerInfo(DBinderCallerInfo &callerInfo) override;
80     void SetStatus(uint32_t status) override;
81     void SetCallerInfo(DBinderCallerInfo &callerInfo) override;
82     void SetSeqNum(uint64_t seq) override;
83     int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) override;
84 
85 private:
86     pid_t callerPid_;
87     pid_t callerUid_;
88     std::string callerDeviceID_;
89     uint64_t callerTokenID_;
90     uint64_t firstTokenID_;
91     uint64_t seqNumber_ = 0;
92     int32_t clientFd_ = 0;
93     uint32_t status_;
94     int32_t result_ = 0;
95 };
96 
MockDBinderBaseInvoker()97 MockDBinderBaseInvoker::MockDBinderBaseInvoker()
98     : callerPid_(getpid()), callerUid_(getuid()), callerDeviceID_(""),
99     callerTokenID_(0), firstTokenID_(0), status_(0)
100 {
101 }
102 
103 /* These functions can be overridden if needed by unittest, here just implement them by default */
GetStatus()104 uint32_t MockDBinderBaseInvoker::GetStatus()
105 {
106     return status_;
107 }
108 
GetCallerInfo(DBinderCallerInfo & callerInfo)109 void MockDBinderBaseInvoker::GetCallerInfo(DBinderCallerInfo &callerInfo)
110 {
111     callerInfo.callerPid = callerPid_;
112     callerInfo.callerUid = callerUid_;
113     callerInfo.callerDeviceID = callerDeviceID_;
114     callerInfo.clientFd = clientFd_;
115     callerInfo.callerTokenID = callerTokenID_;
116     callerInfo.firstTokenID = firstTokenID_;
117 }
118 
SetStatus(uint32_t status)119 void MockDBinderBaseInvoker::SetStatus(uint32_t status)
120 {
121     status_ = status;
122 }
123 
SetCallerInfo(DBinderCallerInfo & callerInfo)124 void MockDBinderBaseInvoker::SetCallerInfo(DBinderCallerInfo &callerInfo)
125 {
126     callerPid_ = callerInfo.callerPid;
127     callerUid_ = callerInfo.callerUid;
128     callerDeviceID_ = callerInfo.callerDeviceID;
129     clientFd_ = callerInfo.clientFd;
130     callerTokenID_ = callerInfo.callerTokenID;
131     firstTokenID_ = callerInfo.firstTokenID;
132 }
133 
SendReply(MessageParcel & reply,uint32_t flags,int32_t result)134 int MockDBinderBaseInvoker::SendReply(MessageParcel &reply, uint32_t flags, int32_t result)
135 {
136     (void)reply;
137     (void)flags;
138     result_ = result;
139 
140     return 0;
141 }
142 
SetSeqNum(uint64_t seq)143 void MockDBinderBaseInvoker::SetSeqNum(uint64_t seq)
144 {
145     seqNumber_ = seq;
146 }
147 
148 } // namespace OHOS
149 #endif // OHOS_MOCK_DBINDER_BASE_INVOKER_H