1 /*
2 * Copyright (c) 2021 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, uint32_t devType);
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, uint32_t *devType) {
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 * cfg)92 Messenger *CreateMessengerImpl(const MessengerConfig *cfg)
93 {
94 return nullptr;
95 }
96
DestroyMessengerImpl(Messenger * messenger)97 void DestroyMessengerImpl(Messenger *messenger)
98 {
99 return;
100 }
101
IsMessengerReadyImpl(const Messenger * messenger)102 bool IsMessengerReadyImpl(const Messenger *messenger)
103 {
104 return GetDslmMsgInterface()->IsMessengerReady(messenger);
105 }
106
SendMsgToImpl(const Messenger * messenger,uint64_t transNo,const DeviceIdentify * devId,const uint8_t * msg,uint32_t msgLen)107 uint64_t SendMsgToImpl(const Messenger *messenger, uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg,
108 uint32_t msgLen)
109 {
110 return GetDslmMsgInterface()->SendMsgTo(messenger, transNo, devId, msg, msgLen);
111 }
112
GetDeviceOnlineStatusImpl(const Messenger * messenger,const DeviceIdentify * devId,uint32_t * devType)113 bool GetDeviceOnlineStatusImpl(const Messenger *messenger, const DeviceIdentify *devId, uint32_t *devType)
114 {
115 return GetDslmMsgInterface()->GetDeviceOnlineStatus(messenger, devId, devType);
116 }
117
GetSelfDeviceIdentifyImpl(const Messenger * messenger,DeviceIdentify * devId,uint32_t * devType)118 bool GetSelfDeviceIdentifyImpl(const Messenger *messenger, DeviceIdentify *devId, uint32_t *devType)
119 {
120 return GetDslmMsgInterface()->GetSelfDeviceIdentify(messenger, devId, devType);
121 }
122
ForEachDeviceProcessImpl(const Messenger * messenger,const DeviceProcessor processor,void * para)123 void ForEachDeviceProcessImpl(const Messenger *messenger, const DeviceProcessor processor, void *para)
124 {
125 return;
126 }
127
GetDeviceStatisticInfoImpl(const Messenger * messenger,const DeviceIdentify * devId,StatisticInformation * info)128 bool GetDeviceStatisticInfoImpl(const Messenger *messenger, const DeviceIdentify *devId, StatisticInformation *info)
129 {
130 return true;
131 }
132 }
133 } // namespace DslmUnitTest
134 } // namespace Security
135 } // namespace OHOS
136