Source Code Cross Referenced for TelnetClientTest.java in  » Net » Apache-commons-net-1.4.1 » org » apache » commons » net » telnet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache commons net 1.4.1 » org.apache.commons.net.telnet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-2004 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.net.telnet;
017:
018:        import junit.framework.TestCase;
019:
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:        import java.io.OutputStream;
023:        import java.io.PipedInputStream;
024:        import java.io.PipedOutputStream;
025:
026:        /***
027:         * JUnit test class for TelnetClient.s
028:         * Implements protocol compliance tests
029:         * <p>
030:         * @author Bruno D'Avanzo
031:         ***/
032:        public class TelnetClientTest extends TestCase implements 
033:                TelnetNotificationHandler {
034:            /**
035:             * Handy holder to hold both sides of the connection
036:             * used in testing for clarity.
037:             */
038:            private class TestConnection {
039:                TelnetTestSimpleServer server;
040:                TelnetClient client;
041:                int port;
042:
043:                TestConnection(TelnetTestSimpleServer server,
044:                        TelnetClient client, int port) {
045:                    this .server = server;
046:                    this .client = client;
047:                    this .port = port;
048:                }
049:
050:                protected void close() {
051:                    TelnetClientTest.this .closeConnection(this .server,
052:                            this .client, this .port);
053:                }
054:            }
055:
056:            // four connections with different properties
057:            // to use in tests.
058:            private TestConnection STANDARD;
059:            private TestConnection OPTIONS;
060:            private TestConnection ANSI;
061:            private TestConnection NOREAD;
062:
063:            private int NUM_CONNECTIONS = 4;
064:
065:            protected int numdo = 0;
066:            protected int numdont = 0;
067:            protected int numwill = 0;
068:            protected int numwont = 0;
069:
070:            /***
071:             * main for running the test.
072:             ***/
073:            public static void main(String args[]) {
074:                junit.textui.TestRunner.run(TelnetClientTest.class);
075:            }
076:
077:            /***
078:             * open connections needed for the tests for the test.
079:             ***/
080:            protected void setUp() throws Exception {
081:                super .setUp();
082:                for (int port = 3333, socket = 0; socket < NUM_CONNECTIONS
083:                        && port < 4000; port++) {
084:                    TelnetTestSimpleServer server = null;
085:                    TelnetClient client = null;
086:                    try {
087:                        server = new TelnetTestSimpleServer(port);
088:                        switch (socket) {
089:                        case 0:
090:                            client = new TelnetClient();
091:                            // redundant but makes code clearer.
092:                            client.setReaderThread(true);
093:                            break;
094:                        case 1:
095:                            client = new TelnetClient();
096:                            TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
097:                                    "VT100", false, false, true, false);
098:                            EchoOptionHandler echoopt = new EchoOptionHandler(
099:                                    true, false, true, false);
100:                            SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(
101:                                    true, true, true, true);
102:
103:                            client.addOptionHandler(ttopt);
104:                            client.addOptionHandler(echoopt);
105:                            client.addOptionHandler(gaopt);
106:                            break;
107:                        case 2:
108:                            client = new TelnetClient("ANSI");
109:                            break;
110:                        case 3:
111:                            client = new TelnetClient();
112:                            client.setReaderThread(false);
113:                            break;
114:                        }
115:                        client.connect("127.0.0.1", port);
116:                        switch (socket) {
117:                        case 0:
118:                            STANDARD = new TestConnection(server, client, port);
119:                            break;
120:                        case 1:
121:                            OPTIONS = new TestConnection(server, client, port);
122:                            break;
123:                        case 2:
124:                            ANSI = new TestConnection(server, client, port);
125:                            break;
126:                        case 3:
127:                            NOREAD = new TestConnection(server, client, port);
128:                            break;
129:
130:                        }
131:
132:                        // only increment socket number on success
133:                        socket++;
134:                    } catch (IOException e) {
135:                        closeConnection(server, client, port);
136:                        System.err
137:                                .println("failed to open client-server connection on port "
138:                                        + port);
139:                    }
140:                }
141:                Thread.sleep(1000);
142:            }
143:
144:            /* 
145:             * @throws java.lang.Exception
146:             */
147:            protected void tearDown() throws Exception {
148:                NOREAD.close();
149:                ANSI.close();
150:                OPTIONS.close();
151:                STANDARD.close();
152:                try {
153:                    Thread.sleep(1000);
154:                } catch (InterruptedException ie) {
155:                    //do nothing
156:                }
157:                super .tearDown();
158:            }
159:
160:            void closeConnection(TelnetTestSimpleServer server,
161:                    TelnetClient client, int port) {
162:                if (server != null) {
163:                    server.disconnect();
164:                    server.stop();
165:                }
166:                try {
167:                    if (client != null) {
168:                        client.disconnect();
169:                    }
170:                } catch (IOException e) {
171:                    System.err
172:                            .println("failed to close client-server connection on port "
173:                                    + port);
174:                    System.err.println("ERROR in closeConnection(), "
175:                            + e.getMessage());
176:                }
177:
178:            }
179:
180:            /***
181:             * tests the initial condition of the sessions
182:             ***/
183:            public void testInitial() throws Exception {
184:                boolean connect1_ok = false;
185:                boolean connect2_ok = false;
186:                boolean connect3_ok = false;
187:                boolean init2_ok = false;
188:                boolean add_invalid_ok1 = false;
189:                boolean add_invalid_ok2 = false;
190:                byte buffread2[] = new byte[9];
191:                byte expected2[] = { (byte) TelnetCommand.IAC,
192:                        (byte) TelnetCommand.WILL, (byte) TelnetOption.ECHO,
193:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
194:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
195:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
196:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD, };
197:
198:                SimpleOptionHandler hand = new SimpleOptionHandler(550);
199:                try {
200:                    STANDARD.client.addOptionHandler(hand);
201:                } catch (Exception e) {
202:                    add_invalid_ok1 = true;
203:                }
204:
205:                try {
206:                    OPTIONS.client.addOptionHandler(hand);
207:                } catch (Exception e) {
208:                    add_invalid_ok2 = true;
209:                }
210:
211:                InputStream is1 = STANDARD.server.getInputStream();
212:                Thread.sleep(1000);
213:                if (is1.available() == 0) {
214:                    connect1_ok = true;
215:                }
216:
217:                Thread.sleep(1000);
218:                InputStream is2 = OPTIONS.server.getInputStream();
219:                if (is2.available() == 9) {
220:                    is2.read(buffread2);
221:                    connect2_ok = true;
222:
223:                    if (equalBytes(buffread2, expected2))
224:                        init2_ok = true;
225:                }
226:
227:                InputStream is3 = ANSI.server.getInputStream();
228:                Thread.sleep(1000);
229:                if (is3.available() == 0) {
230:                    connect3_ok = true;
231:                }
232:
233:                assertTrue(connect1_ok);
234:                assertTrue(connect2_ok);
235:                assertTrue(connect3_ok);
236:                assertTrue(!STANDARD.client
237:                        .getLocalOptionState(TelnetOption.ECHO));
238:                assertTrue(!STANDARD.client
239:                        .getRemoteOptionState(TelnetOption.ECHO));
240:                assertTrue(!OPTIONS.client
241:                        .getLocalOptionState(TelnetOption.ECHO));
242:                assertTrue(!OPTIONS.client
243:                        .getRemoteOptionState(TelnetOption.ECHO));
244:                assertTrue(!ANSI.client
245:                        .getLocalOptionState(TelnetOption.TERMINAL_TYPE));
246:                assertTrue(!ANSI.client
247:                        .getRemoteOptionState(TelnetOption.TERMINAL_TYPE));
248:                assertTrue(init2_ok);
249:                assertTrue(add_invalid_ok1);
250:                assertTrue(add_invalid_ok2);
251:            }
252:
253:            /***
254:             * protocol compliance test for option negotiation
255:             ***/
256:            public void testOptionNegotiation() throws Exception {
257:                boolean negotiation1_ok = false;
258:                byte buffread1[] = new byte[6];
259:                byte send1[] = { (byte) TelnetCommand.IAC,
260:                        (byte) TelnetCommand.DO, (byte) 15,
261:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
262:                        (byte) 15, };
263:                byte expected1[] = { (byte) TelnetCommand.IAC,
264:                        (byte) TelnetCommand.WONT, (byte) 15,
265:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
266:                        (byte) 15, };
267:
268:                boolean negotiation2_ok = false;
269:                byte buffread2[] = new byte[9];
270:                byte send2[] = { (byte) TelnetCommand.IAC,
271:                        (byte) TelnetCommand.DO,
272:                        (byte) TelnetOption.TERMINAL_TYPE,
273:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
274:                        (byte) TelnetOption.ECHO, (byte) TelnetCommand.IAC,
275:                        (byte) TelnetCommand.DO,
276:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
277:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
278:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
279:                byte expected2[] = { (byte) TelnetCommand.IAC,
280:                        (byte) TelnetCommand.WILL,
281:                        (byte) TelnetOption.TERMINAL_TYPE,
282:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
283:                        (byte) TelnetOption.ECHO, (byte) TelnetCommand.IAC,
284:                        (byte) TelnetCommand.DONT,
285:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
286:
287:                byte buffread2b[] = new byte[11];
288:                byte send2b[] = { (byte) TelnetCommand.IAC,
289:                        (byte) TelnetCommand.SB,
290:                        (byte) TelnetOption.TERMINAL_TYPE, (byte) 1,
291:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE, };
292:                byte expected2b[] = { (byte) TelnetCommand.IAC,
293:                        (byte) TelnetCommand.SB,
294:                        (byte) TelnetOption.TERMINAL_TYPE, (byte) 0,
295:                        (byte) 'V', (byte) 'T', (byte) '1', (byte) '0',
296:                        (byte) '0', (byte) TelnetCommand.IAC,
297:                        (byte) TelnetCommand.SE, };
298:
299:                boolean negotiation3_ok = false;
300:                byte buffread3[] = new byte[6];
301:                byte send3[] = { (byte) TelnetCommand.IAC,
302:                        (byte) TelnetCommand.DO,
303:                        (byte) TelnetOption.TERMINAL_TYPE,
304:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
305:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
306:                byte expected3[] = { (byte) TelnetCommand.IAC,
307:                        (byte) TelnetCommand.WILL,
308:                        (byte) TelnetOption.TERMINAL_TYPE,
309:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
310:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
311:                byte buffread3b[] = new byte[10];
312:                byte send3b[] = { (byte) TelnetCommand.IAC,
313:                        (byte) TelnetCommand.SB,
314:                        (byte) TelnetOption.TERMINAL_TYPE, (byte) 1,
315:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE, };
316:                byte expected3b[] = { (byte) TelnetCommand.IAC,
317:                        (byte) TelnetCommand.SB,
318:                        (byte) TelnetOption.TERMINAL_TYPE, (byte) 0,
319:                        (byte) 'A', (byte) 'N', (byte) 'S', (byte) 'I',
320:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.SE, };
321:
322:                InputStream is1 = STANDARD.server.getInputStream();
323:                OutputStream os1 = STANDARD.server.getOutputStream();
324:                is1.skip(is1.available());
325:                os1.write(send1);
326:                os1.flush();
327:                Thread.sleep(1000);
328:                if (is1.available() == 6) {
329:                    is1.read(buffread1);
330:
331:                    if (equalBytes(buffread1, expected1))
332:                        negotiation1_ok = true;
333:                }
334:
335:                InputStream is2 = OPTIONS.server.getInputStream();
336:                OutputStream os2 = OPTIONS.server.getOutputStream();
337:                Thread.sleep(1000);
338:                is2.skip(is2.available());
339:                os2.write(send2);
340:                os2.flush();
341:                Thread.sleep(1000);
342:                if (is2.available() == 9) {
343:                    is2.read(buffread2);
344:
345:                    if (equalBytes(buffread2, expected2))
346:                        negotiation2_ok = true;
347:
348:                    if (negotiation2_ok) {
349:                        negotiation2_ok = false;
350:                        os2.write(send2b);
351:                        os2.flush();
352:                        Thread.sleep(1000);
353:                        if (is2.available() == 11) {
354:                            is2.read(buffread2b);
355:
356:                            if (equalBytes(buffread2b, expected2b))
357:                                negotiation2_ok = true;
358:                        }
359:                    }
360:                }
361:
362:                InputStream is3 = ANSI.server.getInputStream();
363:                OutputStream os3 = ANSI.server.getOutputStream();
364:                Thread.sleep(1000);
365:                is3.skip(is3.available());
366:                os3.write(send3);
367:                os3.flush();
368:                Thread.sleep(1000);
369:                if (is3.available() == 6) {
370:                    is3.read(buffread3);
371:
372:                    if (equalBytes(buffread3, expected3))
373:                        negotiation3_ok = true;
374:
375:                    if (negotiation3_ok) {
376:                        negotiation3_ok = false;
377:                        os3.write(send3b);
378:                        os3.flush();
379:                        Thread.sleep(1000);
380:                        if (is3.available() == 10) {
381:                            is3.read(buffread3b);
382:                            if (equalBytes(buffread3b, expected3b))
383:                                negotiation3_ok = true;
384:                        }
385:                    }
386:                }
387:
388:                assertTrue(negotiation1_ok);
389:                assertTrue(negotiation2_ok);
390:                assertTrue(negotiation3_ok);
391:                assertTrue(!STANDARD.client.getLocalOptionState(15));
392:                assertTrue(!STANDARD.client.getRemoteOptionState(15));
393:                assertTrue(!STANDARD.client
394:                        .getLocalOptionState(TelnetOption.TERMINAL_TYPE));
395:                assertTrue(!OPTIONS.client
396:                        .getLocalOptionState(TelnetOption.ECHO));
397:                assertTrue(!OPTIONS.client
398:                        .getRemoteOptionState(TelnetOption.ECHO));
399:                assertTrue(OPTIONS.client
400:                        .getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
401:                assertTrue(!OPTIONS.client
402:                        .getRemoteOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
403:                assertTrue(OPTIONS.client
404:                        .getLocalOptionState(TelnetOption.TERMINAL_TYPE));
405:                assertTrue(ANSI.client
406:                        .getLocalOptionState(TelnetOption.TERMINAL_TYPE));
407:                assertTrue(!OPTIONS.client
408:                        .getLocalOptionState(TelnetOption.ECHO));
409:            }
410:
411:            /***
412:             * protocol compliance test for option renegotiation
413:             ***/
414:            public void testOptionRenegotiation() throws Exception {
415:                boolean negotiation1_ok = false;
416:
417:                byte buffread[] = new byte[6];
418:                byte send[] = { (byte) TelnetCommand.IAC,
419:                        (byte) TelnetCommand.DO, (byte) TelnetOption.ECHO,
420:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
421:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
422:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
423:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
424:                byte expected[] = { (byte) TelnetCommand.IAC,
425:                        (byte) TelnetCommand.WONT,
426:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
427:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
428:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
429:
430:                byte buffread2[] = new byte[3];
431:                byte send2[] = { (byte) TelnetCommand.IAC,
432:                        (byte) TelnetCommand.DONT, (byte) TelnetOption.ECHO, };
433:                byte expected2[] = { (byte) TelnetCommand.IAC,
434:                        (byte) TelnetCommand.WONT, (byte) TelnetOption.ECHO, };
435:
436:                InputStream is = OPTIONS.server.getInputStream();
437:                OutputStream os = OPTIONS.server.getOutputStream();
438:                Thread.sleep(1000);
439:                is.skip(is.available());
440:                os.write(send);
441:                os.flush();
442:                Thread.sleep(1000);
443:                if (is.available() == 6) {
444:                    is.read(buffread);
445:
446:                    if (equalBytes(buffread, expected))
447:                        negotiation1_ok = true;
448:
449:                    if (negotiation1_ok) {
450:                        negotiation1_ok = false;
451:                        os.write(send2);
452:                        os.flush();
453:                        Thread.sleep(1000);
454:                        if (is.available() == 3) {
455:                            is.read(buffread2);
456:                            if (equalBytes(buffread2, expected2))
457:                                negotiation1_ok = true;
458:                        }
459:                    }
460:                }
461:
462:                assertTrue(negotiation1_ok);
463:                assertTrue(!OPTIONS.client
464:                        .getLocalOptionState(TelnetOption.ECHO));
465:            }
466:
467:            /***
468:             * test of option negotiation notification
469:             ***/
470:            public void testNotification() throws Exception {
471:                byte buffread1[] = new byte[6];
472:                byte send1[] = { (byte) TelnetCommand.IAC,
473:                        (byte) TelnetCommand.DO, (byte) 15,
474:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
475:                        (byte) 15, };
476:
477:                byte buffread2[] = new byte[9];
478:                byte send2[] = { (byte) TelnetCommand.IAC,
479:                        (byte) TelnetCommand.DO,
480:                        (byte) TelnetOption.TERMINAL_TYPE,
481:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
482:                        (byte) TelnetOption.ECHO, (byte) TelnetCommand.IAC,
483:                        (byte) TelnetCommand.DO,
484:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
485:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WONT,
486:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
487:
488:                byte buffread2b[] = new byte[11];
489:
490:                numdo = 0;
491:                numdont = 0;
492:                numwill = 0;
493:                numwont = 0;
494:                OPTIONS.client.registerNotifHandler(this );
495:
496:                InputStream is1 = STANDARD.server.getInputStream();
497:                OutputStream os1 = STANDARD.server.getOutputStream();
498:                is1.skip(is1.available());
499:                os1.write(send1);
500:                os1.flush();
501:                Thread.sleep(500);
502:                if (is1.available() > 0) {
503:                    is1.read(buffread1);
504:                }
505:
506:                InputStream is2 = OPTIONS.server.getInputStream();
507:                OutputStream os2 = OPTIONS.server.getOutputStream();
508:                Thread.sleep(500);
509:                is2.skip(is2.available());
510:                os2.write(send2);
511:                os2.flush();
512:                Thread.sleep(500);
513:                if (is2.available() > 0) {
514:                    is2.read(buffread2);
515:                    Thread.sleep(1000);
516:                    if (is2.available() > 0) {
517:                        is2.read(buffread2b);
518:                    }
519:                }
520:
521:                assertTrue(numdo == 2);
522:                assertTrue(numdont == 1);
523:                assertTrue(numwont == 1);
524:                assertTrue(numwill == 0);
525:            }
526:
527:            /***
528:             * protocol compliance test in case of option handler removal
529:             ***/
530:            public void testDeleteOptionHandler() throws Exception {
531:                boolean remove_ok = false;
532:                boolean remove_invalid_ok1 = false;
533:                boolean remove_invalid_ok2 = false;
534:
535:                byte buffread[] = new byte[6];
536:                byte send[] = { (byte) TelnetCommand.IAC,
537:                        (byte) TelnetCommand.DO, (byte) TelnetOption.ECHO,
538:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DO,
539:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
540:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
541:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
542:
543:                byte expected[] = { (byte) TelnetCommand.IAC,
544:                        (byte) TelnetCommand.WONT,
545:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD,
546:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
547:                        (byte) TelnetOption.SUPPRESS_GO_AHEAD };
548:
549:                InputStream is = OPTIONS.server.getInputStream();
550:                OutputStream os = OPTIONS.server.getOutputStream();
551:                Thread.sleep(1000);
552:                is.skip(is.available());
553:                os.write(send);
554:                os.flush();
555:                Thread.sleep(1000);
556:                if (is.available() == 0) {
557:                    OPTIONS.client
558:                            .deleteOptionHandler(TelnetOption.SUPPRESS_GO_AHEAD);
559:                    Thread.sleep(1000);
560:                    if (is.available() == 6) {
561:                        is.read(buffread);
562:                        if (equalBytes(buffread, expected))
563:                            remove_ok = true;
564:                    }
565:                }
566:
567:                try {
568:                    OPTIONS.client
569:                            .deleteOptionHandler(TelnetOption.SUPPRESS_GO_AHEAD);
570:                } catch (Exception e) {
571:                    remove_invalid_ok1 = true;
572:                }
573:
574:                try {
575:                    OPTIONS.client.deleteOptionHandler(550);
576:                } catch (Exception e) {
577:                    remove_invalid_ok2 = true;
578:                }
579:
580:                assertTrue(remove_ok);
581:                assertTrue(remove_invalid_ok1);
582:                assertTrue(remove_invalid_ok2);
583:                assertTrue(OPTIONS.client
584:                        .getLocalOptionState(TelnetOption.ECHO));
585:                assertTrue(!OPTIONS.client
586:                        .getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
587:                assertTrue(!OPTIONS.client
588:                        .getLocalOptionState(TelnetOption.SUPPRESS_GO_AHEAD));
589:            }
590:
591:            /***
592:             * test of AYT functionality
593:             ***/
594:            public void testAYT() throws Exception {
595:                boolean ayt_true_ok = false;
596:                boolean ayt_false_ok = false;
597:
598:                byte AYT[] = { (byte) TelnetCommand.IAC,
599:                        (byte) TelnetCommand.AYT };
600:                byte response[] = { (byte) '[', (byte) 'Y', (byte) 'e',
601:                        (byte) 's', (byte) ']' };
602:                String inputs[] = new String[1];
603:                String outputs[] = new String[1];
604:                inputs[0] = new String(AYT);
605:                outputs[0] = new String(response);
606:
607:                OutputStream os = ANSI.server.getOutputStream();
608:                InputStream is = ANSI.server.getInputStream();
609:                TelnetTestResponder tr = new TelnetTestResponder(is, os,
610:                        inputs, outputs, 30000);
611:                assertNotNull(tr);
612:                boolean res1 = ANSI.client.sendAYT(2000);
613:
614:                if (res1 == true)
615:                    ayt_true_ok = true;
616:
617:                Thread.sleep(1000);
618:                is.skip(is.available());
619:
620:                boolean res2 = ANSI.client.sendAYT(2000);
621:
622:                if (res2 == false)
623:                    ayt_false_ok = true;
624:
625:                assertTrue(ayt_true_ok);
626:                assertTrue(ayt_false_ok);
627:            }
628:
629:            /***
630:             * test of Spy functionality
631:             ***/
632:            public void testSpy() throws Exception {
633:                boolean test1spy_ok = false;
634:                boolean test2spy_ok = false;
635:                boolean stopspy_ok = false;
636:                byte expected1[] = { (byte) 't', (byte) 'e', (byte) 's',
637:                        (byte) 't', (byte) '1' };
638:                byte expected2[] = { (byte) 't', (byte) 'e', (byte) 's',
639:                        (byte) 't', (byte) '2' };
640:
641:                PipedOutputStream po = new PipedOutputStream();
642:                PipedInputStream pi = new PipedInputStream(po);
643:
644:                OutputStream os = STANDARD.server.getOutputStream();
645:                OutputStream ostc = STANDARD.client.getOutputStream();
646:
647:                STANDARD.client.registerSpyStream(po);
648:
649:                os.write("test1".getBytes());
650:                os.flush();
651:
652:                Thread.sleep(1000);
653:                byte buffer[] = new byte[5];
654:
655:                if (pi.available() == 5) {
656:                    pi.read(buffer);
657:                    if (equalBytes(buffer, expected1))
658:                        test1spy_ok = true;
659:                }
660:
661:                ostc.write("test2".getBytes());
662:                ostc.flush();
663:
664:                Thread.sleep(1000);
665:
666:                if (pi.available() == 5) {
667:                    pi.read(buffer);
668:                    if (equalBytes(buffer, expected2))
669:                        test2spy_ok = true;
670:                }
671:
672:                STANDARD.client.stopSpyStream();
673:                os.write("test1".getBytes());
674:                os.flush();
675:                ostc.write("test2".getBytes());
676:                ostc.flush();
677:                Thread.sleep(1000);
678:                if (pi.available() == 0) {
679:                    stopspy_ok = true;
680:                }
681:
682:                assertTrue(test1spy_ok);
683:                assertTrue(test2spy_ok);
684:                assertTrue(stopspy_ok);
685:            }
686:
687:            /***
688:             * test of setReaderThread
689:             ***/
690:            public void testSetReaderThread() throws Exception {
691:                boolean negotiation1_ok = false;
692:                boolean negotiation2_ok = false;
693:                boolean read_ok = false;
694:                byte buffread1[] = new byte[6];
695:                byte send1[] = { (byte) TelnetCommand.IAC,
696:                        (byte) TelnetCommand.DO, (byte) 15,
697:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.WILL,
698:                        (byte) 15, };
699:                byte expected1[] = { (byte) TelnetCommand.IAC,
700:                        (byte) TelnetCommand.WONT, (byte) 15,
701:                        (byte) TelnetCommand.IAC, (byte) TelnetCommand.DONT,
702:                        (byte) 15, };
703:
704:                InputStream is1 = NOREAD.server.getInputStream();
705:                OutputStream os1 = NOREAD.server.getOutputStream();
706:                is1.skip(is1.available());
707:                os1.write(send1);
708:                os1.flush();
709:                os1.write("A".getBytes());
710:                os1.flush();
711:                Thread.sleep(1000);
712:                InputStream instr = NOREAD.client.getInputStream();
713:                byte[] buff = new byte[4];
714:                int ret_read = 0;
715:
716:                ret_read = instr.read(buff);
717:                if ((ret_read == 1) && (buff[0] == 'A')) {
718:                    read_ok = true;
719:                }
720:
721:                // if(is1.available() == 6)
722:                //{
723:                is1.read(buffread1);
724:
725:                if (equalBytes(buffread1, expected1))
726:                    negotiation1_ok = true;
727:                //}
728:
729:                InputStream is2 = STANDARD.server.getInputStream();
730:                OutputStream os2 = STANDARD.server.getOutputStream();
731:                Thread.sleep(1000);
732:                is2.skip(is2.available());
733:                os2.write(send1);
734:                os2.flush();
735:                Thread.sleep(1000);
736:                //if(is2.available() == 6)
737:                //{
738:                is2.read(buffread1);
739:
740:                if (equalBytes(buffread1, expected1))
741:                    negotiation2_ok = true;
742:                //}
743:
744:                assertTrue(!NOREAD.client.getReaderThread());
745:                assertTrue(STANDARD.client.getReaderThread());
746:                assertTrue("Expected read_ok to be true, got " + read_ok,
747:                        read_ok);
748:                assertTrue("Expected negotiation1_ok to be true, got "
749:                        + negotiation1_ok, negotiation1_ok);
750:                assertTrue("Expected negotiation2_ok to be true, got "
751:                        + negotiation2_ok, negotiation2_ok);
752:            }
753:
754:            /***
755:             * Helper method. compares two arrays of int
756:             ***/
757:            protected boolean equalBytes(byte a1[], byte a2[]) {
758:                if (a1.length != a2.length) {
759:                    return (false);
760:                } else {
761:                    boolean result = true;
762:                    for (int ii = 0; ii < a1.length; ii++) {
763:
764:                        if (a1[ii] != a2[ii])
765:                            result = false;
766:                    }
767:                    return (result);
768:                }
769:            }
770:
771:            /***
772:             * Callback method called when TelnetClient receives an option
773:             * negotiation command.
774:             * <p>
775:             * @param negotiation_code - type of negotiation command received
776:             * (RECEIVED_DO, RECEIVED_DONT, RECEIVED_WILL, RECEIVED_WONT)
777:             * <p>
778:             * @param option_code - code of the option negotiated
779:             * <p>
780:             ***/
781:            public void receivedNegotiation(int negotiation_code,
782:                    int option_code) {
783:                if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
784:                    numdo++;
785:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
786:                    numdont++;
787:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
788:                    numwill++;
789:                } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {
790:                    numwont++;
791:                }
792:            }
793:
794:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.