jdk/test/java/net/Socket/SocketImplTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2002 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 import java.applet.Applet;
       
    24 import java.io.*;
       
    25 import java.net.*;
       
    26 
       
    27 /**
       
    28  * Simple Applet for exposing the Socket constructor
       
    29  * bug.
       
    30  */
       
    31 public class SocketImplTest extends Applet {
       
    32 
       
    33     static public void main(String[] args) {
       
    34         System.setSecurityManager(new SecurityManager());
       
    35         SocketImplTest s = new SocketImplTest();
       
    36         s.init();
       
    37         s.start();
       
    38     }
       
    39 
       
    40 
       
    41     /**
       
    42      * A no-op SocketImpl descendant.
       
    43      */
       
    44     class MySocketImpl extends SocketImpl {
       
    45         protected void accept(SocketImpl impl) throws IOException {
       
    46         }
       
    47 
       
    48         protected int available(){
       
    49             return 0;
       
    50         }
       
    51 
       
    52         protected void bind(InetAddress host, int port){
       
    53         }
       
    54 
       
    55         protected void close(){
       
    56         }
       
    57 
       
    58         protected void connect(InetAddress address, int port){
       
    59         }
       
    60 
       
    61         protected void connect(String host, int port){
       
    62         }
       
    63 
       
    64         protected void connect(SocketAddress a, int t) throws IOException {
       
    65         }
       
    66 
       
    67 
       
    68         protected void create(boolean stream){
       
    69         }
       
    70 
       
    71         protected InputStream getInputStream(){
       
    72             return null;
       
    73         }
       
    74 
       
    75         protected OutputStream getOutputStream(){
       
    76             return null;
       
    77         }
       
    78 
       
    79         protected void listen(int backlog){
       
    80         }
       
    81 
       
    82         public Object getOption(int optID){
       
    83             return null;
       
    84         }
       
    85 
       
    86         public void setOption(int optID, Object value){
       
    87         }
       
    88 
       
    89         protected void sendUrgentData(int i){
       
    90         }
       
    91     }
       
    92 
       
    93     class MyDatagramSocketImpl extends DatagramSocketImpl {
       
    94         protected void create() throws SocketException {
       
    95         }
       
    96 
       
    97         protected void bind(int lport, InetAddress laddr) throws SocketException {
       
    98         }
       
    99 
       
   100         protected void send(DatagramPacket p) throws IOException {
       
   101         }
       
   102 
       
   103         protected int peek(InetAddress i) throws IOException {
       
   104             return 0;
       
   105         }
       
   106 
       
   107         protected int peekData(DatagramPacket p) throws IOException {
       
   108             return 0;
       
   109         }
       
   110 
       
   111         protected void receive(DatagramPacket p) throws IOException {
       
   112         }
       
   113 
       
   114         protected void setTTL(byte ttl) throws IOException {
       
   115         }
       
   116 
       
   117         protected byte getTTL() throws IOException {
       
   118             return 0;
       
   119         }
       
   120 
       
   121         protected void setTimeToLive(int ttl) throws IOException {
       
   122         }
       
   123 
       
   124         protected int getTimeToLive() throws IOException {
       
   125             return 0;
       
   126         }
       
   127 
       
   128         protected void join(InetAddress inetaddr) throws IOException {
       
   129         }
       
   130 
       
   131         protected void leave(InetAddress inetaddr) throws IOException {
       
   132         }
       
   133 
       
   134         protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
       
   135             throws IOException {
       
   136         }
       
   137 
       
   138         protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
       
   139             throws IOException {
       
   140         }
       
   141 
       
   142         protected void close() {
       
   143         }
       
   144 
       
   145         public Object getOption(int optID){
       
   146             return null;
       
   147         }
       
   148 
       
   149         public void setOption(int optID, Object value){
       
   150         }
       
   151 
       
   152     }
       
   153 
       
   154     /**
       
   155      * A no-op Socket descendant.
       
   156      */
       
   157     class MySocket extends Socket {
       
   158         public MySocket(SocketImpl impl) throws IOException {
       
   159             super(impl);
       
   160         }
       
   161     }
       
   162 
       
   163     class MyDatagramSocket extends DatagramSocket {
       
   164         public MyDatagramSocket(DatagramSocketImpl impl) {
       
   165             super(impl);
       
   166         }
       
   167     }
       
   168 
       
   169     /**
       
   170      * Our test case entrypoint. Generates
       
   171      * a SecurityException.
       
   172      */
       
   173     public void init(){
       
   174         MySocketImpl socketImpl = new MySocketImpl();
       
   175         MyDatagramSocketImpl dgramSocketImpl = new MyDatagramSocketImpl();
       
   176 
       
   177         try{
       
   178             MySocket socko = new MySocket(socketImpl);
       
   179             MyDatagramSocket dsock = new MyDatagramSocket(dgramSocketImpl);
       
   180         } catch(IOException ioex){
       
   181             System.err.println(ioex);
       
   182         } catch(SecurityException sec) {
       
   183             throw new RuntimeException("Failed. Creation of socket throwing SecurityException: ");
       
   184         }
       
   185     }
       
   186 }