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 uint64_t SendMsgToImpl(const Messenger *messenger, uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg,
109     uint32_t msgLen)
110 {
111     return 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     return;
127 }
128 
GetDeviceStatisticInfoImpl(const Messenger * messenger,const DeviceIdentify * devId,StatisticInformation * info)129 bool GetDeviceStatisticInfoImpl(const Messenger *messenger, const DeviceIdentify *devId, StatisticInformation *info)
130 {
131     return true;
132 }
133 }
134 } // namespace DslmUnitTest
135 } // namespace Security
136 } // namespace OHOS
137