jdk/test/java/net/Socket/AddressTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2001 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4507501
       
    27  * @summary Test various methods that should throw IAE when passed improper SocketAddress
       
    28  *
       
    29  */
       
    30 
       
    31 import java.net.*;
       
    32 
       
    33 public class AddressTest {
       
    34     class MySocketAddress extends SocketAddress {
       
    35         public MySocketAddress() {
       
    36         }
       
    37     }
       
    38 
       
    39     public AddressTest() throws Exception {
       
    40         SocketAddress addr = new MySocketAddress();
       
    41         Socket soc = new Socket();
       
    42         ServerSocket serv = new ServerSocket();
       
    43         DatagramSocket ds = new DatagramSocket((SocketAddress)null);
       
    44         DatagramPacket pac = new DatagramPacket(new byte[20], 20);
       
    45         MulticastSocket mul = new MulticastSocket((SocketAddress) null);
       
    46         boolean ok = false;
       
    47         try {
       
    48             soc.bind(addr);
       
    49         } catch (IllegalArgumentException e) {
       
    50             ok = true;
       
    51         } catch (Exception e2) {
       
    52         }
       
    53         if (!ok)
       
    54             throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");
       
    55 
       
    56         ok = false;
       
    57         soc.close();
       
    58         soc = new Socket();
       
    59         try {
       
    60             soc.connect(addr, 100);
       
    61         } catch (IllegalArgumentException e) {
       
    62             ok = true;
       
    63         } catch (Exception e2) {
       
    64         }
       
    65         if (!ok)
       
    66             throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");
       
    67 
       
    68         ok = false;
       
    69         try {
       
    70             serv.bind(addr);
       
    71         } catch (IllegalArgumentException e) {
       
    72             ok = true;
       
    73         } catch (Exception e2) {
       
    74         }
       
    75         if (!ok)
       
    76             throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");
       
    77 
       
    78         ok = false;
       
    79 
       
    80         try {
       
    81             pac.setSocketAddress(addr);
       
    82         } catch (IllegalArgumentException e) {
       
    83             ok = true;
       
    84         } catch (Exception e2) {
       
    85         }
       
    86 
       
    87         if (!ok)
       
    88             throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");
       
    89 
       
    90         ok = false;
       
    91 
       
    92         try {
       
    93             ds.bind(addr);
       
    94         } catch (IllegalArgumentException e) {
       
    95             ok = true;
       
    96         } catch (Exception e2) {
       
    97         }
       
    98 
       
    99         if (!ok)
       
   100             throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");
       
   101 
       
   102         ok = false;
       
   103 
       
   104         try {
       
   105             ds.connect(addr);
       
   106         } catch (IllegalArgumentException e) {
       
   107             ok = true;
       
   108         } catch (Exception e2) {
       
   109         }
       
   110 
       
   111         if (!ok)
       
   112             throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");
       
   113 
       
   114         ok = false;
       
   115 
       
   116         try {
       
   117             mul.bind(addr);
       
   118         } catch (IllegalArgumentException e) {
       
   119             ok = true;
       
   120         } catch (Exception e2) {
       
   121         }
       
   122 
       
   123         if (!ok)
       
   124             throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");
       
   125 
       
   126         ok = false;
       
   127 
       
   128         mul.close();
       
   129         mul = new MulticastSocket(new InetSocketAddress(0));
       
   130         try {
       
   131             mul.joinGroup(addr, null);
       
   132         } catch (IllegalArgumentException e) {
       
   133             ok = true;
       
   134         } catch (Exception e2) {
       
   135         }
       
   136 
       
   137         if (!ok)
       
   138             throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");
       
   139 
       
   140         ok = false;
       
   141         try {
       
   142             mul.leaveGroup(addr, null);
       
   143         } catch (IllegalArgumentException e) {
       
   144             ok = true;
       
   145         } catch (Exception e2) {
       
   146         }
       
   147 
       
   148         if (!ok)
       
   149             throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");
       
   150 
       
   151     }
       
   152 
       
   153     public static void main(String[] args) throws Exception {
       
   154         new AddressTest();
       
   155     }
       
   156 }