| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import java.net.URL; |
| 4 | |
| 5 | import be.nikiroo.utils.Version; |
| 6 | import be.nikiroo.utils.serial.server.ConnectActionClientObject; |
| 7 | import be.nikiroo.utils.serial.server.ConnectActionClientString; |
| 8 | import be.nikiroo.utils.serial.server.ConnectActionServerObject; |
| 9 | import be.nikiroo.utils.serial.server.ConnectActionServerString; |
| 10 | import be.nikiroo.utils.serial.server.ServerBridge; |
| 11 | import be.nikiroo.utils.serial.server.ServerObject; |
| 12 | import be.nikiroo.utils.serial.server.ServerString; |
| 13 | import be.nikiroo.utils.test.TestCase; |
| 14 | import be.nikiroo.utils.test.TestLauncher; |
| 15 | |
| 16 | class SerialServerTest extends TestLauncher { |
| 17 | public SerialServerTest(String[] args) { |
| 18 | super("SerialServer test", args); |
| 19 | |
| 20 | for (String key : new String[] { null, |
| 21 | "some super secret encryption key" }) { |
| 22 | for (boolean bridge : new Boolean[] { false, true }) { |
| 23 | final String skey = (key != null ? "(encrypted)" |
| 24 | : "(plain text)"); |
| 25 | final String sbridge = (bridge ? " with bridge" : ""); |
| 26 | |
| 27 | addSeries(new SerialServerTest(args, key, skey, bridge, |
| 28 | sbridge, "ServerString")); |
| 29 | |
| 30 | addSeries(new SerialServerTest(args, key, skey, bridge, |
| 31 | sbridge, new Object() { |
| 32 | @Override |
| 33 | public String toString() { |
| 34 | return "ServerObject"; |
| 35 | } |
| 36 | })); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private SerialServerTest(final String[] args, final String key, |
| 42 | final String skey, final boolean bridge, final String sbridge, |
| 43 | final String title) { |
| 44 | |
| 45 | super(title + " " + skey + sbridge, args); |
| 46 | |
| 47 | addTest(new TestCase("Simple connection " + skey) { |
| 48 | @Override |
| 49 | public void test() throws Exception { |
| 50 | final String[] rec = new String[1]; |
| 51 | |
| 52 | ServerString server = new ServerString(this.getName(), 0, key) { |
| 53 | @Override |
| 54 | protected String onRequest( |
| 55 | ConnectActionServerString action, Version version, |
| 56 | String data, long id) throws Exception { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onError(Exception e) { |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | int port = server.getPort(); |
| 66 | assertEquals("A port should have been assigned", true, port > 0); |
| 67 | |
| 68 | server.start(); |
| 69 | |
| 70 | ServerBridge br = null; |
| 71 | if (bridge) { |
| 72 | br = new ServerBridge(0, key, "", port, key); |
| 73 | br.setTraceHandler(null); |
| 74 | |
| 75 | port = br.getPort(); |
| 76 | assertEquals( |
| 77 | "A port should have been assigned to the bridge", |
| 78 | true, port > 0); |
| 79 | |
| 80 | br.start(); |
| 81 | } |
| 82 | |
| 83 | try { |
| 84 | try { |
| 85 | new ConnectActionClientString(null, port, key) { |
| 86 | @Override |
| 87 | public void action(Version version) |
| 88 | throws Exception { |
| 89 | rec[0] = "ok"; |
| 90 | } |
| 91 | }.connect(); |
| 92 | } finally { |
| 93 | server.stop(); |
| 94 | } |
| 95 | } finally { |
| 96 | if (br != null) { |
| 97 | br.stop(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | assertNotNull("The client action was not run", rec[0]); |
| 102 | assertEquals("ok", rec[0]); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | addTest(new TestCase("Simple exchange " + skey) { |
| 107 | final String[] sent = new String[1]; |
| 108 | final String[] recd = new String[1]; |
| 109 | final Exception[] err = new Exception[1]; |
| 110 | |
| 111 | @Override |
| 112 | public void test() throws Exception { |
| 113 | ServerString server = new ServerString(this.getName(), 0, key) { |
| 114 | @Override |
| 115 | protected String onRequest( |
| 116 | ConnectActionServerString action, Version version, |
| 117 | String data, long id) throws Exception { |
| 118 | sent[0] = data; |
| 119 | return "pong"; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | protected void onError(Exception e) { |
| 124 | err[0] = e; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | int port = server.getPort(); |
| 129 | |
| 130 | server.start(); |
| 131 | |
| 132 | ServerBridge br = null; |
| 133 | if (bridge) { |
| 134 | br = new ServerBridge(0, key, "", port, key); |
| 135 | br.setTraceHandler(null); |
| 136 | port = br.getPort(); |
| 137 | br.start(); |
| 138 | } |
| 139 | |
| 140 | try { |
| 141 | try { |
| 142 | new ConnectActionClientString(null, port, key) { |
| 143 | @Override |
| 144 | public void action(Version version) |
| 145 | throws Exception { |
| 146 | recd[0] = send("ping"); |
| 147 | } |
| 148 | }.connect(); |
| 149 | } finally { |
| 150 | server.stop(); |
| 151 | } |
| 152 | } finally { |
| 153 | if (br != null) { |
| 154 | br.stop(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (err[0] != null) { |
| 159 | fail("An exception was thrown: " + err[0].getMessage(), |
| 160 | err[0]); |
| 161 | } |
| 162 | |
| 163 | assertEquals("ping", sent[0]); |
| 164 | assertEquals("pong", recd[0]); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | addTest(new TestCase("Multiple exchanges " + skey) { |
| 169 | final String[] sent = new String[3]; |
| 170 | final String[] recd = new String[3]; |
| 171 | final Exception[] err = new Exception[1]; |
| 172 | |
| 173 | @Override |
| 174 | public void test() throws Exception { |
| 175 | ServerString server = new ServerString(this.getName(), 0, key) { |
| 176 | @Override |
| 177 | protected String onRequest( |
| 178 | ConnectActionServerString action, Version version, |
| 179 | String data, long id) throws Exception { |
| 180 | sent[0] = data; |
| 181 | action.send("pong"); |
| 182 | sent[1] = action.rec(); |
| 183 | return "pong2"; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected void onError(Exception e) { |
| 188 | err[0] = e; |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | int port = server.getPort(); |
| 193 | |
| 194 | server.start(); |
| 195 | |
| 196 | ServerBridge br = null; |
| 197 | if (bridge) { |
| 198 | br = new ServerBridge(0, key, "", port, key); |
| 199 | br.setTraceHandler(null); |
| 200 | port = br.getPort(); |
| 201 | br.start(); |
| 202 | } |
| 203 | |
| 204 | try { |
| 205 | try { |
| 206 | new ConnectActionClientString(null, port, key) { |
| 207 | @Override |
| 208 | public void action(Version version) |
| 209 | throws Exception { |
| 210 | recd[0] = send("ping"); |
| 211 | recd[1] = send("ping2"); |
| 212 | } |
| 213 | }.connect(); |
| 214 | } finally { |
| 215 | server.stop(); |
| 216 | } |
| 217 | } finally { |
| 218 | if (br != null) { |
| 219 | br.stop(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (err[0] != null) { |
| 224 | fail("An exception was thrown: " + err[0].getMessage(), |
| 225 | err[0]); |
| 226 | } |
| 227 | |
| 228 | assertEquals("ping", sent[0]); |
| 229 | assertEquals("pong", recd[0]); |
| 230 | assertEquals("ping2", sent[1]); |
| 231 | assertEquals("pong2", recd[1]); |
| 232 | } |
| 233 | }); |
| 234 | |
| 235 | addTest(new TestCase("Multiple call from client " + skey) { |
| 236 | final String[] sent = new String[3]; |
| 237 | final String[] recd = new String[3]; |
| 238 | final Exception[] err = new Exception[1]; |
| 239 | |
| 240 | @Override |
| 241 | public void test() throws Exception { |
| 242 | ServerString server = new ServerString(this.getName(), 0, key) { |
| 243 | @Override |
| 244 | protected String onRequest( |
| 245 | ConnectActionServerString action, Version version, |
| 246 | String data, long id) throws Exception { |
| 247 | sent[Integer.parseInt(data)] = data; |
| 248 | return "" + (Integer.parseInt(data) * 2); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | protected void onError(Exception e) { |
| 253 | err[0] = e; |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | int port = server.getPort(); |
| 258 | |
| 259 | server.start(); |
| 260 | |
| 261 | ServerBridge br = null; |
| 262 | if (bridge) { |
| 263 | br = new ServerBridge(0, key, "", port, key); |
| 264 | br.setTraceHandler(null); |
| 265 | port = br.getPort(); |
| 266 | br.start(); |
| 267 | } |
| 268 | |
| 269 | try { |
| 270 | try { |
| 271 | new ConnectActionClientString(null, port, key) { |
| 272 | @Override |
| 273 | public void action(Version version) |
| 274 | throws Exception { |
| 275 | for (int i = 0; i < 3; i++) { |
| 276 | recd[i] = send("" + i); |
| 277 | } |
| 278 | } |
| 279 | }.connect(); |
| 280 | } finally { |
| 281 | server.stop(); |
| 282 | } |
| 283 | } finally { |
| 284 | if (br != null) { |
| 285 | br.stop(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (err[0] != null) { |
| 290 | fail("An exception was thrown: " + err[0].getMessage(), |
| 291 | err[0]); |
| 292 | } |
| 293 | |
| 294 | assertEquals("0", sent[0]); |
| 295 | assertEquals("0", recd[0]); |
| 296 | assertEquals("1", sent[1]); |
| 297 | assertEquals("2", recd[1]); |
| 298 | assertEquals("2", sent[2]); |
| 299 | assertEquals("4", recd[2]); |
| 300 | } |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | private SerialServerTest(final String[] args, final String key, |
| 305 | final String skey, final boolean bridge, final String sbridge, |
| 306 | final Object title) { |
| 307 | |
| 308 | super(title + " " + skey + sbridge, args); |
| 309 | |
| 310 | addTest(new TestCase("Simple connection " + skey) { |
| 311 | @Override |
| 312 | public void test() throws Exception { |
| 313 | final Object[] rec = new Object[1]; |
| 314 | |
| 315 | ServerObject server = new ServerObject(this.getName(), 0, key) { |
| 316 | @Override |
| 317 | protected Object onRequest( |
| 318 | ConnectActionServerObject action, Version version, |
| 319 | Object data, long id) throws Exception { |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | protected void onError(Exception e) { |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | int port = server.getPort(); |
| 329 | assertEquals("A port should have been assigned", true, port > 0); |
| 330 | |
| 331 | server.start(); |
| 332 | |
| 333 | ServerBridge br = null; |
| 334 | if (bridge) { |
| 335 | br = new ServerBridge(0, key, "", port, key); |
| 336 | br.setTraceHandler(null); |
| 337 | port = br.getPort(); |
| 338 | br.start(); |
| 339 | } |
| 340 | |
| 341 | try { |
| 342 | try { |
| 343 | new ConnectActionClientObject(null, port, key) { |
| 344 | @Override |
| 345 | public void action(Version version) |
| 346 | throws Exception { |
| 347 | rec[0] = true; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | protected void onError(Exception e) { |
| 352 | } |
| 353 | }.connect(); |
| 354 | } finally { |
| 355 | server.stop(); |
| 356 | } |
| 357 | } finally { |
| 358 | if (br != null) { |
| 359 | br.stop(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | assertNotNull("The client action was not run", rec[0]); |
| 364 | assertEquals(true, (boolean) ((Boolean) rec[0])); |
| 365 | } |
| 366 | }); |
| 367 | |
| 368 | addTest(new TestCase("Simple exchange " + skey) { |
| 369 | final Object[] sent = new Object[1]; |
| 370 | final Object[] recd = new Object[1]; |
| 371 | final Exception[] err = new Exception[1]; |
| 372 | |
| 373 | @Override |
| 374 | public void test() throws Exception { |
| 375 | ServerObject server = new ServerObject(this.getName(), 0, key) { |
| 376 | @Override |
| 377 | protected Object onRequest( |
| 378 | ConnectActionServerObject action, Version version, |
| 379 | Object data, long id) throws Exception { |
| 380 | sent[0] = data; |
| 381 | return "pong"; |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | protected void onError(Exception e) { |
| 386 | err[0] = e; |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | int port = server.getPort(); |
| 391 | |
| 392 | server.start(); |
| 393 | |
| 394 | ServerBridge br = null; |
| 395 | if (bridge) { |
| 396 | br = new ServerBridge(0, key, "", port, key); |
| 397 | br.setTraceHandler(null); |
| 398 | port = br.getPort(); |
| 399 | br.start(); |
| 400 | } |
| 401 | |
| 402 | try { |
| 403 | try { |
| 404 | new ConnectActionClientObject(null, port, key) { |
| 405 | @Override |
| 406 | public void action(Version version) |
| 407 | throws Exception { |
| 408 | recd[0] = send("ping"); |
| 409 | } |
| 410 | }.connect(); |
| 411 | } finally { |
| 412 | server.stop(); |
| 413 | } |
| 414 | } finally { |
| 415 | if (br != null) { |
| 416 | br.stop(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (err[0] != null) { |
| 421 | fail("An exception was thrown: " + err[0].getMessage(), |
| 422 | err[0]); |
| 423 | } |
| 424 | |
| 425 | assertEquals("ping", sent[0]); |
| 426 | assertEquals("pong", recd[0]); |
| 427 | } |
| 428 | }); |
| 429 | |
| 430 | addTest(new TestCase("Multiple exchanges " + skey) { |
| 431 | final Object[] sent = new Object[3]; |
| 432 | final Object[] recd = new Object[3]; |
| 433 | final Exception[] err = new Exception[1]; |
| 434 | |
| 435 | @Override |
| 436 | public void test() throws Exception { |
| 437 | ServerObject server = new ServerObject(this.getName(), 0, key) { |
| 438 | @Override |
| 439 | protected Object onRequest( |
| 440 | ConnectActionServerObject action, Version version, |
| 441 | Object data, long id) throws Exception { |
| 442 | sent[0] = data; |
| 443 | action.send("pong"); |
| 444 | sent[1] = action.rec(); |
| 445 | return "pong2"; |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | protected void onError(Exception e) { |
| 450 | err[0] = e; |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | int port = server.getPort(); |
| 455 | |
| 456 | server.start(); |
| 457 | |
| 458 | ServerBridge br = null; |
| 459 | if (bridge) { |
| 460 | br = new ServerBridge(0, key, "", port, key); |
| 461 | br.setTraceHandler(null); |
| 462 | port = br.getPort(); |
| 463 | br.start(); |
| 464 | } |
| 465 | |
| 466 | try { |
| 467 | try { |
| 468 | new ConnectActionClientObject(null, port, key) { |
| 469 | @Override |
| 470 | public void action(Version version) |
| 471 | throws Exception { |
| 472 | recd[0] = send("ping"); |
| 473 | recd[1] = send("ping2"); |
| 474 | } |
| 475 | }.connect(); |
| 476 | } finally { |
| 477 | server.stop(); |
| 478 | } |
| 479 | } finally { |
| 480 | if (br != null) { |
| 481 | br.stop(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (err[0] != null) { |
| 486 | fail("An exception was thrown: " + err[0].getMessage(), |
| 487 | err[0]); |
| 488 | } |
| 489 | |
| 490 | assertEquals("ping", sent[0]); |
| 491 | assertEquals("pong", recd[0]); |
| 492 | assertEquals("ping2", sent[1]); |
| 493 | assertEquals("pong2", recd[1]); |
| 494 | } |
| 495 | }); |
| 496 | |
| 497 | addTest(new TestCase("Object array of URLs " + skey) { |
| 498 | final Object[] sent = new Object[1]; |
| 499 | final Object[] recd = new Object[1]; |
| 500 | final Exception[] err = new Exception[1]; |
| 501 | |
| 502 | @Override |
| 503 | public void test() throws Exception { |
| 504 | ServerObject server = new ServerObject(this.getName(), 0, key) { |
| 505 | @Override |
| 506 | protected Object onRequest( |
| 507 | ConnectActionServerObject action, Version version, |
| 508 | Object data, long id) throws Exception { |
| 509 | sent[0] = data; |
| 510 | return new Object[] { "ACK" }; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | protected void onError(Exception e) { |
| 515 | err[0] = e; |
| 516 | } |
| 517 | }; |
| 518 | |
| 519 | int port = server.getPort(); |
| 520 | |
| 521 | server.start(); |
| 522 | |
| 523 | ServerBridge br = null; |
| 524 | if (bridge) { |
| 525 | br = new ServerBridge(0, key, "", port, key); |
| 526 | br.setTraceHandler(null); |
| 527 | port = br.getPort(); |
| 528 | br.start(); |
| 529 | } |
| 530 | |
| 531 | try { |
| 532 | try { |
| 533 | new ConnectActionClientObject(null, port, key) { |
| 534 | @Override |
| 535 | public void action(Version version) |
| 536 | throws Exception { |
| 537 | recd[0] = send(new Object[] { |
| 538 | "key", |
| 539 | new URL( |
| 540 | "https://example.com/from_client"), |
| 541 | "https://example.com/from_client" }); |
| 542 | } |
| 543 | }.connect(); |
| 544 | } finally { |
| 545 | server.stop(); |
| 546 | } |
| 547 | } finally { |
| 548 | if (br != null) { |
| 549 | br.stop(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (err[0] != null) { |
| 554 | fail("An exception was thrown: " + err[0].getMessage(), |
| 555 | err[0]); |
| 556 | } |
| 557 | |
| 558 | Object[] sento = (Object[]) (sent[0]); |
| 559 | Object[] recdo = (Object[]) (recd[0]); |
| 560 | |
| 561 | assertEquals("key", sento[0]); |
| 562 | assertEquals("https://example.com/from_client", |
| 563 | ((URL) sento[1]).toString()); |
| 564 | assertEquals("https://example.com/from_client", sento[2]); |
| 565 | assertEquals("ACK", recdo[0]); |
| 566 | } |
| 567 | }); |
| 568 | |
| 569 | addTest(new TestCase("Multiple call from client " + skey) { |
| 570 | final Object[] sent = new Object[3]; |
| 571 | final Object[] recd = new Object[3]; |
| 572 | final Exception[] err = new Exception[1]; |
| 573 | |
| 574 | @Override |
| 575 | public void test() throws Exception { |
| 576 | ServerObject server = new ServerObject(this.getName(), 0, key) { |
| 577 | @Override |
| 578 | protected Object onRequest( |
| 579 | ConnectActionServerObject action, Version version, |
| 580 | Object data, long id) throws Exception { |
| 581 | sent[(Integer) data] = data; |
| 582 | return ((Integer) data) * 2; |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | protected void onError(Exception e) { |
| 587 | err[0] = e; |
| 588 | } |
| 589 | }; |
| 590 | |
| 591 | int port = server.getPort(); |
| 592 | |
| 593 | server.start(); |
| 594 | |
| 595 | ServerBridge br = null; |
| 596 | if (bridge) { |
| 597 | br = new ServerBridge(0, key, "", port, key); |
| 598 | br.setTraceHandler(null); |
| 599 | port = br.getPort(); |
| 600 | br.start(); |
| 601 | } |
| 602 | |
| 603 | try { |
| 604 | try { |
| 605 | new ConnectActionClientObject(null, port, key) { |
| 606 | @Override |
| 607 | public void action(Version version) |
| 608 | throws Exception { |
| 609 | for (int i = 0; i < 3; i++) { |
| 610 | recd[i] = send(i); |
| 611 | } |
| 612 | } |
| 613 | }.connect(); |
| 614 | } finally { |
| 615 | server.stop(); |
| 616 | } |
| 617 | } finally { |
| 618 | if (br != null) { |
| 619 | br.stop(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (err[0] != null) { |
| 624 | fail("An exception was thrown: " + err[0].getMessage(), |
| 625 | err[0]); |
| 626 | } |
| 627 | |
| 628 | assertEquals(0, sent[0]); |
| 629 | assertEquals(0, recd[0]); |
| 630 | assertEquals(1, sent[1]); |
| 631 | assertEquals(2, recd[1]); |
| 632 | assertEquals(2, sent[2]); |
| 633 | assertEquals(4, recd[2]); |
| 634 | } |
| 635 | }); |
| 636 | } |
| 637 | } |