• 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_METHOD2(TranslateIRemoteObject, int(int32_t cmd, const sptr<IRemoteObject> &obj));
54     MOCK_METHOD1(QueryServerSessionObject, std::shared_ptr<DBinderSessionObject>(uint32_t handle));
55     MOCK_METHOD1(UpdateClientSession, bool(std::shared_ptr<DBinderSessionObject> sessionObject));
56     MOCK_METHOD1(QueryHandleBySession, uint32_t(std::shared_ptr<DBinderSessionObject> session));
57     MOCK_METHOD1(QueryClientSessionObject, std::shared_ptr<DBinderSessionObject>(uint32_t databusHandle));
58     MOCK_METHOD2(NewSessionOfBinderProxy, std::shared_ptr<DBinderSessionObject>(uint32_t handle,
59         std::shared_ptr<DBinderSessionObject> session));
60     MOCK_METHOD2(QuerySessionOfBinderProxy, std::shared_ptr<DBinderSessionObject>(uint32_t handle,
61         std::shared_ptr<DBinderSessionObject> session));
62     MOCK_METHOD2(CreateServerSessionObject, std::shared_ptr<DBinderSessionObject>(binder_uintptr_t binder,
63         std::shared_ptr<DBinderSessionObject> sessionObject));
64     MOCK_METHOD3(FlattenSession, uint32_t(unsigned char *sessionOffset,
65         const std::shared_ptr<DBinderSessionObject> connectSession, uint32_t binderVersion));
66     MOCK_METHOD2(UnFlattenSession, std::shared_ptr<DBinderSessionObject>(unsigned char *sessionOffset,
67         uint32_t binderVersion));
68     MOCK_METHOD1(OnSendMessage, int(std::shared_ptr<DBinderSessionObject> sessionOfPeer));
69     MOCK_METHOD0(CreateProcessThread, bool());
70     MOCK_CONST_METHOD0(GetSeqNum, uint64_t());
71     MOCK_CONST_METHOD0(GetClientFd, int32_t());
72     MOCK_METHOD1(SetClientFd, void(int32_t fd));
73     MOCK_METHOD1(SetCallerPid, void(pid_t pid));
74     MOCK_METHOD1(SetCallerUid, void(pid_t uid));
75     MOCK_METHOD1(SetCallerDeviceID, void(const std::string &deviceId));
76     MOCK_METHOD1(SetCallerTokenID, void(const uint32_t tokerId));
77     MOCK_METHOD3(OnSendRawData, int(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size));
78 
79     uint32_t GetStatus() override;
80     void GetCallerInfo(DBinderCallerInfo &callerInfo) override;
81     void SetStatus(uint32_t status) override;
82     void SetCallerInfo(DBinderCallerInfo &callerInfo) override;
83     void SetSeqNum(uint64_t seq) override;
84     int SendReply(MessageParcel &reply, uint32_t flags, int32_t result) override;
85 
86 private:
87     pid_t callerPid_;
88     pid_t callerUid_;
89     std::string callerDeviceID_;
90     uint64_t callerTokenID_;
91     uint64_t firstTokenID_;
92     uint64_t seqNumber_ = 0;
93     int32_t clientFd_ = 0;
94     uint32_t status_;
95     int32_t result_ = 0;
96 };
97 
MockDBinderBaseInvoker()98 MockDBinderBaseInvoker::MockDBinderBaseInvoker()
99     : callerPid_(getpid()), callerUid_(getuid()), callerDeviceID_(""),
100     callerTokenID_(0), firstTokenID_(0), status_(0)
101 {
102 }
103 
104 /* These functions can be overridden if needed by unittest, here just implement them by default */
GetStatus()105 uint32_t MockDBinderBaseInvoker::GetStatus()
106 {
107     return status_;
108 }
109 
GetCallerInfo(DBinderCallerInfo & callerInfo)110 void MockDBinderBaseInvoker::GetCallerInfo(DBinderCallerInfo &callerInfo)
111 {
112     callerInfo.callerPid = callerPid_;
113     callerInfo.callerUid = callerUid_;
114     callerInfo.callerDeviceID = callerDeviceID_;
115     callerInfo.clientFd = clientFd_;
116     callerInfo.callerTokenID = callerTokenID_;
117     callerInfo.firstTokenID = firstTokenID_;
118 }
119 
SetStatus(uint32_t status)120 void MockDBinderBaseInvoker::SetStatus(uint32_t status)
121 {
122     status_ = status;
123 }
124 
SetCallerInfo(DBinderCallerInfo & callerInfo)125 void MockDBinderBaseInvoker::SetCallerInfo(DBinderCallerInfo &callerInfo)
126 {
127     callerPid_ = callerInfo.callerPid;
128     callerUid_ = callerInfo.callerUid;
129     callerDeviceID_ = callerInfo.callerDeviceID;
130     clientFd_ = callerInfo.clientFd;
131     callerTokenID_ = callerInfo.callerTokenID;
132     firstTokenID_ = callerInfo.firstTokenID;
133 }
134 
SendReply(MessageParcel & reply,uint32_t flags,int32_t result)135 int MockDBinderBaseInvoker::SendReply(MessageParcel &reply, uint32_t flags, int32_t result)
136 {
137     (void)reply;
138     (void)flags;
139     result_ = result;
140 
141     return 0;
142 }
143 
SetSeqNum(uint64_t seq)144 void MockDBinderBaseInvoker::SetSeqNum(uint64_t seq)
145 {
146     seqNumber_ = seq;
147 }
148 
149 } // namespace OHOS
150 #endif // OHOS_MOCK_DBINDER_BASE_INVOKER_H