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 "PMockBackend.h" | ||
11 | |||
12 | ///Load a mock of socket | ||
13 | /** @param fileName : file name of the mock to be loaded | ||
14 | * @param[out] vecMessage : vector of message to be loaded | ||
15 | * @return true on success, false otherwise | ||
16 | */ | ||
17 | ✗ | bool phoenix_loadMockSocket(const std::string & prefix, const std::string & host, size_t port, PVecMockMessage & vecMessage){ | |
18 | ✗ | std::stringstream socketFileName; | |
19 | ✗ | socketFileName << prefix << host << "_" << port << ".pmockbackend"; | |
20 | ✗ | return data_load(socketFileName.str(), vecMessage); | |
21 | ✗ | } | |
22 | |||
23 | ///Save a mock of socket | ||
24 | /** @param fileName : file name of the mock to be saved | ||
25 | * @param[out] vecMessage : vector of message to be saved | ||
26 | * @return true on success, false otherwise | ||
27 | */ | ||
28 | ✗ | bool phoenix_saveMockSocket(const std::string & prefix, const std::string & host, size_t port, const PVecMockMessage & vecMessage){ | |
29 | ✗ | std::stringstream socketFileName; | |
30 | ✗ | socketFileName << prefix << host << "_" << port << ".pmockbackend"; | |
31 | ✗ | return data_save(socketFileName.str(), vecMessage); | |
32 | ✗ | } | |
33 | |||
34 | ///Check given value compare to the reference size | ||
35 | /** @param testName : name of the current test | ||
36 | * @param vecData : vector of data to be checked | ||
37 | * @param vecReferenceData : vector of reference data | ||
38 | * @return true on success, false otherwise | ||
39 | */ | ||
40 | template<> | ||
41 | ✗ | bool checkValue<DataStreamType>(const std::string & testName, const std::vector<DataStreamType> & vecData, const std::vector<DataStreamType> & vecReferenceData){ | |
42 | ✗ | if(vecData.size() != vecReferenceData.size()){ | |
43 | ✗ | std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData.size(" << vecData.size() << ") != vecReferenceData.size("<<vecReferenceData.size()<<")" << std::endl; | |
44 | ✗ | return false; | |
45 | } | ||
46 | ✗ | bool b(true); | |
47 | ✗ | for(size_t i(0lu); i < vecData.size() && b; ++i){ | |
48 | ✗ | b &= vecData[i] == vecReferenceData[i]; | |
49 | ✗ | if(!b){ | |
50 | ✗ | std::cout << "checkValue<DataStreamType> : "<<testName<<" => vecData["<<i<<"](" << (int)vecData[i] << ") != vecReferenceData["<<i<<"]("<<(int)vecReferenceData[i]<<")" << std::endl; | |
51 | } | ||
52 | } | ||
53 | // if(b){std::cout << "checkValue : "<<testName<<" => Ok"<<std::endl;} | ||
54 | // else{std::cout << "checkValue : "<<testName<<" => WRONG!!!!"<<std::endl;} | ||
55 | ✗ | return b; | |
56 | } | ||
57 | |||
58 | ///Default constructor of PMockBackend | ||
59 | ✗ | PMockBackend::PMockBackend(){ | |
60 | |||
61 | ✗ | } | |
62 | |||
63 | ///Create param for a client socket | ||
64 | /** @return corresponding parameters | ||
65 | */ | ||
66 | 4 | PMockBackend::Param PMockBackend::client(){ | |
67 | 4 | PMockBackend::Param param; | |
68 | 4 | return param; | |
69 | } | ||
70 | |||
71 | ///Create param for a server socket | ||
72 | /** @return corresponding parameters | ||
73 | */ | ||
74 | 2 | PMockBackend::Param PMockBackend::server(){ | |
75 | 2 | return PMockBackend::client(); | |
76 | } | ||
77 | |||
78 | ///Create a client socket | ||
79 | /** @param[out] socket : socket to be created | ||
80 | * @param address : address of the server, the client has to connect to | ||
81 | * @param port : port to be used for the connection | ||
82 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
83 | * @return true if the socket has been created, false otherwise | ||
84 | */ | ||
85 | 4 | bool PMockBackend::createClientSocket(PMockBackend::Socket & socket, const std::string & address, size_t port, const PMockParam & param){ | |
86 | 4 | socket.eventIndex = 0lu; | |
87 |
1/1✓ Branch 0 (2→3) taken 4 times.
|
4 | std::stringstream socketFileName; |
88 |
3/3✓ Branch 0 (3→4) taken 4 times.
✓ Branch 2 (4→5) taken 2 times.
✓ Branch 3 (4→6) taken 2 times.
|
4 | if(socket.param.mockPrefix != ""){ |
89 |
1/1✓ Branch 0 (5→6) taken 2 times.
|
2 | socketFileName << socket.param.mockPrefix; |
90 | } | ||
91 |
4/4✓ Branch 0 (6→7) taken 4 times.
✓ Branch 2 (7→8) taken 4 times.
✓ Branch 4 (8→9) taken 4 times.
✓ Branch 6 (9→10) taken 4 times.
|
4 | socketFileName << address << "_" << port << ".pmockbackend"; |
92 |
1/1✓ Branch 0 (10→11) taken 4 times.
|
4 | socket.fileNameMessage = socketFileName.str(); |
93 | 4 | bool b(true); | |
94 |
2/2✓ Branch 0 (13→14) taken 2 times.
✓ Branch 1 (13→20) taken 2 times.
|
4 | if(!socket.param.isMockRecord){ //If we are not in record mode, we load all the exchange |
95 |
1/1✓ Branch 0 (14→15) taken 2 times.
|
2 | b &= data_load(socket.fileNameMessage, socket.vecMessage); |
96 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→20) taken 2 times.
|
2 | if(!b){ |
97 | ✗ | std::cerr << "PMockBackend::createClientSocket : cannot load file '"<<socket.fileNameMessage<<"'" << std::endl; | |
98 | } | ||
99 | } | ||
100 | 4 | return b; | |
101 | 4 | } | |
102 | |||
103 | ///Create a client socket | ||
104 | /** @param[out] socket : socket to be created | ||
105 | * @param param : extra customisable parameters for the creation of the socket (depends on the backend) | ||
106 | * @return true if the socket has been created, false otherwise | ||
107 | */ | ||
108 | 2 | bool PMockBackend::createServerSocket(PMockBackend::Socket & socket, const std::string & address, size_t port, const PMockParam & param){ | |
109 | 2 | return createClientSocket(socket, address, port, param); | |
110 | } | ||
111 | |||
112 | ///Set the mock prefix (where to find/save it) | ||
113 | /** @param[out] socket : socket to be modified | ||
114 | * @param mockPrefix : prefix of the mock to find or write it | ||
115 | */ | ||
116 | 4 | void PMockBackend::setMockPrefix(Socket & socket, const std::string & mockPrefix){ | |
117 | 4 | socket.param.mockPrefix = mockPrefix; | |
118 | 4 | } | |
119 | |||
120 | ///Set the mock prefix (where to find/save it) | ||
121 | /** @param[out] socket : socket to be modified | ||
122 | * @param isMockRecord : true if the mock has to be recorded | ||
123 | */ | ||
124 | 4 | void PMockBackend::setIsMockRecord(Socket & socket, bool isMockRecord){ | |
125 | 4 | socket.param.isMockRecord = isMockRecord; | |
126 | 4 | } | |
127 | |||
128 | |||
129 | ///Send message on the given socket | ||
130 | /** @param socket : socket to be used | ||
131 | * @param msg : message to be sent | ||
132 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
133 | * @return true on success, false otherwise | ||
134 | */ | ||
135 | 20 | bool PMockBackend::send(PMockBackend::Socket & socket, const PMockBackend::Message & msg, PSendFlag::PSendFlag flag){ | |
136 | 20 | bool b(true); | |
137 |
1/2✓ Branch 0 (2→3) taken 20 times.
✗ Branch 1 (2→4) not taken.
|
20 | if(socket.param.isMockRecord){ //If we record all events |
138 | 20 | socket.vecMessage.push_back(msg); | |
139 | }else{ | ||
140 | ✗ | if(socket.vecMessage.size() == 0lu){ | |
141 | ✗ | std::cerr << "PMockBackend::send : empty vector of message" << std::endl; | |
142 | ✗ | return false; | |
143 | } | ||
144 | //Let's pick the current message | ||
145 | ✗ | if(socket.eventIndex >= socket.vecMessage.size()){ | |
146 | ✗ | socket.eventIndex = 0lu; //Let's loop if all the events were used | |
147 | } | ||
148 | //Let's check if the current message matches the message we are supposed to send | ||
149 | ✗ | std::stringstream strEvent; | |
150 | ✗ | strEvent << socket.eventIndex; | |
151 | ✗ | b &= checkValue("PMockBackend::send : event " + strEvent.str(), msg, socket.vecMessage[socket.eventIndex]); | |
152 | ✗ | } | |
153 | 20 | ++socket.eventIndex; | |
154 | 20 | return b; | |
155 | } | ||
156 | |||
157 | ///Recieve message from the given socket | ||
158 | /** @param socket : socket to be used | ||
159 | * @param msg : message to be recieved | ||
160 | * @param flag : flag to be used to send the message (BLOCK, NON_BLOCK, etc) | ||
161 | * @return true on success, false otherwise | ||
162 | */ | ||
163 | 20 | bool PMockBackend::recv(PMockBackend::Socket & socket, PMockBackend::Message & msg, PRecvFlag::PRecvFlag flag){ | |
164 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 20 times.
|
20 | if(socket.param.isMockRecord){ //If we record all events |
165 | ✗ | socket.vecMessage.push_back(msg); | |
166 | }else{ | ||
167 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 20 times.
|
20 | if(socket.vecMessage.size() == 0lu){ |
168 | ✗ | std::cerr << "PMockBackend::recv : empty vector of message" << std::endl; | |
169 | ✗ | return false; | |
170 | } | ||
171 | //Let's pick the current message | ||
172 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 20 times.
|
20 | if(socket.eventIndex >= socket.vecMessage.size()){ |
173 | ✗ | socket.eventIndex = 0lu; //Let's loop if all the events were used | |
174 | } | ||
175 | 20 | msg = socket.vecMessage[socket.eventIndex]; | |
176 | } | ||
177 | 20 | ++socket.eventIndex; | |
178 | 20 | return true; | |
179 | } | ||
180 | |||
181 | ///Resize a message | ||
182 | /** @param[out] msg : message to be resized | ||
183 | * @param sizeMsg : new size of the message | ||
184 | */ | ||
185 | 10 | void PMockBackend::msgResize(PMockBackend::Message & msg, size_t sizeMsg){ | |
186 | 10 | msg.resize(sizeMsg); | |
187 | 10 | } | |
188 | |||
189 | ///Get the size of a message | ||
190 | /** @param msg : message to be used | ||
191 | * @return size of the message in bytes | ||
192 | */ | ||
193 | 10 | size_t PMockBackend::msgSize(const PMockBackend::Message & msg){ | |
194 | 10 | return msg.size(); | |
195 | } | ||
196 | |||
197 | ///Get the data of a message | ||
198 | /** @param msg : message to be used | ||
199 | * @return data of the message in bytes | ||
200 | */ | ||
201 | ✗ | const DataStreamIter PMockBackend::msgData(const PMockBackend::Message & msg){ | |
202 | ✗ | return (const DataStreamIter)msg.data(); | |
203 | } | ||
204 | |||
205 | ///Get the data of a message | ||
206 | /** @param msg : message to be used | ||
207 | * @return data of the message in bytes | ||
208 | */ | ||
209 | 20 | DataStreamIter PMockBackend::msgData(PMockBackend::Message & msg){ | |
210 | 20 | return msg.data(); | |
211 | } | ||
212 | |||
213 | ///Close the given socket | ||
214 | /** @param[out] socket : socket to be closed | ||
215 | */ | ||
216 | 8 | void PMockBackend::close(PMockBackend::Socket & socket){ | |
217 |
2/2✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→4) taken 4 times.
|
8 | if(socket.param.isMockRecord){ |
218 | 4 | data_save(socket.fileNameMessage, socket.vecMessage); | |
219 | } | ||
220 | 8 | } | |
221 | |||
222 | ///Say if the given socket is connected | ||
223 | /** @param socket : socket to be checked | ||
224 | * @return true if the socket is connected, false otherwise | ||
225 | */ | ||
226 | 2 | bool PMockBackend::isConnected(const PMockBackend::Socket & socket){ | |
227 | 2 | return true; | |
228 | } | ||
229 | |||
230 | ///Copy current backend message data into mock message | ||
231 | /** @param[out] mockMsg : mock message | ||
232 | * @param msg : message of the current backend to be converted | ||
233 | */ | ||
234 | ✗ | void PMockBackend::msgToMock(DataStreamMsg & mockMsg, const PMockBackend::Message & msg){ | |
235 | ✗ | size_t dataSize(PMockBackend::msgSize(msg)); | |
236 | ✗ | mockMsg.resize(dataSize); | |
237 | ✗ | memcpy(mockMsg.data(), PMockBackend::msgData(msg), dataSize); | |
238 | ✗ | } | |
239 | |||
240 | ///Copy mock message data into current backend message | ||
241 | /** @param[out] msg : message of the current backend to be converted | ||
242 | * @param mockMsg : mock message | ||
243 | */ | ||
244 | ✗ | void PMockBackend::mockToMsg(PMockBackend::Message & msg, DataStreamMsg & mockMsg){ | |
245 | ✗ | size_t dataSize(mockMsg.size()); | |
246 | ✗ | PMockBackend::msgResize(msg, dataSize); | |
247 | ✗ | memcpy(PMockBackend::msgData(msg), mockMsg.data(), dataSize); | |
248 | ✗ | } | |
249 | |||
250 | |||
251 |