jdk/test/java/net/Socket/AddressTest.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 5506 202f599c92aa
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4507501
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test various methods that should throw IAE when passed improper SocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class AddressTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    class MySocketAddress extends SocketAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        public MySocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public AddressTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        SocketAddress addr = new MySocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        Socket soc = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        ServerSocket serv = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        DatagramSocket ds = new DatagramSocket((SocketAddress)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        DatagramPacket pac = new DatagramPacket(new byte[20], 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        MulticastSocket mul = new MulticastSocket((SocketAddress) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            soc.bind(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        soc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        soc = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            soc.connect(addr, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            serv.bind(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            pac.setSocketAddress(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ds.bind(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            ds.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            mul.bind(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        mul.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        mul = new MulticastSocket(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            mul.joinGroup(addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            mul.leaveGroup(addr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        new AddressTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}