GCC Code Coverage Report


Directory: ./
File: src/PMockBackendFile.cpp
Date: 2025-12-02 14:33:23
Exec Total Coverage
Lines: 64 69 92.8%
Functions: 22 25 88.0%
Branches: 19 21 90.5%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #include <sstream>
8
9 #include "data_stream_check_value.h"
10 #include "PMockBackendFile.h"
11
12 ///Create a mock backend file
13 /** @param[out] mock : mock socket file to be initialised
14 * @param socketParam : parameters of the socket (hostname, port, etc)
15 * @param prefix : prefix of the mock file
16 * @param extraParam : extra parameters of the mock configuration
17 * @return true on success, false otherwise
18 */
19 1 bool phoenix_createMockBackend(PMockSocketFile & mock, const PSocketParam & socketParam, const std::string & prefix, const PMockParamFile & extraParam){
20 1 mock.setIsMockRecord(true);
21 1 bool b = mock.createClientSocket(socketParam, extraParam);
22 1 mock.setMockPrefix(prefix);
23 1 return b;
24 }
25
26 ///Default constructor of hte PMockSocketFile
27
1/1
✓ Branch 0 (5→6) taken 10 times.
30 PMockSocketFile::PMockSocketFile(){
28
29 10 }
30
31 ///Default destructor of hte PMockSocketFile
32 10 PMockSocketFile::~PMockSocketFile(){
33
34 10 }
35
36 ///Initialise a client socket
37 /** @param socketParam : parameters to be use to initialise the socket (hostname, port, etc)
38 * @param extraParam : extra customisable parameters for the creation of the socket (depends on the backend)
39 */
40 8 bool PMockSocketFile::createClientSocket(const PSocketParam & socketParam, const PMockSocketFile::Param & extraParam){
41 8 p_socketParam = socketParam;
42 p_extraParam = extraParam;
43 8 return true;
44 }
45
46 ///Initialise a server socket
47 /** @param socketParam : parameters to be use to initialise the socket (hostname, port, etc)
48 * @param extraParam : extra customisable parameters for the creation of the socket (depends on the backend)
49 */
50 bool PMockSocketFile::createServerSocket(const PSocketParam & socketParam, const PMockSocketFile::Param & extraParam){
51 return createClientSocket(socketParam, extraParam);
52 }
53
54 ///Set the mock prefix (where to find/save it)
55 /** @param mockPrefix : prefix of the mock to find or write it
56 */
57 10 void PMockSocketFile::setMockPrefix(const std::string & mockPrefix){
58
1/1
✓ Branch 0 (2→3) taken 10 times.
10 std::stringstream socketFileName;
59
5/5
✓ Branch 0 (3→4) taken 10 times.
✓ Branch 2 (4→5) taken 10 times.
✓ Branch 4 (5→6) taken 10 times.
✓ Branch 6 (6→7) taken 10 times.
✓ Branch 8 (7→8) taken 10 times.
10 socketFileName << mockPrefix << p_socketParam.hostname << "_" << p_socketParam.port << ".pmockbackend";
60
2/2
✓ Branch 0 (8→9) taken 10 times.
✓ Branch 2 (9→10) taken 10 times.
10 p_mock.setFileName(socketFileName.str());
61 10 }
62
63 ///Set the mock prefix (where to find/save it)
64 /** @param isMockRecord : true if the mock has to be recorded
65 */
66 10 void PMockSocketFile::setIsMockRecord(bool isMockRecord){
67 10 p_mock.setIsRecord(isMockRecord);
68 10 }
69
70 ///Specialisation to send a Message with the socket
71 /** @param msg : Message to be sent
72 * @param flag : sending flag (BLOCK, NON_BLOCK)
73 * @return status of the send
74 */
75 17 PSendStatus::PSendStatus PMockSocketFile::sendMsg(const PMockSocketFile::Message & msg, PSendFlag::PSendFlag flag){
76
2/2
✓ Branch 0 (3→4) taken 12 times.
✓ Branch 1 (3→5) taken 5 times.
17 if(p_mock.getIsRecord()){
77 12 p_mock.append(msg);
78 }else{
79
6/6
✓ Branch 0 (7→8) taken 5 times.
✓ Branch 2 (10→11) taken 5 times.
✓ Branch 4 (13→14) taken 5 times.
✓ Branch 6 (16→17) taken 5 times.
✓ Branch 8 (17→18) taken 3 times.
✓ Branch 10 (18→19) taken 3 times.
40 check_stream_assert(p_mock.checkCurrentValue("PMockSocketFile::sendMsg", msg));
80 }
81 15 return PSendStatus::OK;
82 }
83
84 ///Recieved data with the socket
85 /** @param[out] msg : Message to be recieved with the socket
86 * @param flag : recieving flag (BLOCK, NON_BLOCK)
87 * @return status of the recv
88 */
89 16 PRecvStatus::PRecvStatus PMockSocketFile::recvMsg(PMockSocketFile::Message & msg, PRecvFlag::PRecvFlag flag){
90
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 16 times.
16 if(p_mock.getIsRecord()){
91 p_mock.append(msg);
92 return PRecvStatus::OK;
93 }else{
94 16 p_mock.getCurrentValue(msg);
95
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 14 times.
14 if(msg.size() == 0lu){ //If the message is empty, it is not received
96 return PRecvStatus::NO_MESSAGE_RECEIVED;
97 }else{
98 14 return PRecvStatus::OK;
99 }
100 }
101 }
102
103 ///Say if the PMockSocketFile is connected
104 /** @return true if the PMockSocketFile is connected, false otherwise
105 */
106 3 bool PMockSocketFile::isConnected() const{
107 3 return true;
108 }
109
110 ///Close the PMockSocketFile
111 4 void PMockSocketFile::close(){
112 4 p_mock.close();
113 4 }
114
115 ///Default constructor of PMockBackendFile
116 7 PMockBackendFile::PMockBackendFile(){}
117
118 ///Destructor of PMockBackendFile
119 7 PMockBackendFile::~PMockBackendFile(){}
120
121 ///Create param for a client socket
122 /** @return corresponding parameters
123 */
124 7 PMockBackendFile::Param PMockBackendFile::client(){
125 PMockBackendFile::Param param;
126 7 return param;
127 }
128
129 ///Create param for a server socket
130 /** @return corresponding parameters
131 */
132 1 PMockBackendFile::Param PMockBackendFile::server(){
133 1 return PMockBackendFile::client();
134 }
135
136 ///Create a client socket
137 /** @param[out] socket : socket to be created
138 * @param socketParam : parameters of the socket (host, port, send/recv timeout)
139 * @param param : extra customisable parameters for the creation of the socket (depends on the backend)
140 * @return true if the socket has been created, false otherwise
141 */
142 7 bool PMockBackendFile::createClientSocket(PMockBackendFile::Socket & socket, const PSocketParam & socketParam, const PMockBackendFile::Param & param){
143 7 return socket.createClientSocket(socketParam, param);
144 }
145
146 ///Create a client socket
147 /** @param[out] socket : socket to be created
148 * @param socketParam : parameters of the socket (host, port, send/recv timeout)
149 * @param param : extra customisable parameters for the creation of the socket (depends on the backend)
150 * @return true if the socket has been created, false otherwise
151 */
152 1 bool PMockBackendFile::createServerSocket(PMockBackendFile::Socket & socket, const PSocketParam & socketParam, const PMockBackendFile::Param & param){
153 1 return createClientSocket(socket, socketParam, param);
154 }
155
156 ///Resize a message
157 /** @param[out] msg : message to be resized
158 * @param sizeMsg : new size of the message
159 */
160 3 void PMockBackendFile::msgResize(PMockBackendFile::Message & msg, size_t sizeMsg){
161 3 msg.resize(sizeMsg);
162 3 }
163
164 ///Get the size of a message
165 /** @param msg : message to be used
166 * @return size of the message in bytes
167 */
168 3 size_t PMockBackendFile::msgSize(const PMockBackendFile::Message & msg){
169 3 return msg.size();
170 }
171
172 ///Get the data of a message
173 /** @param msg : message to be used
174 * @return data of the message in bytes
175 */
176 2 const DataStreamIter PMockBackendFile::msgData(const PMockBackendFile::Message & msg){
177 2 return (const DataStreamIter)msg.data();
178 }
179
180 ///Get the data of a message
181 /** @param msg : message to be used
182 * @return data of the message in bytes
183 */
184 4 DataStreamIter PMockBackendFile::msgData(PMockBackendFile::Message & msg){
185 4 return msg.data();
186 }
187
188 ///Copy current backend message data into mock message
189 /** @param[out] mockMsg : mock message
190 * @param msg : message of the current backend to be converted
191 */
192 1 void PMockBackendFile::msgToMock(DataStreamMsg & mockMsg, const PMockBackendFile::Message & msg){
193 1 size_t dataSize(PMockBackendFile::msgSize(msg));
194 1 mockMsg.resize(dataSize);
195 1 memcpy(mockMsg.data(), PMockBackendFile::msgData(msg), dataSize);
196 1 }
197
198 ///Copy mock message data into current backend message
199 /** @param[out] msg : message of the current backend to be converted
200 * @param mockMsg : mock message
201 */
202 1 void PMockBackendFile::mockToMsg(PMockBackendFile::Message & msg, DataStreamMsg & mockMsg){
203 1 size_t dataSize(mockMsg.size());
204 1 PMockBackendFile::msgResize(msg, dataSize);
205 1 memcpy(PMockBackendFile::msgData(msg), mockMsg.data(), dataSize);
206 1 }
207
208
209