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