1 /* 2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * 26 * @bug 4488458 27 * @summary Test that MutlicastSocket.joinGroup is working for 28 * various multicast and non-multicast addresses. 29 */ 30 import java.net.*; 31 import java.util.Enumeration; 32 import java.io.IOException; 33 34 public class MulticastAddresses { 35 main(String args[])36 public static void main(String args[]) throws Exception { 37 38 boolean ipv6_available = false; 39 NetworkInterface ni = null; 40 41 /* 42 * Examine the network interfaces and determine :- 43 * 44 * 1. If host has IPv6 support 45 * 2. Get reference to a non-loopback interface 46 */ 47 Enumeration nifs = NetworkInterface.getNetworkInterfaces(); 48 while (nifs.hasMoreElements()) { 49 NetworkInterface this_ni = (NetworkInterface)nifs.nextElement(); 50 51 Enumeration addrs = this_ni.getInetAddresses(); 52 while (addrs.hasMoreElements()) { 53 InetAddress addr = (InetAddress)addrs.nextElement(); 54 if (addr instanceof Inet6Address) { 55 ipv6_available = true; 56 } 57 58 if (!addr.isLoopbackAddress() && ni == null) { 59 ni = this_ni; 60 } 61 } 62 63 if (ipv6_available) { 64 break; 65 } 66 } 67 68 int failures = 0; 69 70 String multicasts[] = { 71 "224.80.80.80", 72 "ff01::1", 73 "ff02::1234", 74 "ff05::a", 75 "ff0e::1234:a" }; 76 77 String non_multicasts[] = { 78 "129.1.1.1", 79 "::1", 80 "::129.1.1.1", 81 "fe80::a00:20ff:fee5:bc02" }; 82 83 MulticastSocket s = new MulticastSocket(); 84 85 /* test valid multicast addresses */ 86 87 for (int i=0; i<multicasts.length; i++) { 88 InetAddress ia = InetAddress.getByName(multicasts[i]); 89 if (ia instanceof Inet6Address && !ipv6_available) { 90 continue; 91 } 92 93 System.out.println("Test: " + ia); 94 95 try { 96 97 System.out.print(" joinGroup(InetAddress) "); 98 s.joinGroup(ia); 99 s.leaveGroup(ia); 100 System.out.println(" Passed."); 101 102 System.out.print(" joinGroup(InetAddress,NetworkInterface) "); 103 s.joinGroup(new InetSocketAddress(ia,0), ni); 104 s.leaveGroup(new InetSocketAddress(ia,0), ni); 105 System.out.println(" Passed."); 106 } catch (IOException e) { 107 failures++; 108 System.out.println("Failed: " + e.getMessage()); 109 } 110 111 } 112 113 /* test non-multicast addresses */ 114 115 for (int i=0; i<non_multicasts.length; i++) { 116 InetAddress ia = InetAddress.getByName(non_multicasts[i]); 117 if (ia instanceof Inet6Address && !ipv6_available) { 118 continue; 119 } 120 121 boolean failed = false; 122 123 System.out.println("Test: " + ia + " "); 124 try { 125 System.out.println(" joinGroup(InetAddress) "); 126 s.joinGroup(ia); 127 128 System.out.println("Failed!! -- incorrectly joined group"); 129 failed = true; 130 } catch (IOException e) { 131 System.out.println(" Passed: " + e.getMessage()); 132 } 133 134 if (failed) { 135 s.leaveGroup(ia); 136 failures++; 137 } 138 } 139 140 /* done */ 141 142 s.close(); 143 144 if (failures > 0) { 145 throw new Exception(failures + " test(s) failed - see log file."); 146 } 147 } 148 149 } 150