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 org.apache.harmony.luni.tests.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; 29 30import junit.framework.TestCase; 31 32public class UnixSocketTest extends TestCase { 33 34 /** 35 * @tests java.net.Socket#getInputStream() 36 */ 37 public void test_getInputStream() throws IOException { 38 // Simple read/write test over the IO streams 39 final ServerSocket pingServer = new ServerSocket(0); 40 Socket pingClient = new Socket(); 41 42 try { 43 pingClient.connect(new InetSocketAddress( 44 InetAddress.getLocalHost(), pingServer.getLocalPort())); 45 46 Socket worker = pingServer.accept(); 47 pingServer.close(); 48 49 // Write some data to the server to provoke it 50 OutputStream clientOut = pingClient.getOutputStream(); 51 clientOut.write(new byte[256]); 52 InputStream in = worker.getInputStream(); 53 in.read(); 54 55 OutputStream out = worker.getOutputStream(); 56 out.write(new byte[42]); 57 worker.close(); 58 InputStream clientIn = pingClient.getInputStream(); 59 clientIn.read(new byte[42]); 60 61 try { 62 clientIn.read(); 63 fail("Should throw SocketException"); 64 } catch (SocketException e) { 65 // expected 66 } 67 clientIn.close(); 68 69 try { 70 clientIn.read(); 71 fail("Should throw SocketException"); 72 } catch (SocketException e) { 73 // expected 74 } 75 try { 76 clientIn.read(new byte[5]); 77 fail("Should throw SocketException"); 78 } catch (SocketException e) { 79 // expected 80 } 81 } finally { 82 pingClient.close(); 83 pingServer.close(); 84 } 85 } 86 87 public void test_connectLjava_net_SocketAddressI() throws Exception { 88 // Now validate that we get a interrupted exception if we try to connect 89 // to an address on which nobody is accepting connections and the 90 // timeout expired 91 Socket theSocket = new Socket(); 92 try { 93 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 94 1), 200); 95 fail("No interrupted exception when connecting to address nobody listening on with short timeout 200"); 96 } catch (ConnectException e) { 97 // Expected 98 } 99 theSocket.close(); 100 } 101 102 public void test_getOutputStream() throws Exception { 103 // Regression test for HARMONY-2934 104 // Port 0 is not allowed to be used in connect() on some platforms, 105 // get a free port here 106 ServerSocket ss = new ServerSocket(0); 107 int port = ss.getLocalPort(); 108 ss.close(); 109 110 Socket socket = new Socket("127.0.0.1", port, false); 111 OutputStream o = socket.getOutputStream(); 112 try { 113 o.write(1); 114 } catch (SocketException e) { 115 // expected 116 } finally { 117 socket.close(); 118 } 119 } 120} 121