• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "remote_object_mock.h"
17 #include <cstring>
18 #include "if_softbus_client.h"
19 #include "softbus_log.h"
20 #include "softbus_ipc_def.h"
21 #include "securec.h"
22 
23 using testing::_;
24 namespace OHOS {
RemoteObjectMock()25 RemoteObjectMock::RemoteObjectMock()
26     : IRemoteObject(std::u16string())
27 {
28     DLOGI("construct");
29 }
30 
~RemoteObjectMock()31 RemoteObjectMock::~RemoteObjectMock()
32 {
33     DLOGI("destroy");
34     instance_ = nullptr;
35 }
36 
SetSelf(const sptr<RemoteObjectMock> & self)37 void RemoteObjectMock::SetSelf(const sptr<RemoteObjectMock> &self)
38 {
39     instance_ = self;
40 }
41 
Destroy()42 void RemoteObjectMock::Destroy()
43 {
44     instance_ = nullptr;
45 }
46 
SetupStub(const sptr<RemoteObjectMock> & self)47 void RemoteObjectMock::SetupStub(const sptr<RemoteObjectMock> &self)
48 {
49     SetSelf(self);
50     EXPECT_CALL(*self, SendRequest(CLIENT_DISCOVERY_DEVICE_FOUND, _, _, _))
51         .WillRepeatedly(ActionOfSendRequestForOnDeviceFound);
52     EXPECT_CALL(*self, SendRequest(CLIENT_DISCOVERY_SUCC, _, _, _))
53         .WillRepeatedly(ActionOfSendRequestForOnDiscoverySuccess);
54     EXPECT_CALL(*self, SendRequest(CLIENT_DISCOVERY_FAIL, _, _, _))
55         .WillRepeatedly(ActionOfSendRequestForOnDiscoveryFailed);
56     EXPECT_CALL(*self, SendRequest(CLIENT_PUBLISH_SUCC, _, _, _))
57         .WillRepeatedly(ActionOfSendRequestForOnPublishSuccess);
58     EXPECT_CALL(*self, SendRequest(CLIENT_PUBLISH_FAIL, _, _, _))
59         .WillRepeatedly(ActionOfSendRequestForOnPublishFail);
60 }
61 
Get()62 sptr<RemoteObjectMock> RemoteObjectMock::Get()
63 {
64     return instance_;
65 }
66 
GetResult(uint32_t code,const DeviceInfo * deviceInfo,int publishId,int subscribeId,int reason)67 bool RemoteObjectMock::GetResult(uint32_t code, const DeviceInfo *deviceInfo, int publishId, int subscribeId,
68                                  int reason)
69 {
70     if (descriptor_ != ISoftBusClient::GetDescriptor()) {
71         DLOGE("descriptor mismatch");
72         return false;
73     }
74     if (deviceInfo != nullptr) {
75         if (memcmp(deviceInfo, &deviceInfo_, sizeof(DeviceInfo)) != 0) {
76             DLOGE("device info mismatch");
77             return false;
78         }
79     }
80     if (code != code_) {
81         DLOGE("code mismatch");
82         return false;
83     }
84     if (publishId != publishId_) {
85         DLOGE("publish id mismatch");
86         return false;
87     }
88     if (subscribeId != subscribeId_) {
89         DLOGE("subscribe id mismatch");
90         return false;
91     }
92     if (reason != reason_) {
93         DLOGE("reason mismatch");
94         return false;
95     }
96     return true;
97 }
98 
ActionOfSendRequestForOnDeviceFound(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)99 int RemoteObjectMock::ActionOfSendRequestForOnDeviceFound(uint32_t code, MessageParcel &data, MessageParcel &reply,
100                                                           MessageOption &option)
101 {
102     DLOGI("code=%u", code);
103     Get()->code_ = code;
104     Get()->descriptor_ = data.ReadInterfaceToken();
105     return memcpy_s(&Get()->deviceInfo_, sizeof(DeviceInfo), data.ReadBuffer(sizeof(DeviceInfo)), sizeof(DeviceInfo));
106 }
107 
ActionOfSendRequestForOnDiscoveryFailed(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)108 int RemoteObjectMock::ActionOfSendRequestForOnDiscoveryFailed(uint32_t code, MessageParcel &data, MessageParcel &reply,
109                                                               MessageOption &option)
110 {
111     DLOGI("code=%u", code);
112     Get()->code_ = code;
113     Get()->descriptor_ = data.ReadInterfaceToken();
114     Get()->subscribeId_ = data.ReadInt32();
115     Get()->reason_ = data.ReadInt32();
116     return 0;
117 }
118 
ActionOfSendRequestForOnDiscoverySuccess(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)119 int RemoteObjectMock::ActionOfSendRequestForOnDiscoverySuccess(uint32_t code, MessageParcel &data, MessageParcel &reply,
120                                                                MessageOption &option)
121 {
122     DLOGI("code=%u", code);
123     Get()->code_ = code;
124     Get()->descriptor_ = data.ReadInterfaceToken();
125     Get()->subscribeId_ = data.ReadInt32();
126     return 0;
127 }
128 
ActionOfSendRequestForOnPublishSuccess(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)129 int RemoteObjectMock::ActionOfSendRequestForOnPublishSuccess(uint32_t code, MessageParcel &data, MessageParcel &reply,
130                                                              MessageOption &option)
131 {
132     DLOGI("code=%u", code);
133     Get()->code_ = code;
134     Get()->descriptor_ = data.ReadInterfaceToken();
135     Get()->publishId_ = data.ReadInt32();
136     return 0;
137 }
138 
ActionOfSendRequestForOnPublishFail(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)139 int RemoteObjectMock::ActionOfSendRequestForOnPublishFail(uint32_t code, MessageParcel &data, MessageParcel &reply,
140                                                           MessageOption &option)
141 {
142     DLOGI("code=%u", code);
143     Get()->code_ = code;
144     Get()->descriptor_ = data.ReadInterfaceToken();
145     Get()->publishId_ = data.ReadInt32();
146     Get()->reason_ = data.ReadInt32();
147     return 0;
148 }
149 }