Mercurial > thymian
comparison 3rdparty/vmime/tests/testUtils.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 // | |
| 2 // VMime library (http://www.vmime.org) | |
| 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org> | |
| 4 // | |
| 5 // This program is free software; you can redistribute it and/or | |
| 6 // modify it under the terms of the GNU General Public License as | |
| 7 // published by the Free Software Foundation; either version 3 of | |
| 8 // the License, or (at your option) any later version. | |
| 9 // | |
| 10 // This program is distributed in the hope that it will be useful, | |
| 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 // General Public License for more details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU General Public License along | |
| 16 // with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 // | |
| 19 // Linking this library statically or dynamically with other modules is making | |
| 20 // a combined work based on this library. Thus, the terms and conditions of | |
| 21 // the GNU General Public License cover the whole combination. | |
| 22 // | |
| 23 | |
| 24 #include "testUtils.hpp" | |
| 25 | |
| 26 #include "vmime/utility/stringUtils.hpp" | |
| 27 | |
| 28 #include <cstring> | |
| 29 | |
| 30 | |
| 31 | |
| 32 // testSocket | |
| 33 | |
| 34 void testSocket::connect(const vmime::string& address, const vmime::port_t port) | |
| 35 { | |
| 36 m_address = address; | |
| 37 m_port = port; | |
| 38 m_connected = true; | |
| 39 | |
| 40 onConnected(); | |
| 41 } | |
| 42 | |
| 43 | |
| 44 void testSocket::disconnect() | |
| 45 { | |
| 46 m_address.clear(); | |
| 47 m_port = 0; | |
| 48 m_connected = false; | |
| 49 } | |
| 50 | |
| 51 | |
| 52 bool testSocket::isConnected() const | |
| 53 { | |
| 54 return m_connected; | |
| 55 } | |
| 56 | |
| 57 | |
| 58 vmime::size_t testSocket::getBlockSize() const | |
| 59 { | |
| 60 return 16384; | |
| 61 } | |
| 62 | |
| 63 | |
| 64 unsigned int testSocket::getStatus() const | |
| 65 { | |
| 66 return 0; | |
| 67 } | |
| 68 | |
| 69 | |
| 70 const vmime::string testSocket::getPeerName() const | |
| 71 { | |
| 72 return "test.vmime.org"; | |
| 73 } | |
| 74 | |
| 75 | |
| 76 const vmime::string testSocket::getPeerAddress() const | |
| 77 { | |
| 78 return "127.0.0.1"; | |
| 79 } | |
| 80 | |
| 81 | |
| 82 vmime::shared_ptr <vmime::net::timeoutHandler> testSocket::getTimeoutHandler() | |
| 83 { | |
| 84 return vmime::null; | |
| 85 } | |
| 86 | |
| 87 | |
| 88 void testSocket::setTracer(vmime::shared_ptr <vmime::net::tracer> tracer) | |
| 89 { | |
| 90 } | |
| 91 | |
| 92 | |
| 93 vmime::shared_ptr <vmime::net::tracer> testSocket::getTracer() | |
| 94 { | |
| 95 return vmime::null; | |
| 96 } | |
| 97 | |
| 98 | |
| 99 bool testSocket::waitForRead(const int msecs) | |
| 100 { | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 | |
| 105 bool testSocket::waitForWrite(const int msecs) | |
| 106 { | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 | |
| 111 void testSocket::receive(vmime::string& buffer) | |
| 112 { | |
| 113 buffer = m_inBuffer; | |
| 114 m_inBuffer.clear(); | |
| 115 } | |
| 116 | |
| 117 | |
| 118 void testSocket::send(const vmime::string& buffer) | |
| 119 { | |
| 120 m_outBuffer += buffer; | |
| 121 | |
| 122 onDataReceived(); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 void testSocket::send(const char* str) | |
| 127 { | |
| 128 sendRaw(reinterpret_cast <const vmime::byte_t*>(str), strlen(str)); | |
| 129 } | |
| 130 | |
| 131 | |
| 132 vmime::size_t testSocket::receiveRaw(vmime::byte_t* buffer, const size_t count) | |
| 133 { | |
| 134 const size_t n = std::min(count, static_cast <size_t>(m_inBuffer.size())); | |
| 135 | |
| 136 std::copy(m_inBuffer.begin(), m_inBuffer.begin() + n, buffer); | |
| 137 m_inBuffer.erase(m_inBuffer.begin(), m_inBuffer.begin() + n); | |
| 138 | |
| 139 return n; | |
| 140 } | |
| 141 | |
| 142 | |
| 143 void testSocket::sendRaw(const vmime::byte_t* buffer, const size_t count) | |
| 144 { | |
| 145 send(vmime::utility::stringUtils::makeStringFromBytes(buffer, count)); | |
| 146 } | |
| 147 | |
| 148 | |
| 149 vmime::size_t testSocket::sendRawNonBlocking(const vmime::byte_t* buffer, const size_t count) | |
| 150 { | |
| 151 sendRaw(buffer, count); | |
| 152 return count; | |
| 153 } | |
| 154 | |
| 155 | |
| 156 void testSocket::localSend(const vmime::string& buffer) | |
| 157 { | |
| 158 m_inBuffer += buffer; | |
| 159 } | |
| 160 | |
| 161 | |
| 162 void testSocket::localReceive(vmime::string& buffer) | |
| 163 { | |
| 164 buffer = m_outBuffer; | |
| 165 m_outBuffer.clear(); | |
| 166 } | |
| 167 | |
| 168 | |
| 169 bool testSocket::localReceiveLine(vmime::string& line) | |
| 170 { | |
| 171 vmime::size_t eol; | |
| 172 | |
| 173 if ((eol = m_outBuffer.find('\n')) != vmime::string::npos) | |
| 174 { | |
| 175 line = vmime::string(m_outBuffer.begin(), m_outBuffer.begin() + eol); | |
| 176 | |
| 177 if (!line.empty() && line[line.length() - 1] == '\r') | |
| 178 line.erase(line.end() - 1, line.end()); | |
| 179 | |
| 180 m_outBuffer.erase(m_outBuffer.begin(), m_outBuffer.begin() + eol + 1); | |
| 181 | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 | |
| 189 vmime::size_t testSocket::localReceiveRaw(vmime::byte_t* buffer, const size_t count) | |
| 190 { | |
| 191 const size_t received = std::min(count, static_cast <size_t>(m_outBuffer.size())); | |
| 192 | |
| 193 if (received != 0) | |
| 194 { | |
| 195 if (buffer != NULL) | |
| 196 std::copy(m_outBuffer.begin(), m_outBuffer.begin() + received, buffer); | |
| 197 | |
| 198 m_outBuffer.erase(m_outBuffer.begin(), m_outBuffer.begin() + received); | |
| 199 } | |
| 200 | |
| 201 return received; | |
| 202 } | |
| 203 | |
| 204 | |
| 205 void testSocket::onDataReceived() | |
| 206 { | |
| 207 // Override | |
| 208 } | |
| 209 | |
| 210 | |
| 211 void testSocket::onConnected() | |
| 212 { | |
| 213 // Override | |
| 214 } | |
| 215 | |
| 216 | |
| 217 // lineBasedTestSocket | |
| 218 | |
| 219 void lineBasedTestSocket::onDataReceived() | |
| 220 { | |
| 221 vmime::string chunk; | |
| 222 localReceive(chunk); | |
| 223 | |
| 224 m_buffer += chunk; | |
| 225 | |
| 226 vmime::size_t eol; | |
| 227 | |
| 228 while ((eol = m_buffer.find('\n')) != vmime::string::npos) | |
| 229 { | |
| 230 vmime::string line(std::string(m_buffer.begin(), m_buffer.begin() + eol)); | |
| 231 | |
| 232 if (!line.empty() && line[line.length() - 1] == '\r') | |
| 233 line.erase(line.end() - 1, line.end()); | |
| 234 | |
| 235 m_lines.push_back(line); | |
| 236 m_buffer.erase(m_buffer.begin(), m_buffer.begin() + eol + 1); | |
| 237 } | |
| 238 | |
| 239 while (!m_lines.empty()) | |
| 240 processCommand(); | |
| 241 } | |
| 242 | |
| 243 | |
| 244 const vmime::string lineBasedTestSocket::getNextLine() | |
| 245 { | |
| 246 const vmime::string line = m_lines.front(); | |
| 247 m_lines.erase(m_lines.begin(), m_lines.begin() + 1); | |
| 248 return line; | |
| 249 } | |
| 250 | |
| 251 bool lineBasedTestSocket::haveMoreLines() const | |
| 252 { | |
| 253 return !m_lines.empty(); | |
| 254 } | |
| 255 | |
| 256 | |
| 257 // testTimeoutHandler | |
| 258 | |
| 259 testTimeoutHandler::testTimeoutHandler(const unsigned long delay) | |
| 260 : m_delay(delay), m_start(0) | |
| 261 { | |
| 262 } | |
| 263 | |
| 264 | |
| 265 bool testTimeoutHandler::isTimeOut() | |
| 266 { | |
| 267 return (vmime::platform::getHandler()->getUnixTime() - m_start) >= m_delay; | |
| 268 } | |
| 269 | |
| 270 | |
| 271 void testTimeoutHandler::resetTimeOut() | |
| 272 { | |
| 273 m_start = vmime::platform::getHandler()->getUnixTime(); | |
| 274 } | |
| 275 | |
| 276 | |
| 277 bool testTimeoutHandler::handleTimeOut() | |
| 278 { | |
| 279 return false; | |
| 280 } | |
| 281 | |
| 282 | |
| 283 // testTimeoutHandlerFactory : public vmime::net::timeoutHandlerFactory | |
| 284 | |
| 285 vmime::shared_ptr <vmime::net::timeoutHandler> testTimeoutHandlerFactory::create() | |
| 286 { | |
| 287 return vmime::make_shared <testTimeoutHandler>(); | |
| 288 } | |
| 289 | |
| 290 | |
| 291 | |
| 292 // Exception helper | |
| 293 std::ostream& operator<<(std::ostream& os, const vmime::exception& e) | |
| 294 { | |
| 295 os << "* vmime::exceptions::" << e.name() << std::endl; | |
| 296 os << " what = " << e.what() << std::endl; | |
| 297 | |
| 298 // More information for special exceptions | |
| 299 if (dynamic_cast <const vmime::exceptions::command_error*>(&e)) | |
| 300 { | |
| 301 const vmime::exceptions::command_error& cee = | |
| 302 dynamic_cast <const vmime::exceptions::command_error&>(e); | |
| 303 | |
| 304 os << " command = " << cee.command() << std::endl; | |
| 305 os << " response = " << cee.response() << std::endl; | |
| 306 } | |
| 307 | |
| 308 if (dynamic_cast <const vmime::exceptions::invalid_response*>(&e)) | |
| 309 { | |
| 310 const vmime::exceptions::invalid_response& ir = | |
| 311 dynamic_cast <const vmime::exceptions::invalid_response&>(e); | |
| 312 | |
| 313 os << " response = " << ir.response() << std::endl; | |
| 314 } | |
| 315 | |
| 316 if (dynamic_cast <const vmime::exceptions::connection_greeting_error*>(&e)) | |
| 317 { | |
| 318 const vmime::exceptions::connection_greeting_error& cgee = | |
| 319 dynamic_cast <const vmime::exceptions::connection_greeting_error&>(e); | |
| 320 | |
| 321 os << " response = " << cgee.response() << std::endl; | |
| 322 } | |
| 323 | |
| 324 if (dynamic_cast <const vmime::exceptions::authentication_error*>(&e)) | |
| 325 { | |
| 326 const vmime::exceptions::authentication_error& aee = | |
| 327 dynamic_cast <const vmime::exceptions::authentication_error&>(e); | |
| 328 | |
| 329 os << " response = " << aee.response() << std::endl; | |
| 330 } | |
| 331 | |
| 332 if (dynamic_cast <const vmime::exceptions::filesystem_exception*>(&e)) | |
| 333 { | |
| 334 const vmime::exceptions::filesystem_exception& fse = | |
| 335 dynamic_cast <const vmime::exceptions::filesystem_exception&>(e); | |
| 336 | |
| 337 os << " path = " << vmime::platform::getHandler()-> | |
| 338 getFileSystemFactory()->pathToString(fse.path()) << std::endl; | |
| 339 } | |
| 340 | |
| 341 if (e.other() != NULL) | |
| 342 os << *e.other(); | |
| 343 | |
| 344 return os; | |
| 345 } | |
| 346 | |
| 347 | |
| 348 const vmime::string toHex(const vmime::string str) | |
| 349 { | |
| 350 static const char hexChars[] = "0123456789abcdef"; | |
| 351 | |
| 352 vmime::string res = "\n"; | |
| 353 | |
| 354 for (size_t i = 0 ; i < str.length() ; i += 16) | |
| 355 { | |
| 356 size_t r = std::min | |
| 357 (static_cast <size_t>(16), str.length() - i); | |
| 358 | |
| 359 vmime::string hex; | |
| 360 vmime::string chr; | |
| 361 | |
| 362 for (size_t j = 0 ; j < r ; ++j) | |
| 363 { | |
| 364 const unsigned char c = str[i + j]; | |
| 365 | |
| 366 hex += hexChars[c / 16]; | |
| 367 hex += hexChars[c % 16]; | |
| 368 hex += " "; | |
| 369 | |
| 370 if (c >= 32 && c <= 127) | |
| 371 chr += c; | |
| 372 else | |
| 373 chr += '.'; | |
| 374 } | |
| 375 | |
| 376 for (size_t j = r ; j < 16 ; ++j) | |
| 377 hex += " "; | |
| 378 | |
| 379 res += hex + " " + chr + "\n"; | |
| 380 } | |
| 381 | |
| 382 return res; | |
| 383 } |
