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.net.Socket;
022: import java.net.InetSocketAddress;
023: import javax.net.ssl.SSLServerSocket;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: /**
030: * SSLServerSocketImplTest test
031: */
032: public class SSLServerSocketImplTest extends TestCase {
033:
034: private static boolean doLog = false;
035:
036: /**
037: * Sets up the test case.
038: */
039: public void setUp() {
040: if (doLog) {
041: System.out.println("");
042: System.out.println("========================");
043: System.out.println("====== Running the test: " + getName());
044: }
045: }
046:
047: private SSLServerSocket createSSLServerSocket() throws Exception {
048: return new SSLServerSocketImpl(JSSETestData.getSSLParameters());
049: }
050:
051: /**
052: * SSLServerSocketImpl(SSLParameters sslParameters) method testing.
053: */
054: public void testSSLServerSocketImpl1() throws Exception {
055: Client client = null;
056: SSLServerSocket ssocket = null;
057: try {
058: ssocket = new SSLServerSocketImpl(JSSETestData
059: .getSSLParameters());
060: ssocket.bind(null);
061: ssocket.setUseClientMode(true);
062:
063: final SSLServerSocket s = ssocket;
064: Thread thread = new Thread() {
065: public void run() {
066: try {
067: s.accept().close();
068: } catch (Exception e) {
069: }
070: }
071: };
072:
073: thread.start();
074:
075: client = new Client(ssocket.getLocalPort());
076: client.start();
077:
078: int timeout = 10; // wait no more than 5 seconds for handshake
079: while (!client.handshakeStarted()) {
080: // wait for handshake start
081: try {
082: Thread.sleep(500);
083: } catch (Exception e) {
084: }
085: timeout--;
086: if (timeout < 0) {
087: try {
088: client.close();
089: } catch (IOException ex) {
090: }
091: try {
092: ssocket.close();
093: } catch (IOException ex) {
094: }
095: fail("Handshake was not started");
096: }
097: }
098: } finally {
099: if (client != null) {
100: try {
101: client.close();
102: } catch (IOException ex) {
103: }
104: }
105: if (ssocket != null) {
106: try {
107: ssocket.close();
108: } catch (IOException ex) {
109: }
110: }
111: }
112: }
113:
114: /**
115: * SSLServerSocketImpl(int port, SSLParameters sslParameters) method
116: * testing.
117: */
118: public void testSSLServerSocketImpl2() throws Exception {
119: Client client = null;
120: SSLServerSocket ssocket = null;
121: try {
122: ssocket = new SSLServerSocketImpl(0, JSSETestData
123: .getSSLParameters());
124: ssocket.setUseClientMode(true);
125:
126: final SSLServerSocket s = ssocket;
127: Thread thread = new Thread() {
128: public void run() {
129: try {
130: s.accept().close();
131: } catch (Exception e) {
132: }
133: }
134: };
135:
136: thread.start();
137:
138: client = new Client(ssocket.getLocalPort());
139: client.start();
140:
141: int timeout = 10; // wait no more than 5 seconds for handshake
142: while (!client.handshakeStarted()) {
143: // wait for handshake start
144: try {
145: Thread.sleep(500);
146: } catch (Exception e) {
147: }
148: timeout--;
149: if (timeout < 0) {
150: try {
151: client.close();
152: } catch (IOException ex) {
153: }
154: try {
155: ssocket.close();
156: } catch (IOException ex) {
157: }
158: fail("Handshake was not started");
159: }
160: }
161: } finally {
162: if (client != null) {
163: try {
164: client.close();
165: } catch (IOException ex) {
166: }
167: }
168: if (ssocket != null) {
169: try {
170: ssocket.close();
171: } catch (IOException ex) {
172: }
173: }
174: }
175: }
176:
177: /**
178: * SSLServerSocketImpl(int port, int backlog,
179: * SSLParameters sslParameters) method testing.
180: */
181: public void testSSLServerSocketImpl3() throws Exception {
182: Client client = null;
183: SSLServerSocket ssocket = null;
184: try {
185: ssocket = new SSLServerSocketImpl(0, 1, JSSETestData
186: .getSSLParameters());
187: ssocket.setUseClientMode(true);
188:
189: final SSLServerSocket s = ssocket;
190: Thread thread = new Thread() {
191: public void run() {
192: try {
193: s.accept().close();
194: } catch (Exception e) {
195: }
196: }
197: };
198:
199: thread.start();
200:
201: client = new Client(ssocket.getLocalPort());
202: client.start();
203:
204: int timeout = 10; // wait no more than 5 seconds for handshake
205: while (!client.handshakeStarted()) {
206: // wait for handshake start
207: try {
208: Thread.sleep(500);
209: } catch (Exception e) {
210: }
211: timeout--;
212: if (timeout < 0) {
213: try {
214: client.close();
215: } catch (IOException ex) {
216: }
217: try {
218: ssocket.close();
219: } catch (IOException ex) {
220: }
221: fail("Handshake was not started");
222: }
223: }
224: } finally {
225: if (client != null) {
226: try {
227: client.close();
228: } catch (IOException ex) {
229: }
230: }
231: if (ssocket != null) {
232: try {
233: ssocket.close();
234: } catch (IOException ex) {
235: }
236: }
237: }
238: }
239:
240: /**
241: * SSLServerSocketImpl(int port, int backlog, InetAddress iAddress,
242: * SSLParameters sslParameters) method testing.
243: */
244: public void testSSLServerSocketImpl4() throws Exception {
245: Client client = null;
246: SSLServerSocket ssocket = null;
247: try {
248: ssocket = new SSLServerSocketImpl(0, 1, null, JSSETestData
249: .getSSLParameters());
250: ssocket.setUseClientMode(true);
251:
252: final SSLServerSocket s = ssocket;
253: Thread thread = new Thread() {
254: public void run() {
255: try {
256: s.accept().close();
257: } catch (Exception e) {
258: }
259: }
260: };
261:
262: thread.start();
263:
264: client = new Client(ssocket.getLocalPort());
265: client.start();
266:
267: int timeout = 10; // wait no more than 5 seconds for handshake
268: while (!client.handshakeStarted()) {
269: // wait for handshake start
270: try {
271: Thread.sleep(500);
272: } catch (Exception e) {
273: }
274: timeout--;
275: if (timeout < 0) {
276: try {
277: client.close();
278: } catch (IOException ex) {
279: }
280: try {
281: ssocket.close();
282: } catch (IOException ex) {
283: }
284: fail("Handshake was not started");
285: }
286: }
287: } finally {
288: if (client != null) {
289: try {
290: client.close();
291: } catch (IOException ex) {
292: }
293: }
294: if (ssocket != null) {
295: try {
296: ssocket.close();
297: } catch (IOException ex) {
298: }
299: }
300: }
301: }
302:
303: /**
304: * getSupportedCipherSuites() method testing.
305: */
306: public void testGetSupportedCipherSuites() throws Exception {
307: SSLServerSocket ssocket = createSSLServerSocket();
308: String[] supported = ssocket.getSupportedCipherSuites();
309: assertNotNull(supported);
310: supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
311: supported = ssocket.getEnabledCipherSuites();
312: for (int i = 0; i < supported.length; i++) {
313: if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
314: fail("Modification of the returned result "
315: + "causes the modification of the internal state");
316: }
317: }
318: }
319:
320: /**
321: * getEnabledCipherSuites() method testing.
322: */
323: public void testGetEnabledCipherSuites() throws Exception {
324: SSLServerSocket ssocket = createSSLServerSocket();
325: String[] enabled = ssocket.getEnabledCipherSuites();
326: assertNotNull(enabled);
327: String[] supported = ssocket.getSupportedCipherSuites();
328: for (int i = 0; i < enabled.length; i++) {
329: //System.out.println("Checking of "+enabled[i]);
330: found: {
331: for (int j = 0; j < supported.length; j++) {
332: if (enabled[i].equals(supported[j])) {
333: break found;
334: }
335: }
336: fail("Enabled suite does not belong to the set "
337: + "of supported cipher suites: " + enabled[i]);
338: }
339: }
340: ssocket.setEnabledCipherSuites(supported);
341: for (int i = 0; i < supported.length; i++) {
342: enabled = new String[supported.length - i];
343: System.arraycopy(supported, 0, enabled, 0, supported.length
344: - i);
345: ssocket.setEnabledCipherSuites(enabled);
346: String[] result = ssocket.getEnabledCipherSuites();
347: if (result.length != enabled.length) {
348: fail("Returned result does not correspond to expected.");
349: }
350: for (int k = 0; k < result.length; k++) {
351: found: {
352: for (int n = 0; n < enabled.length; n++) {
353: if (result[k].equals(enabled[n])) {
354: break found;
355: }
356: }
357: if (result.length != enabled.length) {
358: fail("Returned result does not correspond "
359: + "to expected.");
360: }
361: }
362: }
363: }
364: }
365:
366: /**
367: * setEnabledCipherSuites(String[] suites) method testing.
368: */
369: public void testSetEnabledCipherSuites() throws Exception {
370: SSLServerSocket ssocket = createSSLServerSocket();
371: String[] enabled = ssocket.getEnabledCipherSuites();
372: assertNotNull(enabled);
373: String[] supported = ssocket.getSupportedCipherSuites();
374: for (int i = 0; i < enabled.length; i++) {
375: //System.out.println("Checking of "+enabled[i]);
376: found: {
377: for (int j = 0; j < supported.length; j++) {
378: if (enabled[i].equals(supported[j])) {
379: break found;
380: }
381: }
382: fail("Enabled suite does not belong to the set "
383: + "of supported cipher suites: " + enabled[i]);
384: }
385: }
386: ssocket.setEnabledCipherSuites(supported);
387: ssocket.setEnabledCipherSuites(enabled);
388: ssocket.setEnabledCipherSuites(supported);
389: String[] more_than_supported = new String[supported.length + 1];
390: for (int i = 0; i < supported.length + 1; i++) {
391: more_than_supported[i] = "NOT_SUPPORTED_CIPHER_SUITE";
392: System.arraycopy(supported, 0, more_than_supported, 0, i);
393: System.arraycopy(supported, i, more_than_supported, i + 1,
394: supported.length - i);
395: try {
396: ssocket.setEnabledCipherSuites(more_than_supported);
397: fail("Expected IllegalArgumentException was not thrown");
398: } catch (IllegalArgumentException e) {
399: }
400: }
401: enabled = ssocket.getEnabledCipherSuites();
402: enabled[0] = "NOT_SUPPORTED_CIPHER_SUITE";
403: enabled = ssocket.getEnabledCipherSuites();
404: for (int i = 0; i < enabled.length; i++) {
405: if ("NOT_SUPPORTED_CIPHER_SUITE".equals(enabled[i])) {
406: fail("Modification of the returned result "
407: + "causes the modification of the internal state");
408: }
409: }
410: }
411:
412: /**
413: * getSupportedProtocols() method testing.
414: */
415: public void testGetSupportedProtocols() throws Exception {
416: SSLServerSocket ssocket = createSSLServerSocket();
417: String[] supported = ssocket.getSupportedProtocols();
418: assertNotNull(supported);
419: assertFalse(supported.length == 0);
420: supported[0] = "NOT_SUPPORTED_PROTOCOL";
421: supported = ssocket.getSupportedProtocols();
422: for (int i = 0; i < supported.length; i++) {
423: if ("NOT_SUPPORTED_PROTOCOL".equals(supported[i])) {
424: fail("Modification of the returned result "
425: + "causes the modification of the internal state");
426: }
427: }
428: }
429:
430: /**
431: * getEnabledProtocols() method testing.
432: */
433: public void testGetEnabledProtocols() throws Exception {
434: SSLServerSocket ssocket = createSSLServerSocket();
435: String[] enabled = ssocket.getEnabledProtocols();
436: assertNotNull(enabled);
437: String[] supported = ssocket.getSupportedProtocols();
438: for (int i = 0; i < enabled.length; i++) {
439: //System.out.println("Checking of "+enabled[i]);
440: found: {
441: for (int j = 0; j < supported.length; j++) {
442: if (enabled[i].equals(supported[j])) {
443: break found;
444: }
445: }
446: fail("Enabled protocol does not belong to the set "
447: + "of supported protocols: " + enabled[i]);
448: }
449: }
450: ssocket.setEnabledProtocols(supported);
451: for (int i = 0; i < supported.length; i++) {
452: enabled = new String[supported.length - i];
453: System.arraycopy(supported, i, enabled, 0, supported.length
454: - i);
455: //System.out.println("");
456: //for (int k=0; k<supported.length - i; k++) {
457: // System.out.println("---- "+enabled[k]);
458: //}
459: ssocket.setEnabledProtocols(enabled);
460: String[] result = ssocket.getEnabledProtocols();
461: if (result.length != enabled.length) {
462: fail("Returned result does not correspond to expected.");
463: }
464: for (int k = 0; k < result.length; k++) {
465: found: {
466: for (int n = 0; n < enabled.length; n++) {
467: if (result[k].equals(enabled[n])) {
468: break found;
469: }
470: }
471: if (result.length != enabled.length) {
472: fail("Returned result does not correspond "
473: + "to expected.");
474: }
475: }
476: }
477: }
478: }
479:
480: /**
481: * setEnabledProtocols(String[] protocols) method testing.
482: */
483: public void testSetEnabledProtocols() throws Exception {
484: SSLServerSocket ssocket = createSSLServerSocket();
485: String[] enabled = ssocket.getEnabledProtocols();
486: assertNotNull(enabled);
487: String[] supported = ssocket.getSupportedProtocols();
488: for (int i = 0; i < enabled.length; i++) {
489: //System.out.println("Checking of "+enabled[i]);
490: found: {
491: for (int j = 0; j < supported.length; j++) {
492: if (enabled[i].equals(supported[j])) {
493: break found;
494: }
495: }
496: fail("Enabled suite does not belong to the set "
497: + "of supported cipher suites: " + enabled[i]);
498: }
499: }
500: ssocket.setEnabledProtocols(supported);
501: ssocket.setEnabledProtocols(enabled);
502: ssocket.setEnabledProtocols(supported);
503: String[] more_than_supported = new String[supported.length + 1];
504: for (int i = 0; i < supported.length + 1; i++) {
505: more_than_supported[i] = "NOT_SUPPORTED_PROTOCOL";
506: System.arraycopy(supported, 0, more_than_supported, 0, i);
507: System.arraycopy(supported, i, more_than_supported, i + 1,
508: supported.length - i);
509: try {
510: ssocket.setEnabledProtocols(more_than_supported);
511: fail("Expected IllegalArgumentException was not thrown");
512: } catch (IllegalArgumentException e) {
513: }
514: }
515: enabled = ssocket.getEnabledProtocols();
516: enabled[0] = "NOT_SUPPORTED_PROTOCOL";
517: enabled = ssocket.getEnabledProtocols();
518: for (int i = 0; i < enabled.length; i++) {
519: if ("NOT_SUPPORTED_PROTOCOL".equals(enabled[i])) {
520: fail("Modification of the returned result "
521: + "causes the modification of the internal state");
522: }
523: }
524: }
525:
526: /**
527: * setUseClientMode(boolean mode) method testing.
528: * getUseClientMode() method testing.
529: */
530: public void testSetGetUseClientMode() throws Exception {
531: SSLServerSocket ssocket = createSSLServerSocket();
532:
533: ssocket.setUseClientMode(false);
534: assertFalse("Result does not correspond to expected", ssocket
535: .getUseClientMode());
536: ssocket.setUseClientMode(true);
537: assertTrue("Result does not correspond to expected", ssocket
538: .getUseClientMode());
539: }
540:
541: /**
542: * setNeedClientAuth(boolean need) method testing.
543: * getNeedClientAuth() method testing.
544: */
545: public void testSetGetNeedClientAuth() throws Exception {
546: SSLServerSocket ssocket = createSSLServerSocket();
547:
548: ssocket.setWantClientAuth(true);
549: ssocket.setNeedClientAuth(false);
550: assertFalse("Result does not correspond to expected", ssocket
551: .getNeedClientAuth());
552: assertFalse("Socket did not reset its want client auth state",
553: ssocket.getWantClientAuth());
554: ssocket.setWantClientAuth(true);
555: ssocket.setNeedClientAuth(true);
556: assertTrue("Result does not correspond to expected", ssocket
557: .getNeedClientAuth());
558: assertFalse("Socket did not reset its want client auth state",
559: ssocket.getWantClientAuth());
560: }
561:
562: /**
563: * setWantClientAuth(boolean want) method testing.
564: * getWantClientAuth() method testing.
565: */
566: public void testSetGetWantClientAuth() throws Exception {
567: SSLServerSocket ssocket = createSSLServerSocket();
568:
569: ssocket.setNeedClientAuth(true);
570: ssocket.setWantClientAuth(false);
571: assertFalse("Result does not correspond to expected", ssocket
572: .getWantClientAuth());
573: assertFalse("Socket did not reset its want client auth state",
574: ssocket.getNeedClientAuth());
575: ssocket.setNeedClientAuth(true);
576: ssocket.setWantClientAuth(true);
577: assertTrue("Result does not correspond to expected", ssocket
578: .getWantClientAuth());
579: assertFalse("Socket did not reset its want client auth state",
580: ssocket.getNeedClientAuth());
581: }
582:
583: /**
584: * setEnableSessionCreation(boolean flag) method testing.
585: * getEnableSessionCreation() method testing.
586: */
587: public void testSetGetEnableSessionCreation() throws Exception {
588: SSLServerSocket ssocket = createSSLServerSocket();
589:
590: ssocket.setEnableSessionCreation(false);
591: assertFalse("Result does not correspond to expected", ssocket
592: .getEnableSessionCreation());
593: ssocket.setEnableSessionCreation(true);
594: assertTrue("Result does not correspond to expected", ssocket
595: .getEnableSessionCreation());
596: }
597:
598: /**
599: * toString() method testing.
600: */
601: public void testToString() throws Exception {
602: SSLServerSocket ssocket = createSSLServerSocket();
603: assertNotNull("String representation is null", ssocket
604: .toString());
605: }
606:
607: private static class Client extends Thread {
608:
609: private boolean closed;
610: private boolean handshake_started = false;
611: private Socket client = null;
612: private int port;
613:
614: public Client(int port) throws IOException {
615: super ();
616: this .port = port;
617: client = new Socket();
618: client.setSoTimeout(10000);
619: }
620:
621: public int getPort() {
622: return client.getLocalPort();
623: }
624:
625: public void run() {
626: while (!closed) {
627: try {
628: if (doLog) {
629: System.out.print(".");
630: }
631: if (!handshake_started) {
632: client.connect(new InetSocketAddress(
633: "localhost", port));
634: client.getInputStream().read();
635: handshake_started = true;
636: }
637: Thread.sleep(1000);
638: } catch (Exception e) {
639: e.printStackTrace();
640: }
641: }
642: if (client != null) {
643: try {
644: client.close();
645: } catch (IOException e) {
646: }
647: }
648: //System.out.println("===== client has been stopped");
649: }
650:
651: public boolean handshakeStarted() {
652: return handshake_started;
653: }
654:
655: public void close() throws IOException {
656: closed = true;
657: client.close();
658: }
659:
660: };
661:
662: public static Test suite() {
663: return new TestSuite(SSLServerSocketImplTest.class);
664: }
665:
666: public static void main(String[] args) {
667: junit.textui.TestRunner.run(suite());
668: }
669: }
|