Path: blob/master/test/jdk/java/net/Socket/SocketImplTest.java
41152 views
/*1* Copyright (c) 2002, 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*/22import java.applet.Applet;23import java.io.*;24import java.net.*;2526/**27* Simple Applet for exposing the Socket constructor28* bug.29*/30public class SocketImplTest extends Applet {3132static public void main(String[] args) {33System.setSecurityManager(new SecurityManager());34SocketImplTest s = new SocketImplTest();35s.init();36s.start();37}383940/**41* A no-op SocketImpl descendant.42*/43class MySocketImpl extends SocketImpl {44protected void accept(SocketImpl impl) throws IOException {45}4647protected int available(){48return 0;49}5051protected void bind(InetAddress host, int port){52}5354protected void close(){55}5657protected void connect(InetAddress address, int port){58}5960protected void connect(String host, int port){61}6263protected void connect(SocketAddress a, int t) throws IOException {64}656667protected void create(boolean stream){68}6970protected InputStream getInputStream(){71return null;72}7374protected OutputStream getOutputStream(){75return null;76}7778protected void listen(int backlog){79}8081public Object getOption(int optID){82return null;83}8485public void setOption(int optID, Object value){86}8788protected void sendUrgentData(int i){89}90}9192class MyDatagramSocketImpl extends DatagramSocketImpl {93protected void create() throws SocketException {94}9596protected void bind(int lport, InetAddress laddr) throws SocketException {97}9899protected void send(DatagramPacket p) throws IOException {100}101102protected int peek(InetAddress i) throws IOException {103return 0;104}105106protected int peekData(DatagramPacket p) throws IOException {107return 0;108}109110protected void receive(DatagramPacket p) throws IOException {111}112113protected void setTTL(byte ttl) throws IOException {114}115116protected byte getTTL() throws IOException {117return 0;118}119120protected void setTimeToLive(int ttl) throws IOException {121}122123protected int getTimeToLive() throws IOException {124return 0;125}126127protected void join(InetAddress inetaddr) throws IOException {128}129130protected void leave(InetAddress inetaddr) throws IOException {131}132133protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)134throws IOException {135}136137protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)138throws IOException {139}140141protected void close() {142}143144public Object getOption(int optID){145return null;146}147148public void setOption(int optID, Object value){149}150151}152153/**154* A no-op Socket descendant.155*/156class MySocket extends Socket {157public MySocket(SocketImpl impl) throws IOException {158super(impl);159}160}161162class MyDatagramSocket extends DatagramSocket {163public MyDatagramSocket(DatagramSocketImpl impl) {164super(impl);165}166}167168/**169* Our test case entrypoint. Generates170* a SecurityException.171*/172public void init(){173MySocketImpl socketImpl = new MySocketImpl();174MyDatagramSocketImpl dgramSocketImpl = new MyDatagramSocketImpl();175176try{177MySocket socko = new MySocket(socketImpl);178MyDatagramSocket dsock = new MyDatagramSocket(dgramSocketImpl);179} catch(IOException ioex){180System.err.println(ioex);181} catch(SecurityException sec) {182throw new RuntimeException("Failed. Creation of socket throwing SecurityException: ");183}184}185}186187188