Mercurial > thymian
comparison 3rdparty/vmime/tests/testUtils.hpp @ 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 <ostream> | |
| 25 #include <iostream> | |
| 26 #include <sstream> | |
| 27 #include <vector> | |
| 28 #include <string> | |
| 29 | |
| 30 | |
| 31 // VMime | |
| 32 #include "vmime/vmime.hpp" | |
| 33 | |
| 34 | |
| 35 // CppUnit | |
| 36 #pragma GCC diagnostic ignored "-Wold-style-cast" | |
| 37 #include <cppunit/TestAssert.h> | |
| 38 #include <cppunit/extensions/HelperMacros.h> | |
| 39 #pragma GCC diagnostic warning "-Wold-style-cast" | |
| 40 | |
| 41 #define VASSERT(msg, cond) \ | |
| 42 CPPUNIT_ASSERT_MESSAGE(std::string(msg), cond) | |
| 43 | |
| 44 #define VASSERT_TRUE(msg, cond) \ | |
| 45 VASSERT(msg, cond) | |
| 46 #define VASSERT_FALSE(msg, cond) \ | |
| 47 VASSERT(msg, !(cond)) | |
| 48 | |
| 49 #define VASSERT_NOT_NULL(msg, cond) \ | |
| 50 VASSERT(msg, cond != NULL) | |
| 51 #define VASSERT_NULL(msg, cond) \ | |
| 52 VASSERT(msg, cond == NULL) | |
| 53 | |
| 54 #define VASSERT_EQ(msg, expected, actual) \ | |
| 55 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(msg), expected, actual) | |
| 56 #define VASSERT_NEQ(msg, expected, actual) \ | |
| 57 CPPUNIT_ASSERT_MESSAGE(std::string(msg), (expected) != (actual)) | |
| 58 | |
| 59 #define VASSERT_THROW(msg, expression, exceptionType) \ | |
| 60 CPPUNIT_ASSERT_THROW(expression, exceptionType) | |
| 61 #define VASSERT_NO_THROW(msg, expression) \ | |
| 62 CPPUNIT_ASSERT_NO_THROW(expression) | |
| 63 | |
| 64 #define VMIME_TEST_SUITE_BEGIN(testSuiteName) \ | |
| 65 class testSuiteName; \ | |
| 66 typedef testSuiteName VMIME_TEST_SUITE; \ | |
| 67 class testSuiteName : public CppUnit::TestFixture { public: | |
| 68 #define VMIME_TEST_SUITE_END \ | |
| 69 }; \ | |
| 70 \ | |
| 71 /*static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry1);*/ \ | |
| 72 /*static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry2)(VMIME_TEST_SUITE_MODULE);*/ \ | |
| 73 extern void registerTestModule(const char* name); \ | |
| 74 extern const char* getTestModuleNameFromSourceFile(const char *path); \ | |
| 75 template <typename T> \ | |
| 76 struct AutoRegisterModule { \ | |
| 77 AutoRegisterModule() { \ | |
| 78 static const char* moduleName = getTestModuleNameFromSourceFile(__FILE__); \ | |
| 79 static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry1); \ | |
| 80 static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry2)(moduleName); \ | |
| 81 registerTestModule(moduleName); \ | |
| 82 } \ | |
| 83 }; \ | |
| 84 static AutoRegisterModule <VMIME_TEST_SUITE> autoRegisterModule; | |
| 85 | |
| 86 #define VMIME_TEST_LIST_BEGIN CPPUNIT_TEST_SUITE(VMIME_TEST_SUITE); | |
| 87 #define VMIME_TEST_LIST_END CPPUNIT_TEST_SUITE_END(); public: | |
| 88 #define VMIME_TEST(name) CPPUNIT_TEST(name); | |
| 89 | |
| 90 | |
| 91 namespace CppUnit | |
| 92 { | |
| 93 // Work-around for comparing 'std::string' against 'char*' | |
| 94 inline void assertEquals(const char* expected, const std::string actual, | |
| 95 SourceLine sourceLine, const std::string &message) | |
| 96 { | |
| 97 assertEquals(std::string(expected), actual, sourceLine, message); | |
| 98 } | |
| 99 | |
| 100 template <typename X, typename Y> | |
| 101 void assertEquals(const X expected, const Y actual, | |
| 102 SourceLine sourceLine, const std::string &message) | |
| 103 { | |
| 104 assertEquals(static_cast <Y>(expected), actual, sourceLine, message); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 | |
| 109 namespace std | |
| 110 { | |
| 111 | |
| 112 | |
| 113 inline std::ostream& operator<<(std::ostream& os, const vmime::charset& ch) | |
| 114 { | |
| 115 os << "[charset: " << ch.getName() << "]"; | |
| 116 return (os); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 inline std::ostream& operator<<(std::ostream& os, const vmime::word& w) | |
| 121 { | |
| 122 os << "[word: charset=" << w.getCharset().getName() | |
| 123 << ", buffer=" << w.getBuffer(); | |
| 124 | |
| 125 if (!w.getLanguage().empty()) | |
| 126 os << ", lang=" << w.getLanguage(); | |
| 127 | |
| 128 os << "]"; | |
| 129 | |
| 130 return (os); | |
| 131 } | |
| 132 | |
| 133 | |
| 134 inline std::ostream& operator<<(std::ostream& os, const vmime::text& txt) | |
| 135 { | |
| 136 os << "[text: ["; | |
| 137 | |
| 138 for (size_t i = 0 ; i < txt.getWordCount() ; ++i) | |
| 139 { | |
| 140 const vmime::word& w = *txt.getWordAt(i); | |
| 141 | |
| 142 if (i != 0) | |
| 143 os << ","; | |
| 144 | |
| 145 os << w; | |
| 146 } | |
| 147 | |
| 148 os << "]]"; | |
| 149 | |
| 150 return (os); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 inline std::ostream& operator<<(std::ostream& os, const vmime::emailAddress& email) | |
| 155 { | |
| 156 os << email.generate(); | |
| 157 | |
| 158 return (os); | |
| 159 } | |
| 160 | |
| 161 | |
| 162 inline std::ostream& operator<<(std::ostream& os, const vmime::mailbox& mbox) | |
| 163 { | |
| 164 os << "[mailbox: name=" << mbox.getName() << ", email=" << mbox.getEmail() << "]"; | |
| 165 | |
| 166 return (os); | |
| 167 } | |
| 168 | |
| 169 | |
| 170 inline std::ostream& operator<<(std::ostream& os, const vmime::mailboxGroup& group) | |
| 171 { | |
| 172 os << "[mailbox-group: name=" << group.getName() << ", list=["; | |
| 173 | |
| 174 for (size_t i = 0 ; i < group.getMailboxCount() ; ++i) | |
| 175 { | |
| 176 if (i != 0) | |
| 177 os << ","; | |
| 178 | |
| 179 os << *group.getMailboxAt(i); | |
| 180 } | |
| 181 | |
| 182 os << "]]"; | |
| 183 | |
| 184 return (os); | |
| 185 } | |
| 186 | |
| 187 | |
| 188 inline std::ostream& operator<<(std::ostream& os, const vmime::addressList& list) | |
| 189 { | |
| 190 os << "[address-list: ["; | |
| 191 | |
| 192 for (size_t i = 0 ; i < list.getAddressCount() ; ++i) | |
| 193 { | |
| 194 const vmime::address& addr = *list.getAddressAt(i); | |
| 195 | |
| 196 if (i != 0) | |
| 197 os << ","; | |
| 198 | |
| 199 if (addr.isGroup()) | |
| 200 { | |
| 201 const vmime::mailboxGroup& group = | |
| 202 dynamic_cast <const vmime::mailboxGroup&>(addr); | |
| 203 | |
| 204 os << group; | |
| 205 } | |
| 206 else | |
| 207 { | |
| 208 const vmime::mailbox& mbox = | |
| 209 dynamic_cast <const vmime::mailbox&>(addr); | |
| 210 | |
| 211 os << mbox; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 os << "]]"; | |
| 216 | |
| 217 return (os); | |
| 218 } | |
| 219 | |
| 220 | |
| 221 inline std::ostream& operator<<(std::ostream& os, const vmime::datetime& d) | |
| 222 { | |
| 223 os << "[datetime: " << d.getYear() << "/" << d.getMonth() << "/" << d.getDay(); | |
| 224 os << " " << d.getHour() << ":" << d.getMinute() << ":" << d.getSecond(); | |
| 225 os << " #" << d.getZone() << "]"; | |
| 226 | |
| 227 return (os); | |
| 228 } | |
| 229 | |
| 230 | |
| 231 inline std::ostream& operator<<(std::ostream& os, const vmime::encoding& enc) | |
| 232 { | |
| 233 os << enc.generate(); | |
| 234 | |
| 235 return (os); | |
| 236 } | |
| 237 | |
| 238 | |
| 239 } | |
| 240 | |
| 241 | |
| 242 | |
| 243 // Used to test network features. | |
| 244 // | |
| 245 // This works like a local pipe: client reads and writes data using receive() | |
| 246 // and send(). Server reads incoming data with localReceive() and sends data | |
| 247 // to client with localSend(). | |
| 248 | |
| 249 class testSocket : public vmime::net::socket | |
| 250 { | |
| 251 public: | |
| 252 | |
| 253 void connect(const vmime::string& address, const vmime::port_t port); | |
| 254 void disconnect(); | |
| 255 | |
| 256 bool isConnected() const; | |
| 257 | |
| 258 bool waitForWrite(const int msecs = 30000); | |
| 259 bool waitForRead(const int msecs = 30000); | |
| 260 | |
| 261 void receive(vmime::string& buffer); | |
| 262 void send(const vmime::string& buffer); | |
| 263 void send(const char* str); | |
| 264 | |
| 265 size_t receiveRaw(vmime::byte_t* buffer, const size_t count); | |
| 266 void sendRaw(const vmime::byte_t* buffer, const size_t count); | |
| 267 size_t sendRawNonBlocking(const vmime::byte_t* buffer, const size_t count); | |
| 268 | |
| 269 size_t getBlockSize() const; | |
| 270 | |
| 271 unsigned int getStatus() const; | |
| 272 | |
| 273 const vmime::string getPeerName() const; | |
| 274 const vmime::string getPeerAddress() const; | |
| 275 | |
| 276 vmime::shared_ptr <vmime::net::timeoutHandler> getTimeoutHandler(); | |
| 277 | |
| 278 void setTracer(vmime::shared_ptr <vmime::net::tracer> tracer); | |
| 279 vmime::shared_ptr <vmime::net::tracer> getTracer(); | |
| 280 | |
| 281 /** Send data to client. | |
| 282 * | |
| 283 * @param buffer data to send | |
| 284 */ | |
| 285 void localSend(const vmime::string& buffer); | |
| 286 | |
| 287 /** Receive data from client. | |
| 288 * | |
| 289 * @param buffer buffer in which to store received data | |
| 290 */ | |
| 291 void localReceive(vmime::string& buffer); | |
| 292 | |
| 293 /** Receive a line from client. | |
| 294 * | |
| 295 * @param buffer buffer in which to store received line | |
| 296 * @return true if a line has been read, or false otherwise | |
| 297 */ | |
| 298 bool localReceiveLine(vmime::string& buffer); | |
| 299 | |
| 300 /** Receive data from client. | |
| 301 * | |
| 302 * @param buffer buffer in which to store received data | |
| 303 * @param count number of bytes to receive | |
| 304 * @return number of bytes received | |
| 305 */ | |
| 306 vmime::size_t localReceiveRaw(vmime::byte_t* buffer, const size_t count); | |
| 307 | |
| 308 protected: | |
| 309 | |
| 310 /** Called when the client has sent some data. | |
| 311 */ | |
| 312 virtual void onDataReceived(); | |
| 313 | |
| 314 /** Called when the client has connected. | |
| 315 */ | |
| 316 virtual void onConnected(); | |
| 317 | |
| 318 private: | |
| 319 | |
| 320 vmime::string m_address; | |
| 321 vmime::port_t m_port; | |
| 322 bool m_connected; | |
| 323 | |
| 324 vmime::string m_inBuffer; | |
| 325 vmime::string m_outBuffer; | |
| 326 }; | |
| 327 | |
| 328 | |
| 329 template <typename T> | |
| 330 class testSocketFactory : public vmime::net::socketFactory | |
| 331 { | |
| 332 public: | |
| 333 | |
| 334 vmime::shared_ptr <vmime::net::socket> create() | |
| 335 { | |
| 336 return vmime::make_shared <T>(); | |
| 337 } | |
| 338 | |
| 339 vmime::shared_ptr <vmime::net::socket> create(vmime::shared_ptr <vmime::net::timeoutHandler> /* th */) | |
| 340 { | |
| 341 return vmime::make_shared <T>(); | |
| 342 } | |
| 343 }; | |
| 344 | |
| 345 | |
| 346 class lineBasedTestSocket : public testSocket | |
| 347 { | |
| 348 public: | |
| 349 | |
| 350 void onDataReceived(); | |
| 351 | |
| 352 const vmime::string getNextLine(); | |
| 353 bool haveMoreLines() const; | |
| 354 | |
| 355 virtual void processCommand() = 0; | |
| 356 | |
| 357 private: | |
| 358 | |
| 359 std::vector <vmime::string> m_lines; | |
| 360 std::string m_buffer; | |
| 361 }; | |
| 362 | |
| 363 | |
| 364 class testTimeoutHandler : public vmime::net::timeoutHandler | |
| 365 { | |
| 366 public: | |
| 367 | |
| 368 testTimeoutHandler(const unsigned long delay = 3); | |
| 369 | |
| 370 bool isTimeOut(); | |
| 371 void resetTimeOut(); | |
| 372 bool handleTimeOut(); | |
| 373 | |
| 374 private: | |
| 375 | |
| 376 unsigned long m_delay; | |
| 377 unsigned long m_start; | |
| 378 }; | |
| 379 | |
| 380 | |
| 381 class testTimeoutHandlerFactory : public vmime::net::timeoutHandlerFactory | |
| 382 { | |
| 383 public: | |
| 384 | |
| 385 vmime::shared_ptr <vmime::net::timeoutHandler> create(); | |
| 386 }; | |
| 387 | |
| 388 | |
| 389 // Exception helper | |
| 390 std::ostream& operator<<(std::ostream& os, const vmime::exception& e); | |
| 391 | |
| 392 | |
| 393 // Conversion to hexadecimal for easier debugging | |
| 394 const vmime::string toHex(const vmime::string str); |
