1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package libcore.java.net; 19 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.net.ConnectException; 24import java.net.InetAddress; 25import java.net.InetSocketAddress; 26import java.net.ServerSocket; 27import java.net.Socket; 28import java.net.SocketException; 29import junit.framework.TestCase; 30 31public class OldUnixSocketTest extends TestCase { 32 33 public void test_getInputStream() throws IOException { 34 // Simple read/write test over the IO streams 35 final ServerSocket pingServer = new ServerSocket(0); 36 Socket pingClient = new Socket(); 37 38 try { 39 pingClient.connect(new InetSocketAddress( 40 InetAddress.getLocalHost(), pingServer.getLocalPort())); 41 42 Socket worker = pingServer.accept(); 43 pingServer.close(); 44 45 // Write some data to the server to provoke it 46 OutputStream clientOut = pingClient.getOutputStream(); 47 clientOut.write(new byte[256]); 48 InputStream in = worker.getInputStream(); 49 in.read(); 50 51 OutputStream out = worker.getOutputStream(); 52 out.write(new byte[42]); 53 worker.close(); 54 InputStream clientIn = pingClient.getInputStream(); 55 clientIn.read(new byte[42]); 56 57 try { 58 int i = clientIn.read(); 59 fail("Should throw SocketException; got i=" + i); 60 } catch (SocketException e) { 61 // expected 62 } 63 clientIn.close(); 64 65 try { 66 clientIn.read(); 67 fail("Should throw SocketException"); 68 } catch (SocketException e) { 69 // expected 70 } 71 try { 72 clientIn.read(new byte[5]); 73 fail("Should throw SocketException"); 74 } catch (SocketException e) { 75 // expected 76 } 77 } finally { 78 pingClient.close(); 79 pingServer.close(); 80 } 81 } 82 83 public void test_connectLjava_net_SocketAddressI() throws Exception { 84 // Now validate that we get a interrupted exception if we try to connect 85 // to an address on which nobody is accepting connections and the 86 // timeout expired 87 Socket theSocket = new Socket(); 88 try { 89 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 90 1), 200); 91 fail("No interrupted exception when connecting to address nobody listening on with short timeout 200"); 92 } catch (ConnectException e) { 93 // Expected 94 } 95 theSocket.close(); 96 } 97 98 public void test_getOutputStream() throws Exception { 99 // Regression test for HARMONY-2934 100 // Port 0 is not allowed to be used in connect() on some platforms, 101 // get a free port here 102 ServerSocket ss = new ServerSocket(0); 103 int port = ss.getLocalPort(); 104 ss.close(); 105 106 Socket socket = new Socket("127.0.0.1", port, false); 107 OutputStream o = socket.getOutputStream(); 108 try { 109 o.write(1); 110 } catch (SocketException e) { 111 // expected 112 } finally { 113 socket.close(); 114 } 115 } 116} 117