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 "dslm_msg_interface_mock.h"
17
18 #include <memory>
19 #include <thread>
20 #include <vector>
21
22 using namespace OHOS::Security::DslmUnitTest;
23 using namespace testing;
24 using namespace testing::ext;
25
26 extern "C" {
27 // just for testing
28 extern Messenger *g_messenger;
29 extern int32_t OnPeerMsgReceived(const DeviceIdentify *devId, const uint8_t *msg, uint32_t len);
30 extern int32_t OnSendResultNotifier(const DeviceIdentify *devId, uint64_t transNo, uint32_t result);
31 extern int32_t OnPeerStatusReceiver(const DeviceIdentify *deviceId, uint32_t status, int32_t level);
32 }
33
34 namespace OHOS {
35 namespace Security {
36 namespace DslmUnitTest {
GetDslmMsgInterface()37 static DslmMsgInterface *GetDslmMsgInterface()
38 {
39 return reinterpret_cast<DslmMsgInterfaceMock *>(g_messenger);
40 }
41
DslmMsgInterfaceMock()42 DslmMsgInterfaceMock::DslmMsgInterfaceMock()
43 {
44 g_messenger = reinterpret_cast<Messenger *>(this);
45 ON_CALL(*this, IsMessengerReady).WillByDefault(Return(true));
46 }
47
~DslmMsgInterfaceMock()48 DslmMsgInterfaceMock::~DslmMsgInterfaceMock()
49 {
50 g_messenger = nullptr;
51 }
52
MakeMsgLoopback() const53 void DslmMsgInterfaceMock::MakeMsgLoopback() const
54 {
55 auto loopback = [this](const Messenger *messenger, uint64_t transNo, const DeviceIdentify *devId,
56 const uint8_t *msg, uint32_t msgLen) {
57 this->MakeMsgReceivedFrom(devId, msg, msgLen);
58 return 0;
59 };
60
61 ON_CALL(*this, SendMsgTo).WillByDefault(loopback);
62 }
63
MakeSelfDeviceId(const DeviceIdentify * self) const64 void DslmMsgInterfaceMock::MakeSelfDeviceId(const DeviceIdentify *self) const
65 {
66 auto loopback = [this, self](const Messenger *messenger, DeviceIdentify *devId, int32_t *level) {
67 *devId = *self;
68 return true;
69 };
70
71 ON_CALL(*this, GetSelfDeviceIdentify).WillByDefault(loopback);
72 }
73
MakeDeviceOnline(const DeviceIdentify * devId) const74 void DslmMsgInterfaceMock::MakeDeviceOnline(const DeviceIdentify *devId) const
75 {
76 OnPeerStatusReceiver(devId, 1, 0);
77 }
78
MakeDeviceOffline(const DeviceIdentify * devId) const79 void DslmMsgInterfaceMock::MakeDeviceOffline(const DeviceIdentify *devId) const
80 {
81 OnPeerStatusReceiver(devId, 0, 0);
82 }
83
MakeMsgReceivedFrom(const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen) const84 void DslmMsgInterfaceMock::MakeMsgReceivedFrom(const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen) const
85 {
86 auto msgBuffer = std::make_shared<std::vector<uint8_t>>(msg, msg + msgLen);
87 std::thread t([devId, msgBuffer]() { OnPeerMsgReceived(devId, msgBuffer->data(), msgBuffer->size()); });
88 t.detach();
89 }
90
91 extern "C" {
CreateMessengerImpl(const MessengerConfig * config)92 Messenger *CreateMessengerImpl(const MessengerConfig *config)
93 {
94 (void)config;
95 return g_messenger;
96 }
97
DestroyMessengerImpl(Messenger * messenger)98 void DestroyMessengerImpl(Messenger *messenger)
99 {
100 (void)messenger;
101 }
102
IsMessengerReadyImpl(const Messenger * messenger)103 bool IsMessengerReadyImpl(const Messenger *messenger)
104 {
105 return GetDslmMsgInterface()->IsMessengerReady(messenger);
106 }
107
SendMsgToImpl(const Messenger * messenger,uint64_t transNo,const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen)108 void SendMsgToImpl(const Messenger *messenger, uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg,
109 uint32_t msgLen)
110 {
111 (void)GetDslmMsgInterface()->SendMsgTo(messenger, transNo, devId, msg, msgLen);
112 }
113
GetDeviceOnlineStatusImpl(const Messenger * messenger,const DeviceIdentify * devId,int32_t * level)114 bool GetDeviceOnlineStatusImpl(const Messenger *messenger, const DeviceIdentify *devId, int32_t *level)
115 {
116 return GetDslmMsgInterface()->GetDeviceOnlineStatus(messenger, devId, level);
117 }
118
GetSelfDeviceIdentifyImpl(const Messenger * messenger,DeviceIdentify * devId,int32_t * level)119 bool GetSelfDeviceIdentifyImpl(const Messenger *messenger, DeviceIdentify *devId, int32_t *level)
120 {
121 return GetDslmMsgInterface()->GetSelfDeviceIdentify(messenger, devId, level);
122 }
123
ForEachDeviceProcessImpl(const Messenger * messenger,const DeviceProcessor processor,void * para)124 void ForEachDeviceProcessImpl(const Messenger *messenger, const DeviceProcessor processor, void *para)
125 {
126 static_cast<void>(messenger);
127 static_cast<void>(processor);
128 static_cast<void>(para);
129 }
130
GetDeviceStatisticInfoImpl(const Messenger * messenger,const DeviceIdentify * devId,StatisticInformation * info)131 bool GetDeviceStatisticInfoImpl(const Messenger *messenger, const DeviceIdentify *devId, StatisticInformation *info)
132 {
133 static_cast<void>(messenger);
134 static_cast<void>(devId);
135 static_cast<void>(info);
136 return false;
137 }
138
Socket(SocketInfo info)139 int32_t Socket(SocketInfo info)
140 {
141 if (info.name == nullptr) {
142 return 0;
143 }
144 return info.name[0];
145 }
146
SendBytes(int32_t socket,const void * data,uint32_t len)147 int32_t SendBytes(int32_t socket, const void *data, uint32_t len)
148 {
149 static_cast<void>(socket);
150 static_cast<void>(data);
151 static_cast<void>(len);
152 return 0;
153 }
154
Bind(int32_t socket,const QosTV qos[],uint32_t qosCount,const ISocketListener * listener)155 int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener)
156 {
157 if (auto interface = GetDslmMsgInterface(); interface) {
158 return interface->Bind(socket, qos, qosCount, listener);
159 }
160 return 0;
161 }
162
Shutdown(int32_t socket)163 void Shutdown(int32_t socket)
164 {
165 if (auto interface = GetDslmMsgInterface(); interface) {
166 interface->Shutdown(socket);
167 }
168 }
169
Listen(int32_t socket,const QosTV qos[],uint32_t qosCount,const ISocketListener * listener)170 int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener)
171 {
172 if (auto interface = GetDslmMsgInterface(); interface) {
173 return interface->Listen(socket, qos, qosCount, listener);
174 }
175 return 0;
176 }
177 }
178 } // namespace DslmUnitTest
179 } // namespace Security
180 } // namespace OHOS
181