Source Code Cross Referenced for SSLSocketFactoriesTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » xnet » provider » jsse » 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 » Apache Harmony Java SE » org package » org.apache.harmony.xnet.provider.jsse 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.xnet.provider.jsse;
019:
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:        import java.io.OutputStream;
023:        import java.net.InetAddress;
024:        import java.net.InetSocketAddress;
025:        import java.net.Socket;
026:        import java.util.Arrays;
027:        import javax.net.ssl.SSLServerSocket;
028:        import javax.net.ssl.SSLServerSocketFactory;
029:        import javax.net.ssl.SSLSocket;
030:        import javax.net.ssl.SSLSocketFactory;
031:
032:        import junit.framework.Test;
033:        import junit.framework.TestCase;
034:        import junit.framework.TestSuite;
035:
036:        /**
037:         * SSLSocketImplTest
038:         */
039:        public class SSLSocketFactoriesTest extends TestCase {
040:
041:            // turn on/off the debug logging
042:            private boolean doLog = false;
043:
044:            /**
045:             * Sets up the test case.
046:             */
047:            public void setUp() throws Exception {
048:                super .setUp();
049:                if (doLog) {
050:                    System.out.println("========================");
051:                    System.out.println("====== Running the test: " + getName());
052:                    System.out.println("========================");
053:                }
054:            }
055:
056:            public void tearDown() throws Exception {
057:                super .tearDown();
058:            }
059:
060:            /**
061:             * Tests default initialized factories.
062:             */
063:            public void testDefaultInitialized() throws Exception {
064:
065:                SSLServerSocketFactory ssfactory = (SSLServerSocketFactory) SSLServerSocketFactory
066:                        .getDefault();
067:                SSLSocketFactory sfactory = (SSLSocketFactory) SSLSocketFactory
068:                        .getDefault();
069:
070:                assertNotNull(ssfactory.getDefaultCipherSuites());
071:                assertNotNull(ssfactory.getSupportedCipherSuites());
072:                assertNotNull(ssfactory.createServerSocket());
073:
074:                assertNotNull(sfactory.getDefaultCipherSuites());
075:                assertNotNull(sfactory.getSupportedCipherSuites());
076:                assertNotNull(sfactory.createSocket());
077:            }
078:
079:            public void testSocketCreation() throws Throwable {
080:                SSLSocketFactory socketFactory = new SSLSocketFactoryImpl(
081:                        JSSETestData.getSSLParameters());
082:                SSLServerSocketFactory serverSocketFactory = new SSLServerSocketFactoryImpl(
083:                        JSSETestData.getSSLParameters());
084:
085:                String[] enabled = { "TLS_RSA_WITH_RC4_128_MD5" };
086:                for (int i = 0; i < 4; i++) {
087:                    SSLServerSocket ssocket;
088:                    switch (i) {
089:                    case 0:
090:                        if (doLog) {
091:                            System.out
092:                                    .println("*** ServerSocketFactory.createServerSocket()");
093:                        }
094:                        ssocket = (SSLServerSocket) serverSocketFactory
095:                                .createServerSocket();
096:                        ssocket.bind(null);
097:                        break;
098:                    case 1:
099:                        if (doLog) {
100:                            System.out
101:                                    .println("*** ServerSocketFactory.createServerSocket(int)");
102:                        }
103:                        ssocket = (SSLServerSocket) serverSocketFactory
104:                                .createServerSocket(0);
105:                        break;
106:                    case 2:
107:                        if (doLog) {
108:                            System.out
109:                                    .println("*** ServerSocketFactory.createServerSocket(int,int)");
110:                        }
111:                        ssocket = (SSLServerSocket) serverSocketFactory
112:                                .createServerSocket(0, 6);
113:                        break;
114:                    default:
115:                        if (doLog) {
116:                            System.out
117:                                    .println("*** ServerSocketFactory."
118:                                            + "createServerSocket(int,int,InetAddress)");
119:                        }
120:                        ssocket = (SSLServerSocket) serverSocketFactory
121:                                .createServerSocket(0, 6, null);
122:                        break;
123:                    }
124:                    ssocket.setUseClientMode(false);
125:                    ssocket.setEnabledCipherSuites(enabled);
126:                    for (int j = 0; j < 6; j++) {
127:                        SSLSocket csocket;
128:                        switch (j) {
129:                        case 0:
130:                            if (doLog) {
131:                                System.out
132:                                        .println("=== SocketFactory.createSocket()");
133:                            }
134:                            csocket = (SSLSocket) socketFactory.createSocket();
135:                            csocket.connect(new InetSocketAddress("localhost",
136:                                    ssocket.getLocalPort()));
137:                            break;
138:                        case 1:
139:                            if (doLog) {
140:                                System.out
141:                                        .println("=== SocketFactory.createSocket(String,int)");
142:                            }
143:                            csocket = (SSLSocket) socketFactory.createSocket(
144:                                    "localhost", ssocket.getLocalPort());
145:                            break;
146:                        case 2:
147:                            if (doLog) {
148:                                System.out
149:                                        .println("=== SocketFactory.createSocket("
150:                                                + "String,int,InetAddress,int)");
151:                            }
152:                            csocket = (SSLSocket) socketFactory.createSocket(
153:                                    "localhost", ssocket.getLocalPort(),
154:                                    InetAddress.getByName("localhost"), 0);
155:                            break;
156:                        case 3:
157:                            if (doLog) {
158:                                System.out
159:                                        .println("=== SocketFactory.createSocket("
160:                                                + "InetAddress,int)");
161:                            }
162:                            csocket = (SSLSocket) socketFactory.createSocket(
163:                                    InetAddress.getByName("localhost"), ssocket
164:                                            .getLocalPort());
165:                            break;
166:                        case 4:
167:                            if (doLog) {
168:                                System.out
169:                                        .println("=== SocketFactory.createSocket("
170:                                                + "InetAddress,int,InetAddress,int)");
171:                            }
172:                            csocket = (SSLSocket) socketFactory.createSocket(
173:                                    InetAddress.getByName("localhost"), ssocket
174:                                            .getLocalPort(), InetAddress
175:                                            .getByName("localhost"), 0);
176:                            break;
177:                        default:
178:                            if (doLog) {
179:                                System.out
180:                                        .println("=== SSLSocketFactory.createSocket("
181:                                                + "socket,String,int,boolean)");
182:                            }
183:                            Socket socket = new Socket(InetAddress
184:                                    .getByName("localhost"), ssocket
185:                                    .getLocalPort());
186:                            csocket = (SSLSocket) socketFactory.createSocket(
187:                                    socket, "localhost",
188:                                    ssocket.getLocalPort(), true);
189:                            break;
190:
191:                        }
192:                        csocket.setUseClientMode(true);
193:                        csocket.setEnabledCipherSuites(enabled);
194:                        doTest(ssocket, csocket);
195:                    }
196:                }
197:            }
198:
199:            /**
200:             * SSLSocketFactory.getSupportedCipherSuites() method testing.
201:             */
202:            public void testGetSupportedCipherSuites1() throws Exception {
203:                SSLSocketFactory socketFactory = new SSLSocketFactoryImpl(
204:                        JSSETestData.getSSLParameters());
205:                String[] supported = socketFactory.getSupportedCipherSuites();
206:                assertNotNull(supported);
207:                supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
208:                supported = socketFactory.getSupportedCipherSuites();
209:                for (int i = 0; i < supported.length; i++) {
210:                    if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
211:                        fail("Modification of the returned result "
212:                                + "causes the modification of the internal state");
213:                    }
214:                }
215:            }
216:
217:            /**
218:             * SSLServerSocketFactory.getSupportedCipherSuites() method testing.
219:             */
220:            public void testGetSupportedCipherSuites2() throws Exception {
221:                SSLServerSocketFactory serverSocketFactory = new SSLServerSocketFactoryImpl(
222:                        JSSETestData.getSSLParameters());
223:                String[] supported = serverSocketFactory
224:                        .getSupportedCipherSuites();
225:                assertNotNull(supported);
226:                supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
227:                supported = serverSocketFactory.getSupportedCipherSuites();
228:                for (int i = 0; i < supported.length; i++) {
229:                    if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
230:                        fail("Modification of the returned result "
231:                                + "causes the modification of the internal state");
232:                    }
233:                }
234:            }
235:
236:            /**
237:             * SSLSocketFactory.getDefaultCipherSuites() method testing.
238:             */
239:            public void testGetDefaultCipherSuites1() throws Exception {
240:                SSLSocketFactory socketFactory = new SSLSocketFactoryImpl(
241:                        JSSETestData.getSSLParameters());
242:                String[] supported = socketFactory.getSupportedCipherSuites();
243:                String[] defaultcs = socketFactory.getDefaultCipherSuites();
244:                assertNotNull(supported);
245:                assertNotNull(defaultcs);
246:                for (int i = 0; i < defaultcs.length; i++) {
247:                    found: {
248:                        for (int j = 0; j < supported.length; j++) {
249:                            if (defaultcs[i].equals(supported[j])) {
250:                                break found;
251:                            }
252:                        }
253:                        fail("Default suite does not belong to the set "
254:                                + "of supported cipher suites: " + defaultcs[i]);
255:                    }
256:                }
257:            }
258:
259:            /**
260:             * SSLServerSocketFactory.getDefaultCipherSuites() method testing.
261:             */
262:            public void testGetDefaultCipherSuites2() throws Exception {
263:                SSLServerSocketFactory serverSocketFactory = new SSLServerSocketFactoryImpl(
264:                        JSSETestData.getSSLParameters());
265:                String[] supported = serverSocketFactory
266:                        .getSupportedCipherSuites();
267:                String[] defaultcs = serverSocketFactory
268:                        .getDefaultCipherSuites();
269:                assertNotNull(supported);
270:                assertNotNull(defaultcs);
271:                for (int i = 0; i < defaultcs.length; i++) {
272:                    found: {
273:                        for (int j = 0; j < supported.length; j++) {
274:                            if (defaultcs[i].equals(supported[j])) {
275:                                break found;
276:                            }
277:                        }
278:                        fail("Default suite does not belong to the set "
279:                                + "of supported cipher suites: " + defaultcs[i]);
280:                    }
281:                }
282:            }
283:
284:            /**
285:             * Performs SSL connection between the sockets
286:             * @return
287:             */
288:            public void doTest(SSLServerSocket ssocket, SSLSocket csocket)
289:                    throws Throwable {
290:                final String server_message = "Hello from SSL Server Socket!";
291:                final String client_message = "Hello from SSL Socket!";
292:                Thread server = null;
293:                Thread client = null;
294:                final Throwable[] throwed = new Throwable[1];
295:                try {
296:                    final SSLServerSocket ss = ssocket;
297:                    final SSLSocket s = csocket;
298:                    server = new Thread() {
299:                        public void run() {
300:                            InputStream is = null;
301:                            OutputStream os = null;
302:                            SSLSocket s = null;
303:                            try {
304:                                s = (SSLSocket) ss.accept();
305:                                if (doLog) {
306:                                    System.out.println("Socket accepted: " + s);
307:                                }
308:                                is = s.getInputStream();
309:                                os = s.getOutputStream();
310:                                // send the message to the client
311:                                os.write(server_message.getBytes());
312:                                // read the response
313:                                byte[] buff = new byte[client_message.length()];
314:                                int len = is.read(buff);
315:                                if (doLog) {
316:                                    System.out
317:                                            .println("Received message of length "
318:                                                    + len
319:                                                    + ": '"
320:                                                    + new String(buff, 0, len)
321:                                                    + "'");
322:                                }
323:                                assertTrue(
324:                                        "Read message does not equal to expected",
325:                                        Arrays
326:                                                .equals(client_message
327:                                                        .getBytes(), buff));
328:                                os.write(-1);
329:                                assertEquals("Read data differs from expected",
330:                                        255, is.read());
331:                                if (doLog) {
332:                                    System.out.println("Server is closed: "
333:                                            + s.isClosed());
334:                                }
335:                                assertEquals("Returned value should be -1",
336:                                // initiate an exchange of closure alerts
337:                                        -1, is.read());
338:                                if (doLog) {
339:                                    System.out.println("Server is closed: "
340:                                            + s.isClosed());
341:                                }
342:                                assertEquals("Returned value should be -1",
343:                                // initiate an exchange of closure alerts
344:                                        -1, is.read());
345:                            } catch (Throwable e) {
346:                                synchronized (throwed) {
347:                                    if (doLog) {
348:                                        e.printStackTrace();
349:                                    }
350:                                    if (throwed[0] == null) {
351:                                        throwed[0] = e;
352:                                    }
353:                                }
354:                            } finally {
355:                                try {
356:                                    if (is != null) {
357:                                        is.close();
358:                                    }
359:                                } catch (IOException ex) {
360:                                }
361:                                try {
362:                                    if (os != null) {
363:                                        os.close();
364:                                    }
365:                                } catch (IOException ex) {
366:                                }
367:                                try {
368:                                    if (s != null) {
369:                                        s.close();
370:                                    }
371:                                } catch (IOException ex) {
372:                                }
373:                            }
374:                        }
375:                    };
376:
377:                    client = new Thread() {
378:                        public void run() {
379:                            InputStream is = null;
380:                            OutputStream os = null;
381:                            try {
382:                                assertTrue("Client was not connected", s
383:                                        .isConnected());
384:                                if (doLog) {
385:                                    System.out.println("Client connected");
386:                                }
387:                                is = s.getInputStream();
388:                                os = s.getOutputStream();
389:                                s.startHandshake();
390:                                if (doLog) {
391:                                    System.out.println("Client: HS was done");
392:                                }
393:                                // read the message from the server
394:                                byte[] buff = new byte[server_message.length()];
395:                                int len = is.read(buff);
396:                                if (doLog) {
397:                                    System.out
398:                                            .println("Received message of length "
399:                                                    + len
400:                                                    + ": '"
401:                                                    + new String(buff, 0, len)
402:                                                    + "'");
403:                                }
404:                                assertTrue(
405:                                        "Read message does not equal to expected",
406:                                        Arrays
407:                                                .equals(server_message
408:                                                        .getBytes(), buff));
409:                                // send the response
410:                                buff = (" " + client_message + " ").getBytes();
411:                                os.write(buff, 1, buff.length - 2);
412:                                assertEquals("Read data differs from expected",
413:                                        255, is.read());
414:                                os.write(-1);
415:                                if (doLog) {
416:                                    System.out.println("Client is closed: "
417:                                            + s.isClosed());
418:                                }
419:                                s.close();
420:                                if (doLog) {
421:                                    System.out.println("Client is closed: "
422:                                            + s.isClosed());
423:                                }
424:                            } catch (Throwable e) {
425:                                synchronized (throwed) {
426:                                    if (doLog) {
427:                                        e.printStackTrace();
428:                                    }
429:                                    if (throwed[0] == null) {
430:                                        throwed[0] = e;
431:                                    }
432:                                }
433:                            } finally {
434:                                try {
435:                                    if (is != null) {
436:                                        is.close();
437:                                    }
438:                                } catch (IOException ex) {
439:                                }
440:                                try {
441:                                    if (os != null) {
442:                                        os.close();
443:                                    }
444:                                } catch (IOException ex) {
445:                                }
446:                                try {
447:                                    if (s != null) {
448:                                        s.close();
449:                                    }
450:                                } catch (IOException ex) {
451:                                }
452:                            }
453:                        }
454:                    };
455:
456:                    server.start();
457:                    client.start();
458:
459:                    while (server.isAlive() || client.isAlive()) {
460:                        if (throwed[0] != null) {
461:                            throw throwed[0];
462:                        }
463:                        try {
464:                            Thread.sleep(500);
465:                        } catch (Exception e) {
466:                        }
467:                    }
468:                } finally {
469:                    if (server != null) {
470:                        server.stop();
471:                    }
472:                    if (client != null) {
473:                        client.stop();
474:                    }
475:                }
476:                if (throwed[0] != null) {
477:                    throw throwed[0];
478:                }
479:            }
480:
481:            public static Test suite() {
482:                return new TestSuite(SSLSocketFactoriesTest.class);
483:            }
484:
485:            public static void main(String[] args) {
486:                junit.textui.TestRunner.run(suite());
487:            }
488:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.