Source Code Cross Referenced for MulticastSocketTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:
0018:        package org.apache.harmony.luni.tests.java.net;
0019:
0020:        import java.io.IOException;
0021:        import java.net.BindException;
0022:        import java.net.DatagramPacket;
0023:        import java.net.Inet4Address;
0024:        import java.net.Inet6Address;
0025:        import java.net.InetAddress;
0026:        import java.net.InetSocketAddress;
0027:        import java.net.MulticastSocket;
0028:        import java.net.NetworkInterface;
0029:        import java.net.SocketAddress;
0030:        import java.net.SocketException;
0031:        import java.net.UnknownHostException;
0032:        import java.util.Enumeration;
0033:
0034:        import tests.support.Support_NetworkInterface;
0035:        import tests.support.Support_PortManager;
0036:
0037:        public class MulticastSocketTest extends SocketTestCase {
0038:
0039:            Thread t;
0040:
0041:            MulticastSocket mss;
0042:
0043:            MulticastServer server;
0044:
0045:            // private member variables used for tests
0046:            boolean atLeastOneInterface = false;
0047:
0048:            boolean atLeastTwoInterfaces = false;
0049:
0050:            private NetworkInterface networkInterface1 = null;
0051:
0052:            private NetworkInterface networkInterface2 = null;
0053:
0054:            private NetworkInterface IPV6networkInterface1 = null;
0055:
0056:            static class MulticastServer extends Thread {
0057:
0058:                public MulticastSocket ms;
0059:
0060:                boolean running = true;
0061:
0062:                volatile public byte[] rbuf = new byte[512];
0063:
0064:                volatile DatagramPacket rdp = null;
0065:
0066:                private InetAddress groupAddr = null;
0067:                private SocketAddress groupSockAddr = null;
0068:                private NetworkInterface groupNI = null;
0069:
0070:                public void run() {
0071:                    try {
0072:                        byte[] tmpbuf = new byte[512];
0073:                        DatagramPacket tmpPack = new DatagramPacket(tmpbuf,
0074:                                tmpbuf.length);
0075:
0076:                        while (running) {
0077:                            try {
0078:                                ms.receive(tmpPack);
0079:
0080:                                System.arraycopy(tmpPack.getData(), 0, rdp
0081:                                        .getData(), rdp.getOffset(), tmpPack
0082:                                        .getLength());
0083:                                rdp.setLength(tmpPack.getLength());
0084:                                rdp.setAddress(tmpPack.getAddress());
0085:                                rdp.setPort(tmpPack.getPort());
0086:                            } catch (java.io.InterruptedIOException e) {
0087:                                Thread.yield();
0088:                            }
0089:                        }
0090:                    } catch (java.io.IOException e) {
0091:                        System.out.println("Multicast server failed: " + e);
0092:                    } finally {
0093:                        ms.close();
0094:                    }
0095:                }
0096:
0097:                public void stopServer() {
0098:                    running = false;
0099:                    try {
0100:                        if (groupAddr != null) {
0101:                            ms.leaveGroup(groupAddr);
0102:                        } else if (groupSockAddr != null) {
0103:                            ms.leaveGroup(groupSockAddr, groupNI);
0104:                        }
0105:                    } catch (IOException e) {
0106:                    }
0107:                }
0108:
0109:                public MulticastServer(InetAddress anAddress, int aPort)
0110:                        throws java.io.IOException {
0111:                    rbuf = new byte[512];
0112:                    rbuf[0] = -1;
0113:                    rdp = new DatagramPacket(rbuf, rbuf.length);
0114:                    ms = new MulticastSocket(aPort);
0115:                    ms.setSoTimeout(2000);
0116:                    groupAddr = anAddress;
0117:                    ms.joinGroup(groupAddr);
0118:                }
0119:
0120:                public MulticastServer(SocketAddress anAddress, int aPort,
0121:                        NetworkInterface netInterface)
0122:                        throws java.io.IOException {
0123:                    rbuf = new byte[512];
0124:                    rbuf[0] = -1;
0125:                    rdp = new DatagramPacket(rbuf, rbuf.length);
0126:                    ms = new MulticastSocket(aPort);
0127:                    ms.setSoTimeout(2000);
0128:                    groupSockAddr = anAddress;
0129:                    groupNI = netInterface;
0130:                    ms.joinGroup(groupSockAddr, groupNI);
0131:                }
0132:            }
0133:
0134:            /**
0135:             * @tests java.net.MulticastSocket#MulticastSocket()
0136:             */
0137:            public void test_Constructor() throws IOException {
0138:                // regression test for 497
0139:                MulticastSocket s = new MulticastSocket();
0140:                // regression test for Harmony-1162
0141:                assertTrue(s.getReuseAddress());
0142:            }
0143:
0144:            /**
0145:             * @tests java.net.MulticastSocket#MulticastSocket(int)
0146:             */
0147:            public void test_ConstructorI() {
0148:                // Test for method java.net.MulticastSocket(int)
0149:                // Used in tests
0150:                MulticastSocket dup = null;
0151:                try {
0152:                    mss = new MulticastSocket();
0153:                    int port = mss.getLocalPort();
0154:                    dup = new MulticastSocket(port);
0155:                    // regression test for Harmony-1162
0156:                    assertTrue(dup.getReuseAddress());
0157:                } catch (IOException e) {
0158:                    fail("duplicate binding not allowed: " + e);
0159:                }
0160:                if (dup != null)
0161:                    dup.close();
0162:            }
0163:
0164:            /**
0165:             * @tests java.net.MulticastSocket#getInterface()
0166:             */
0167:            public void test_getInterface() throws Exception {
0168:                // Test for method java.net.InetAddress
0169:                // java.net.MulticastSocket.getInterface()
0170:                assertTrue("Used for testing.", true);
0171:
0172:                int groupPort = Support_PortManager.getNextPortForUDP();
0173:
0174:                if (atLeastOneInterface) {
0175:                    // validate that we get the expected response when one was not
0176:                    // set
0177:                    mss = new MulticastSocket(groupPort);
0178:                    String preferIPv4StackValue = System
0179:                            .getProperty("java.net.preferIPv4Stack");
0180:                    String preferIPv6AddressesValue = System
0181:                            .getProperty("java.net.preferIPv6Addresses");
0182:                    if (((preferIPv4StackValue == null) || preferIPv4StackValue
0183:                            .equalsIgnoreCase("false"))
0184:                            && (preferIPv6AddressesValue != null)
0185:                            && (preferIPv6AddressesValue.equals("true"))) {
0186:                        // we expect an IPv6 ANY in this case
0187:                        assertTrue("inet Address returned when not set:"
0188:                                + mss.getInterface().toString(), InetAddress
0189:                                .getByName("::0").equals(mss.getInterface()));
0190:                    } else {
0191:                        // we expect an IPv4 ANY in this case
0192:                        assertTrue("inet Address returned when not set:"
0193:                                + mss.getInterface().toString(), InetAddress
0194:                                .getByName("0.0.0.0")
0195:                                .equals(mss.getInterface()));
0196:                    }
0197:
0198:                    // validate that we get the expected response when we set via
0199:                    // setInterface
0200:                    Enumeration addresses = networkInterface1
0201:                            .getInetAddresses();
0202:                    if (addresses != null) {
0203:                        InetAddress firstAddress = (InetAddress) addresses
0204:                                .nextElement();
0205:                        mss.setInterface(firstAddress);
0206:                        assertTrue(
0207:                                "getNetworkInterface did not return interface set by setInterface.  Expected:"
0208:                                        + firstAddress + " Got:"
0209:                                        + mss.getInterface(), firstAddress
0210:                                        .equals(mss.getInterface()));
0211:
0212:                        groupPort = Support_PortManager.getNextPortForUDP();
0213:                        mss = new MulticastSocket(groupPort);
0214:                        mss.setNetworkInterface(networkInterface1);
0215:                        assertTrue(
0216:                                "getInterface did not return interface set by setNeworkInterface Expected: "
0217:                                        + firstAddress + "Got:"
0218:                                        + mss.getInterface(), NetworkInterface
0219:                                        .getByInetAddress(mss.getInterface())
0220:                                        .equals(networkInterface1));
0221:                    }
0222:
0223:                }
0224:            }
0225:
0226:            /**
0227:             * @throws IOException 
0228:             * @tests java.net.MulticastSocket#getNetworkInterface()
0229:             */
0230:            public void test_getNetworkInterface() throws IOException {
0231:                int groupPort = Support_PortManager.getNextPortForUDP();
0232:                if (atLeastOneInterface) {
0233:                    // validate that we get the expected response when one was not
0234:                    // set
0235:                    mss = new MulticastSocket(groupPort);
0236:                    NetworkInterface theInterface = mss.getNetworkInterface();
0237:                    assertNotNull(
0238:                            "network interface returned wrong network interface when not set:"
0239:                                    + theInterface, theInterface
0240:                                    .getInetAddresses());
0241:                    InetAddress firstAddress = (InetAddress) theInterface
0242:                            .getInetAddresses().nextElement();
0243:                    // validate we the first address in the network interface is the
0244:                    // ANY address
0245:                    String preferIPv4StackValue = System
0246:                            .getProperty("java.net.preferIPv4Stack");
0247:                    String preferIPv6AddressesValue = System
0248:                            .getProperty("java.net.preferIPv6Addresses");
0249:                    if (((preferIPv4StackValue == null) || preferIPv4StackValue
0250:                            .equalsIgnoreCase("false"))
0251:                            && (preferIPv6AddressesValue != null)
0252:                            && (preferIPv6AddressesValue.equals("true"))) {
0253:                        assertTrue(
0254:                                "network interface returned wrong network interface when not set:"
0255:                                        + theInterface, InetAddress.getByName(
0256:                                        "::0").equals(firstAddress));
0257:
0258:                    } else {
0259:                        assertTrue(
0260:                                "network interface returned wrong network interface when not set:"
0261:                                        + theInterface, InetAddress.getByName(
0262:                                        "0.0.0.0").equals(firstAddress));
0263:                    }
0264:
0265:                    mss.setNetworkInterface(networkInterface1);
0266:                    assertTrue(
0267:                            "getNetworkInterface did not return interface set by setNeworkInterface",
0268:                            networkInterface1.equals(mss.getNetworkInterface()));
0269:
0270:                    if (atLeastTwoInterfaces) {
0271:                        mss.setNetworkInterface(networkInterface2);
0272:                        assertTrue(
0273:                                "getNetworkInterface did not return network interface set by second setNetworkInterface call",
0274:                                networkInterface2.equals(mss
0275:                                        .getNetworkInterface()));
0276:                    }
0277:
0278:                    groupPort = Support_PortManager.getNextPortForUDP();
0279:                    mss = new MulticastSocket(groupPort);
0280:                    if (IPV6networkInterface1 != null) {
0281:                        mss.setNetworkInterface(IPV6networkInterface1);
0282:                        assertTrue(
0283:                                "getNetworkInterface did not return interface set by setNeworkInterface",
0284:                                IPV6networkInterface1.equals(mss
0285:                                        .getNetworkInterface()));
0286:                    }
0287:
0288:                    // validate that we get the expected response when we set via
0289:                    // setInterface
0290:                    groupPort = Support_PortManager.getNextPortForUDP();
0291:                    mss = new MulticastSocket(groupPort);
0292:                    Enumeration addresses = networkInterface1
0293:                            .getInetAddresses();
0294:                    if (addresses != null) {
0295:                        firstAddress = (InetAddress) addresses.nextElement();
0296:                        mss.setInterface(firstAddress);
0297:                        assertTrue(
0298:                                "getNetworkInterface did not return interface set by setInterface",
0299:                                networkInterface1.equals(mss
0300:                                        .getNetworkInterface()));
0301:                    }
0302:                }
0303:            }
0304:
0305:            /**
0306:             * @tests java.net.MulticastSocket#getTimeToLive()
0307:             */
0308:            public void test_getTimeToLive() {
0309:                try {
0310:                    mss = new MulticastSocket();
0311:                    mss.setTimeToLive(120);
0312:                    assertTrue("Returned incorrect 1st TTL: "
0313:                            + mss.getTimeToLive(), mss.getTimeToLive() == 120);
0314:                    mss.setTimeToLive(220);
0315:                    assertTrue("Returned incorrect 2nd TTL: "
0316:                            + mss.getTimeToLive(), mss.getTimeToLive() == 220);
0317:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0318:                } catch (Exception e) {
0319:                    handleException(e, SO_MULTICAST);
0320:                }
0321:            }
0322:
0323:            /**
0324:             * @tests java.net.MulticastSocket#getTTL()
0325:             */
0326:            public void test_getTTL() {
0327:                // Test for method byte java.net.MulticastSocket.getTTL()
0328:
0329:                try {
0330:                    mss = new MulticastSocket();
0331:                    mss.setTTL((byte) 120);
0332:                    assertTrue("Returned incorrect TTL: " + mss.getTTL(), mss
0333:                            .getTTL() == 120);
0334:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0335:                } catch (Exception e) {
0336:                    handleException(e, SO_MULTICAST);
0337:                }
0338:            }
0339:
0340:            /**
0341:             * @tests java.net.MulticastSocket#joinGroup(java.net.InetAddress)
0342:             */
0343:            public void test_joinGroupLjava_net_InetAddress() throws Exception {
0344:                // Test for method void
0345:                // java.net.MulticastSocket.joinGroup(java.net.InetAddress)
0346:                String msg = null;
0347:                InetAddress group = null;
0348:                int[] ports = Support_PortManager.getNextPortsForUDP(2);
0349:                int groupPort = ports[0];
0350:                group = InetAddress.getByName("224.0.0.3");
0351:                server = new MulticastServer(group, groupPort);
0352:                server.start();
0353:                Thread.sleep(1000);
0354:                msg = "Hello World";
0355:                mss = new MulticastSocket(ports[1]);
0356:                DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
0357:                        .length(), group, groupPort);
0358:                mss.send(sdp, (byte) 10);
0359:                Thread.sleep(1000);
0360:
0361:                assertTrue("Group member did not recv data: ", new String(
0362:                        server.rdp.getData(), 0, server.rdp.getLength())
0363:                        .equals(msg));
0364:            }
0365:
0366:            /**
0367:             * @throws IOException 
0368:             * @throws InterruptedException 
0369:             * @tests java.net.MulticastSocket#joinGroup(java.net.SocketAddress,java.net.NetworkInterface)
0370:             */
0371:            public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface()
0372:                    throws IOException, InterruptedException {
0373:                // security manager that allows us to check that we only return the
0374:                // addresses that we should
0375:                class mySecurityManager extends SecurityManager {
0376:
0377:                    public void checkMulticast(InetAddress address) {
0378:                        throw new SecurityException("not allowed");
0379:                    }
0380:                }
0381:
0382:                String msg = null;
0383:                InetAddress group = null;
0384:                SocketAddress groupSockAddr = null;
0385:                int[] ports = Support_PortManager.getNextPortsForUDP(2);
0386:                int groupPort = ports[0];
0387:                int serverPort = ports[1];
0388:
0389:                Enumeration theInterfaces = NetworkInterface
0390:                        .getNetworkInterfaces();
0391:
0392:                // first validate that we handle a null group ok
0393:                mss = new MulticastSocket(groupPort);
0394:                try {
0395:                    mss.joinGroup(null, null);
0396:                    fail("Did not get exception when group was null");
0397:                } catch (IllegalArgumentException e) {
0398:                }
0399:
0400:                // now validate we get the expected error if the address specified
0401:                // is not a multicast group
0402:                try {
0403:                    groupSockAddr = new InetSocketAddress(InetAddress
0404:                            .getByName("255.255.255.255"), groupPort);
0405:                    mss.joinGroup(groupSockAddr, null);
0406:                    fail("Did not get exception when group is not a multicast address");
0407:                } catch (IOException e) {
0408:                }
0409:
0410:                // now try to join a group if we are not authorized
0411:                // set the security manager that will make the first address not
0412:                // visible
0413:                System.setSecurityManager(new mySecurityManager());
0414:                try {
0415:                    group = InetAddress.getByName("224.0.0.3");
0416:                    groupSockAddr = new InetSocketAddress(group, groupPort);
0417:                    mss.joinGroup(groupSockAddr, null);
0418:                    fail("Did not get exception when joining group is not allowed");
0419:                } catch (SecurityException e) {
0420:                }
0421:                System.setSecurityManager(null);
0422:
0423:                if (atLeastOneInterface) {
0424:                    // now validate that we can properly join a group with a null
0425:                    // network interface
0426:                    ports = Support_PortManager.getNextPortsForUDP(2);
0427:                    groupPort = ports[0];
0428:                    serverPort = ports[1];
0429:                    mss = new MulticastSocket(groupPort);
0430:                    mss.joinGroup(groupSockAddr, null);
0431:                    mss.setTimeToLive(2);
0432:                    Thread.sleep(1000);
0433:
0434:                    // set up the server and join the group on networkInterface1
0435:                    group = InetAddress.getByName("224.0.0.3");
0436:                    groupSockAddr = new InetSocketAddress(group, groupPort);
0437:                    server = new MulticastServer(groupSockAddr, serverPort,
0438:                            networkInterface1);
0439:                    server.start();
0440:                    Thread.sleep(1000);
0441:                    msg = "Hello World";
0442:                    DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
0443:                            .length(), group, serverPort);
0444:                    mss.setTimeToLive(2);
0445:                    mss.send(sdp);
0446:                    Thread.sleep(1000);
0447:                    // now vaildate that we received the data as expected
0448:                    assertTrue("Group member did not recv data: ", new String(
0449:                            server.rdp.getData(), 0, server.rdp.getLength())
0450:                            .equals(msg));
0451:                    server.stopServer();
0452:
0453:                    // now validate that we handled the case were we join a
0454:                    // different multicast address.
0455:                    // verify we do not receive the data
0456:                    ports = Support_PortManager.getNextPortsForUDP(2);
0457:                    serverPort = ports[0];
0458:                    server = new MulticastServer(groupSockAddr, serverPort,
0459:                            networkInterface1);
0460:                    server.start();
0461:                    Thread.sleep(1000);
0462:
0463:                    groupPort = ports[1];
0464:                    mss = new MulticastSocket(groupPort);
0465:                    InetAddress group2 = InetAddress.getByName("224.0.0.4");
0466:                    mss.setTimeToLive(10);
0467:                    msg = "Hello World - Different Group";
0468:                    sdp = new DatagramPacket(msg.getBytes(), msg.length(),
0469:                            group2, serverPort);
0470:                    mss.send(sdp);
0471:                    Thread.sleep(1000);
0472:                    assertFalse(
0473:                            "Group member received data when sent on different group: ",
0474:                            new String(server.rdp.getData(), 0, server.rdp
0475:                                    .getLength()).equals(msg));
0476:                    server.stopServer();
0477:
0478:                    // if there is more than one network interface then check that
0479:                    // we can join on specific interfaces and that we only receive
0480:                    // if data is received on that interface
0481:                    if (atLeastTwoInterfaces) {
0482:                        // set up server on first interfaces
0483:                        NetworkInterface loopbackInterface = NetworkInterface
0484:                                .getByInetAddress(InetAddress
0485:                                        .getByName("127.0.0.1"));
0486:
0487:                        theInterfaces = NetworkInterface.getNetworkInterfaces();
0488:                        while (theInterfaces.hasMoreElements()) {
0489:
0490:                            NetworkInterface this Interface = (NetworkInterface) theInterfaces
0491:                                    .nextElement();
0492:                            if ((this Interface.getInetAddresses() != null && this Interface
0493:                                    .getInetAddresses().hasMoreElements())
0494:                                    && (Support_NetworkInterface
0495:                                            .useInterface(this Interface) == true)) {
0496:                                // get the first address on the interface
0497:
0498:                                // start server which is joined to the group and has
0499:                                // only asked for packets on this interface
0500:                                Enumeration addresses = this Interface
0501:                                        .getInetAddresses();
0502:
0503:                                NetworkInterface sendingInterface = null;
0504:                                boolean isLoopback = false;
0505:                                if (addresses != null) {
0506:                                    InetAddress firstAddress = (InetAddress) addresses
0507:                                            .nextElement();
0508:                                    if (firstAddress.isLoopbackAddress()) {
0509:                                        isLoopback = true;
0510:                                    }
0511:                                    if (firstAddress instanceof  Inet4Address) {
0512:                                        group = InetAddress
0513:                                                .getByName("224.0.0.4");
0514:                                        if (networkInterface1
0515:                                                .equals(NetworkInterface
0516:                                                        .getByInetAddress(InetAddress
0517:                                                                .getByName("127.0.0.1")))) {
0518:                                            sendingInterface = networkInterface2;
0519:                                        } else {
0520:                                            sendingInterface = networkInterface1;
0521:                                        }
0522:                                    } else {
0523:                                        // if this interface only seems to support
0524:                                        // IPV6 addresses
0525:                                        group = InetAddress
0526:                                                .getByName("FF01:0:0:0:0:0:2:8001");
0527:                                        sendingInterface = IPV6networkInterface1;
0528:                                    }
0529:                                }
0530:
0531:                                InetAddress useAddress = null;
0532:                                addresses = sendingInterface.getInetAddresses();
0533:                                if ((addresses != null)
0534:                                        && (addresses.hasMoreElements())) {
0535:                                    useAddress = (InetAddress) addresses
0536:                                            .nextElement();
0537:                                }
0538:
0539:                                ports = Support_PortManager
0540:                                        .getNextPortsForUDP(2);
0541:                                serverPort = ports[0];
0542:                                groupPort = ports[1];
0543:                                groupSockAddr = new InetSocketAddress(group,
0544:                                        serverPort);
0545:                                server = new MulticastServer(groupSockAddr,
0546:                                        serverPort, this Interface);
0547:                                server.start();
0548:                                Thread.sleep(1000);
0549:
0550:                                // Now send out a package on interface
0551:                                // networkInterface 1. We should
0552:                                // only see the packet if we send it on interface 1
0553:                                InetSocketAddress theAddress = new InetSocketAddress(
0554:                                        useAddress, groupPort);
0555:                                mss = new MulticastSocket(groupPort);
0556:                                mss.setNetworkInterface(sendingInterface);
0557:                                msg = "Hello World - Again"
0558:                                        + this Interface.getName();
0559:                                sdp = new DatagramPacket(msg.getBytes(), msg
0560:                                        .length(), group, serverPort);
0561:                                mss.send(sdp);
0562:                                Thread.sleep(1000);
0563:                                if (this Interface.equals(sendingInterface)) {
0564:                                    assertTrue(
0565:                                            "Group member did not recv data when bound on specific interface: ",
0566:                                            new String(server.rdp.getData(), 0,
0567:                                                    server.rdp.getLength())
0568:                                                    .equals(msg));
0569:                                } else {
0570:                                    assertFalse(
0571:                                            "Group member received data on other interface when only asked for it on one interface: ",
0572:                                            new String(server.rdp.getData(), 0,
0573:                                                    server.rdp.getLength())
0574:                                                    .equals(msg));
0575:                                }
0576:
0577:                                server.stopServer();
0578:                            }
0579:                        }
0580:
0581:                        // validate that we can join the same address on two
0582:                        // different interfaces but not on the same interface
0583:                        groupPort = Support_PortManager.getNextPortForUDP();
0584:                        mss = new MulticastSocket(groupPort);
0585:                        mss.joinGroup(groupSockAddr, networkInterface1);
0586:                        mss.joinGroup(groupSockAddr, networkInterface2);
0587:                        try {
0588:                            mss.joinGroup(groupSockAddr, networkInterface1);
0589:                            fail("Did not get expected exception when joining for second time on same interface");
0590:                        } catch (IOException e) {
0591:                        }
0592:                    }
0593:                }
0594:                System.setSecurityManager(null);
0595:            }
0596:
0597:            /**
0598:             * @tests java.net.MulticastSocket#leaveGroup(java.net.InetAddress)
0599:             */
0600:            public void test_leaveGroupLjava_net_InetAddress() {
0601:                // Test for method void
0602:                // java.net.MulticastSocket.leaveGroup(java.net.InetAddress)
0603:                String msg = null;
0604:                boolean except = false;
0605:                InetAddress group = null;
0606:                int[] ports = Support_PortManager.getNextPortsForUDP(2);
0607:                int groupPort = ports[0];
0608:
0609:                try {
0610:                    group = InetAddress.getByName("224.0.0.3");
0611:                    msg = "Hello World";
0612:                    mss = new MulticastSocket(ports[1]);
0613:                    DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
0614:                            .length(), group, groupPort);
0615:                    mss.send(sdp, (byte) 10);
0616:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0617:                } catch (Exception e) {
0618:                    handleException(e, SO_MULTICAST);
0619:                }
0620:                try {
0621:                    // Try to leave s group that mss is not a member of
0622:                    mss.leaveGroup(group);
0623:                } catch (java.io.IOException e) {
0624:                    // Correct
0625:                    except = true;
0626:                }
0627:                assertTrue(
0628:                        "Failed to throw exception leaving non-member group",
0629:                        except);
0630:            }
0631:
0632:            /**
0633:             * @tests java.net.MulticastSocket#leaveGroup(java.net.SocketAddress,java.net.NetworkInterface)
0634:             */
0635:            public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface()
0636:                    throws Exception {
0637:                // security manager that allows us to check that we only return the
0638:                // addresses that we should
0639:                class mySecurityManager extends SecurityManager {
0640:
0641:                    public void checkMulticast(InetAddress address) {
0642:                        throw new SecurityException("not allowed");
0643:                    }
0644:                }
0645:
0646:                String msg = null;
0647:                InetAddress group = null;
0648:                int groupPort = Support_PortManager.getNextPortForUDP();
0649:                SocketAddress groupSockAddr = null;
0650:                SocketAddress groupSockAddr2 = null;
0651:
0652:                Enumeration theInterfaces = NetworkInterface
0653:                        .getNetworkInterfaces();
0654:
0655:                // first validate that we handle a null group ok
0656:                mss = new MulticastSocket(groupPort);
0657:                try {
0658:                    mss.leaveGroup(null, null);
0659:                    fail("Did not get exception when group was null");
0660:                } catch (IllegalArgumentException e) {
0661:                }
0662:
0663:                // now validate we get the expected error if the address specified
0664:                // is not a multicast group
0665:                try {
0666:                    group = InetAddress.getByName("255.255.255.255");
0667:                    groupSockAddr = new InetSocketAddress(group, groupPort);
0668:                    mss.leaveGroup(groupSockAddr, null);
0669:                    fail("Did not get exception when group is not a multicast address");
0670:                } catch (IOException e) {
0671:                }
0672:
0673:                // now try to leave a group if we are not authorized
0674:                // set the security manager that will make the first address not
0675:                // visible
0676:                System.setSecurityManager(new mySecurityManager());
0677:                try {
0678:                    group = InetAddress.getByName("224.0.0.3");
0679:                    groupSockAddr = new InetSocketAddress(group, groupPort);
0680:                    mss.leaveGroup(groupSockAddr, null);
0681:                    fail("Did not get exception when joining group is not allowed");
0682:                } catch (SecurityException e) {
0683:                }
0684:                System.setSecurityManager(null);
0685:
0686:                if (atLeastOneInterface) {
0687:
0688:                    // now test that we can join and leave a group successfully
0689:                    groupPort = Support_PortManager.getNextPortForUDP();
0690:                    mss = new MulticastSocket(groupPort);
0691:                    groupSockAddr = new InetSocketAddress(group, groupPort);
0692:                    mss.joinGroup(groupSockAddr, null);
0693:                    mss.leaveGroup(groupSockAddr, null);
0694:                    try {
0695:                        mss.leaveGroup(groupSockAddr, null);
0696:                        fail("Did not get exception when trying to leave group that was allready left");
0697:                    } catch (IOException e) {
0698:                    }
0699:
0700:                    InetAddress group2 = InetAddress.getByName("224.0.0.4");
0701:                    groupSockAddr2 = new InetSocketAddress(group2, groupPort);
0702:                    mss.joinGroup(groupSockAddr, networkInterface1);
0703:                    try {
0704:                        mss.leaveGroup(groupSockAddr2, networkInterface1);
0705:                        fail("Did not get exception when trying to leave group that was never joined");
0706:                    } catch (IOException e) {
0707:                    }
0708:
0709:                    mss.leaveGroup(groupSockAddr, networkInterface1);
0710:                    if (atLeastTwoInterfaces) {
0711:                        mss.joinGroup(groupSockAddr, networkInterface1);
0712:                        try {
0713:                            mss.leaveGroup(groupSockAddr, networkInterface2);
0714:                            fail("Did not get exception when trying to leave group on wrong interface joined on ["
0715:                                    + networkInterface1
0716:                                    + "] left on ["
0717:                                    + networkInterface2 + "]");
0718:                        } catch (IOException e) {
0719:                        }
0720:                    }
0721:                }
0722:
0723:                System.setSecurityManager(null);
0724:            }
0725:
0726:            /**
0727:             * @tests java.net.MulticastSocket#send(java.net.DatagramPacket, byte)
0728:             */
0729:            public void test_sendLjava_net_DatagramPacketB() {
0730:                // Test for method void
0731:                // java.net.MulticastSocket.send(java.net.DatagramPacket, byte)
0732:
0733:                String msg = "Hello World";
0734:                InetAddress group = null;
0735:                int[] ports = Support_PortManager.getNextPortsForUDP(2);
0736:                int groupPort = ports[0];
0737:
0738:                try {
0739:                    group = InetAddress.getByName("224.0.0.3");
0740:                    mss = new MulticastSocket(ports[1]);
0741:                    server = new MulticastServer(group, groupPort);
0742:                    server.start();
0743:                    Thread.sleep(200);
0744:                    DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
0745:                            .length(), group, groupPort);
0746:                    mss.send(sdp, (byte) 10);
0747:                    Thread.sleep(1000);
0748:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0749:                } catch (Exception e) {
0750:                    handleException(e, SO_MULTICAST);
0751:                    try {
0752:                        mss.close();
0753:                    } catch (Exception ex) {
0754:                    }
0755:                    ;
0756:                    return;
0757:                }
0758:                mss.close();
0759:                byte[] data = server.rdp.getData();
0760:                int length = server.rdp.getLength();
0761:                assertTrue("Failed to send data. Received " + length,
0762:                        new String(data, 0, length).equals(msg));
0763:            }
0764:
0765:            /**
0766:             * @tests java.net.MulticastSocket#setInterface(java.net.InetAddress)
0767:             */
0768:            public void test_setInterfaceLjava_net_InetAddress() {
0769:                // Test for method void
0770:                // java.net.MulticastSocket.setInterface(java.net.InetAddress)
0771:                // Note that the machine is not multi-homed
0772:
0773:                try {
0774:                    mss = new MulticastSocket();
0775:                    mss.setInterface(InetAddress.getLocalHost());
0776:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST_INTERFACE);
0777:                } catch (Exception e) {
0778:                    handleException(e, SO_MULTICAST_INTERFACE);
0779:                    return;
0780:                }
0781:                try {
0782:                    InetAddress theInterface = mss.getInterface();
0783:                    // under IPV6 we are not guarrenteed to get the same address back as
0784:                    // the address, all we should be guaranteed is that we get an
0785:                    // address on the same interface
0786:                    if (theInterface instanceof  Inet6Address) {
0787:                        assertTrue(
0788:                                "Failed to return correct interface IPV6",
0789:                                NetworkInterface
0790:                                        .getByInetAddress(mss.getInterface())
0791:                                        .equals(
0792:                                                NetworkInterface
0793:                                                        .getByInetAddress(theInterface)));
0794:                    } else {
0795:                        assertTrue(
0796:                                "Failed to return correct interface IPV4 got:"
0797:                                        + mss.getInterface() + " excpeted: "
0798:                                        + InetAddress.getLocalHost(), mss
0799:                                        .getInterface().equals(
0800:                                                InetAddress.getLocalHost()));
0801:                    }
0802:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0803:                } catch (SocketException e) {
0804:                    handleException(e, SO_MULTICAST);
0805:                } catch (UnknownHostException e) {
0806:                    fail("Exception during setInterface test: " + e.toString());
0807:                }
0808:
0809:                // Regression test for Harmony-2410
0810:                try {
0811:                    mss = new MulticastSocket();
0812:                    mss.setInterface(InetAddress.getByName("224.0.0.5"));
0813:                } catch (UnknownHostException uhe) {
0814:                    fail("Unable to get InetAddress by name from '224.0.0.5' addr: "
0815:                            + uhe.toString());
0816:                } catch (SocketException se) {
0817:                    // expected
0818:                } catch (IOException ioe) {
0819:                    handleException(ioe, SO_MULTICAST_INTERFACE);
0820:                    return;
0821:                }
0822:            }
0823:
0824:            /**
0825:             * @throws IOException 
0826:             * @throws InterruptedException 
0827:             * @tests java.net.MulticastSocket#setNetworkInterface(java.net.NetworkInterface)
0828:             */
0829:            public void test_setNetworkInterfaceLjava_net_NetworkInterface()
0830:                    throws IOException, InterruptedException {
0831:                String msg = null;
0832:                InetAddress group = null;
0833:                int[] ports = Support_PortManager.getNextPortsForUDP(2);
0834:                int groupPort = ports[0];
0835:                int serverPort = ports[1];
0836:                if (atLeastOneInterface) {
0837:                    // validate that null interface is handled ok
0838:                    mss = new MulticastSocket(groupPort);
0839:
0840:                    // this should through a socket exception to be compatible
0841:                    try {
0842:                        mss.setNetworkInterface(null);
0843:                        fail("No socket exception when we set then network interface with NULL");
0844:                    } catch (SocketException ex) {
0845:                    }
0846:
0847:                    // validate that we can get and set the interface
0848:                    groupPort = Support_PortManager.getNextPortForUDP();
0849:                    mss = new MulticastSocket(groupPort);
0850:                    mss.setNetworkInterface(networkInterface1);
0851:                    assertTrue(
0852:                            "Interface did not seem to be set by setNeworkInterface",
0853:                            networkInterface1.equals(mss.getNetworkInterface()));
0854:
0855:                    // set up the server and join the group
0856:                    group = InetAddress.getByName("224.0.0.3");
0857:
0858:                    Enumeration theInterfaces = NetworkInterface
0859:                            .getNetworkInterfaces();
0860:                    while (theInterfaces.hasMoreElements()) {
0861:                        NetworkInterface this Interface = (NetworkInterface) theInterfaces
0862:                                .nextElement();
0863:                        if (this Interface.getInetAddresses() != null
0864:                                && this Interface.getInetAddresses()
0865:                                        .hasMoreElements()) {
0866:                            if ((!((InetAddress) this Interface
0867:                                    .getInetAddresses().nextElement())
0868:                                    .isLoopbackAddress())
0869:                                    &&
0870:                                    // for windows we cannot use these pseudo
0871:                                    // interfaces for the test as the packets still
0872:                                    // come from the actual interface, not the
0873:                                    // Pseudo interface that was set
0874:                                    (Support_NetworkInterface
0875:                                            .useInterface(this Interface) == true)) {
0876:                                ports = Support_PortManager
0877:                                        .getNextPortsForUDP(2);
0878:                                serverPort = ports[0];
0879:                                server = new MulticastServer(group, serverPort);
0880:                                server.start();
0881:                                // give the server some time to start up
0882:                                Thread.sleep(1000);
0883:
0884:                                // Send the packets on a particular interface. The
0885:                                // source address in the received packet
0886:                                // should be one of the addresses for the interface
0887:                                // set
0888:                                groupPort = ports[1];
0889:                                mss = new MulticastSocket(groupPort);
0890:                                mss.setNetworkInterface(this Interface);
0891:                                msg = this Interface.getName();
0892:                                byte theBytes[] = msg.getBytes();
0893:                                DatagramPacket sdp = new DatagramPacket(
0894:                                        theBytes, theBytes.length, group,
0895:                                        serverPort);
0896:                                mss.send(sdp);
0897:                                Thread.sleep(1000);
0898:                                String receivedMessage = new String(server.rdp
0899:                                        .getData(), 0, server.rdp.getLength());
0900:                                assertTrue(
0901:                                        "Group member did not recv data when send on a specific interface: ",
0902:                                        receivedMessage.equals(msg));
0903:                                // stop the server
0904:                                server.stopServer();
0905:                            }
0906:                        }
0907:                    }
0908:                }
0909:            }
0910:
0911:            /**
0912:             * @tests java.net.MulticastSocket#setTimeToLive(int)
0913:             */
0914:            public void test_setTimeToLiveI() {
0915:                try {
0916:                    mss = new MulticastSocket();
0917:                    mss.setTimeToLive(120);
0918:                    assertTrue("Returned incorrect 1st TTL: "
0919:                            + mss.getTimeToLive(), mss.getTimeToLive() == 120);
0920:                    mss.setTimeToLive(220);
0921:                    assertTrue("Returned incorrect 2nd TTL: "
0922:                            + mss.getTimeToLive(), mss.getTimeToLive() == 220);
0923:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0924:                } catch (Exception e) {
0925:                    handleException(e, SO_MULTICAST);
0926:                }
0927:            }
0928:
0929:            /**
0930:             * @tests java.net.MulticastSocket#setTTL(byte)
0931:             */
0932:            public void test_setTTLB() {
0933:                // Test for method void java.net.MulticastSocket.setTTL(byte)
0934:                try {
0935:                    mss = new MulticastSocket();
0936:                    mss.setTTL((byte) 120);
0937:                    assertTrue("Failed to set TTL: " + mss.getTTL(), mss
0938:                            .getTTL() == 120);
0939:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
0940:                } catch (Exception e) {
0941:                    handleException(e, SO_MULTICAST);
0942:                }
0943:            }
0944:
0945:            /**
0946:             * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress)
0947:             */
0948:            public void test_ConstructorLjava_net_SocketAddress()
0949:                    throws Exception {
0950:                MulticastSocket ms = new MulticastSocket((SocketAddress) null);
0951:                assertTrue("should not be bound", !ms.isBound()
0952:                        && !ms.isClosed() && !ms.isConnected());
0953:                ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
0954:                        Support_PortManager.getNextPortForUDP()));
0955:                assertTrue("should be bound", ms.isBound() && !ms.isClosed()
0956:                        && !ms.isConnected());
0957:                ms.close();
0958:                assertTrue("should be closed", ms.isClosed());
0959:                ms = new MulticastSocket(new InetSocketAddress(InetAddress
0960:                        .getLocalHost(), Support_PortManager
0961:                        .getNextPortForUDP()));
0962:                assertTrue("should be bound", ms.isBound() && !ms.isClosed()
0963:                        && !ms.isConnected());
0964:                ms.close();
0965:                assertTrue("should be closed", ms.isClosed());
0966:                ms = new MulticastSocket(new InetSocketAddress("localhost",
0967:                        Support_PortManager.getNextPortForUDP()));
0968:                assertTrue("should be bound", ms.isBound() && !ms.isClosed()
0969:                        && !ms.isConnected());
0970:                ms.close();
0971:                assertTrue("should be closed", ms.isClosed());
0972:                boolean exception = false;
0973:                try {
0974:                    ms = new MulticastSocket(new InetSocketAddress(
0975:                            "unresolvedname", Support_PortManager
0976:                                    .getNextPortForUDP()));
0977:                } catch (IOException e) {
0978:                    exception = true;
0979:                }
0980:                assertTrue("Expected IOException", exception);
0981:
0982:                // regression test for Harmony-1162
0983:                InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 0);
0984:                MulticastSocket s = new MulticastSocket(addr);
0985:                assertTrue(s.getReuseAddress());
0986:            }
0987:
0988:            /**
0989:             * @tests java.net.MulticastSocket#getLoopbackMode()
0990:             */
0991:            public void test_getLoopbackMode() {
0992:                try {
0993:                    MulticastSocket ms = new MulticastSocket(
0994:                            (SocketAddress) null);
0995:                    assertTrue("should not be bound", !ms.isBound()
0996:                            && !ms.isClosed() && !ms.isConnected());
0997:                    ms.getLoopbackMode();
0998:                    assertTrue("should not be bound", !ms.isBound()
0999:                            && !ms.isClosed() && !ms.isConnected());
1000:                    ms.close();
1001:                    assertTrue("should be closed", ms.isClosed());
1002:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
1003:                } catch (IOException e) {
1004:                    handleException(e, SO_USELOOPBACK);
1005:                }
1006:            }
1007:
1008:            /**
1009:             * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
1010:             */
1011:            public void test_setLoopbackModeZ() {
1012:                try {
1013:                    MulticastSocket ms = new MulticastSocket();
1014:                    ms.setLoopbackMode(true);
1015:                    assertTrue("loopback should be true", ms.getLoopbackMode());
1016:                    ms.setLoopbackMode(false);
1017:                    assertTrue("loopback should be false", !ms
1018:                            .getLoopbackMode());
1019:                    ms.close();
1020:                    assertTrue("should be closed", ms.isClosed());
1021:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
1022:                } catch (IOException e) {
1023:                    handleException(e, SO_USELOOPBACK);
1024:                }
1025:            }
1026:
1027:            /**
1028:             * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
1029:             */
1030:            public void test_setLoopbackModeSendReceive() throws IOException {
1031:                final String ADDRESS = "224.1.2.3";
1032:                final int PORT = Support_PortManager.getNextPortForUDP();
1033:                final String message = "Hello, world!";
1034:
1035:                // test send receive
1036:                MulticastSocket socket = null;
1037:                try {
1038:                    // open a multicast socket
1039:                    socket = new MulticastSocket(PORT);
1040:                    socket.setLoopbackMode(false); // false indecates doing loop back
1041:                    socket.joinGroup(InetAddress.getByName(ADDRESS));
1042:
1043:                    // send the datagram
1044:                    byte[] sendData = message.getBytes();
1045:                    DatagramPacket sendDatagram = new DatagramPacket(sendData,
1046:                            0, sendData.length, new InetSocketAddress(
1047:                                    InetAddress.getByName(ADDRESS), PORT));
1048:                    socket.send(sendDatagram);
1049:
1050:                    // receive the datagram
1051:                    byte[] recvData = new byte[100];
1052:                    DatagramPacket recvDatagram = new DatagramPacket(recvData,
1053:                            recvData.length);
1054:                    socket.setSoTimeout(5000); // prevent eternal block in
1055:                    // socket.receive()
1056:                    socket.receive(recvDatagram);
1057:                    String recvMessage = new String(recvData, 0, recvDatagram
1058:                            .getLength());
1059:                    assertEquals(message, recvMessage);
1060:                } finally {
1061:                    if (socket != null)
1062:                        socket.close();
1063:                }
1064:            }
1065:
1066:            /**
1067:             * @tests java.net.MulticastSocket#setReuseAddress(boolean)
1068:             */
1069:            public void test_setReuseAddressZ() throws Exception {
1070:                try {
1071:                    // test case were we set it to false
1072:                    MulticastSocket theSocket1 = null;
1073:                    MulticastSocket theSocket2 = null;
1074:                    try {
1075:                        InetSocketAddress theAddress = new InetSocketAddress(
1076:                                InetAddress.getLocalHost(), Support_PortManager
1077:                                        .getNextPortForUDP());
1078:                        theSocket1 = new MulticastSocket(null);
1079:                        theSocket2 = new MulticastSocket(null);
1080:                        theSocket1.setReuseAddress(false);
1081:                        theSocket2.setReuseAddress(false);
1082:                        theSocket1.bind(theAddress);
1083:                        theSocket2.bind(theAddress);
1084:                        fail("No exception when trying to connect to do duplicate socket bind with re-useaddr set to false");
1085:                    } catch (BindException e) {
1086:
1087:                    }
1088:                    if (theSocket1 != null)
1089:                        theSocket1.close();
1090:                    if (theSocket2 != null)
1091:                        theSocket2.close();
1092:
1093:                    // test case were we set it to true
1094:                    InetSocketAddress theAddress = new InetSocketAddress(
1095:                            InetAddress.getLocalHost(), Support_PortManager
1096:                                    .getNextPortForUDP());
1097:                    theSocket1 = new MulticastSocket(null);
1098:                    theSocket2 = new MulticastSocket(null);
1099:                    theSocket1.setReuseAddress(true);
1100:                    theSocket2.setReuseAddress(true);
1101:                    theSocket1.bind(theAddress);
1102:                    theSocket2.bind(theAddress);
1103:
1104:                    if (theSocket1 != null)
1105:                        theSocket1.close();
1106:                    if (theSocket2 != null)
1107:                        theSocket2.close();
1108:
1109:                    // test the default case which we expect to be the same on all
1110:                    // platforms
1111:                    try {
1112:                        theAddress = new InetSocketAddress(InetAddress
1113:                                .getLocalHost(), Support_PortManager
1114:                                .getNextPortForUDP());
1115:                        theSocket1 = new MulticastSocket(null);
1116:                        theSocket2 = new MulticastSocket(null);
1117:                        theSocket1.bind(theAddress);
1118:                        theSocket2.bind(theAddress);
1119:                    } catch (BindException e) {
1120:                        fail("unexpected exception when trying to connect to do duplicate socket bind with re-useaddr left as default");
1121:                    }
1122:                    if (theSocket1 != null)
1123:                        theSocket1.close();
1124:                    if (theSocket2 != null)
1125:                        theSocket2.close();
1126:                    ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
1127:                } catch (Exception e) {
1128:                    handleException(e, SO_REUSEADDR);
1129:                }
1130:            }
1131:
1132:            /**
1133:             * Sets up the fixture, for example, open a network connection. This method
1134:             * is called before a test is executed.
1135:             */
1136:            protected void setUp() {
1137:
1138:                Enumeration theInterfaces = null;
1139:                try {
1140:                    theInterfaces = NetworkInterface.getNetworkInterfaces();
1141:                } catch (Exception e) {
1142:                }
1143:
1144:                // only consider interfaces that have addresses associated with them.
1145:                // Otherwise tests don't work so well
1146:                if (theInterfaces != null) {
1147:
1148:                    atLeastOneInterface = false;
1149:                    while (theInterfaces.hasMoreElements()
1150:                            && (atLeastOneInterface == false)) {
1151:                        networkInterface1 = (NetworkInterface) theInterfaces
1152:                                .nextElement();
1153:                        if ((networkInterface1.getInetAddresses() != null)
1154:                                && networkInterface1.getInetAddresses()
1155:                                        .hasMoreElements() &&
1156:                                // we only want real interfaces
1157:                                (Support_NetworkInterface
1158:                                        .useInterface(networkInterface1) == true)) {
1159:                            atLeastOneInterface = true;
1160:                        }
1161:                    }
1162:
1163:                    atLeastTwoInterfaces = false;
1164:                    if (theInterfaces.hasMoreElements()) {
1165:                        while (theInterfaces.hasMoreElements()
1166:                                && (atLeastTwoInterfaces == false)) {
1167:                            networkInterface2 = (NetworkInterface) theInterfaces
1168:                                    .nextElement();
1169:                            if ((networkInterface2.getInetAddresses() != null)
1170:                                    && networkInterface2.getInetAddresses()
1171:                                            .hasMoreElements() &&
1172:                                    // we only want real interfaces
1173:                                    (Support_NetworkInterface
1174:                                            .useInterface(networkInterface2) == true)) {
1175:                                atLeastTwoInterfaces = true;
1176:                            }
1177:                        }
1178:                    }
1179:
1180:                    Enumeration addresses;
1181:
1182:                    // first the first interface that supports IPV6 if one exists
1183:                    try {
1184:                        theInterfaces = NetworkInterface.getNetworkInterfaces();
1185:                    } catch (Exception e) {
1186:                    }
1187:                    boolean found = false;
1188:                    while (theInterfaces.hasMoreElements() && !found) {
1189:                        NetworkInterface nextInterface = (NetworkInterface) theInterfaces
1190:                                .nextElement();
1191:                        addresses = nextInterface.getInetAddresses();
1192:                        if (addresses != null) {
1193:                            while (addresses.hasMoreElements()) {
1194:                                InetAddress nextAddress = (InetAddress) addresses
1195:                                        .nextElement();
1196:                                if (nextAddress instanceof  Inet6Address) {
1197:                                    IPV6networkInterface1 = nextInterface;
1198:                                    found = true;
1199:                                    break;
1200:                                }
1201:                            }
1202:                        }
1203:                    }
1204:                }
1205:            }
1206:
1207:            /**
1208:             * Tears down the fixture, for example, close a network connection. This
1209:             * method is called after a test is executed.
1210:             */
1211:            protected void tearDown() {
1212:
1213:                if (t != null)
1214:                    t.interrupt();
1215:                if (mss != null)
1216:                    mss.close();
1217:                if (server != null)
1218:                    server.stopServer();
1219:            }
1220:        }
w___w__w.ja_va__2_s__.___com_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.