Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

dalvik: Commit

dalvik


Commit MetaInfo

Revision275acdff64a58f407cec552e7027cb07414b48ef (tree)
Time2010-03-03 16:58:39
AuthorElliott Hughes <enh@goog...>
CommiterElliott Hughes

Log Message

Copy Froyo's version of MulticastSocketTest to Eclair for CTS.

Partners are seeing test failures from the old tests, which were very bogus.

Change Summary

Incremental Difference

--- a/libcore/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java
+++ b/libcore/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java
@@ -17,11 +17,6 @@
1717
1818 package tests.api.java.net;
1919
20-import dalvik.annotation.KnownFailure;
21-import dalvik.annotation.TestTargetClass;
22-import dalvik.annotation.TestLevel;
23-import dalvik.annotation.TestTargetNew;
24-
2520 import java.io.IOException;
2621 import java.net.BindException;
2722 import java.net.DatagramPacket;
@@ -34,290 +29,205 @@ import java.net.NetworkInterface;
3429 import java.net.SocketAddress;
3530 import java.net.SocketException;
3631 import java.net.UnknownHostException;
37-import java.security.Permission;
32+import java.util.ArrayList;
3833 import java.util.Enumeration;
39-import java.util.concurrent.ArrayBlockingQueue;
40-import java.util.concurrent.BlockingQueue;
41-import java.util.concurrent.TimeUnit;
4234
4335 import tests.support.Support_NetworkInterface;
4436 import tests.support.Support_PortManager;
4537
46-@TestTargetClass(MulticastSocket.class)
4738 public class MulticastSocketTest extends SocketTestCase {
4839
49- Thread t;
40+ Thread t;
41+
42+ MulticastSocket mss;
5043
51- MulticastSocket mss;
44+ MulticastServer server;
5245
53- MulticastServer server;
46+ // private member variables used for tests
47+ boolean atLeastOneInterface = false;
5448
55- // private member variables used for tests
56- boolean atLeastOneInterface = false;
49+ boolean atLeastTwoInterfaces = false;
5750
58- boolean atLeastTwoInterfaces = false;
51+ private NetworkInterface networkInterface1 = null;
5952
60- private NetworkInterface networkInterface1 = null;
53+ private NetworkInterface networkInterface2 = null;
6154
62- private NetworkInterface networkInterface2 = null;
55+ private NetworkInterface IPV6networkInterface1 = null;
6356
64- private NetworkInterface IPV6networkInterface1 = null;
57+ static class MulticastServer extends Thread {
6558
66- static class MulticastServer extends Thread {
59+ public MulticastSocket ms;
6760
68- public MulticastSocket ms;
61+ boolean running = true;
6962
70- volatile boolean running = true;
63+ volatile public byte[] rbuf = new byte[512];
7164
72- private final BlockingQueue<DatagramPacket> queue
73- = new ArrayBlockingQueue<DatagramPacket>(1);
65+ volatile DatagramPacket rdp = null;
66+
67+ private InetAddress groupAddr = null;
68+ private SocketAddress groupSockAddr = null;
69+ private NetworkInterface groupNI = null;
7470
7571 public void run() {
76- try {
72+ try {
73+ byte[] tmpbuf = new byte[512];
74+ DatagramPacket tmpPack =
75+ new DatagramPacket(tmpbuf, tmpbuf.length);
76+
7777 while (running) {
78- try {
79- byte[] rbuf = new byte[512];
80- rbuf[0] = -1;
81- DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
82- ms.receive(rdp);
83- queue.put(rdp);
78+ try {
79+ ms.receive(tmpPack);
80+
81+ System.arraycopy(tmpPack.getData(), 0, rdp.getData(),
82+ rdp.getOffset(), tmpPack.getLength());
83+ rdp.setLength(tmpPack.getLength());
84+ rdp.setAddress(tmpPack.getAddress());
85+ rdp.setPort(tmpPack.getPort());
8486 } catch (java.io.InterruptedIOException e) {
85- } catch (InterruptedException e) {
86- }
87+ Thread.yield();
88+ }
89+ }
90+ } catch (java.io.IOException e) {
91+ System.out.println("Multicast server failed: " + e);
92+ } finally {
93+ ms.close();
94+ }
95+ }
96+
97+ public void stopServer() {
98+ running = false;
99+ try {
100+ if (groupAddr != null) {
101+ ms.leaveGroup(groupAddr);
102+ } else if (groupSockAddr != null) {
103+ ms.leaveGroup(groupSockAddr, groupNI);
87104 }
88- } catch (java.io.IOException e) {
89- System.out.println("Multicast server failed: " + e);
90- } finally {
91- ms.close();
92- }
93- }
94-
95- public synchronized void leaveGroup(InetAddress aGroup)
96- throws java.io.IOException {
97- ms.leaveGroup(aGroup);
105+ } catch (IOException e) {}
98106 }
99107
100108 public MulticastServer(InetAddress anAddress, int aPort)
101109 throws java.io.IOException {
110+ rbuf = new byte[512];
111+ rbuf[0] = -1;
112+ rdp = new DatagramPacket(rbuf, rbuf.length);
102113 ms = new MulticastSocket(aPort);
103114 ms.setSoTimeout(2000);
104- ms.joinGroup(anAddress);
115+ groupAddr = anAddress;
116+ ms.joinGroup(groupAddr);
105117 }
106-
118+
119+
107120 public MulticastServer(SocketAddress anAddress, int aPort,
108- NetworkInterface netInterface) throws java.io.IOException {
109- ms = new MulticastSocket(aPort);
110- ms.setSoTimeout(2000);
111- ms.joinGroup(anAddress, netInterface);
112- }
113-
114- public DatagramPacket receive() throws InterruptedException {
115- return queue.poll(1000, TimeUnit.MILLISECONDS);
116- }
117-
118- public void stopServer() throws InterruptedException {
119- running = false;
120- interrupt();
121- join();
122- }
123- }
124-
125- /**
126- * @tests java.net.MulticastSocket#MulticastSocket()
127- */
128- @TestTargetNew(
129- level = TestLevel.SUFFICIENT,
130- notes = "IOException exception checking missed.",
131- method = "MulticastSocket",
132- args = {}
133- )
134- public void test_Constructor() throws IOException {
135- // regression test for 497
121+ NetworkInterface netInterface) throws java.io.IOException {
122+ rbuf = new byte[512];
123+ rbuf[0] = -1;
124+ rdp = new DatagramPacket(rbuf, rbuf.length);
125+ ms = new MulticastSocket(aPort);
126+ ms.setSoTimeout(2000);
127+ groupSockAddr = anAddress;
128+ groupNI = netInterface;
129+ ms.joinGroup(groupSockAddr, groupNI);
130+ }
131+ }
132+
133+ /**
134+ * @tests java.net.MulticastSocket#MulticastSocket()
135+ */
136+ public void test_Constructor() throws IOException {
137+ // regression test for 497
136138 MulticastSocket s = new MulticastSocket();
137139 // regression test for Harmony-1162
138140 assertTrue(s.getReuseAddress());
139-
140- SecurityManager sm = new SecurityManager() {
141-
142- public void checkPermission(Permission perm) {
143- }
144-
145- public void checkListen(int port) {
146- throw new SecurityException();
147- }
148- };
149-
150- SecurityManager oldSm = System.getSecurityManager();
151- System.setSecurityManager(sm);
152- try {
153- new MulticastSocket();
154- fail("SecurityException should be thrown.");
155- } catch (SecurityException e) {
156- // expected
157- } catch (SocketException e) {
158- fail("SocketException was thrown.");
159- } catch (IOException e) {
160- fail("IOException was thrown.");
161- e.printStackTrace();
162- } finally {
163- System.setSecurityManager(oldSm);
164- }
165- }
166-
167- /**
168- * @tests java.net.MulticastSocket#MulticastSocket(int)
169- */
170- @TestTargetNew(
171- level = TestLevel.SUFFICIENT,
172- notes = "IOException exception checking missed.",
173- method = "MulticastSocket",
174- args = {int.class}
175- )
176- public void test_ConstructorI() {
177- // Test for method java.net.MulticastSocket(int)
178- // Used in tests
179- MulticastSocket dup = null;
180- try {
181- mss = new MulticastSocket();
182- int port = mss.getLocalPort();
183- dup = new MulticastSocket(port);
141+ }
142+
143+ /**
144+ * @tests java.net.MulticastSocket#MulticastSocket(int)
145+ */
146+ public void test_ConstructorI() throws IOException {
147+ MulticastSocket orig = new MulticastSocket();
148+ int port = orig.getLocalPort();
149+ orig.close();
150+ MulticastSocket dup = null;
151+ try {
152+ dup = new MulticastSocket(port);
184153 // regression test for Harmony-1162
185154 assertTrue(dup.getReuseAddress());
186- } catch (IOException e) {
187- fail("duplicate binding not allowed: " + e);
188- }
189-
190-
191-
192- if (dup != null)
193- dup.close();
194-
195- SecurityManager sm = new SecurityManager() {
196-
197- public void checkPermission(Permission perm) {
198- }
199-
200- public void checkListen(int port) {
201- throw new SecurityException();
202- }
203- };
204-
205- SecurityManager oldSm = System.getSecurityManager();
206- System.setSecurityManager(sm);
207- try {
208- new MulticastSocket(1);
209- fail("SecurityException should be thrown.");
210- } catch (SecurityException e) {
211- // expected
212- } catch (SocketException e) {
213- fail("SocketException was thrown.");
214- } catch (IOException e) {
215- fail("IOException was thrown.");
216- e.printStackTrace();
217- } finally {
218- System.setSecurityManager(oldSm);
219- }
220- }
221-
222- /**
223- * @tests java.net.MulticastSocket#getInterface()
224- */
225- @TestTargetNew(
226- level = TestLevel.COMPLETE,
227- notes = "",
228- method = "getInterface",
229- args = {}
230- )
231- @KnownFailure("No interfaces if there's no debugger connected")
232- public void test_getInterface() {
233- // Test for method java.net.InetAddress
234- // java.net.MulticastSocket.getInterface()
235- assertTrue("Used for testing.", true);
155+ } catch (IOException e) {
156+ fail("duplicate binding not allowed: " + e);
157+ }
158+ if (dup != null)
159+ dup.close();
160+ }
161+
162+ /**
163+ * @tests java.net.MulticastSocket#getInterface()
164+ */
165+ public void test_getInterface() throws Exception {
166+ // Test for method java.net.InetAddress
167+ // java.net.MulticastSocket.getInterface()
168+ assertTrue("Used for testing.", true);
169+
170+ int groupPort = Support_PortManager.getNextPortForUDP();
171+
172+ if (atLeastOneInterface) {
173+ // validate that we get the expected response when one was not
174+ // set
175+ mss = new MulticastSocket(groupPort);
176+ String preferIPv4StackValue = System
177+ .getProperty("java.net.preferIPv4Stack");
178+ String preferIPv6AddressesValue = System
179+ .getProperty("java.net.preferIPv6Addresses");
180+ if (((preferIPv4StackValue == null) || preferIPv4StackValue
181+ .equalsIgnoreCase("false"))
182+ && (preferIPv6AddressesValue != null)
183+ && (preferIPv6AddressesValue.equals("true"))) {
184+ // we expect an IPv6 ANY in this case
185+ assertEquals("inet Address returned when not set",
186+ InetAddress.getByName("::0"),
187+ mss.getInterface());
188+ } else {
189+ // we expect an IPv4 ANY in this case
190+ assertEquals("inet Address returned when not set",
191+ InetAddress.getByName("0.0.0.0"),
192+ mss.getInterface());
193+ }
236194
237- int groupPort = Support_PortManager.getNextPortForUDP();
238- try {
239- if (atLeastOneInterface) {
240- // validate that we get the expected response when one was not
241- // set
242- mss = new MulticastSocket(groupPort);
243- String preferIPv4StackValue = System
244- .getProperty("java.net.preferIPv4Stack");
245- String preferIPv6AddressesValue = System
246- .getProperty("java.net.preferIPv6Addresses");
247- if (((preferIPv4StackValue == null) || preferIPv4StackValue
248- .equalsIgnoreCase("false"))
249- && (preferIPv6AddressesValue != null)
250- && (preferIPv6AddressesValue.equals("true"))) {
251- // we expect an IPv6 ANY in this case
252- assertTrue("inet Address returned when not set:"
253- + mss.getInterface().toString(), InetAddress
254- .getByName("::0").equals(mss.getInterface()));
255- } else {
256- // we expect an IPv4 ANY in this case
257- assertNotNull("inet Address returned when not set:",
258- mss.getInterface());
259- }
195+ // validate that we get the expected response when we set via
196+ // setInterface
197+ Enumeration addresses = networkInterface1.getInetAddresses();
198+ if (addresses.hasMoreElements()) {
199+ InetAddress firstAddress = (InetAddress) addresses
200+ .nextElement();
201+ mss.setInterface(firstAddress);
202+ assertEquals("getNetworkInterface did not return interface set by setInterface",
203+ firstAddress, mss.getInterface());
204+
205+ groupPort = Support_PortManager.getNextPortForUDP();
206+ mss = new MulticastSocket(groupPort);
207+ mss.setNetworkInterface(networkInterface1);
208+ assertEquals("getInterface did not return interface set by setNetworkInterface",
209+ networkInterface1,
210+ NetworkInterface.getByInetAddress(mss.getInterface()));
211+ }
260212
261- // validate that we get the expected response when we set via
262- // setInterface
263- Enumeration addresses = networkInterface1.getInetAddresses();
264- if (addresses != null) {
265- InetAddress firstAddress = (InetAddress) addresses
266- .nextElement();
267- mss.setInterface(firstAddress);
268- assertTrue(
269- "getNetworkInterface did not return interface set " +
270- "by setInterface. Expected:"
271- + firstAddress + " Got:"
272- + mss.getInterface(), firstAddress
273- .equals(mss.getInterface()));
274-
275- groupPort = Support_PortManager.getNextPortForUDP();
276- mss = new MulticastSocket(groupPort);
277- mss.setNetworkInterface(networkInterface1);
278- InetAddress addr = mss.getInterface();
279- NetworkInterface if1 = NetworkInterface.getByInetAddress(addr);
280- assertEquals(
281- "getInterface did not return interface set by " +
282- "setNeworkInterface Expected: " + firstAddress
283- + "Got:" + mss.getInterface(),
284- networkInterface1, if1);
285- }
286- mss.close();
287- try {
288- mss.getInterface();
289- fail("SocketException was not thrown.");
290- } catch(SocketException ioe) {
291- //expected
292213 }
293- }
294- } catch (Exception e) {
295- fail("Exception during getInterface test: " + e.toString());
296- }
297- }
214+ }
298215
299- /**
300- * @throws IOException
301- * @tests java.net.MulticastSocket#getNetworkInterface()
302- */
303- @TestTargetNew(
304- level = TestLevel.COMPLETE,
305- notes = "",
306- method = "getNetworkInterface",
307- args = {}
308- )
309- @KnownFailure("No interfaces if there's no debugger connected")
310- public void test_getNetworkInterface() throws IOException {
216+ /**
217+ * @throws IOException
218+ * @tests java.net.MulticastSocket#getNetworkInterface()
219+ */
220+ public void test_getNetworkInterface() throws IOException {
311221 int groupPort = Support_PortManager.getNextPortForUDP();
312222 if (atLeastOneInterface) {
313223 // validate that we get the expected response when one was not
314224 // set
315225 mss = new MulticastSocket(groupPort);
316226 NetworkInterface theInterface = mss.getNetworkInterface();
317- assertNotNull(
318- "network interface returned wrong network interface when " +
319- "not set:"
320- + theInterface, theInterface.getInetAddresses());
227+ assertTrue(
228+ "network interface returned wrong network interface when not set:"
229+ + theInterface, theInterface.getInetAddresses()
230+ .hasMoreElements());
321231 InetAddress firstAddress = (InetAddress) theInterface
322232 .getInetAddresses().nextElement();
323233 // validate we the first address in the network interface is the
@@ -330,42 +240,34 @@ public class MulticastSocketTest extends SocketTestCase {
330240 .equalsIgnoreCase("false"))
331241 && (preferIPv6AddressesValue != null)
332242 && (preferIPv6AddressesValue.equals("true"))) {
333- assertTrue(
334- "network interface returned wrong network interface " +
335- "when not set:"
336- + theInterface, InetAddress.getByName("::0")
337- .equals(firstAddress));
243+ assertEquals("network interface returned wrong network interface when not set:"
244+ + theInterface,
245+ firstAddress, InetAddress.getByName("::0"));
338246
339247 } else {
340- assertTrue(
341- "network interface returned wrong network interface " +
342- "when not set:"
343- + theInterface, InetAddress
344- .getByName("0.0.0.0").equals(firstAddress));
248+ assertEquals("network interface returned wrong network interface when not set:"
249+ + theInterface,
250+ InetAddress.getByName("0.0.0.0"),
251+ firstAddress);
345252 }
346253
347254 mss.setNetworkInterface(networkInterface1);
348- assertTrue(
349- "getNetworkInterface did not return interface set by " +
350- "setNeworkInterface",
351- networkInterface1.equals(mss.getNetworkInterface()));
255+ assertEquals("getNetworkInterface did not return interface set by setNeworkInterface",
256+ networkInterface1, mss.getNetworkInterface());
352257
353258 if (atLeastTwoInterfaces) {
354259 mss.setNetworkInterface(networkInterface2);
355- assertTrue(
356- "getNetworkInterface did not return network interface " +
357- "set by second setNetworkInterface call",
358- networkInterface2.equals(mss.getNetworkInterface()));
260+ assertEquals("getNetworkInterface did not return network interface set by second setNetworkInterface call",
261+ networkInterface2, mss.getNetworkInterface());
359262 }
360263
361264 groupPort = Support_PortManager.getNextPortForUDP();
362265 mss = new MulticastSocket(groupPort);
363266 if (IPV6networkInterface1 != null) {
364267 mss.setNetworkInterface(IPV6networkInterface1);
365- assertTrue(
366- "getNetworkInterface did not return interface set " +
367- "by setNeworkInterface",
368- IPV6networkInterface1.equals(mss.getNetworkInterface()));
268+ assertEquals("getNetworkInterface did not return interface set by setNeworkInterface",
269+ IPV6networkInterface1,
270+ mss.getNetworkInterface());
369271 }
370272
371273 // validate that we get the expected response when we set via
@@ -373,192 +275,100 @@ public class MulticastSocketTest extends SocketTestCase {
373275 groupPort = Support_PortManager.getNextPortForUDP();
374276 mss = new MulticastSocket(groupPort);
375277 Enumeration addresses = networkInterface1.getInetAddresses();
376- if (addresses != null) {
278+ if (addresses.hasMoreElements()) {
377279 firstAddress = (InetAddress) addresses.nextElement();
378280 mss.setInterface(firstAddress);
379- assertTrue(
380- "getNetworkInterface did not return interface set " +
381- "by setInterface",
382- networkInterface1.equals(mss.getNetworkInterface()));
383- }
384-
385- mss.close();
386- try {
387- mss.getNetworkInterface();
388- fail("SocketException was not thrown.");
389- } catch(SocketException ioe) {
390- //expected
391- }
392- }
393- }
394-
395- /**
396- * @tests java.net.MulticastSocket#getTimeToLive()
397- */
398- @TestTargetNew(
399- level = TestLevel.COMPLETE,
400- notes = "",
401- method = "getTimeToLive",
402- args = {}
403- )
404- public void test_getTimeToLive() {
405- try {
406- mss = new MulticastSocket();
407- mss.setTimeToLive(120);
408- assertTrue("Returned incorrect 1st TTL: " + mss.getTimeToLive(),
409- mss.getTimeToLive() == 120);
410- mss.setTimeToLive(220);
411- assertTrue("Returned incorrect 2nd TTL: " + mss.getTimeToLive(),
412- mss.getTimeToLive() == 220);
413- mss.close();
414- try {
415- mss.getTimeToLive();
416- fail("IOException was not thrown.");
417- } catch(IOException ioe) {
418- //expected
419- }
420- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
421- } catch (Exception e) {
422- handleException(e, SO_MULTICAST);
423- }
424- }
425-
426- /**
427- * @tests java.net.MulticastSocket#getTTL()
428- */
429- @TestTargetNew(
430- level = TestLevel.COMPLETE,
431- notes = "",
432- method = "getTTL",
433- args = {}
434- )
435- public void test_getTTL() {
436- // Test for method byte java.net.MulticastSocket.getTTL()
437-
438- try {
439- mss = new MulticastSocket();
440- mss.setTTL((byte) 120);
441- assertTrue("Returned incorrect TTL: " + mss.getTTL(),
442- mss.getTTL() == 120);
443- mss.close();
444- try {
445- mss.getTTL();
446- fail("IOException was not thrown.");
447- } catch(IOException ioe) {
448- //expected
281+ assertEquals("getNetworkInterface did not return interface set by setInterface",
282+ networkInterface1,
283+ mss.getNetworkInterface());
449284 }
450- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
451- } catch (Exception e) {
452- handleException(e, SO_MULTICAST);
453285 }
454286 }
455287
456- /**
457- * @tests java.net.MulticastSocket#joinGroup(java.net.InetAddress)
458- */
459- @TestTargetNew(
460- level = TestLevel.COMPLETE,
461- notes = "",
462- method = "joinGroup",
463- args = {java.net.InetAddress.class}
464- )
465- public void test_joinGroupLjava_net_InetAddress() throws InterruptedException {
466- // Test for method void
467- // java.net.MulticastSocket.joinGroup(java.net.InetAddress)
468- String msg = null;
469- InetAddress group = null;
470- int[] ports = Support_PortManager.getNextPortsForUDP(2);
471- int groupPort = ports[0];
472- try {
473- group = InetAddress.getByName("224.0.0.3");
474- server = new MulticastServer(group, groupPort);
475- server.start();
476- Thread.sleep(1000);
477- msg = "Hello World";
478- mss = new MulticastSocket(ports[1]);
479- DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
480- .length(), group, groupPort);
481- mss.joinGroup(group);
482- mss.send(sdp, (byte) 10);
483-
484- SecurityManager sm = new SecurityManager() {
485-
486- public void checkPermission(Permission perm) {
487- }
488-
489- public void checkMulticast(InetAddress maddr) {
490- throw new SecurityException();
491- }
492- };
493-
494- SecurityManager oldSm = System.getSecurityManager();
495- System.setSecurityManager(sm);
496- try {
497- mss.joinGroup(group);
498- fail("SecurityException should be thrown.");
499- } catch (SecurityException e) {
500- // expected
501- } catch (SocketException e) {
502- fail("SocketException was thrown.");
503- } catch (IOException e) {
504- fail("IOException was thrown.");
505- e.printStackTrace();
506- } finally {
507- System.setSecurityManager(oldSm);
508- }
509-
510- mss.close();
511- try {
512- mss.joinGroup(group);
513- fail("SocketException was not thrown.");
514- } catch(SocketException ioe) {
515- //expected
516- }
517- } catch (Exception e) {
518- fail("Exception during joinGroup test: " + e.toString());
519- }
520- DatagramPacket rdb = server.receive();
521-
522- assertEquals("Group member did not recv data: ", msg,
523- new String(rdb.getData(), 0, rdb.getLength()));
524-
525- }
526-
527- /**
528- * @throws IOException
529- * @throws InterruptedException
530- * @tests java.net.MulticastSocket#joinGroup(java.net.SocketAddress,
531- * java.net.NetworkInterface)
532- */
533- @TestTargetNew(
534- level = TestLevel.COMPLETE,
535- notes = "",
536- method = "joinGroup",
537- args = {java.net.SocketAddress.class, java.net.NetworkInterface.class}
538- )
539- @KnownFailure(value="bug 2155705")
540- public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface()
541- throws IOException, InterruptedException {
542- // security manager that allows us to check that we only return the
543- // addresses that we should
544- class mySecurityManager extends SecurityManager {
545-
546- public void checkPermission(Permission perm) {
547- }
548-
549- public void checkMulticast(InetAddress address) {
550- throw new SecurityException("not allowed");
551- }
552- }
553-
554- String msg = null;
555- InetAddress group = null;
556- SocketAddress groupSockAddr = null;
557- int[] ports = Support_PortManager.getNextPortsForUDP(2);
558- int groupPort = ports[0];
559- int serverPort = ports[1];
560-
561- Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
288+ /**
289+ * @tests java.net.MulticastSocket#getTimeToLive()
290+ */
291+ public void test_getTimeToLive() {
292+ try {
293+ mss = new MulticastSocket();
294+ mss.setTimeToLive(120);
295+ assertEquals("Returned incorrect 1st TTL",
296+ 120, mss.getTimeToLive());
297+ mss.setTimeToLive(220);
298+ assertEquals("Returned incorrect 2nd TTL",
299+ 220, mss.getTimeToLive());
300+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
301+ } catch (Exception e) {
302+ handleException(e, SO_MULTICAST);
303+ }
304+ }
305+
306+ /**
307+ * @tests java.net.MulticastSocket#getTTL()
308+ */
309+ public void test_getTTL() {
310+ // Test for method byte java.net.MulticastSocket.getTTL()
311+
312+ try {
313+ mss = new MulticastSocket();
314+ mss.setTTL((byte) 120);
315+ assertEquals("Returned incorrect TTL",
316+ 120, mss.getTTL());
317+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
318+ } catch (Exception e) {
319+ handleException(e, SO_MULTICAST);
320+ }
321+ }
322+
323+ /**
324+ * @tests java.net.MulticastSocket#joinGroup(java.net.InetAddress)
325+ */
326+ public void test_joinGroupLjava_net_InetAddress() throws Exception {
327+ // Test for method void
328+ // java.net.MulticastSocket.joinGroup(java.net.InetAddress)
329+ String msg = null;
330+ InetAddress group = null;
331+ int[] ports = Support_PortManager.getNextPortsForUDP(2);
332+ int groupPort = ports[0];
333+ group = InetAddress.getByName("224.0.0.3");
334+ server = new MulticastServer(group, groupPort);
335+ server.start();
336+ Thread.sleep(1000);
337+ msg = "Hello World";
338+ mss = new MulticastSocket(ports[1]);
339+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
340+ .length(), group, groupPort);
341+ mss.send(sdp, (byte) 10);
342+ Thread.sleep(1000);
343+
344+ assertEquals("Group member did not recv data",
345+ msg,
346+ new String(server.rdp.getData(), 0, server.rdp.getLength()));
347+ }
348+
349+ /**
350+ * @throws IOException
351+ * @throws InterruptedException
352+ * @tests java.net.MulticastSocket#joinGroup(java.net.SocketAddress,java.net.NetworkInterface)
353+ */
354+ public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface() throws IOException, InterruptedException {
355+ // security manager that allows us to check that we only return the
356+ // addresses that we should
357+ class mySecurityManager extends SecurityManager {
358+
359+ public void checkMulticast(InetAddress address) {
360+ throw new SecurityException("not allowed");
361+ }
362+ }
363+
364+ String msg = null;
365+ InetAddress group = null;
366+ SocketAddress groupSockAddr = null;
367+ int[] ports = Support_PortManager.getNextPortsForUDP(2);
368+ int groupPort = ports[0];
369+ int serverPort = ports[1];
370+
371+ Enumeration<NetworkInterface> theInterfaces = NetworkInterface.getNetworkInterfaces();
562372
563373 // first validate that we handle a null group ok
564374 mss = new MulticastSocket(groupPort);
@@ -614,11 +424,11 @@ public class MulticastSocketTest extends SocketTestCase {
614424 .length(), group, serverPort);
615425 mss.setTimeToLive(2);
616426 mss.send(sdp);
617- DatagramPacket rdp = server.receive();
618-
427+ Thread.sleep(1000);
619428 // now vaildate that we received the data as expected
620- assertEquals("Group member did not recv data: ", msg,
621- new String(rdp.getData(), 0, rdp.getLength()));
429+ assertEquals("Group member did not recv data",
430+ msg,
431+ new String(server.rdp.getData(), 0, server.rdp.getLength()));
622432 server.stopServer();
623433
624434 // now validate that we handled the case were we join a
@@ -639,9 +449,11 @@ public class MulticastSocketTest extends SocketTestCase {
639449 sdp = new DatagramPacket(msg.getBytes(), msg.length(), group2,
640450 serverPort);
641451 mss.send(sdp);
642- rdp = server.receive();
643- assertNull("Group member received data when sent on different group",
644- rdp);
452+ Thread.sleep(1000);
453+ assertFalse(
454+ "Group member received data when sent on different group: ",
455+ new String(server.rdp.getData(), 0, server.rdp.getLength())
456+ .equals(msg));
645457 server.stopServer();
646458
647459 // if there is more than one network interface then check that
@@ -652,39 +464,47 @@ public class MulticastSocketTest extends SocketTestCase {
652464 NetworkInterface loopbackInterface = NetworkInterface
653465 .getByInetAddress(InetAddress.getByName("127.0.0.1"));
654466
467+ boolean anyLoop = networkInterface1.equals(loopbackInterface) || networkInterface2.equals(loopbackInterface);
468+
469+ ArrayList<NetworkInterface> realInterfaces = new ArrayList<NetworkInterface>();
655470 theInterfaces = NetworkInterface.getNetworkInterfaces();
656471 while (theInterfaces.hasMoreElements()) {
657-
658- NetworkInterface thisInterface = (NetworkInterface)
659- theInterfaces.nextElement();
660- if ((thisInterface.getInetAddresses() != null && thisInterface
661- .getInetAddresses().hasMoreElements())
472+ NetworkInterface thisInterface = (NetworkInterface) theInterfaces.nextElement();
473+ if (thisInterface.getInetAddresses().hasMoreElements()
662474 && (Support_NetworkInterface
663- .useInterface(thisInterface) == true)) {
475+ .useInterface(thisInterface) == true)){
476+ realInterfaces.add(thisInterface);
477+ }
478+ }
479+
480+ for (int i = 0; i < realInterfaces.size(); i++) {
481+ final int SECOND = 1;
482+ NetworkInterface thisInterface = realInterfaces.get(i);
483+
664484 // get the first address on the interface
665485
666486 // start server which is joined to the group and has
667487 // only asked for packets on this interface
668- Enumeration addresses = thisInterface
669- .getInetAddresses();
488+ Enumeration<InetAddress> addresses = thisInterface.getInetAddresses();
670489
671490 NetworkInterface sendingInterface = null;
672- boolean isLoopback = false;
673- if (addresses != null) {
674- InetAddress firstAddress = (InetAddress) addresses
675- .nextElement();
676- if (firstAddress.isLoopbackAddress()) {
677- isLoopback = true;
678- }
491+ if (addresses.hasMoreElements()) {
492+ InetAddress firstAddress = (InetAddress) addresses.nextElement();
679493 if (firstAddress instanceof Inet4Address) {
680494 group = InetAddress.getByName("224.0.0.4");
681- if (networkInterface1.equals(NetworkInterface
682- .getByInetAddress(InetAddress
683- .getByName("127.0.0.1")))) {
684- sendingInterface = networkInterface2;
495+ if (anyLoop) {
496+ if (networkInterface1.equals(loopbackInterface)) {
497+ sendingInterface = networkInterface2;
498+ } else {
499+ sendingInterface = networkInterface1;
500+ }
685501 } else {
686- sendingInterface = networkInterface1;
502+ if(i == SECOND){
503+ sendingInterface = networkInterface2;
504+ } else {
505+ sendingInterface = networkInterface1;
687506 }
507+ }
688508 } else {
689509 // if this interface only seems to support
690510 // IPV6 addresses
@@ -694,13 +514,6 @@ public class MulticastSocketTest extends SocketTestCase {
694514 }
695515 }
696516
697- InetAddress useAddress = null;
698- addresses = sendingInterface.getInetAddresses();
699- if ((addresses != null)
700- && (addresses.hasMoreElements())) {
701- useAddress = (InetAddress) addresses.nextElement();
702- }
703-
704517 ports = Support_PortManager.getNextPortsForUDP(2);
705518 serverPort = ports[0];
706519 groupPort = ports[1];
@@ -713,33 +526,27 @@ public class MulticastSocketTest extends SocketTestCase {
713526 // Now send out a package on interface
714527 // networkInterface 1. We should
715528 // only see the packet if we send it on interface 1
716- InetSocketAddress theAddress = new InetSocketAddress(
717- useAddress, groupPort);
718529 mss = new MulticastSocket(groupPort);
719530 mss.setNetworkInterface(sendingInterface);
720531 msg = "Hello World - Again" + thisInterface.getName();
721532 sdp = new DatagramPacket(msg.getBytes(), msg.length(),
722533 group, serverPort);
723534 mss.send(sdp);
724- rdp = server.receive();
725-
535+ Thread.sleep(1000);
726536 if (thisInterface.equals(sendingInterface)) {
727- assertEquals(
728- "Group member did not recv data when " +
729- "bound on specific interface: ", msg,
730- new String(rdp.getData(), 0, rdp.getLength()));
537+ assertEquals("Group member did not recv data when bound on specific interface",
538+ msg,
539+ new String(server.rdp.getData(), 0, server.rdp.getLength()));
731540 } else {
732541 assertFalse(
733- "Group member received data on other " +
734- "interface when only asked for it on one " +
735- "interface: ",
736- new String(rdp.getData(), 0,
737- rdp.getLength()).equals(msg));
542+ "Group member received data on other interface when only asked for it on one interface: ",
543+ new String(server.rdp.getData(), 0,
544+ server.rdp.getLength()).equals(msg));
738545 }
739546
740547 server.stopServer();
741548 }
742- }
549+
743550
744551 // validate that we can join the same address on two
745552 // different interfaces but not on the same interface
@@ -749,374 +556,252 @@ public class MulticastSocketTest extends SocketTestCase {
749556 mss.joinGroup(groupSockAddr, networkInterface2);
750557 try {
751558 mss.joinGroup(groupSockAddr, networkInterface1);
752- fail("Did not get expected exception when joining for " +
753- "second time on same interface");
559+ fail("Did not get expected exception when joining for second time on same interface");
754560 } catch (IOException e) {
755561 }
756562 }
757563 }
758- System.setSecurityManager(null);
759- }
760-
761- /**
762- * @tests java.net.MulticastSocket#leaveGroup(java.net.InetAddress)
763- */
764- @TestTargetNew(
765- level = TestLevel.COMPLETE,
766- notes = "",
767- method = "leaveGroup",
768- args = {java.net.InetAddress.class}
769- )
770- public void test_leaveGroupLjava_net_InetAddress() {
771- // Test for method void
772- // java.net.MulticastSocket.leaveGroup(java.net.InetAddress)
773- String msg = null;
774- boolean except = false;
775- InetAddress group = null;
776- int[] ports = Support_PortManager.getNextPortsForUDP(2);
777- int groupPort = ports[0];
778-
779- try {
780- group = InetAddress.getByName("224.0.0.3");
781- msg = "Hello World";
782- mss = new MulticastSocket(ports[1]);
783- DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
784- .length(), group, groupPort);
785- mss.send(sdp, (byte) 10);
786- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
787- } catch (Exception e) {
788- handleException(e, SO_MULTICAST);
789- }
790- try {
791- // Try to leave s group that mss is not a member of
792- mss.leaveGroup(group);
793- } catch (java.io.IOException e) {
794- // Correct
795- except = true;
796- }
797- assertTrue("Failed to throw exception leaving non-member group", except);
798-
799- SecurityManager sm = new SecurityManager() {
800-
801- public void checkPermission(Permission perm) {
802- }
803-
804- public void checkMulticast(InetAddress maddr) {
805- throw new SecurityException();
806- }
807- };
808-
809- SecurityManager oldSm = System.getSecurityManager();
810- System.setSecurityManager(sm);
811- try {
812- mss.leaveGroup(group);
813- fail("SecurityException should be thrown.");
814- } catch (SecurityException e) {
815- // expected
816- } catch (SocketException e) {
817- fail("SocketException was thrown.");
818- } catch (IOException e) {
819- fail("IOException was thrown.");
820- e.printStackTrace();
821- } finally {
822- System.setSecurityManager(oldSm);
823- }
824- }
825-
826- /**
827- * @tests java.net.MulticastSocket#leaveGroup(java.net.SocketAddress,
828- * java.net.NetworkInterface)
829- */
830- @TestTargetNew(
831- level = TestLevel.COMPLETE,
832- notes = "",
833- method = "leaveGroup",
834- args = {java.net.SocketAddress.class, java.net.NetworkInterface.class}
835- )
836- @KnownFailure(value="bug 2155705")
837- public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface() {
838- // security manager that allows us to check that we only return the
839- // addresses that we should
840- class mySecurityManager extends SecurityManager {
841-
842- public void checkPermission(Permission p) {
843- }
844-
845- public void checkMulticast(InetAddress address) {
846- throw new SecurityException("not allowed");
847- }
848- }
849-
850- String msg = null;
851- InetAddress group = null;
852- int groupPort = Support_PortManager.getNextPortForUDP();
853- SocketAddress groupSockAddr = null;
854- SocketAddress groupSockAddr2 = null;
855-
856- try {
857- Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
858-
859- // first validate that we handle a null group ok
860- mss = new MulticastSocket(groupPort);
861- try {
862- mss.leaveGroup(null, null);
863- fail("Did not get exception when group was null");
864- } catch (IllegalArgumentException e) {
865- }
866-
867- // now validate we get the expected error if the address specified
868- // is not a multicast group
869- try {
870- group = InetAddress.getByName("255.255.255.255");
871- groupSockAddr = new InetSocketAddress(group, groupPort);
872- mss.leaveGroup(groupSockAddr, null);
873- fail("Did not get exception when group is not a " +
874- "multicast address");
875- } catch (IOException e) {
876- }
877-
878- // now try to leave a group if we are not authorized
879- // set the security manager that will make the first address not
880- // visible
881- System.setSecurityManager(new mySecurityManager());
882- try {
883- group = InetAddress.getByName("224.0.0.3");
884- groupSockAddr = new InetSocketAddress(group, groupPort);
885- mss.leaveGroup(groupSockAddr, null);
886- fail("Did not get exception when joining group is " +
887- "not allowed");
888- } catch (SecurityException e) {
889- }
890- System.setSecurityManager(null);
891-
892- if (atLeastOneInterface) {
893-
894- // now test that we can join and leave a group successfully
895- groupPort = Support_PortManager.getNextPortForUDP();
564+ System.setSecurityManager(null);
565+ }
566+
567+ /**
568+ * @tests java.net.MulticastSocket#leaveGroup(java.net.InetAddress)
569+ */
570+ public void test_leaveGroupLjava_net_InetAddress() {
571+ // Test for method void
572+ // java.net.MulticastSocket.leaveGroup(java.net.InetAddress)
573+ String msg = null;
574+ boolean except = false;
575+ InetAddress group = null;
576+ int[] ports = Support_PortManager.getNextPortsForUDP(2);
577+ int groupPort = ports[0];
578+
579+ try {
580+ group = InetAddress.getByName("224.0.0.3");
581+ msg = "Hello World";
582+ mss = new MulticastSocket(ports[1]);
583+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
584+ .length(), group, groupPort);
585+ mss.send(sdp, (byte) 10);
586+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
587+ } catch (Exception e) {
588+ handleException(e, SO_MULTICAST);
589+ }
590+ try {
591+ // Try to leave s group that mss is not a member of
592+ mss.leaveGroup(group);
593+ } catch (java.io.IOException e) {
594+ // Correct
595+ except = true;
596+ }
597+ assertTrue("Failed to throw exception leaving non-member group", except);
598+ }
599+
600+ /**
601+ * @tests java.net.MulticastSocket#leaveGroup(java.net.SocketAddress,java.net.NetworkInterface)
602+ */
603+ public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface() throws Exception {
604+ // security manager that allows us to check that we only return the
605+ // addresses that we should
606+ class mySecurityManager extends SecurityManager {
607+
608+ public void checkMulticast(InetAddress address) {
609+ throw new SecurityException("not allowed");
610+ }
611+ }
612+
613+ String msg = null;
614+ InetAddress group = null;
615+ int groupPort = Support_PortManager.getNextPortForUDP();
616+ SocketAddress groupSockAddr = null;
617+ SocketAddress groupSockAddr2 = null;
618+
619+ Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
620+
621+ // first validate that we handle a null group ok
896622 mss = new MulticastSocket(groupPort);
897- groupSockAddr = new InetSocketAddress(group, groupPort);
898- mss.joinGroup(groupSockAddr, null);
899- mss.leaveGroup(groupSockAddr, null);
900623 try {
901- mss.leaveGroup(groupSockAddr, null);
902- fail(
903- "Did not get exception when trying to leave " +
904- "group that was allready left");
905- } catch (IOException e) {
624+ mss.leaveGroup(null, null);
625+ fail("Did not get exception when group was null");
626+ } catch (IllegalArgumentException e) {
906627 }
907628
908- InetAddress group2 = InetAddress.getByName("224.0.0.4");
909- groupSockAddr2 = new InetSocketAddress(group2, groupPort);
910- mss.joinGroup(groupSockAddr, networkInterface1);
629+ // now validate we get the expected error if the address specified
630+ // is not a multicast group
911631 try {
912- mss.leaveGroup(groupSockAddr2, networkInterface1);
913- fail(
914- "Did not get exception when trying to leave " +
915- "group that was never joined");
632+ group = InetAddress.getByName("255.255.255.255");
633+ groupSockAddr = new InetSocketAddress(group, groupPort);
634+ mss.leaveGroup(groupSockAddr, null);
635+ fail("Did not get exception when group is not a multicast address");
916636 } catch (IOException e) {
917637 }
918638
919- mss.leaveGroup(groupSockAddr, networkInterface1);
920- if (atLeastTwoInterfaces) {
921- mss.joinGroup(groupSockAddr, networkInterface1);
922- try {
923- mss.leaveGroup(groupSockAddr, networkInterface2);
924- fail(
925- "Did not get exception when trying to leave " +
926- "group on wrong interface joined on ["
927- + networkInterface1
928- + "] left on ["
929- + networkInterface2 + "]");
930- } catch (IOException e) {
931- }
639+ // now try to leave a group if we are not authorized
640+ // set the security manager that will make the first address not
641+ // visible
642+ System.setSecurityManager(new mySecurityManager());
643+ try {
644+ group = InetAddress.getByName("224.0.0.3");
645+ groupSockAddr = new InetSocketAddress(group, groupPort);
646+ mss.leaveGroup(groupSockAddr, null);
647+ fail("Did not get exception when joining group is not allowed");
648+ } catch (SecurityException e) {
932649 }
933- }
934- } catch (Exception e) {
935- fail("Exception during leaveGroup test: " + e.toString());
936- } finally {
937- System.setSecurityManager(null);
938- }
939- }
650+ System.setSecurityManager(null);
940651
941- /**
942- * @tests java.net.MulticastSocket#send(java.net.DatagramPacket, byte)
943- */
944- @TestTargetNew(
945- level = TestLevel.COMPLETE,
946- notes = "",
947- method = "send",
948- args = {java.net.DatagramPacket.class, byte.class}
949- )
950- public void test_sendLjava_net_DatagramPacketB() throws InterruptedException {
951- // Test for method void
952- // java.net.MulticastSocket.send(java.net.DatagramPacket, byte)
953-
954- String msg = "Hello World";
955- InetAddress group = null;
956- int[] ports = Support_PortManager.getNextPortsForUDP(2);
957- int groupPort = ports[0];
652+ if (atLeastOneInterface) {
958653
959- try {
960- group = InetAddress.getByName("224.0.0.3");
961- mss = new MulticastSocket(ports[1]);
962- server = new MulticastServer(group, groupPort);
963- server.start();
964- Thread.sleep(200);
965- DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
966- .length(), group, groupPort);
967- mss.send(sdp, (byte) 10);
968- Thread.sleep(1000);
969- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
970- } catch (Exception e) {
971- handleException(e, SO_MULTICAST);
972- try {
973- mss.close();
974- } catch (Exception ex) {
975- }
976-
977- return;
978- }
979-
980- DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
981- .length(), group, groupPort);
982-
983-
984- SecurityManager sm = new SecurityManager() {
985-
986- public void checkPermission(Permission perm) {
987- }
988-
989- public void checkConnect(String host,
990- int port) {
991- throw new SecurityException();
992- }
993-
994- public void checkMulticast(InetAddress maddr) {
995- throw new SecurityException();
996- }
997- };
998-
999- SecurityManager oldSm = System.getSecurityManager();
1000- System.setSecurityManager(sm);
1001- try {
1002- mss.send(sdp);
1003- fail("SecurityException should be thrown.");
1004- } catch (SecurityException e) {
1005- // expected
1006- } catch (SocketException e) {
1007- fail("SocketException was thrown.");
1008- } catch (IOException e) {
1009- fail("IOException was thrown.");
1010- e.printStackTrace();
1011- } finally {
1012- System.setSecurityManager(oldSm);
1013- }
1014-
1015- mss.close();
1016-
1017- try {
1018- mss.send(sdp);
1019- fail("IOException should be thrown.");
1020- } catch(IOException ioe) {
1021- //expected
1022- }
1023-
1024- DatagramPacket rdp = server.receive();
1025- assertEquals("Failed to send data. Received " + rdp.getLength(), msg,
1026- new String(rdp.getData(), 0, rdp.getLength()));
1027- }
1028-
1029- /**
1030- * @tests java.net.MulticastSocket#setInterface(java.net.InetAddress)
1031- */
1032- @TestTargetNew(
1033- level = TestLevel.COMPLETE,
1034- notes = "",
1035- method = "setInterface",
1036- args = {java.net.InetAddress.class}
1037- )
1038- public void test_setInterfaceLjava_net_InetAddress() {
1039- // Test for method void
1040- // java.net.MulticastSocket.setInterface(java.net.InetAddress)
1041- // Note that the machine is not multi-homed
654+ // now test that we can join and leave a group successfully
655+ groupPort = Support_PortManager.getNextPortForUDP();
656+ mss = new MulticastSocket(groupPort);
657+ groupSockAddr = new InetSocketAddress(group, groupPort);
658+ mss.joinGroup(groupSockAddr, null);
659+ mss.leaveGroup(groupSockAddr, null);
660+ try {
661+ mss.leaveGroup(groupSockAddr, null);
662+ fail(
663+ "Did not get exception when trying to leave group that was allready left");
664+ } catch (IOException e) {
665+ }
1042666
1043- try {
1044- mss = new MulticastSocket();
1045- mss.setInterface(InetAddress.getLocalHost());
1046- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST_INTERFACE);
1047- } catch (Exception e) {
1048- //handleException(e, SO_MULTICAST_INTERFACE);
1049- return;
1050- }
1051- try {
1052- InetAddress theInterface = mss.getInterface();
1053- // under IPV6 we are not guarrenteed to get the same address back as
1054- // the address, all we should be guaranteed is that we get an
1055- // address on the same interface
1056- if (theInterface instanceof Inet6Address) {
1057- assertTrue(
1058- "Failed to return correct interface IPV6",
1059- NetworkInterface
1060- .getByInetAddress(mss.getInterface())
1061- .equals(
1062- NetworkInterface
1063- .getByInetAddress(theInterface)));
1064- } else {
1065- assertTrue("Failed to return correct interface IPV4 got:"
1066- + mss.getInterface() + " excpeted: "
1067- + InetAddress.getLocalHost(), mss.getInterface()
1068- .equals(InetAddress.getLocalHost()));
1069- }
1070- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
1071- } catch (SocketException e) {
1072- handleException(e, SO_MULTICAST);
1073- } catch (UnknownHostException e) {
1074- fail("Exception during setInterface test: " + e.toString());
1075- }
667+ InetAddress group2 = InetAddress.getByName("224.0.0.4");
668+ groupSockAddr2 = new InetSocketAddress(group2, groupPort);
669+ mss.joinGroup(groupSockAddr, networkInterface1);
670+ try {
671+ mss.leaveGroup(groupSockAddr2, networkInterface1);
672+ fail(
673+ "Did not get exception when trying to leave group that was never joined");
674+ } catch (IOException e) {
675+ }
1076676
1077- // Regression test for Harmony-2410
1078- try {
1079- mss = new MulticastSocket();
1080- mss.setInterface(InetAddress.getByName("224.0.0.5"));
1081- } catch (UnknownHostException uhe) {
1082- fail("Unable to get InetAddress by name from '224.0.0.5' addr: "
1083- + uhe.toString());
1084- } catch (SocketException se) {
1085- // expected
1086- } catch (IOException ioe) {
1087- handleException(ioe, SO_MULTICAST_INTERFACE);
1088- return;
1089- }
1090- }
677+ mss.leaveGroup(groupSockAddr, networkInterface1);
678+ if (atLeastTwoInterfaces) {
679+ mss.joinGroup(groupSockAddr, networkInterface1);
680+ try {
681+ mss.leaveGroup(groupSockAddr, networkInterface2);
682+ fail(
683+ "Did not get exception when trying to leave group on wrong interface joined on ["
684+ + networkInterface1
685+ + "] left on ["
686+ + networkInterface2 + "]");
687+ } catch (IOException e) {
688+ }
689+ }
690+ }
1091691
1092- /**
1093- * @throws IOException
1094- * @throws InterruptedException
1095- * @tests java.net.MulticastSocket#setNetworkInterface(
1096- * java.net.NetworkInterface)
1097- */
1098- @TestTargetNew(
1099- level = TestLevel.COMPLETE,
1100- notes = "",
1101- method = "setNetworkInterface",
1102- args = {java.net.NetworkInterface.class}
1103- )
1104- public void test_setNetworkInterfaceLjava_net_NetworkInterface()
1105- throws IOException, InterruptedException {
1106- String msg = null;
1107- InetAddress group = null;
1108- int[] ports = Support_PortManager.getNextPortsForUDP(2);
1109- int groupPort = ports[0];
1110- int serverPort = ports[1];
1111- if (atLeastOneInterface) {
692+ System.setSecurityManager(null);
693+ }
694+
695+ /**
696+ * @tests java.net.MulticastSocket#send(java.net.DatagramPacket, byte)
697+ */
698+ public void test_sendLjava_net_DatagramPacketB() {
699+ // Test for method void
700+ // java.net.MulticastSocket.send(java.net.DatagramPacket, byte)
701+
702+ String msg = "Hello World";
703+ InetAddress group = null;
704+ int[] ports = Support_PortManager.getNextPortsForUDP(2);
705+ int groupPort = ports[0];
706+
707+ try {
708+ group = InetAddress.getByName("224.0.0.3");
709+ mss = new MulticastSocket(ports[1]);
710+ server = new MulticastServer(group, groupPort);
711+ server.start();
712+ Thread.sleep(200);
713+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
714+ .length(), group, groupPort);
715+ mss.send(sdp, (byte) 10);
716+ Thread.sleep(1000);
717+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
718+ } catch (Exception e) {
719+ handleException(e, SO_MULTICAST);
720+ try {
721+ mss.close();
722+ } catch (Exception ex) {
723+ }
724+ ;
725+ return;
726+ }
727+ mss.close();
728+ byte[] data = server.rdp.getData();
729+ int length = server.rdp.getLength();
730+ assertEquals("Failed to send data. Received " + length,
731+ msg, new String(data, 0, length));
732+ }
733+
734+ /**
735+ * @tests java.net.MulticastSocket#setInterface(java.net.InetAddress)
736+ */
737+ public void test_setInterfaceLjava_net_InetAddress() throws UnknownHostException {
738+ // Test for method void
739+ // java.net.MulticastSocket.setInterface(java.net.InetAddress)
740+ // Note that the machine is not multi-homed
741+
742+ try {
743+ mss = new MulticastSocket();
744+ mss.setInterface(InetAddress.getLocalHost());
745+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST_INTERFACE);
746+ } catch (Exception e) {
747+ handleException(e, SO_MULTICAST_INTERFACE);
748+ return;
749+ }
750+ try {
751+ InetAddress theInterface = mss.getInterface();
752+ // under IPV6 we are not guarrenteed to get the same address back as
753+ // the address, all we should be guaranteed is that we get an
754+ // address on the same interface
755+ if (theInterface instanceof Inet6Address) {
756+ assertTrue(
757+ "Failed to return correct interface IPV6",
758+ NetworkInterface
759+ .getByInetAddress(mss.getInterface())
760+ .equals(
761+ NetworkInterface
762+ .getByInetAddress(theInterface)));
763+ } else {
764+ assertTrue("Failed to return correct interface IPV4 got:"
765+ + mss.getInterface() + " excpeted: "
766+ + InetAddress.getLocalHost(), mss.getInterface()
767+ .equals(InetAddress.getLocalHost()));
768+ }
769+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
770+ } catch (SocketException e) {
771+ handleException(e, SO_MULTICAST);
772+ }
773+
774+ // Regression test for Harmony-2410
775+ try {
776+ mss = new MulticastSocket();
777+ mss.setInterface(InetAddress.getByName("224.0.0.5"));
778+ } catch (SocketException se) {
779+ // expected
780+ } catch (IOException ioe) {
781+ handleException(ioe, SO_MULTICAST_INTERFACE);
782+ return;
783+ }
784+ }
785+
786+ /**
787+ * @throws IOException
788+ * @throws InterruptedException
789+ * @tests java.net.MulticastSocket#setNetworkInterface(java.net.NetworkInterface)
790+ */
791+ public void test_setNetworkInterfaceLjava_net_NetworkInterface() throws IOException, InterruptedException {
792+ String msg = null;
793+ InetAddress group = null;
794+ int[] ports = Support_PortManager.getNextPortsForUDP(2);
795+ int groupPort = ports[0];
796+ int serverPort = ports[1];
797+ if (atLeastOneInterface) {
1112798 // validate that null interface is handled ok
1113799 mss = new MulticastSocket(groupPort);
1114800
1115801 // this should through a socket exception to be compatible
1116802 try {
1117803 mss.setNetworkInterface(null);
1118- fail("No socket exception when we set then network " +
1119- "interface with NULL");
804+ fail("No socket exception when we set then network interface with NULL");
1120805 } catch (SocketException ex) {
1121806 }
1122807
@@ -1124,9 +809,8 @@ public class MulticastSocketTest extends SocketTestCase {
1124809 groupPort = Support_PortManager.getNextPortForUDP();
1125810 mss = new MulticastSocket(groupPort);
1126811 mss.setNetworkInterface(networkInterface1);
1127- assertTrue(
1128- "Interface did not seem to be set by setNeworkInterface",
1129- networkInterface1.equals(mss.getNetworkInterface()));
812+ assertEquals("Interface did not seem to be set by setNeworkInterface",
813+ networkInterface1, mss.getNetworkInterface());
1130814
1131815 // set up the server and join the group
1132816 group = InetAddress.getByName("224.0.0.3");
@@ -1135,8 +819,7 @@ public class MulticastSocketTest extends SocketTestCase {
1135819 while (theInterfaces.hasMoreElements()) {
1136820 NetworkInterface thisInterface = (NetworkInterface) theInterfaces
1137821 .nextElement();
1138- if (thisInterface.getInetAddresses() != null
1139- && thisInterface.getInetAddresses().hasMoreElements()) {
822+ if (thisInterface.getInetAddresses().hasMoreElements()) {
1140823 if ((!((InetAddress) thisInterface.getInetAddresses()
1141824 .nextElement()).isLoopbackAddress())
1142825 &&
@@ -1165,95 +848,57 @@ public class MulticastSocketTest extends SocketTestCase {
1165848 DatagramPacket sdp = new DatagramPacket(theBytes,
1166849 theBytes.length, group, serverPort);
1167850 mss.send(sdp);
1168- DatagramPacket rdp = server.receive();
1169-
1170- String receivedMessage = new String(rdp.getData(), 0, rdp.getLength());
1171- assertEquals("Group member did not recv data when send on "
1172- + "a specific interface: ", msg, receivedMessage);
1173- assertEquals("Datagram was not received as expected.",
1174- thisInterface, NetworkInterface.getByInetAddress(rdp.getAddress()));
1175-
851+ Thread.sleep(1000);
852+ String receivedMessage = new String(server.rdp
853+ .getData(), 0, server.rdp.getLength());
854+ assertEquals("Group member did not recv data sent on a specific interface",
855+ msg, receivedMessage);
1176856 // stop the server
1177857 server.stopServer();
1178858 }
1179859 }
1180860 }
1181861 }
1182- }
1183-
1184- /**
1185- * @tests java.net.MulticastSocket#setTimeToLive(int)
1186- */
1187- @TestTargetNew(
1188- level = TestLevel.COMPLETE,
1189- notes = "",
1190- method = "setTimeToLive",
1191- args = {int.class}
1192- )
1193- public void test_setTimeToLiveI() {
1194- try {
1195- mss = new MulticastSocket();
1196- mss.setTimeToLive(120);
1197- assertTrue("Returned incorrect 1st TTL: " + mss.getTimeToLive(),
1198- mss.getTimeToLive() == 120);
1199- mss.setTimeToLive(220);
1200- assertTrue("Returned incorrect 2nd TTL: " + mss.getTimeToLive(),
1201- mss.getTimeToLive() == 220);
1202- mss.close();
1203- try {
1204- mss.setTimeToLive(1);
1205- fail("IOException was not thrown.");
1206- }catch(IOException ioe) {
1207- //expected
1208- }
1209-
1210- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
1211- } catch (Exception e) {
1212- handleException(e, SO_MULTICAST);
1213- }
1214- }
1215-
1216- /**
1217- * @tests java.net.MulticastSocket#setTTL(byte)
1218- */
1219- @TestTargetNew(
1220- level = TestLevel.COMPLETE,
1221- notes = "",
1222- method = "setTTL",
1223- args = {byte.class}
1224- )
1225- public void test_setTTLB() {
1226- // Test for method void java.net.MulticastSocket.setTTL(byte)
1227- try {
1228- mss = new MulticastSocket();
1229- mss.setTTL((byte) 120);
1230- assertTrue("Failed to set TTL: " + mss.getTTL(),
1231- mss.getTTL() == 120);
1232-
1233- mss.close();
1234- try {
1235- mss.setTTL((byte) 1);
1236- fail("IOException was not thrown.");
1237- } catch(IOException ioe) {
1238- //expected
1239- }
1240- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
1241- } catch (Exception e) {
1242- handleException(e, SO_MULTICAST);
1243- }
1244- }
1245-
1246- /**
1247- * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress)
1248- */
1249- @TestTargetNew(
1250- level = TestLevel.COMPLETE,
1251- notes = "",
1252- method = "MulticastSocket",
1253- args = {java.net.SocketAddress.class}
1254- )
1255- public void test_ConstructorLjava_net_SocketAddress() throws Exception {
1256- MulticastSocket ms = new MulticastSocket((SocketAddress) null);
862+ }
863+
864+ /**
865+ * @tests java.net.MulticastSocket#setTimeToLive(int)
866+ */
867+ public void test_setTimeToLiveI() {
868+ try {
869+ mss = new MulticastSocket();
870+ mss.setTimeToLive(120);
871+ assertEquals("Returned incorrect 1st TTL",
872+ 120, mss.getTimeToLive());
873+ mss.setTimeToLive(220);
874+ assertEquals("Returned incorrect 2nd TTL",
875+ 220, mss.getTimeToLive());
876+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
877+ } catch (Exception e) {
878+ handleException(e, SO_MULTICAST);
879+ }
880+ }
881+
882+ /**
883+ * @tests java.net.MulticastSocket#setTTL(byte)
884+ */
885+ public void test_setTTLB() {
886+ // Test for method void java.net.MulticastSocket.setTTL(byte)
887+ try {
888+ mss = new MulticastSocket();
889+ mss.setTTL((byte) 120);
890+ assertEquals("Failed to set TTL", 120, mss.getTTL());
891+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
892+ } catch (Exception e) {
893+ handleException(e, SO_MULTICAST);
894+ }
895+ }
896+
897+ /**
898+ * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress)
899+ */
900+ public void test_ConstructorLjava_net_SocketAddress() throws Exception {
901+ MulticastSocket ms = new MulticastSocket((SocketAddress) null);
1257902 assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
1258903 && !ms.isConnected());
1259904 ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
@@ -1287,109 +932,48 @@ public class MulticastSocketTest extends SocketTestCase {
1287932 InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 0);
1288933 MulticastSocket s = new MulticastSocket(addr);
1289934 assertTrue(s.getReuseAddress());
1290-
1291- InetSocketAddress isa = new InetSocketAddress(InetAddress
1292- .getLocalHost(), Support_PortManager.getNextPortForUDP());
1293-
1294- SecurityManager sm = new SecurityManager() {
1295-
1296- public void checkPermission(Permission perm) {
1297- }
1298-
1299- public void checkListen(int port) {
1300- throw new SecurityException();
1301- }
1302- };
1303-
1304- SecurityManager oldSm = System.getSecurityManager();
1305- System.setSecurityManager(sm);
1306- try {
1307- new MulticastSocket(isa);
1308- fail("SecurityException should be thrown.");
1309- } catch (SecurityException e) {
1310- // expected
1311- } catch (SocketException e) {
1312- fail("SocketException was thrown.");
1313- } catch (IOException e) {
1314- fail("IOException was thrown.");
1315- e.printStackTrace();
1316- } finally {
1317- System.setSecurityManager(oldSm);
1318- }
1319- }
1320-
1321- /**
1322- * @tests java.net.MulticastSocket#getLoopbackMode()
1323- */
1324- @TestTargetNew(
1325- level = TestLevel.COMPLETE,
1326- notes = "",
1327- method = "getLoopbackMode",
1328- args = {}
1329- )
1330- public void test_getLoopbackMode() {
1331- try {
1332- MulticastSocket ms = new MulticastSocket((SocketAddress) null);
1333- assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
1334- && !ms.isConnected());
1335- ms.getLoopbackMode();
1336- assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
1337- && !ms.isConnected());
1338- ms.close();
1339- assertTrue("should be closed", ms.isClosed());
1340- try {
1341- ms.getLoopbackMode();
1342- fail("SocketException was not thrown.");
1343- } catch(SocketException ioe) {
1344- //expected
1345- }
1346- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
1347- } catch (IOException e) {
1348- handleException(e, SO_USELOOPBACK);
1349- }
1350- }
1351-
1352- /**
1353- * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
1354- */
1355- @TestTargetNew(
1356- level = TestLevel.COMPLETE,
1357- notes = "",
1358- method = "setLoopbackMode",
1359- args = {boolean.class}
1360- )
1361- public void test_setLoopbackModeZ() {
1362- try {
1363- MulticastSocket ms = new MulticastSocket();
1364- ms.setLoopbackMode(true);
1365- assertTrue("loopback should be true", ms.getLoopbackMode());
1366- ms.setLoopbackMode(false);
1367- assertTrue("loopback should be false", !ms.getLoopbackMode());
1368- ms.close();
1369- assertTrue("should be closed", ms.isClosed());
1370-
1371- try {
1372- ms.setLoopbackMode(true);
1373- fail("SocketException was not thrown.");
1374- } catch(SocketException se) {
1375- //expected
1376- }
1377-
1378- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
1379- } catch (IOException e) {
1380- handleException(e, SO_USELOOPBACK);
1381- }
1382- }
935+ }
936+
937+ /**
938+ * @tests java.net.MulticastSocket#getLoopbackMode()
939+ */
940+ public void test_getLoopbackMode() {
941+ try {
942+ MulticastSocket ms = new MulticastSocket((SocketAddress) null);
943+ assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
944+ && !ms.isConnected());
945+ ms.getLoopbackMode();
946+ assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
947+ && !ms.isConnected());
948+ ms.close();
949+ assertTrue("should be closed", ms.isClosed());
950+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
951+ } catch (IOException e) {
952+ handleException(e, SO_USELOOPBACK);
953+ }
954+ }
955+
956+ /**
957+ * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
958+ */
959+ public void test_setLoopbackModeZ() {
960+ try {
961+ MulticastSocket ms = new MulticastSocket();
962+ ms.setLoopbackMode(true);
963+ assertTrue("loopback should be true", ms.getLoopbackMode());
964+ ms.setLoopbackMode(false);
965+ assertTrue("loopback should be false", !ms.getLoopbackMode());
966+ ms.close();
967+ assertTrue("should be closed", ms.isClosed());
968+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
969+ } catch (IOException e) {
970+ handleException(e, SO_USELOOPBACK);
971+ }
972+ }
1383973
1384974 /**
1385975 * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
1386976 */
1387- @TestTargetNew(
1388- level = TestLevel.ADDITIONAL,
1389- notes = "SocketException checking missed",
1390- method = "setLoopbackMode",
1391- args = {boolean.class}
1392- )
1393977 public void test_setLoopbackModeSendReceive() throws IOException{
1394978 final String ADDRESS = "224.1.2.3";
1395979 final int PORT = Support_PortManager.getNextPortForUDP();
@@ -1420,120 +1004,99 @@ public class MulticastSocketTest extends SocketTestCase {
14201004 String recvMessage = new String(recvData, 0, recvDatagram
14211005 .getLength());
14221006 assertEquals(message, recvMessage);
1423- } finally {
1007+ }finally {
14241008 if (socket != null)
14251009 socket.close();
14261010 }
14271011 }
14281012
14291013
1430- /**
1431- * @tests java.net.MulticastSocket#setReuseAddress(boolean)
1432- */
1433- @TestTargetNew(
1434- level = TestLevel.PARTIAL,
1435- notes = "SocketException checking missesd. Method inherited from " +
1436- "class java.net.DatagramSocket",
1437- method = "setReuseAddress",
1438- args = {boolean.class}
1439- )
1440- public void test_setReuseAddressZ() {
1441- try {
1442- // test case were we set it to false
1443- MulticastSocket theSocket1 = null;
1444- MulticastSocket theSocket2 = null;
1445- try {
1446- InetSocketAddress theAddress = new InetSocketAddress(
1447- InetAddress.getLocalHost(), Support_PortManager
1448- .getNextPortForUDP());
1449- theSocket1 = new MulticastSocket(null);
1450- theSocket2 = new MulticastSocket(null);
1451- theSocket1.setReuseAddress(false);
1452- theSocket2.setReuseAddress(false);
1453- theSocket1.bind(theAddress);
1454- theSocket2.bind(theAddress);
1455- fail(
1456- "No exception when trying to connect to do " +
1457- "duplicate socket bind with re-useaddr set to false");
1458- } catch (BindException e) {
1459-
1460- }
1461- if (theSocket1 != null)
1462- theSocket1.close();
1463- if (theSocket2 != null)
1464- theSocket2.close();
1465-
1466- // test case were we set it to true
1467- try {
1468- InetSocketAddress theAddress = new InetSocketAddress(
1469- InetAddress.getLocalHost(), Support_PortManager
1470- .getNextPortForUDP());
1471- theSocket1 = new MulticastSocket(null);
1472- theSocket2 = new MulticastSocket(null);
1473- theSocket1.setReuseAddress(true);
1474- theSocket2.setReuseAddress(true);
1475- theSocket1.bind(theAddress);
1476- theSocket2.bind(theAddress);
1477- } catch (Exception e) {
1478- fail(
1479- "unexpected exception when trying to connect " +
1480- "to do duplicate socket bind with re-useaddr set " +
1481- "to true");
1482- }
1483- if (theSocket1 != null)
1484- theSocket1.close();
1485- if (theSocket2 != null)
1486- theSocket2.close();
1487-
1488- // test the default case which we expect to be the same on all
1489- // platforms
1490- try {
1491- InetSocketAddress theAddress = new InetSocketAddress(
1492- InetAddress.getLocalHost(), Support_PortManager
1493- .getNextPortForUDP());
1494- theSocket1 = new MulticastSocket(null);
1495- theSocket2 = new MulticastSocket(null);
1496- theSocket1.bind(theAddress);
1497- theSocket2.bind(theAddress);
1498- } catch (BindException e) {
1499- fail(
1500- "unexpected exception when trying to connect to do " +
1501- "duplicate socket bind with re-useaddr left as default");
1502- }
1503- if (theSocket1 != null)
1504- theSocket1.close();
1505- if (theSocket2 != null)
1506- theSocket2.close();
1507- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
1508- } catch (Exception e) {
1509- handleException(e, SO_REUSEADDR);
1510- }
1511- }
1512-
1513- /**
1514- * Sets up the fixture, for example, open a network connection. This method
1515- * is called before a test is executed.
1516- */
1517- protected void setUp() {
1518-
1519- Enumeration theInterfaces = null;
1520- try {
1521- theInterfaces = NetworkInterface.getNetworkInterfaces();
1522- } catch (Exception e) {
1523- }
1524-
1525- // only consider interfaces that have addresses associated with them.
1526- // Otherwise tests don't work so well
1527- if (theInterfaces != null) {
1528-
1529- atLeastOneInterface = false;
1530- while (theInterfaces.hasMoreElements()
1014+ /**
1015+ * @tests java.net.MulticastSocket#setReuseAddress(boolean)
1016+ */
1017+ public void test_setReuseAddressZ() throws Exception {
1018+ try {
1019+ // test case were we set it to false
1020+ MulticastSocket theSocket1 = null;
1021+ MulticastSocket theSocket2 = null;
1022+ try {
1023+ InetSocketAddress theAddress = new InetSocketAddress(
1024+ InetAddress.getLocalHost(), Support_PortManager
1025+ .getNextPortForUDP());
1026+ theSocket1 = new MulticastSocket(null);
1027+ theSocket2 = new MulticastSocket(null);
1028+ theSocket1.setReuseAddress(false);
1029+ theSocket2.setReuseAddress(false);
1030+ theSocket1.bind(theAddress);
1031+ theSocket2.bind(theAddress);
1032+ fail(
1033+ "No exception when trying to connect to do duplicate socket bind with re-useaddr set to false");
1034+ } catch (BindException e) {
1035+
1036+ }
1037+ if (theSocket1 != null)
1038+ theSocket1.close();
1039+ if (theSocket2 != null)
1040+ theSocket2.close();
1041+
1042+ // test case were we set it to true
1043+ InetSocketAddress theAddress = new InetSocketAddress(
1044+ InetAddress.getLocalHost(), Support_PortManager
1045+ .getNextPortForUDP());
1046+ theSocket1 = new MulticastSocket(null);
1047+ theSocket2 = new MulticastSocket(null);
1048+ theSocket1.setReuseAddress(true);
1049+ theSocket2.setReuseAddress(true);
1050+ theSocket1.bind(theAddress);
1051+ theSocket2.bind(theAddress);
1052+
1053+ if (theSocket1 != null)
1054+ theSocket1.close();
1055+ if (theSocket2 != null)
1056+ theSocket2.close();
1057+
1058+ // test the default case which we expect to be
1059+ // the same on all platforms
1060+ theAddress =
1061+ new InetSocketAddress(
1062+ InetAddress.getLocalHost(),
1063+ Support_PortManager.getNextPortForUDP());
1064+ theSocket1 = new MulticastSocket(null);
1065+ theSocket2 = new MulticastSocket(null);
1066+ theSocket1.bind(theAddress);
1067+ theSocket2.bind(theAddress);
1068+ if (theSocket1 != null)
1069+ theSocket1.close();
1070+ if (theSocket2 != null)
1071+ theSocket2.close();
1072+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
1073+ } catch (Exception e) {
1074+ handleException(e, SO_REUSEADDR);
1075+ }
1076+ }
1077+
1078+ /**
1079+ * Sets up the fixture, for example, open a network connection. This method
1080+ * is called before a test is executed.
1081+ */
1082+ protected void setUp() {
1083+
1084+ Enumeration theInterfaces = null;
1085+ try {
1086+ theInterfaces = NetworkInterface.getNetworkInterfaces();
1087+ } catch (Exception e) {
1088+ }
1089+
1090+ // only consider interfaces that have addresses associated with them.
1091+ // Otherwise tests don't work so well
1092+ if (theInterfaces != null) {
1093+
1094+ atLeastOneInterface = false;
1095+ while (theInterfaces.hasMoreElements()
15311096 && (atLeastOneInterface == false)) {
15321097 networkInterface1 = (NetworkInterface) theInterfaces
15331098 .nextElement();
1534- if ((networkInterface1.getInetAddresses() != null)
1535- && networkInterface1.getInetAddresses()
1536- .hasMoreElements() &&
1099+ if (networkInterface1.getInetAddresses().hasMoreElements() &&
15371100 // we only want real interfaces
15381101 (Support_NetworkInterface
15391102 .useInterface(networkInterface1) == true)) {
@@ -1541,15 +1104,14 @@ public class MulticastSocketTest extends SocketTestCase {
15411104 }
15421105 }
15431106
1544- atLeastTwoInterfaces = false;
1545- if (theInterfaces.hasMoreElements()) {
1107+ atLeastTwoInterfaces = false;
1108+ if (theInterfaces.hasMoreElements()) {
15461109 while (theInterfaces.hasMoreElements()
15471110 && (atLeastTwoInterfaces == false)) {
15481111 networkInterface2 = (NetworkInterface) theInterfaces
15491112 .nextElement();
1550- if ((networkInterface2.getInetAddresses() != null)
1551- && networkInterface2.getInetAddresses()
1552- .hasMoreElements() &&
1113+ if (networkInterface2.getInetAddresses().hasMoreElements()
1114+ &&
15531115 // we only want real interfaces
15541116 (Support_NetworkInterface
15551117 .useInterface(networkInterface2) == true)) {
@@ -1558,44 +1120,44 @@ public class MulticastSocketTest extends SocketTestCase {
15581120 }
15591121 }
15601122
1561- Enumeration addresses;
1562-
1563- // first the first interface that supports IPV6 if one exists
1564- try {
1565- theInterfaces = NetworkInterface.getNetworkInterfaces();
1566- } catch (Exception e) {
1567- }
1568- boolean found = false;
1569- while (theInterfaces.hasMoreElements() && !found) {
1570- NetworkInterface nextInterface = (NetworkInterface) theInterfaces
1571- .nextElement();
1572- addresses = nextInterface.getInetAddresses();
1573- if (addresses != null) {
1574- while (addresses.hasMoreElements()) {
1575- InetAddress nextAddress = (InetAddress) addresses
1576- .nextElement();
1577- if (nextAddress instanceof Inet6Address) {
1578- IPV6networkInterface1 = nextInterface;
1579- found = true;
1580- break;
1581- }
1582- }
1583- }
1584- }
1585- }
1586- }
1587-
1588- /**
1589- * Tears down the fixture, for example, close a network connection. This
1590- * method is called after a test is executed.
1591- */
1592- protected void tearDown() throws InterruptedException {
1593-
1594- if (t != null)
1595- t.interrupt();
1596- if (mss != null)
1597- mss.close();
1598- if (server != null)
1599- server.stopServer();
1600- }
1123+ Enumeration addresses;
1124+
1125+ // first the first interface that supports IPV6 if one exists
1126+ try {
1127+ theInterfaces = NetworkInterface.getNetworkInterfaces();
1128+ } catch (Exception e) {
1129+ }
1130+ boolean found = false;
1131+ while (theInterfaces.hasMoreElements() && !found) {
1132+ NetworkInterface nextInterface = (NetworkInterface) theInterfaces
1133+ .nextElement();
1134+ addresses = nextInterface.getInetAddresses();
1135+ if (addresses.hasMoreElements()) {
1136+ while (addresses.hasMoreElements()) {
1137+ InetAddress nextAddress = (InetAddress) addresses
1138+ .nextElement();
1139+ if (nextAddress instanceof Inet6Address) {
1140+ IPV6networkInterface1 = nextInterface;
1141+ found = true;
1142+ break;
1143+ }
1144+ }
1145+ }
1146+ }
1147+ }
1148+ }
1149+
1150+ /**
1151+ * Tears down the fixture, for example, close a network connection. This
1152+ * method is called after a test is executed.
1153+ */
1154+ protected void tearDown() {
1155+
1156+ if (t != null)
1157+ t.interrupt();
1158+ if (mss != null)
1159+ mss.close();
1160+ if (server != null)
1161+ server.stopServer();
1162+ }
16011163 }
Show on old repository browser