| 1 | /* |
| 2 | Copyright (C) 2004 by Intra2net AG |
| 3 | |
| 4 | The software in this package is distributed under the GNU General |
| 5 | Public License version 2 (with a special exception described below). |
| 6 | |
| 7 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 8 | in the file COPYING.GPL. |
| 9 | |
| 10 | As a special exception, if other files instantiate templates or use macros |
| 11 | or inline functions from this file, or you compile this file and link it |
| 12 | with other works to produce a work based on this file, this file |
| 13 | does not by itself cause the resulting work to be covered |
| 14 | by the GNU General Public License. |
| 15 | |
| 16 | However the source code for this file must still be made available |
| 17 | in accordance with section (3) of the GNU General Public License. |
| 18 | |
| 19 | This exception does not invalidate any other reasons why a work based |
| 20 | on this file might be covered by the GNU General Public License. |
| 21 | */ |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include <iostream> |
| 30 | #include <string> |
| 31 | #include <sstream> |
| 32 | #include <stdexcept> |
| 33 | |
| 34 | #include <cppunit/extensions/TestFactoryRegistry.h> |
| 35 | #include <cppunit/ui/text/TestRunner.h> |
| 36 | #include <cppunit/extensions/HelperMacros.h> |
| 37 | |
| 38 | #include <socket_client.hxx> |
| 39 | #include <socket_server.hxx> |
| 40 | |
| 41 | using namespace std; |
| 42 | using namespace libt2n; |
| 43 | using namespace CppUnit; |
| 44 | |
| 45 | |
| 46 | class test_comm : public TestFixture |
| 47 | { |
| 48 | CPPUNIT_TEST_SUITE(test_comm); |
| 49 | |
| 50 | CPPUNIT_TEST(UnixCommToServer); |
| 51 | CPPUNIT_TEST(UnixCommToServerAndBack); |
| 52 | CPPUNIT_TEST(UnixCommToServerAndBackBig); |
| 53 | CPPUNIT_TEST(IPCommToServer); |
| 54 | CPPUNIT_TEST(IPCommToServerAndBack); |
| 55 | CPPUNIT_TEST(IPCommToServerAndBackBig); |
| 56 | |
| 57 | CPPUNIT_TEST_SUITE_END(); |
| 58 | |
| 59 | pid_t child_pid; |
| 60 | |
| 61 | public: |
| 62 | |
| 63 | void setUp() |
| 64 | { } |
| 65 | |
| 66 | void tearDown() |
| 67 | { |
| 68 | // make sure the server-child is dead before the next test runs |
| 69 | kill(child_pid,SIGKILL); |
| 70 | sleep(1); |
| 71 | } |
| 72 | |
| 73 | void UnixCommToServer() |
| 74 | { |
| 75 | string data; |
| 76 | |
| 77 | switch(child_pid=fork()) |
| 78 | { |
| 79 | case -1: |
| 80 | { |
| 81 | CPPUNIT_FAIL("fork error"); |
| 82 | break; |
| 83 | } |
| 84 | case 0: |
| 85 | // child |
| 86 | { |
| 87 | try |
| 88 | { |
| 89 | // wait till server is up |
| 90 | sleep(1); |
| 91 | socket_client_connection sc("./socket"); |
| 92 | sc.write("hello"); |
| 93 | } catch(...) |
| 94 | { |
| 95 | std::cerr << "exception in child. ignoring\n"; |
| 96 | } |
| 97 | |
| 98 | // don't call atexit and stuff |
| 99 | _exit(0); |
| 100 | } |
| 101 | |
| 102 | default: |
| 103 | // parent |
| 104 | { |
| 105 | socket_server ss("./socket"); |
| 106 | |
| 107 | time_t t0 = time(NULL); |
| 108 | |
| 109 | // max 10 sec |
| 110 | while (time(NULL) < t0 + 10 ) |
| 111 | { |
| 112 | ss.fill_buffer(1000000); |
| 113 | |
| 114 | if(ss.get_packet(data)) |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); |
| 120 | } |
| 121 | |
| 122 | void UnixCommToServerAndBack() |
| 123 | { |
| 124 | switch(child_pid=fork()) |
| 125 | { |
| 126 | case -1: |
| 127 | { |
| 128 | CPPUNIT_FAIL("fork error"); |
| 129 | break; |
| 130 | } |
| 131 | case 0: |
| 132 | // child |
| 133 | { |
| 134 | try |
| 135 | { |
| 136 | socket_server ss("./socket"); |
| 137 | ss.set_logging(&cerr,debug); |
| 138 | |
| 139 | time_t t0 = time(NULL); |
| 140 | |
| 141 | // max 10 sec |
| 142 | while (time(NULL) < t0 + 10 ) |
| 143 | { |
| 144 | ss.fill_buffer(1000000); |
| 145 | |
| 146 | string data; |
| 147 | unsigned int cid; |
| 148 | |
| 149 | if(ss.get_packet(data,cid)) |
| 150 | { |
| 151 | server_connection* con=ss.get_connection(cid); |
| 152 | |
| 153 | if (data=="QUIT") |
| 154 | break; |
| 155 | |
| 156 | if (data=="ABC") |
| 157 | con->write("DEF"); |
| 158 | else |
| 159 | con->write("xyz"); |
| 160 | } |
| 161 | } |
| 162 | } catch(...) |
| 163 | { |
| 164 | std::cerr << "exception in child. ignoring\n"; |
| 165 | } |
| 166 | |
| 167 | // don't call atexit and stuff |
| 168 | _exit(0); |
| 169 | } |
| 170 | |
| 171 | default: |
| 172 | // parent |
| 173 | { |
| 174 | string data; |
| 175 | |
| 176 | // wait till server is up |
| 177 | sleep(1); |
| 178 | socket_client_connection sc("./socket"); |
| 179 | |
| 180 | sc.write("ABC"); |
| 181 | |
| 182 | sc.fill_buffer(1000000); |
| 183 | sc.get_packet(data); |
| 184 | |
| 185 | CPPUNIT_ASSERT_EQUAL(string("DEF"),data); |
| 186 | |
| 187 | sc.write("HAHA"); |
| 188 | |
| 189 | sc.fill_buffer(1000000); |
| 190 | sc.get_packet(data); |
| 191 | |
| 192 | CPPUNIT_ASSERT_EQUAL(string("xyz"),data); |
| 193 | |
| 194 | sc.write("QUIT"); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void UnixCommToServerAndBackBig() |
| 200 | { |
| 201 | switch(child_pid=fork()) |
| 202 | { |
| 203 | case -1: |
| 204 | { |
| 205 | CPPUNIT_FAIL("fork error"); |
| 206 | break; |
| 207 | } |
| 208 | case 0: |
| 209 | // child |
| 210 | { |
| 211 | try |
| 212 | { |
| 213 | socket_server ss("./socket"); |
| 214 | ss.set_logging(&cerr,debug); |
| 215 | |
| 216 | time_t t0 = time(NULL); |
| 217 | |
| 218 | // max 10 sec |
| 219 | while (time(NULL) < t0 + 10 ) |
| 220 | { |
| 221 | ss.fill_buffer(1000000); |
| 222 | |
| 223 | string data; |
| 224 | unsigned int cid; |
| 225 | |
| 226 | if(ss.get_packet(data,cid)) |
| 227 | { |
| 228 | server_connection* con=ss.get_connection(cid); |
| 229 | |
| 230 | if (data=="QUIT") |
| 231 | break; |
| 232 | |
| 233 | con->write(string().insert(0,100*1024,'y')); |
| 234 | } |
| 235 | } |
| 236 | std::cerr << "child: OVER" << std::endl; |
| 237 | } catch(...) |
| 238 | { |
| 239 | std::cerr << "exception in child. ignoring\n"; |
| 240 | } |
| 241 | |
| 242 | // don't call atexit and stuff |
| 243 | _exit(0); |
| 244 | } |
| 245 | |
| 246 | default: |
| 247 | // parent |
| 248 | { |
| 249 | string data; |
| 250 | |
| 251 | // wait till server is up |
| 252 | sleep(1); |
| 253 | socket_client_connection sc("./socket"); |
| 254 | |
| 255 | sc.write(string().insert(0,100*1024,'x')); |
| 256 | |
| 257 | while (!sc.get_packet(data)) |
| 258 | sc.fill_buffer(1000000); |
| 259 | |
| 260 | CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y'),data); |
| 261 | |
| 262 | sc.write("QUIT"); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void IPCommToServer() |
| 268 | { |
| 269 | string data; |
| 270 | |
| 271 | switch(child_pid=fork()) |
| 272 | { |
| 273 | case -1: |
| 274 | { |
| 275 | CPPUNIT_FAIL("fork error"); |
| 276 | break; |
| 277 | } |
| 278 | case 0: |
| 279 | // child |
| 280 | { |
| 281 | try |
| 282 | { |
| 283 | // wait till server is up |
| 284 | sleep(1); |
| 285 | socket_client_connection sc(6666); |
| 286 | sc.write("hello"); |
| 287 | } catch(...) |
| 288 | { |
| 289 | std::cerr << "exception in child. ignoring\n"; |
| 290 | } |
| 291 | |
| 292 | // don't call atexit and stuff |
| 293 | _exit(0); |
| 294 | } |
| 295 | |
| 296 | default: |
| 297 | // parent |
| 298 | { |
| 299 | socket_server ss(6666); |
| 300 | |
| 301 | time_t t0 = time(NULL); |
| 302 | |
| 303 | // max 10 sec |
| 304 | while (time(NULL) < t0 + 10 ) |
| 305 | { |
| 306 | ss.fill_buffer(1000000); |
| 307 | |
| 308 | if(ss.get_packet(data)) |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); |
| 314 | } |
| 315 | |
| 316 | void IPCommToServerAndBack() |
| 317 | { |
| 318 | switch(child_pid=fork()) |
| 319 | { |
| 320 | case -1: |
| 321 | { |
| 322 | CPPUNIT_FAIL("fork error"); |
| 323 | break; |
| 324 | } |
| 325 | case 0: |
| 326 | // child |
| 327 | { |
| 328 | try |
| 329 | { |
| 330 | socket_server ss(6666); |
| 331 | ss.set_logging(&cerr,debug); |
| 332 | |
| 333 | time_t t0 = time(NULL); |
| 334 | |
| 335 | // max 10 sec |
| 336 | while (time(NULL) < t0 + 10 ) |
| 337 | { |
| 338 | ss.fill_buffer(1000000); |
| 339 | |
| 340 | string data; |
| 341 | unsigned int cid; |
| 342 | |
| 343 | if(ss.get_packet(data,cid)) |
| 344 | { |
| 345 | server_connection* con=ss.get_connection(cid); |
| 346 | |
| 347 | if (data=="QUIT") |
| 348 | break; |
| 349 | |
| 350 | if (data=="ABC") |
| 351 | con->write("DEF"); |
| 352 | else |
| 353 | con->write("xyz"); |
| 354 | } |
| 355 | } |
| 356 | } catch(...) |
| 357 | { |
| 358 | std::cerr << "exception in child. ignoring\n"; |
| 359 | } |
| 360 | // don't call atexit and stuff |
| 361 | _exit(0); |
| 362 | } |
| 363 | |
| 364 | default: |
| 365 | // parent |
| 366 | { |
| 367 | string data; |
| 368 | |
| 369 | // wait till server is up |
| 370 | sleep(1); |
| 371 | socket_client_connection sc(6666); |
| 372 | sc.write("ABC"); |
| 373 | |
| 374 | sc.fill_buffer(1000000); |
| 375 | sc.get_packet(data); |
| 376 | |
| 377 | CPPUNIT_ASSERT_EQUAL(string("DEF"),data); |
| 378 | |
| 379 | sc.write("HAHA"); |
| 380 | |
| 381 | sc.fill_buffer(1000000); |
| 382 | sc.get_packet(data); |
| 383 | |
| 384 | CPPUNIT_ASSERT_EQUAL(string("xyz"),data); |
| 385 | |
| 386 | sc.write("QUIT"); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
| 392 | void IPCommToServerAndBackBig() |
| 393 | { |
| 394 | switch(child_pid=fork()) |
| 395 | { |
| 396 | case -1: |
| 397 | { |
| 398 | CPPUNIT_FAIL("fork error"); |
| 399 | break; |
| 400 | } |
| 401 | case 0: |
| 402 | // child |
| 403 | { |
| 404 | try |
| 405 | { |
| 406 | socket_server ss(6666); |
| 407 | ss.set_logging(&cerr,debug); |
| 408 | |
| 409 | time_t t0 = time(NULL); |
| 410 | |
| 411 | // max 10 sec |
| 412 | while (time(NULL) < t0 + 10 ) |
| 413 | { |
| 414 | ss.fill_buffer(1000000); |
| 415 | |
| 416 | string data; |
| 417 | unsigned int cid; |
| 418 | |
| 419 | if(ss.get_packet(data,cid)) |
| 420 | { |
| 421 | server_connection* con=ss.get_connection(cid); |
| 422 | |
| 423 | socket_handler* alias= dynamic_cast< socket_handler* >(con); |
| 424 | |
| 425 | if (data=="QUIT") |
| 426 | break; |
| 427 | |
| 428 | alias->set_write_block_size( 4093 ); |
| 429 | con->write(string().insert(0,2048*1024,'y')); |
| 430 | } |
| 431 | } |
| 432 | } catch(...) |
| 433 | { |
| 434 | std::cerr << "exception in child. ignoring\n"; |
| 435 | } |
| 436 | // don't call atexit and stuff |
| 437 | _exit(0); |
| 438 | } |
| 439 | |
| 440 | default: |
| 441 | // parent |
| 442 | { |
| 443 | string data; |
| 444 | |
| 445 | // wait till server is up |
| 446 | sleep(1); |
| 447 | socket_client_connection sc(6666); |
| 448 | |
| 449 | sc.write(string().insert(0,100*1024,'x')); |
| 450 | |
| 451 | while (!sc.get_packet(data)) |
| 452 | sc.fill_buffer(1000000); |
| 453 | |
| 454 | CPPUNIT_ASSERT_EQUAL(string().insert(0,2048*1024,'y'),data); |
| 455 | |
| 456 | sc.write("QUIT"); |
| 457 | } |
| 458 | } |
| 459 | } // eo IPCommToServerAndBackBig() |
| 460 | }; |
| 461 | |
| 462 | CPPUNIT_TEST_SUITE_REGISTRATION(test_comm); |