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.luni.tests.java.net;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InterruptedIOException;
023: import java.io.OutputStream;
024: import java.net.BindException;
025: import java.net.ConnectException;
026: import java.net.InetAddress;
027: import java.net.InetSocketAddress;
028: import java.net.ServerSocket;
029: import java.net.Socket;
030: import java.net.SocketAddress;
031: import java.net.SocketException;
032: import java.net.SocketImpl;
033: import java.net.UnknownHostException;
034: import java.util.Date;
035: import java.util.Properties;
036:
037: import tests.support.Support_Configuration;
038: import tests.support.Support_Exec;
039:
040: public class ServerSocketTest extends SocketTestCase {
041:
042: boolean interrupted;
043:
044: ServerSocket s;
045:
046: Socket sconn;
047:
048: Thread t;
049:
050: static class SSClient implements Runnable {
051: Socket cs;
052:
053: int port;
054:
055: public SSClient(int prt) {
056: port = prt;
057: }
058:
059: public void run() {
060: try {
061: // Go to sleep so the server can setup and wait for connection
062: Thread.sleep(1000);
063: cs = new Socket(InetAddress.getLocalHost()
064: .getHostName(), port);
065: // Sleep again to allow server side processing. Thread is
066: // stopped by server.
067: Thread.sleep(10000);
068: } catch (InterruptedException e) {
069: return;
070: } catch (Throwable e) {
071: System.out.println("Error establishing client: "
072: + e.toString());
073: } finally {
074: try {
075: if (cs != null)
076: cs.close();
077: } catch (Exception e) {
078: }
079: }
080: }
081: }
082:
083: /**
084: * @tests java.net.ServerSocket#ServerSocket()
085: */
086: public void test_Constructor() {
087: // Test for method java.net.ServerSocket(int)
088: assertTrue("Used during tests", true);
089: }
090:
091: /**
092: * @tests java.net.ServerSocket#ServerSocket(int)
093: */
094: public void test_ConstructorI() {
095: // Test for method java.net.ServerSocket(int)
096: assertTrue("Used during tests", true);
097: }
098:
099: /**
100: * @tests java.net.ServerSocket#ServerSocket(int)
101: */
102: public void test_ConstructorI_SocksSet() throws IOException {
103: // Harmony-623 regression test
104: ServerSocket ss = null;
105: Properties props = (Properties) System.getProperties().clone();
106: try {
107: System.setProperty("socksProxyHost", "127.0.0.1");
108: System.setProperty("socksProxyPort", "12345");
109: ss = new ServerSocket(0);
110: } finally {
111: System.setProperties(props);
112: if (null != ss) {
113: ss.close();
114: }
115: }
116: }
117:
118: /**
119: * @tests java.net.ServerSocket#ServerSocket(int, int)
120: */
121: public void test_ConstructorII() throws IOException {
122: try {
123: s = new ServerSocket(0, 10);
124: s.setSoTimeout(2000);
125: startClient(s.getLocalPort());
126: sconn = s.accept();
127: } catch (InterruptedIOException e) {
128: return;
129: }
130:
131: ServerSocket s1 = new ServerSocket(0);
132: try {
133: try {
134: ServerSocket s2 = new ServerSocket(s1.getLocalPort());
135: s2.close();
136: fail("Was able to create two serversockets on same port");
137: } catch (BindException e) {
138: // Expected
139: }
140: } finally {
141: s1.close();
142: }
143:
144: s1 = new ServerSocket(0);
145: int allocatedPort = s1.getLocalPort();
146: s1.close();
147: s1 = new ServerSocket(allocatedPort);
148: s1.close();
149: }
150:
151: /**
152: * @tests java.net.ServerSocket#ServerSocket(int, int, java.net.InetAddress)
153: */
154: public void test_ConstructorIILjava_net_InetAddress()
155: throws UnknownHostException, IOException {
156: s = new ServerSocket(0, 10, InetAddress.getLocalHost());
157: try {
158: s.setSoTimeout(5000);
159: startClient(s.getLocalPort());
160: sconn = s.accept();
161: assertNotNull("Was unable to accept connection", sconn);
162: sconn.close();
163: } finally {
164: s.close();
165: }
166: }
167:
168: /**
169: * @tests java.net.ServerSocket#accept()
170: */
171: public void test_accept() throws IOException {
172: s = new ServerSocket(0);
173: try {
174: s.setSoTimeout(5000);
175: startClient(s.getLocalPort());
176: sconn = s.accept();
177: int localPort1 = s.getLocalPort();
178: int localPort2 = sconn.getLocalPort();
179: sconn.close();
180: assertEquals("Bad local port value", localPort1, localPort2);
181: } finally {
182: s.close();
183: }
184:
185: try {
186: interrupted = false;
187: final ServerSocket ss = new ServerSocket(0);
188: ss.setSoTimeout(12000);
189: Runnable runnable = new Runnable() {
190: public void run() {
191: try {
192: ss.accept();
193: } catch (InterruptedIOException e) {
194: interrupted = true;
195: } catch (IOException e) {
196: }
197: }
198: };
199: Thread thread = new Thread(runnable, "ServerSocket.accept");
200: thread.start();
201: try {
202: do {
203: Thread.sleep(500);
204: } while (!thread.isAlive());
205: } catch (InterruptedException e) {
206: }
207: ss.close();
208: int c = 0;
209: do {
210: try {
211: Thread.sleep(500);
212: } catch (InterruptedException e) {
213: }
214: if (interrupted) {
215: fail("accept interrupted");
216: }
217: if (++c > 4) {
218: fail("accept call did not exit");
219: }
220: } while (thread.isAlive());
221:
222: interrupted = false;
223: ServerSocket ss2 = new ServerSocket(0);
224: ss2.setSoTimeout(500);
225: Date start = new Date();
226: try {
227: ss2.accept();
228: } catch (InterruptedIOException e) {
229: interrupted = true;
230: }
231: assertTrue("accept not interrupted", interrupted);
232: Date finish = new Date();
233: int delay = (int) (finish.getTime() - start.getTime());
234: assertTrue("timeout too soon: " + delay + " "
235: + start.getTime() + " " + finish.getTime(),
236: delay >= 490);
237: ss2.close();
238: } catch (IOException e) {
239: fail("Unexpected IOException : " + e.getMessage());
240: }
241: }
242:
243: /**
244: * @tests java.net.ServerSocket#close()
245: */
246: public void test_close() throws IOException {
247: try {
248: s = new ServerSocket(0);
249: try {
250: s.close();
251: s.accept();
252: fail("Close test failed");
253: } catch (SocketException e) {
254: // expected;
255: }
256: } finally {
257: s.close();
258: }
259: }
260:
261: /**
262: * @tests java.net.ServerSocket#getInetAddress()
263: */
264: public void test_getInetAddress() throws IOException {
265: InetAddress addr = InetAddress.getLocalHost();
266: s = new ServerSocket(0, 10, addr);
267: try {
268: assertEquals("Returned incorrect InetAdrees", addr, s
269: .getInetAddress());
270: } finally {
271: s.close();
272: }
273: }
274:
275: /**
276: * @tests java.net.ServerSocket#getLocalPort()
277: */
278: public void test_getLocalPort() throws IOException {
279: // Try a specific port number, but don't complain if we don't get it
280: int portNumber = 63024; // I made this up
281: try {
282: try {
283: s = new ServerSocket(portNumber);
284: } catch (BindException e) {
285: // we could not get the port, give up
286: return;
287: }
288: assertEquals("Returned incorrect port", portNumber, s
289: .getLocalPort());
290: } finally {
291: s.close();
292: }
293: }
294:
295: /**
296: * @tests java.net.ServerSocket#getSoTimeout()
297: */
298: public void test_getSoTimeout() throws IOException {
299: s = new ServerSocket(0);
300: try {
301: s.setSoTimeout(100);
302: assertEquals("Returned incorrect sotimeout", 100, s
303: .getSoTimeout());
304: } finally {
305: s.close();
306: }
307: }
308:
309: /**
310: * @tests java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
311: */
312: public void test_setSocketFactoryLjava_net_SocketImplFactory() {
313: // Test for method void
314: // java.net.ServerSocket.setSocketFactory(java.net.SocketImplFactory)
315:
316: // TODO : Implementation
317: }
318:
319: /**
320: * @tests java.net.ServerSocket#setSoTimeout(int)
321: */
322: public void test_setSoTimeoutI() throws IOException {
323: // Timeout should trigger and throw InterruptedIOException
324: try {
325: s = new ServerSocket(0);
326: s.setSoTimeout(100);
327: s.accept();
328: } catch (InterruptedIOException e) {
329: assertEquals("Set incorrect sotimeout", 100, s
330: .getSoTimeout());
331: return;
332: }
333:
334: // Timeout should not trigger in this case
335: s = new ServerSocket(0);
336: startClient(s.getLocalPort());
337: s.setSoTimeout(10000);
338: sconn = s.accept();
339: }
340:
341: /**
342: * @tests java.net.ServerSocket#toString()
343: */
344: public void test_toString() throws Exception {
345: s = new ServerSocket(0);
346: try {
347: int portNumber = s.getLocalPort();
348: assertEquals(
349: "ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport="
350: + portNumber + "]", s.toString());
351: } finally {
352: s.close();
353: }
354: }
355:
356: /**
357: * @tests java.net.ServerSocket#bind(java.net.SocketAddress)
358: */
359: public void test_bindLjava_net_SocketAddress() throws IOException {
360: class mySocketAddress extends SocketAddress {
361: public mySocketAddress() {
362: }
363: }
364: // create servers socket, bind it and then validate basic state
365: ServerSocket theSocket = new ServerSocket();
366: InetSocketAddress theAddress = new InetSocketAddress(
367: InetAddress.getLocalHost(), 0);
368: theSocket.bind(theAddress);
369: int portNumber = theSocket.getLocalPort();
370: assertTrue("Returned incorrect InetSocketAddress(2):"
371: + theSocket.getLocalSocketAddress().toString()
372: + "Expected: "
373: + (new InetSocketAddress(InetAddress.getLocalHost(),
374: portNumber)).toString(), theSocket
375: .getLocalSocketAddress().equals(
376: new InetSocketAddress(InetAddress
377: .getLocalHost(), portNumber)));
378: assertTrue("Server socket not bound when it should be:",
379: theSocket.isBound());
380:
381: // now make sure that it is actually bound and listening on the
382: // address we provided
383: Socket clientSocket = new Socket();
384: InetSocketAddress clAddress = new InetSocketAddress(InetAddress
385: .getLocalHost(), portNumber);
386: clientSocket.connect(clAddress);
387: Socket servSock = theSocket.accept();
388:
389: assertEquals(clAddress, clientSocket.getRemoteSocketAddress());
390: theSocket.close();
391: servSock.close();
392: clientSocket.close();
393:
394: // validate we can specify null for the address in the bind and all
395: // goes ok
396: theSocket = new ServerSocket();
397: theSocket.bind(null);
398: theSocket.close();
399:
400: // Address that we have already bound to
401: theSocket = new ServerSocket();
402: ServerSocket theSocket2 = new ServerSocket();
403: try {
404: theAddress = new InetSocketAddress(InetAddress
405: .getLocalHost(), 0);
406: theSocket.bind(theAddress);
407: SocketAddress localAddress = theSocket
408: .getLocalSocketAddress();
409: theSocket2.bind(localAddress);
410: fail("No exception binding to address that is not available");
411: } catch (IOException ex) {
412: }
413: theSocket.close();
414: theSocket2.close();
415:
416: // validate we get io address when we try to bind to address we
417: // cannot bind to
418: theSocket = new ServerSocket();
419: try {
420: theSocket
421: .bind(new InetSocketAddress(
422: InetAddress
423: .getByAddress(Support_Configuration.nonLocalAddressBytes),
424: 0));
425: fail("No exception was thrown when binding to bad address");
426: } catch (IOException ex) {
427: }
428: theSocket.close();
429:
430: // now validate case where we pass in an unsupported subclass of
431: // SocketAddress
432: theSocket = new ServerSocket();
433: try {
434: theSocket.bind(new mySocketAddress());
435: fail("No exception when binding using unsupported SocketAddress subclass");
436: } catch (IllegalArgumentException ex) {
437: }
438: theSocket.close();
439: }
440:
441: /**
442: * @tests java.net.ServerSocket#bind(java.net.SocketAddress,int)
443: */
444: public void test_bindLjava_net_SocketAddressI() throws IOException {
445: class mySocketAddress extends SocketAddress {
446:
447: public mySocketAddress() {
448: }
449: }
450:
451: // create servers socket, bind it and then validate basic state
452: ServerSocket theSocket = new ServerSocket();
453: InetSocketAddress theAddress = new InetSocketAddress(
454: InetAddress.getLocalHost(), 0);
455: theSocket.bind(theAddress, 5);
456: int portNumber = theSocket.getLocalPort();
457: assertTrue("Returned incorrect InetSocketAddress(2):"
458: + theSocket.getLocalSocketAddress().toString()
459: + "Expected: "
460: + (new InetSocketAddress(InetAddress.getLocalHost(),
461: portNumber)).toString(), theSocket
462: .getLocalSocketAddress().equals(
463: new InetSocketAddress(InetAddress
464: .getLocalHost(), portNumber)));
465: assertTrue("Server socket not bound when it should be:",
466: theSocket.isBound());
467:
468: // now make sure that it is actually bound and listening on the
469: // address we provided
470: SocketAddress localAddress = theSocket.getLocalSocketAddress();
471: Socket clientSocket = new Socket();
472: clientSocket.connect(localAddress);
473: Socket servSock = theSocket.accept();
474:
475: assertTrue(clientSocket.getRemoteSocketAddress().equals(
476: localAddress));
477: theSocket.close();
478: servSock.close();
479: clientSocket.close();
480:
481: // validate we can specify null for the address in the bind and all
482: // goes ok
483: theSocket = new ServerSocket();
484: theSocket.bind(null, 5);
485: theSocket.close();
486:
487: // Address that we have already bound to
488: theSocket = new ServerSocket();
489: ServerSocket theSocket2 = new ServerSocket();
490: try {
491: theAddress = new InetSocketAddress(InetAddress
492: .getLocalHost(), 0);
493: theSocket.bind(theAddress, 5);
494: SocketAddress inuseAddress = theSocket
495: .getLocalSocketAddress();
496: theSocket2.bind(inuseAddress, 5);
497: fail("No exception binding to address that is not available");
498: } catch (IOException ex) {
499: // expected
500: }
501: theSocket.close();
502: theSocket2.close();
503:
504: // validate we get ioException when we try to bind to address we
505: // cannot bind to
506: theSocket = new ServerSocket();
507: try {
508: theSocket
509: .bind(
510: new InetSocketAddress(
511: InetAddress
512: .getByAddress(Support_Configuration.nonLocalAddressBytes),
513: 0), 5);
514: fail("No exception was thrown when binding to bad address");
515: } catch (IOException ex) {
516: }
517: theSocket.close();
518:
519: // now validate case where we pass in an unsupported subclass of
520: // SocketAddress
521: theSocket = new ServerSocket();
522: try {
523: theSocket.bind(new mySocketAddress(), 5);
524: fail("Binding using unsupported SocketAddress subclass should have thrown exception");
525: } catch (IllegalArgumentException ex) {
526: }
527: theSocket.close();
528:
529: // now validate that backlog is respected. We have to do a test that
530: // checks if it is a least a certain number as some platforms make
531: // it higher than we request. Unfortunately non-server versions of
532: // windows artificially limit the backlog to 5 and 5 is the
533: // historical default so it it not a great test.
534: theSocket = new ServerSocket();
535: theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
536: 0);
537: theSocket.bind(theAddress, 4);
538: localAddress = theSocket.getLocalSocketAddress();
539: Socket theSockets[] = new Socket[4];
540: int i = 0;
541: try {
542: for (i = 0; i < 4; i++) {
543: theSockets[i] = new Socket();
544: theSockets[i].connect(localAddress);
545: }
546: } catch (ConnectException ex) {
547: fail("Backlog does not seem to be respected in bind:" + i
548: + ":" + ex.toString());
549: }
550:
551: for (i = 0; i < 4; i++) {
552: theSockets[i].close();
553: }
554:
555: theSocket.close();
556: servSock.close();
557: }
558:
559: /**
560: * @tests java.net.ServerSocket#getLocalSocketAddress()
561: */
562: public void test_getLocalSocketAddress() throws Exception {
563: // set up server connect and then validate that we get the right
564: // response for the local address
565: ServerSocket theSocket = new ServerSocket(0, 5, InetAddress
566: .getLocalHost());
567: int portNumber = theSocket.getLocalPort();
568: assertTrue("Returned incorrect InetSocketAddress(1):"
569: + theSocket.getLocalSocketAddress().toString()
570: + "Expected: "
571: + (new InetSocketAddress(InetAddress.getLocalHost(),
572: portNumber)).toString(), theSocket
573: .getLocalSocketAddress().equals(
574: new InetSocketAddress(InetAddress
575: .getLocalHost(), portNumber)));
576: theSocket.close();
577:
578: // now create a socket that is not bound and validate we get the
579: // right answer
580: theSocket = new ServerSocket();
581: assertNull(
582: "Returned incorrect InetSocketAddress -unbound socket- Expected null",
583: theSocket.getLocalSocketAddress());
584:
585: // now bind the socket and make sure we get the right answer
586: theSocket.bind(new InetSocketAddress(
587: InetAddress.getLocalHost(), 0));
588: int localPort = theSocket.getLocalPort();
589: assertEquals("Returned incorrect InetSocketAddress(2):",
590: theSocket.getLocalSocketAddress(),
591: new InetSocketAddress(InetAddress.getLocalHost(),
592: localPort));
593: theSocket.close();
594: }
595:
596: /**
597: * @tests java.net.ServerSocket#isBound()
598: */
599: public void test_isBound() throws IOException {
600: InetAddress addr = InetAddress.getLocalHost();
601: ServerSocket serverSocket = new ServerSocket();
602: assertFalse("Socket indicated bound when it should be (1)",
603: serverSocket.isBound());
604:
605: // now bind and validate bound ok
606: serverSocket.bind(new InetSocketAddress(addr, 0));
607: assertTrue("Socket indicated not bound when it should be (1)",
608: serverSocket.isBound());
609: serverSocket.close();
610:
611: // now do with some of the other constructors
612: serverSocket = new ServerSocket(0);
613: assertTrue("Socket indicated not bound when it should be (2)",
614: serverSocket.isBound());
615: serverSocket.close();
616:
617: serverSocket = new ServerSocket(0, 5, addr);
618: assertTrue("Socket indicated not bound when it should be (3)",
619: serverSocket.isBound());
620: serverSocket.close();
621:
622: serverSocket = new ServerSocket(0, 5);
623: assertTrue("Socket indicated not bound when it should be (4)",
624: serverSocket.isBound());
625: serverSocket.close();
626: }
627:
628: /**
629: * @tests java.net.ServerSocket#isClosed()
630: */
631: public void test_isClosed() throws IOException {
632: InetAddress addr = InetAddress.getLocalHost();
633: ServerSocket serverSocket = new ServerSocket(0, 5, addr);
634:
635: // validate isClosed returns expected values
636: assertFalse("Socket should indicate it is not closed(1):",
637: serverSocket.isClosed());
638: serverSocket.close();
639: assertTrue("Socket should indicate it is closed(1):",
640: serverSocket.isClosed());
641:
642: // now do with some of the other constructors
643: serverSocket = new ServerSocket(0);
644: assertFalse("Socket should indicate it is not closed(1):",
645: serverSocket.isClosed());
646: serverSocket.close();
647: assertTrue("Socket should indicate it is closed(1):",
648: serverSocket.isClosed());
649:
650: serverSocket = new ServerSocket(0, 5, addr);
651: assertFalse("Socket should indicate it is not closed(1):",
652: serverSocket.isClosed());
653: serverSocket.close();
654: assertTrue("Socket should indicate it is closed(1):",
655: serverSocket.isClosed());
656:
657: serverSocket = new ServerSocket(0, 5);
658: assertFalse("Socket should indicate it is not closed(1):",
659: serverSocket.isClosed());
660: serverSocket.close();
661: assertTrue("Socket should indicate it is closed(1):",
662: serverSocket.isClosed());
663: }
664:
665: /**
666: * @tests java.net.ServerSocket#setReuseAddress(boolean)
667: */
668: public void test_setReuseAddressZ() {
669: try {
670: // set up server and connect
671: InetSocketAddress anyAddress = new InetSocketAddress(
672: InetAddress.getLocalHost(), 0);
673: ServerSocket serverSocket = new ServerSocket();
674: serverSocket.setReuseAddress(false);
675: serverSocket.bind(anyAddress);
676: SocketAddress theAddress = serverSocket
677: .getLocalSocketAddress();
678:
679: // make a connection to the server, then close the server
680: Socket theSocket = new Socket();
681: theSocket.connect(theAddress);
682: Socket stillActiveSocket = serverSocket.accept();
683: serverSocket.close();
684:
685: // now try to rebind the server which should fail with
686: // setReuseAddress to false. On windows platforms the bind is
687: // allowed even then reUseAddress is false so our test uses
688: // the platform to determine what the expected result is.
689: String platform = System.getProperty("os.name");
690: try {
691: serverSocket = new ServerSocket();
692: serverSocket.setReuseAddress(false);
693: serverSocket.bind(theAddress);
694: if ((!platform.startsWith("Windows"))) {
695: fail("No exception when setReuseAddress is false and we bind:"
696: + theAddress.toString());
697: }
698: } catch (IOException ex) {
699: if (platform.startsWith("Windows")) {
700: fail("Got unexpected exception when binding with setReuseAddress false on windows platform:"
701: + theAddress.toString()
702: + ":"
703: + ex.toString());
704: }
705: }
706: stillActiveSocket.close();
707: theSocket.close();
708:
709: // now test case were we set it to true
710: anyAddress = new InetSocketAddress(InetAddress
711: .getLocalHost(), 0);
712: serverSocket = new ServerSocket();
713: serverSocket.setReuseAddress(true);
714: serverSocket.bind(anyAddress);
715: theAddress = serverSocket.getLocalSocketAddress();
716:
717: // make a connection to the server, then close the server
718: theSocket = new Socket();
719: theSocket.connect(theAddress);
720: stillActiveSocket = serverSocket.accept();
721: serverSocket.close();
722:
723: // now try to rebind the server which should pass with
724: // setReuseAddress to true
725: try {
726: serverSocket = new ServerSocket();
727: serverSocket.setReuseAddress(true);
728: serverSocket.bind(theAddress);
729: } catch (IOException ex) {
730: fail("Unexpected exception when setReuseAddress is true and we bind:"
731: + theAddress.toString() + ":" + ex.toString());
732: }
733: stillActiveSocket.close();
734: theSocket.close();
735: ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
736:
737: // now test default case were we expect this to work regardless of
738: // the value set
739: anyAddress = new InetSocketAddress(InetAddress
740: .getLocalHost(), 0);
741: serverSocket = new ServerSocket();
742: serverSocket.bind(anyAddress);
743: theAddress = serverSocket.getLocalSocketAddress();
744:
745: // make a connection to the server, then close the server
746: theSocket = new Socket();
747: theSocket.connect(theAddress);
748: stillActiveSocket = serverSocket.accept();
749: serverSocket.close();
750:
751: // now try to rebind the server which should pass
752: try {
753: serverSocket = new ServerSocket();
754: serverSocket.bind(theAddress);
755: } catch (IOException ex) {
756: fail("Unexpected exception when setReuseAddress is the default case and we bind:"
757: + theAddress.toString() + ":" + ex.toString());
758: }
759: stillActiveSocket.close();
760: theSocket.close();
761:
762: ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
763: } catch (Exception e) {
764: handleException(e, SO_REUSEADDR);
765: }
766: }
767:
768: /**
769: * @tests java.net.ServerSocket#getReuseAddress()
770: */
771: public void test_getReuseAddress() {
772: try {
773: ServerSocket theSocket = new ServerSocket();
774: theSocket.setReuseAddress(true);
775: assertTrue("getReuseAddress false when it should be true",
776: theSocket.getReuseAddress());
777: theSocket.setReuseAddress(false);
778: assertFalse("getReuseAddress true when it should be False",
779: theSocket.getReuseAddress());
780: ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
781: } catch (Exception e) {
782: handleException(e, SO_REUSEADDR);
783: }
784: }
785:
786: /**
787: * @tests java.net.ServerSocket#setReceiveBufferSize(int)
788: */
789: public void test_setReceiveBufferSizeI() {
790: try {
791: // now validate case where we try to set to 0
792: ServerSocket theSocket = new ServerSocket();
793: try {
794: theSocket.setReceiveBufferSize(0);
795: fail("No exception when receive buffer size set to 0");
796: } catch (IllegalArgumentException ex) {
797: }
798: theSocket.close();
799:
800: // now validate case where we try to set to a negative value
801: theSocket = new ServerSocket();
802: try {
803: theSocket.setReceiveBufferSize(-1000);
804: fail("No exception when receive buffer size set to -1000");
805: } catch (IllegalArgumentException ex) {
806: }
807: theSocket.close();
808:
809: // now just try to set a good value to make sure it is set and there
810: // are not exceptions
811: theSocket = new ServerSocket();
812: theSocket.setReceiveBufferSize(1000);
813: theSocket.close();
814: ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
815: } catch (Exception e) {
816: handleException(e, SO_RCVBUF);
817: }
818:
819: }
820:
821: /*
822: * @tests java.net.ServerSocket#getReceiveBufferSize()
823: */
824: public void test_getReceiveBufferSize() {
825: try {
826: ServerSocket theSocket = new ServerSocket();
827:
828: // since the value returned is not necessary what we set we are
829: // limited in what we can test
830: // just validate that it is not 0 or negative
831: assertFalse("get Buffer size returns 0:", 0 == theSocket
832: .getReceiveBufferSize());
833: assertFalse("get Buffer size returns a negative value:",
834: 0 > theSocket.getReceiveBufferSize());
835: ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
836: } catch (Exception e) {
837: handleException(e, SO_RCVBUF);
838: }
839: }
840:
841: /**
842: * @tests java.net.ServerSocket#getChannel()
843: */
844: public void test_getChannel() throws Exception {
845: assertNull(new ServerSocket().getChannel());
846: }
847:
848: /*
849: * @tests java.net.ServerSocket#setPerformancePreference()
850: */
851: public void test_setPerformancePreference_Int_Int_Int()
852: throws Exception {
853: ServerSocket theSocket = new ServerSocket();
854: theSocket.setPerformancePreferences(1, 1, 1);
855: }
856:
857: /**
858: * Sets up the fixture, for example, open a network connection. This method
859: * is called before a test is executed.
860: */
861: protected void setUp() {
862: }
863:
864: /**
865: * Tears down the fixture, for example, close a network connection. This
866: * method is called after a test is executed.
867: */
868: protected void tearDown() {
869:
870: try {
871: if (s != null)
872: s.close();
873: if (sconn != null)
874: sconn.close();
875: if (t != null)
876: t.interrupt();
877: } catch (Exception e) {
878: }
879: }
880:
881: /**
882: * Sets up the fixture, for example, open a network connection. This method
883: * is called before a test is executed.
884: */
885: protected void startClient(int port) {
886: t = new Thread(new SSClient(port), "SSClient");
887: t.start();
888: try {
889: Thread.sleep(1000);
890: } catch (InterruptedException e) {
891: System.out.println("Exception during startClinet()"
892: + e.toString());
893: }
894: }
895:
896: /**
897: * @tests java.net.ServerSocket#implAccept
898: */
899: public void test_implAcceptLjava_net_Socket() throws Exception {
900: // regression test for Harmony-1235
901: try {
902: new MockServerSocket().mockImplAccept(new MockSocket(
903: new MockSocketImpl()));
904: } catch (SocketException e) {
905: // expected
906: }
907: }
908:
909: /**
910: * Regression for HARMONY-3265
911: * @throws Exception
912: */
913: public void test_ServerSocket_init() throws Exception {
914: String[] args = new String[] { "org.apache.harmony.luni.tests.java.net.TestServerSocketInit" };
915: Support_Exec.execJava(args, null, true);
916: }
917:
918: static class MockSocketImpl extends SocketImpl {
919: protected void create(boolean arg0) throws IOException {
920: // empty
921: }
922:
923: protected void connect(String arg0, int arg1)
924: throws IOException {
925: // empty
926: }
927:
928: protected void connect(InetAddress arg0, int arg1)
929: throws IOException {
930: // empty
931: }
932:
933: protected void connect(SocketAddress arg0, int arg1)
934: throws IOException {
935: // empty
936: }
937:
938: protected void bind(InetAddress arg0, int arg1)
939: throws IOException {
940: // empty
941: }
942:
943: protected void listen(int arg0) throws IOException {
944: // empty
945: }
946:
947: protected void accept(SocketImpl arg0) throws IOException {
948: // empty
949: }
950:
951: protected InputStream getInputStream() throws IOException {
952: return null;
953: }
954:
955: protected OutputStream getOutputStream() throws IOException {
956: return null;
957: }
958:
959: protected int available() throws IOException {
960: return 0;
961: }
962:
963: protected void close() throws IOException {
964: // empty
965: }
966:
967: protected void sendUrgentData(int arg0) throws IOException {
968: // empty
969: }
970:
971: public void setOption(int arg0, Object arg1)
972: throws SocketException {
973: // empty
974: }
975:
976: public Object getOption(int arg0) throws SocketException {
977: return null;
978: }
979: }
980:
981: static class MockSocket extends Socket {
982: public MockSocket(SocketImpl impl) throws SocketException {
983: super (impl);
984: }
985: }
986:
987: static class MockServerSocket extends ServerSocket {
988: public MockServerSocket() throws Exception {
989: super ();
990: }
991:
992: public void mockImplAccept(Socket s) throws Exception {
993: super.implAccept(s);
994: }
995: }
996: }
|