Path: blob/master/test/jdk/java/net/Socket/AddressTest.java
41152 views
/*1* Copyright (c) 2001, 2018, 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/*24* @test25* @bug 450750126* @library /test/lib27* @summary Test various methods that should throw IAE when passed improper28* SocketAddress29* @run main AddressTest30* @run main/othervm -Djava.net.preferIPv4Stack=true AddressTest31* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl AddressTest32*/3334import java.net.*;35import jdk.test.lib.net.IPSupport;3637public class AddressTest {38class MySocketAddress extends SocketAddress {39public MySocketAddress() {40}41}4243public AddressTest() throws Exception {44SocketAddress addr = new MySocketAddress();45Socket soc = new Socket();46ServerSocket serv = new ServerSocket();47DatagramSocket ds = new DatagramSocket((SocketAddress)null);48DatagramPacket pac = new DatagramPacket(new byte[20], 20);49MulticastSocket mul = new MulticastSocket((SocketAddress) null);50boolean ok = false;51try {52soc.bind(addr);53} catch (IllegalArgumentException e) {54ok = true;55} catch (Exception e2) {56}57if (!ok)58throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");5960ok = false;61soc.close();62soc = new Socket();63try {64soc.connect(addr, 100);65} catch (IllegalArgumentException e) {66ok = true;67} catch (Exception e2) {68}69if (!ok)70throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");7172ok = false;73try {74serv.bind(addr);75} catch (IllegalArgumentException e) {76ok = true;77} catch (Exception e2) {78}79if (!ok)80throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");8182ok = false;8384try {85pac.setSocketAddress(addr);86} catch (IllegalArgumentException e) {87ok = true;88} catch (Exception e2) {89}9091if (!ok)92throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");9394ok = false;9596try {97ds.bind(addr);98} catch (IllegalArgumentException e) {99ok = true;100} catch (Exception e2) {101}102103if (!ok)104throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");105106ok = false;107108try {109ds.connect(addr);110} catch (IllegalArgumentException e) {111ok = true;112} catch (Exception e2) {113}114115if (!ok)116throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");117118ok = false;119120try {121mul.bind(addr);122} catch (IllegalArgumentException e) {123ok = true;124} catch (Exception e2) {125}126127if (!ok)128throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");129130ok = false;131132mul.close();133mul = new MulticastSocket(new InetSocketAddress(0));134try {135mul.joinGroup(addr, null);136} catch (IllegalArgumentException e) {137ok = true;138} catch (Exception e2) {139}140141if (!ok)142throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");143144ok = false;145try {146mul.leaveGroup(addr, null);147} catch (IllegalArgumentException e) {148ok = true;149} catch (Exception e2) {150}151152if (!ok)153throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");154155}156157public static void main(String[] args) throws Exception {158IPSupport.throwSkippedExceptionIfNonOperational();159new AddressTest();160}161}162163164