Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/AddressTest.java
38821 views
/*1* Copyright (c) 2001, 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* @summary Test various methods that should throw IAE when passed improper SocketAddress27*28*/2930import java.net.*;3132public class AddressTest {33class MySocketAddress extends SocketAddress {34public MySocketAddress() {35}36}3738public AddressTest() throws Exception {39SocketAddress addr = new MySocketAddress();40Socket soc = new Socket();41ServerSocket serv = new ServerSocket();42DatagramSocket ds = new DatagramSocket((SocketAddress)null);43DatagramPacket pac = new DatagramPacket(new byte[20], 20);44MulticastSocket mul = new MulticastSocket((SocketAddress) null);45boolean ok = false;46try {47soc.bind(addr);48} catch (IllegalArgumentException e) {49ok = true;50} catch (Exception e2) {51}52if (!ok)53throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");5455ok = false;56soc.close();57soc = new Socket();58try {59soc.connect(addr, 100);60} catch (IllegalArgumentException e) {61ok = true;62} catch (Exception e2) {63}64if (!ok)65throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");6667ok = false;68try {69serv.bind(addr);70} catch (IllegalArgumentException e) {71ok = true;72} catch (Exception e2) {73}74if (!ok)75throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");7677ok = false;7879try {80pac.setSocketAddress(addr);81} catch (IllegalArgumentException e) {82ok = true;83} catch (Exception e2) {84}8586if (!ok)87throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");8889ok = false;9091try {92ds.bind(addr);93} catch (IllegalArgumentException e) {94ok = true;95} catch (Exception e2) {96}9798if (!ok)99throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");100101ok = false;102103try {104ds.connect(addr);105} catch (IllegalArgumentException e) {106ok = true;107} catch (Exception e2) {108}109110if (!ok)111throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");112113ok = false;114115try {116mul.bind(addr);117} catch (IllegalArgumentException e) {118ok = true;119} catch (Exception e2) {120}121122if (!ok)123throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");124125ok = false;126127mul.close();128mul = new MulticastSocket(new InetSocketAddress(0));129try {130mul.joinGroup(addr, null);131} catch (IllegalArgumentException e) {132ok = true;133} catch (Exception e2) {134}135136if (!ok)137throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");138139ok = false;140try {141mul.leaveGroup(addr, null);142} catch (IllegalArgumentException e) {143ok = true;144} catch (Exception e2) {145}146147if (!ok)148throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");149150}151152public static void main(String[] args) throws Exception {153new AddressTest();154}155}156157158