1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.telnet;
18 import junit.framework.TestCase;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.PipedInputStream;
24 import java.io.PipedOutputStream;
25
26
27
28
29
30
31
32 public class TelnetClientTest
33 extends TestCase implements TelnetNotificationHandler
34 {
35
36
37
38
39 private class TestConnection {
40 private final TelnetTestSimpleServer server;
41 private final TelnetClient client;
42 private final int port;
43 TestConnection(
44 TelnetTestSimpleServer server,
45 TelnetClient client,
46 int port)
47 {
48 this.server = server;
49 this.client = client;
50 this.port = port;
51 }
52 protected void close() {
53 TelnetClientTest.this.closeConnection(
54 this.server, this.client, this.port);
55 }
56 }
57
58
59
60 private TestConnection STANDARD;
61 private TestConnection OPTIONS;
62 private TestConnection ANSI;
63 private TestConnection NOREAD;
64
65 private final int NUM_CONNECTIONS = 4;
66
67
68 protected int numdo = 0;
69 protected int numdont = 0;
70 protected int numwill = 0;
71 protected int numwont = 0;
72
73
74
75
76 @Override
77 protected void setUp() throws Exception
78 {
79 int socket = 0;
80 super.setUp();
81 for (int port = 3333; socket < NUM_CONNECTIONS && port < 4000; port++)
82 {
83 TelnetTestSimpleServer server = null;
84 TelnetClient client = null;
85 try {
86 server = new TelnetTestSimpleServer(port);
87 switch (socket) {
88 case 0:
89 client = new TelnetClient();
90
91 client.setReaderThread(true);
92 client.connect("127.0.0.1", port);
93 STANDARD = new TestConnection(server, client, port);
94 break;
95 case 1:
96 client = new TelnetClient();
97 TerminalTypeOptionHandler ttopt =
98 new TerminalTypeOptionHandler("VT100", false, false, true, false);
99 EchoOptionHandler echoopt =
100 new EchoOptionHandler(true, false, true, false);
101 SuppressGAOptionHandler gaopt =
102 new SuppressGAOptionHandler(true, true, true, true);
103
104 client.addOptionHandler(ttopt);
105 client.addOptionHandler(echoopt);
106 client.addOptionHandler(gaopt);
107 client.connect("127.0.0.1", port);
108 OPTIONS = new TestConnection(server, client, port);
109 break;
110 case 2:
111 client = new TelnetClient("ANSI");
112 client.connect("127.0.0.1", port);
113 ANSI = new TestConnection(server, client, port);
114 break;
115 case 3:
116 client = new TelnetClient();
117 client.setReaderThread(false);
118 client.connect("127.0.0.1", port);
119 NOREAD = new TestConnection(server, client, port);
120 break;
121 }
122
123 socket++;
124 } catch (IOException e) {
125 closeConnection(server, client, port);
126 }
127 }
128 if (socket < NUM_CONNECTIONS) {
129 System.err.println("Only created "+socket+" clients; wanted "+NUM_CONNECTIONS);
130 }
131 Thread.sleep(1000);
132 }
133
134
135
136
137 @Override
138 protected void tearDown() throws Exception {
139 NOREAD.close();
140 ANSI.close();
141 OPTIONS.close();
142 STANDARD.close();
143 try {
144 Thread.sleep(1000);
145 } catch (InterruptedException ie) {
146
147 }
148 super.tearDown();
149 }
150
151
152
153 void closeConnection(TelnetTestSimpleServer server, TelnetClient client, int port) {
154 if (server != null) {
155 server.disconnect();
156 server.stop();
157 }
158 try {
159 if (client != null) {
160 client.disconnect();
161 }
162 } catch (IOException e) {
163 System.err.println("failed to close client-server connection on port " + port);
164 System.err.println("ERROR in closeConnection(), "+ e.getMessage());
165 }
166
167 }
168
169
170
171
172 public void testInitial() throws Exception
173 {
174 boolean connect1_ok = false;
175 boolean connect2_ok = false;
176 boolean connect3_ok = false;
177 boolean init2_ok = false;
178 boolean add_invalid_ok1 = false;
179 boolean add_invalid_ok2 = false;
180 byte buffread2[] = new byte[9];
181 byte expected2[] =
182 {
183 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
184 (byte) TelnetOption.ECHO,
185 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
186 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
187 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
188 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
189 };
190
191 SimpleOptionHandler hand = new SimpleOptionHandler(550);
192 try
193 {
194 STANDARD.client.addOptionHandler(hand);
195 }
196 catch (Exception e)
197 {
198 add_invalid_ok1 = true;
199 }
200
201 try
202 {
203 OPTIONS.client.addOptionHandler(hand);
204 }
205 catch (Exception e)
206 {
207 add_invalid_ok2 = true;
208 }
209
210 InputStream is1 = STANDARD.server.getInputStream();
211 Thread.sleep(1000);
212 if(is1.available() == 0)
213 {
214 connect1_ok = true;
215 }
216
217 Thread.sleep(1000);
218 InputStream is2 = OPTIONS.server.getInputStream();
219 if(is2.available() == 9)
220 {
221 is2.read(buffread2);
222 connect2_ok = true;
223
224 if (equalBytes(buffread2, expected2)) {
225 init2_ok = true;
226 }
227 }
228
229 InputStream is3 = ANSI.server.getInputStream();
230 Thread.sleep(1000);
231 if(is3.available() == 0)
232 {
233 connect3_ok = true;
234 }
235
236
237 assertTrue(connect1_ok);
238 assertTrue(connect2_ok);
239 assertTrue(connect3_ok);
240 assertTrue(!STANDARD.client.getLocalOptionState(TelnetOption.ECHO));
241 assertTrue(!STANDARD.client.getRemoteOptionState(TelnetOption.ECHO));
242 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.ECHO));
243 assertTrue(!OPTIONS.client.getRemoteOptionState(TelnetOption.ECHO));
244 assertTrue(!ANSI.client.getLocalOptionState(TelnetOption.TERMINAL_TYPE));
245 assertTrue(!ANSI.client.getRemoteOptionState(TelnetOption.TERMINAL_TYPE));
246 assertTrue(init2_ok);
247 assertTrue(add_invalid_ok1);
248 assertTrue(add_invalid_ok2);
249 }
250
251
252
253
254 public void testOptionNegotiation() throws Exception
255 {
256 boolean negotiation1_ok = false;
257 byte buffread1[] = new byte[6];
258 byte send1[] =
259 {
260 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO, (byte) 15,
261 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL, (byte) 15,
262 };
263 byte expected1[] =
264 {
265 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT, (byte) 15,
266 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT, (byte) 15,
267 };
268
269 boolean negotiation2_ok = false;
270 byte buffread2[] = new byte[9];
271 byte send2[] =
272 {
273 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
274 (byte) TelnetOption.TERMINAL_TYPE,
275 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
276 (byte) TelnetOption.ECHO,
277 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
278 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
279 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
280 (byte) TelnetOption.SUPPRESS_GO_AHEAD
281 };
282 byte expected2[] =
283 {
284 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
285 (byte) TelnetOption.TERMINAL_TYPE,
286 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
287 (byte) TelnetOption.ECHO,
288 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
289 (byte) TelnetOption.SUPPRESS_GO_AHEAD
290 };
291
292 byte buffread2b[] = new byte[11];
293 byte send2b[] =
294 {
295 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SB,
296 (byte) TelnetOption.TERMINAL_TYPE,
297 (byte) 1, (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE,
298 };
299 byte expected2b[] =
300 {
301 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SB,
302 (byte) TelnetOption.TERMINAL_TYPE,
303 (byte) 0, (byte) 'V', (byte) 'T', (byte) '1', (byte) '0',
304 (byte) '0',
305 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE,
306 };
307
308 boolean negotiation3_ok = false;
309 byte buffread3[] = new byte[6];
310 byte send3[] =
311 {
312 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
313 (byte) TelnetOption.TERMINAL_TYPE,
314 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
315 (byte) TelnetOption.SUPPRESS_GO_AHEAD
316 };
317 byte expected3[] =
318 {
319 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
320 (byte) TelnetOption.TERMINAL_TYPE,
321 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
322 (byte) TelnetOption.SUPPRESS_GO_AHEAD
323 };
324 byte buffread3b[] = new byte[10];
325 byte send3b[] =
326 {
327 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SB,
328 (byte) TelnetOption.TERMINAL_TYPE,
329 (byte) 1, (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE,
330 };
331 byte expected3b[] =
332 {
333 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SB,
334 (byte) TelnetOption.TERMINAL_TYPE,
335 (byte) 0, (byte) 'A', (byte) 'N', (byte) 'S', (byte) 'I',
336 (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE,
337 };
338
339
340 InputStream is1 = STANDARD.server.getInputStream();
341 OutputStream os1 = STANDARD.server.getOutputStream();
342 is1.skip(is1.available());
343 os1.write(send1);
344 os1.flush();
345 Thread.sleep(1000);
346 if(is1.available() == 6)
347 {
348 is1.read(buffread1);
349
350 if (equalBytes(buffread1, expected1)) {
351 negotiation1_ok = true;
352 }
353 }
354
355 InputStream is2 = OPTIONS.server.getInputStream();
356 OutputStream os2 = OPTIONS.server.getOutputStream();
357 Thread.sleep(1000);
358 is2.skip(is2.available());
359 os2.write(send2);
360 os2.flush();
361 Thread.sleep(1000);
362 if(is2.available() == 9)
363 {
364 is2.read(buffread2);
365
366 if (equalBytes(buffread2, expected2)) {
367 negotiation2_ok = true;
368 }
369
370 if(negotiation2_ok)
371 {
372 negotiation2_ok = false;
373 os2.write(send2b);
374 os2.flush();
375 Thread.sleep(1000);
376 if(is2.available() == 11)
377 {
378 is2.read(buffread2b);
379
380 if (equalBytes(buffread2b, expected2b)) {
381 negotiation2_ok = true;
382 }
383 }
384 }
385 }
386
387 InputStream is3 = ANSI.server.getInputStream();
388 OutputStream os3 = ANSI.server.getOutputStream();
389 Thread.sleep(1000);
390 is3.skip(is3.available());
391 os3.write(send3);
392 os3.flush();
393 Thread.sleep(1000);
394 if(is3.available() == 6)
395 {
396 is3.read(buffread3);
397
398 if (equalBytes(buffread3, expected3)) {
399 negotiation3_ok = true;
400 }
401
402 if(negotiation3_ok)
403 {
404 negotiation3_ok = false;
405 os3.write(send3b);
406 os3.flush();
407 Thread.sleep(1000);
408 if(is3.available() == 10)
409 {
410 is3.read(buffread3b);
411 if (equalBytes(buffread3b, expected3b)) {
412 negotiation3_ok = true;
413 }
414 }
415 }
416 }
417
418 assertTrue(negotiation1_ok);
419 assertTrue(negotiation2_ok);
420 assertTrue(negotiation3_ok);
421 assertTrue(!STANDARD.client.getLocalOptionState(15));
422 assertTrue(!STANDARD.client.getRemoteOptionState(15));
423 assertTrue(!STANDARD.client.getLocalOptionState(TelnetOption.TERMINAL_TYPE));
424 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.ECHO));
425 assertTrue(!OPTIONS.client.getRemoteOptionState(TelnetOption.ECHO));
426 assertTrue(OPTIONS.client.getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
427 assertTrue(!OPTIONS.client.getRemoteOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
428 assertTrue(OPTIONS.client.getLocalOptionState(TelnetOption.TERMINAL_TYPE));
429 assertTrue(ANSI.client.getLocalOptionState(TelnetOption.TERMINAL_TYPE));
430 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.ECHO));
431 }
432
433
434
435
436
437 public void testOptionRenegotiation() throws Exception
438 {
439 boolean negotiation1_ok = false;
440
441 byte buffread[] = new byte[6];
442 byte send[] =
443 {
444 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
445 (byte) TelnetOption.ECHO,
446 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
447 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
448 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
449 (byte) TelnetOption.SUPPRESS_GO_AHEAD
450 };
451 byte expected[] =
452 {
453 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
454 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
455 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
456 (byte) TelnetOption.SUPPRESS_GO_AHEAD
457 };
458
459 byte buffread2[] = new byte[3];
460 byte send2[] =
461 {
462 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
463 (byte) TelnetOption.ECHO,
464 };
465 byte expected2[] =
466 {
467 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
468 (byte) TelnetOption.ECHO,
469 };
470
471
472 InputStream is = OPTIONS.server.getInputStream();
473 OutputStream os = OPTIONS.server.getOutputStream();
474 Thread.sleep(1000);
475 is.skip(is.available());
476 os.write(send);
477 os.flush();
478 Thread.sleep(1000);
479 if(is.available() == 6)
480 {
481 is.read(buffread);
482
483 if (equalBytes(buffread, expected)) {
484 negotiation1_ok = true;
485 }
486
487 if(negotiation1_ok)
488 {
489 negotiation1_ok = false;
490 os.write(send2);
491 os.flush();
492 Thread.sleep(1000);
493 if(is.available() == 3)
494 {
495 is.read(buffread2);
496 if (equalBytes(buffread2, expected2)) {
497 negotiation1_ok = true;
498 }
499 }
500 }
501 }
502
503 assertTrue(negotiation1_ok);
504 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.ECHO));
505 }
506
507
508
509
510 public void testNotification() throws Exception
511 {
512 byte buffread1[] = new byte[6];
513 byte send1[] =
514 {
515 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO, (byte) 15,
516 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL, (byte) 15,
517 };
518
519 byte buffread2[] = new byte[9];
520 byte send2[] =
521 {
522 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
523 (byte) TelnetOption.TERMINAL_TYPE,
524 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
525 (byte) TelnetOption.ECHO,
526 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
527 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
528 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
529 (byte) TelnetOption.SUPPRESS_GO_AHEAD
530 };
531
532 byte buffread2b[] = new byte[11];
533
534
535 numdo = 0;
536 numdont = 0;
537 numwill = 0;
538 numwont = 0;
539 OPTIONS.client.registerNotifHandler(this);
540
541 InputStream is1 = STANDARD.server.getInputStream();
542 OutputStream os1 = STANDARD.server.getOutputStream();
543 is1.skip(is1.available());
544 os1.write(send1);
545 os1.flush();
546 Thread.sleep(500);
547 if(is1.available() > 0)
548 {
549 is1.read(buffread1);
550 }
551
552 InputStream is2 = OPTIONS.server.getInputStream();
553 OutputStream os2 = OPTIONS.server.getOutputStream();
554 Thread.sleep(500);
555 is2.skip(is2.available());
556 os2.write(send2);
557 os2.flush();
558 Thread.sleep(500);
559 if(is2.available() > 0)
560 {
561 is2.read(buffread2);
562 Thread.sleep(1000);
563 if(is2.available() > 0)
564 {
565 is2.read(buffread2b);
566 }
567 }
568
569
570 assertEquals(2, numdo);
571 assertEquals(1, numdont);
572 assertEquals(1, numwont);
573 assertEquals(0, numwill);
574 }
575
576
577
578
579
580 public void testDeleteOptionHandler() throws Exception
581 {
582 boolean remove_ok = false;
583 boolean remove_invalid_ok1 = false;
584 boolean remove_invalid_ok2 = false;
585
586 byte buffread[] = new byte[6];
587 byte send[] =
588 {
589 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
590 (byte) TelnetOption.ECHO,
591 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
592 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
593 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
594 (byte) TelnetOption.SUPPRESS_GO_AHEAD
595 };
596
597 byte expected[] =
598 {
599 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
600 (byte) TelnetOption.SUPPRESS_GO_AHEAD,
601 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
602 (byte) TelnetOption.SUPPRESS_GO_AHEAD
603 };
604
605 InputStream is = OPTIONS.server.getInputStream();
606 OutputStream os = OPTIONS.server.getOutputStream();
607 Thread.sleep(1000);
608 is.skip(is.available());
609 os.write(send);
610 os.flush();
611 Thread.sleep(1000);
612 if(is.available() == 0)
613 {
614 OPTIONS.client.deleteOptionHandler(TelnetOption.SUPPRESS_GO_AHEAD);
615 Thread.sleep(1000);
616 if(is.available() == 6)
617 {
618 is.read(buffread);
619 if (equalBytes(buffread, expected)) {
620 remove_ok = true;
621 }
622 }
623 }
624
625 try
626 {
627 OPTIONS.client.deleteOptionHandler(TelnetOption.SUPPRESS_GO_AHEAD);
628 }
629 catch (Exception e)
630 {
631 remove_invalid_ok1 = true;
632 }
633
634 try
635 {
636 OPTIONS.client.deleteOptionHandler(550);
637 }
638 catch (Exception e)
639 {
640 remove_invalid_ok2 = true;
641 }
642
643 assertTrue(remove_ok);
644 assertTrue(remove_invalid_ok1);
645 assertTrue(remove_invalid_ok2);
646 assertTrue(OPTIONS.client.getLocalOptionState(TelnetOption.ECHO));
647 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
648 assertTrue(!OPTIONS.client.getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
649 }
650
651
652
653
654
655 public void testAYT() throws Exception
656 {
657 boolean ayt_true_ok = false;
658 boolean ayt_false_ok = false;
659
660
661 byte AYT[] = { (byte) TelnetCommand.IAC, (byte) TelnetCommand.AYT };
662 byte response[] =
663 { (byte) '[', (byte) 'Y', (byte) 'e', (byte) 's', (byte) ']' };
664 String inputs[] = new String[1];
665 String outputs[] = new String[1];
666 inputs[0] = new String (AYT);
667 outputs[0] = new String (response);
668
669
670 OutputStream os = ANSI.server.getOutputStream();
671 InputStream is = ANSI.server.getInputStream();
672 TelnetTestResponder tr =
673 new TelnetTestResponder(is, os, inputs, outputs, 30000);
674 assertNotNull(tr);
675 boolean res1 = ANSI.client.sendAYT(2000);
676
677 if (res1 == true) {
678 ayt_true_ok=true;
679 }
680
681 Thread.sleep(1000);
682 is.skip(is.available());
683
684 boolean res2 = ANSI.client.sendAYT(2000);
685
686 if (res2 == false) {
687 ayt_false_ok=true;
688 }
689
690
691 assertTrue(ayt_true_ok);
692 assertTrue(ayt_false_ok);
693 }
694
695
696
697
698 public void testSpy() throws Exception
699 {
700 boolean test1spy_ok = false;
701 boolean test2spy_ok = false;
702 boolean stopspy_ok = false;
703 byte expected1[] =
704 { (byte) 't', (byte) 'e', (byte) 's', (byte) 't', (byte) '1' };
705 byte expected2[] =
706 { (byte) 't', (byte) 'e', (byte) 's', (byte) 't', (byte) '2' };
707
708
709 PipedOutputStream po = new PipedOutputStream();
710 PipedInputStream pi = new PipedInputStream(po);
711
712 OutputStream os = STANDARD.server.getOutputStream();
713 OutputStream ostc = STANDARD.client.getOutputStream();
714
715 STANDARD.client.registerSpyStream(po);
716
717 os.write("test1".getBytes());
718 os.flush();
719
720 Thread.sleep(1000);
721 byte buffer[] = new byte[5];
722
723 if(pi.available() == 5)
724 {
725 pi.read(buffer);
726 if (equalBytes(buffer, expected1)) {
727 test1spy_ok = true;
728 }
729 }
730
731 ostc.write("test2".getBytes());
732 ostc.flush();
733
734 Thread.sleep(1000);
735
736 if(pi.available() == 5)
737 {
738 pi.read(buffer);
739 if (equalBytes(buffer, expected2)) {
740 test2spy_ok = true;
741 }
742 }
743
744 STANDARD.client.stopSpyStream();
745 os.write("test1".getBytes());
746 os.flush();
747 ostc.write("test2".getBytes());
748 ostc.flush();
749 Thread.sleep(1000);
750 if(pi.available() == 0)
751 {
752 stopspy_ok = true;
753 }
754
755
756 assertTrue(test1spy_ok);
757 assertTrue(test2spy_ok);
758 assertTrue(stopspy_ok);
759 pi.close();
760 }
761
762
763
764
765 public void testSetReaderThread() throws Exception
766 {
767 boolean negotiation1_ok = false;
768 boolean negotiation2_ok = false;
769 boolean read_ok = false;
770 byte buffread1[] = new byte[6];
771 byte send1[] =
772 {
773 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO, (byte) 15,
774 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL, (byte) 15,
775 };
776 byte expected1[] =
777 {
778 (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT, (byte) 15,
779 (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT, (byte) 15,
780 };
781
782
783 InputStream is1 = NOREAD.server.getInputStream();
784 OutputStream os1 = NOREAD.server.getOutputStream();
785 is1.skip(is1.available());
786 os1.write(send1);
787 os1.flush();
788 os1.write("A".getBytes());
789 os1.flush();
790 Thread.sleep(1000);
791 InputStream instr = NOREAD.client.getInputStream();
792 byte[] buff = new byte[4];
793 int ret_read = 0;
794
795 ret_read = instr.read(buff);
796 if((ret_read == 1) && (buff[0] == 'A'))
797 {
798 read_ok = true;
799 }
800
801
802
803 int read = 0;
804 int pos = 0;
805
806 byte[] tmp = new byte[16];
807 while ( pos < 5 ) {
808 read = is1.read(tmp);
809 System.arraycopy(tmp, 0, buffread1, pos, read);
810 pos+=read;
811 }
812
813 if (equalBytes(buffread1, expected1)) {
814 negotiation1_ok = true;
815
816 }
817
818
819 InputStream is2 = STANDARD.server.getInputStream();
820 OutputStream os2 = STANDARD.server.getOutputStream();
821 Thread.sleep(1000);
822 is2.skip(is2.available());
823 os2.write(send1);
824 os2.flush();
825 Thread.sleep(1000);
826
827 tmp = new byte[16];
828 while ( pos < 5 ) {
829 read = is2.read(tmp);
830 System.arraycopy(tmp, 0, buffread1, pos, read);
831 pos+=read;
832 }
833
834
835 is2.read(buffread1);
836
837 if (equalBytes(buffread1, expected1)) {
838 negotiation2_ok = true;
839
840 }
841
842 assertTrue(!NOREAD.client.getReaderThread());
843 assertTrue(STANDARD.client.getReaderThread());
844 assertTrue("Expected read_ok to be true, got " + read_ok, read_ok);
845 assertTrue("Expected negotiation1_ok to be true, got " + negotiation1_ok, negotiation1_ok);
846 assertTrue("Expected negotiation2_ok to be true, got " + negotiation2_ok, negotiation2_ok);
847 }
848
849
850
851
852
853 protected boolean equalBytes(byte a1[], byte a2[])
854 {
855 if(a1.length != a2.length)
856 {
857 return(false);
858 }
859 else
860 {
861 boolean result = true;
862 for(int ii=0; ii<a1.length; ii++)
863 {
864
865 if (a1[ii]!= a2[ii]) {
866 result = false;
867 }
868 }
869 return(result);
870 }
871 }
872
873
874
875
876
877
878
879
880
881
882
883
884 public void receivedNegotiation(int negotiation_code, int option_code)
885 {
886 if(negotiation_code == TelnetNotificationHandler.RECEIVED_DO)
887 {
888 numdo++;
889 }
890 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_DONT)
891 {
892 numdont++;
893 }
894 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WILL)
895 {
896 numwill++;
897 }
898 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WONT)
899 {
900 numwont++;
901 }
902 }
903
904 }