Path: blob/master/test/jdk/java/net/NetworkInterface/NetworkInterfaceStreamTest.java
41149 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8081678 813115525* @summary Tests for stream returning methods26* @library /lib/testlibrary/bootlib /test/lib27* @build java.base/java.util.stream.OpTestCase28* @run testng/othervm NetworkInterfaceStreamTest29* @run testng/othervm -Djava.net.preferIPv4Stack=true NetworkInterfaceStreamTest30*/3132import org.testng.annotations.BeforeTest;33import org.testng.annotations.Test;3435import java.net.InetAddress;36import java.net.NetworkInterface;37import java.net.SocketException;38import java.util.ArrayList;39import java.util.Collection;40import java.util.Collections;41import java.util.function.Supplier;42import java.util.stream.OpTestCase;43import java.util.stream.Stream;44import java.util.stream.TestData;4546import jdk.test.lib.net.IPSupport;4748public class NetworkInterfaceStreamTest extends OpTestCase {4950private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");5152@BeforeTest53void setup() {54IPSupport.throwSkippedExceptionIfNonOperational();55}5657@Test58public void testNetworkInterfaces() throws SocketException {59Supplier<Stream<NetworkInterface>> ss = () -> {60try {61return NetworkInterface.networkInterfaces()62.filter(ni -> isIncluded(ni));63}64catch (SocketException e) {65throw new RuntimeException(e);66}67};6869Collection<NetworkInterface> enums = Collections.list(NetworkInterface.getNetworkInterfaces());70Collection<NetworkInterface> expected = new ArrayList<>();71enums.forEach(ni -> {72if (isIncluded(ni)) {73expected.add(ni);74}75});76withData(TestData.Factory.ofSupplier("Top-level network interfaces", ss))77.stream(s -> s)78.expectedResult(expected)79.exercise();80}8182private Collection<NetworkInterface> getAllNetworkInterfaces() throws SocketException {83Collection<NetworkInterface> anis = new ArrayList<>();84for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {85getAllSubNetworkInterfaces(ni, anis);86}87return anis;88}8990private void getAllSubNetworkInterfaces(NetworkInterface ni, Collection<NetworkInterface> result) {91if (isIncluded(ni)) {92result.add(ni);93}9495for (NetworkInterface sni : Collections.list(ni.getSubInterfaces())) {96getAllSubNetworkInterfaces(sni, result);97}98}99100private Stream<NetworkInterface> allNetworkInterfaces() throws SocketException {101return NetworkInterface.networkInterfaces()102.filter(ni -> isIncluded(ni))103.flatMap(this::allSubNetworkInterfaces);104}105106private Stream<NetworkInterface> allSubNetworkInterfaces(NetworkInterface ni) {107return Stream.concat(108Stream.of(ni),109ni.subInterfaces().filter(sni -> isIncluded(sni)).flatMap(this::allSubNetworkInterfaces));110}111112@Test113public void testSubNetworkInterfaces() throws SocketException {114Supplier<Stream<NetworkInterface>> ss = () -> {115try {116return allNetworkInterfaces();117}118catch (SocketException e) {119throw new RuntimeException(e);120}121};122123Collection<NetworkInterface> expected = getAllNetworkInterfaces();124withData(TestData.Factory.ofSupplier("All network interfaces", ss))125.stream(s -> s)126.expectedResult(expected)127.exercise();128}129130131@Test132public void testInetAddresses() throws SocketException {133Supplier<Stream<InetAddress>> ss = () -> {134try {135return NetworkInterface.networkInterfaces()136.filter(ni -> isIncluded(ni))137.flatMap(NetworkInterface::inetAddresses);138}139catch (SocketException e) {140throw new RuntimeException(e);141}142};143144Collection<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());145Collection<InetAddress> expected = new ArrayList<>();146for (NetworkInterface ni : nis) {147if (isIncluded(ni)) {148expected.addAll(Collections.list(ni.getInetAddresses()));149}150}151withData(TestData.Factory.ofSupplier("All inet addresses", ss))152.stream(s -> s)153.expectedResult(expected)154.exercise();155}156157/**158* Check if the input network interface should be included in the test. It is necessary to exclude159* "Teredo Tunneling Pseudo-Interface" whose configuration can be variable during a test run.160*161* @param ni a network interace162* @return false if it is a "Teredo Tunneling Pseudo-Interface", otherwise true.163*/164private boolean isIncluded(NetworkInterface ni) {165if (!IS_WINDOWS) {166return true;167}168169String dName = ni.getDisplayName();170return dName == null || !dName.contains("Teredo");171}172173}174175176177