test/java/net/MulticastSocket/TestInterfaces.java

Print this page




  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  * @test
  26  * @bug 4422122
  27  * @summary Test that MulticastSocket.getInterface returns the
  28  *          same InetAddress set by MulticastSocket.setInterface
  29  */
  30 import java.net.*;


  31 import java.util.Enumeration;
  32 import java.io.IOException;
  33 
  34 public class TestInterfaces {
  35 
  36     static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
  37 
  38     public static void main(String args[]) throws Exception {
  39         int failures = 0;
  40 
  41         MulticastSocket soc = new MulticastSocket();
  42 
  43         Enumeration nifs = NetworkInterface.getNetworkInterfaces();
  44         while (nifs.hasMoreElements()) {
  45             NetworkInterface ni = (NetworkInterface)nifs.nextElement();
  46 
  47             /*
  48              * Test MulticastSocket.getInterface
  49              */

  50             Enumeration addrs = ni.getInetAddresses();
  51             while (addrs.hasMoreElements()) {
  52                 InetAddress ia = (InetAddress)addrs.nextElement();
  53 
  54                 System.out.println("********************************");
  55                 System.out.println("MulticastSocket.setInterface(" + ia + ")");
  56 
  57                 try {
  58                     soc.setInterface(ia);
  59                 } catch (IOException ioe) {
  60                     System.err.println("Can't set interface to: " + ia
  61                         + " " + ioe.getMessage());
  62                     continue;
  63                 }
  64 
  65                 InetAddress curr = soc.getInterface();
  66                 if (!curr.equals(ia)) {


  67                     System.err.println("MulticastSocket.getInterface returned: " + curr);
  68                     System.err.println("Failed! Expected: " + ia);
  69                     failures++;
  70                 } else {
  71                     System.out.println("Passed.");
  72                 }
  73             }
  74 
  75             /*
  76              * Test MulticastSocket.getNetworkInterface
  77              */
  78             System.out.println("********************************");
  79             System.out.println("MulticastSocket.setNetworkInterface(" +
  80                 ni.getName() + ")");
  81 
  82             try {
  83                 soc.setNetworkInterface(ni);
  84             } catch (IOException ioe) {
  85                 System.err.println("Can't set interface to: " + ni.getName()
  86                         + " " + ioe.getMessage());
  87                 continue;
  88             }
  89 
  90             // JDK-8022963, Skip (Windows) Teredo Tunneling Pseudo-Interface
  91             String dName = ni.getDisplayName();
  92             if (isWindows && dName != null && dName.contains("Teredo"))
  93                 continue;
  94 
  95             NetworkInterface curr = soc.getNetworkInterface();
  96             if (!curr.equals(ni)) {
  97                 System.err.println("MulticastSocket.getNetworkInterface returned: " + curr);
  98                 System.err.println("Failed! Expected: " + ni);




  99                 failures++;
 100             } else {
 101                 System.out.println("Passed.");
 102             }
 103 
 104         }
 105 
 106         if (failures > 0) {
 107             System.out.println("********************************");
 108             throw new Exception(failures + " test(s) failed!!!");
 109         }
 110 
 111     }
 112 



















 113 }


  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  * @test
  26  * @bug 4422122
  27  * @summary Test that MulticastSocket.getInterface returns the
  28  *          same InetAddress set by MulticastSocket.setInterface
  29  */
  30 import java.net.*;
  31 import java.util.Arrays;
  32 import java.util.Collections;
  33 import java.util.Enumeration;
  34 import java.io.IOException;
  35 
  36 public class TestInterfaces {
  37 
  38     static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
  39 
  40     public static void main(String args[]) throws Exception {
  41         int failures = 0;
  42 
  43         MulticastSocket soc = new MulticastSocket();
  44 
  45         Enumeration nifs = NetworkInterface.getNetworkInterfaces();
  46         while (nifs.hasMoreElements()) {
  47             NetworkInterface ni = (NetworkInterface)nifs.nextElement();
  48 
  49             /*
  50              * Test MulticastSocket.getInterface
  51              */
  52             System.out.println("Testing network interface " + ni);
  53             Enumeration addrs = ni.getInetAddresses();
  54             while (addrs.hasMoreElements()) {
  55                 InetAddress ia = (InetAddress)addrs.nextElement();
  56 
  57                 System.out.println("********************************");
  58                 System.out.println("MulticastSocket.setInterface(" + ia + ")");
  59 
  60                 try {
  61                     soc.setInterface(ia);
  62                 } catch (IOException ioe) {
  63                     System.err.println("Can't set interface to: " + ia
  64                         + " " + ioe.getMessage());
  65                     continue;
  66                 }
  67 
  68                 InetAddress curr = soc.getInterface();
  69                 if (!curr.equals(ia)) {
  70                     System.err.println("NetworkInterface under test " + ni);
  71                     displayInterfaceInformation(ni);
  72                     System.err.println("MulticastSocket.getInterface returned: " + curr);
  73                     System.err.println("Failed! Expected: " + ia);
  74                     failures++;
  75                 } else {
  76                     System.out.println("Passed.");
  77                 }
  78             }
  79 
  80             /*
  81              * Test MulticastSocket.getNetworkInterface
  82              */
  83             System.out.println("********************************");
  84             System.out.println("MulticastSocket.setNetworkInterface(" +
  85                 ni.getName() + ")");
  86 
  87             try {
  88                 soc.setNetworkInterface(ni);
  89             } catch (IOException ioe) {
  90                 System.err.println("Can't set interface to: " + ni.getName()
  91                         + " " + ioe.getMessage());
  92                 continue;
  93             }
  94 
  95             // JDK-8022963, Skip (Windows) Teredo Tunneling Pseudo-Interface
  96             String dName = ni.getDisplayName();
  97             if (isWindows && dName != null && dName.contains("Teredo"))
  98                 continue;
  99 
 100             NetworkInterface curr = soc.getNetworkInterface();
 101             if (!curr.equals(ni)) {
 102                 System.err.println("MulticastSocket.getNetworkInterface returned: " + curr);
 103                 System.err.println("Failed! Expected: " + ni);
 104                 System.err.println("NetworkInterface details for curr variable ");
 105                 displayInterfaceInformation(curr);
 106                 System.err.println("NetworkInterface details for ni variable ");
 107                 displayInterfaceInformation(ni) ;
 108                 failures++;
 109             } else {
 110                 System.out.println("Passed.");
 111             }
 112 
 113         }
 114 
 115         if (failures > 0) {
 116             System.out.println("********************************");
 117             throw new Exception(failures + " test(s) failed!!!");
 118         }
 119 
 120     }
 121 
 122     static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
 123         System.err.println("Display name: " + netint.getDisplayName());
 124         System.err.println("Name: " + netint.getName());
 125         Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
 126 
 127         for (InetAddress inetAddress : Collections.list(inetAddresses))
 128             System.err.println("InetAddress: " + inetAddress);
 129 
 130         System.err.println("Up? " + netint.isUp());
 131         System.err.println("Loopback? " + netint.isLoopback());
 132         System.err.println("PointToPoint? " + netint.isPointToPoint());
 133         System.err.println("Supports multicast? " + netint.supportsMulticast());
 134         System.err.println("Virtual? " + netint.isVirtual());
 135         System.err.println("Hardware address: " +
 136                 Arrays.toString(netint.getHardwareAddress()));
 137         System.err.println("MTU: " + netint.getMTU());
 138         System.err.println("Index: " + netint.getIndex());
 139         System.err.println();
 140     }
 141 }