1 /*
   2  * Copyright (c) 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4927640
  26  * @summary Tests the SCTP protocol implementation
  27  * @author chegar
  28  */
  29 
  30 import java.net.InetSocketAddress;
  31 import java.net.SocketAddress;
  32 import java.io.IOException;
  33 import java.util.Set;
  34 import java.util.concurrent.Callable;
  35 import java.nio.channels.AlreadyConnectedException;
  36 import java.nio.channels.ClosedChannelException;
  37 import java.nio.channels.ConnectionPendingException;
  38 import java.nio.channels.NoConnectionPendingException;
  39 import java.nio.channels.UnresolvedAddressException;
  40 import java.nio.channels.UnsupportedAddressTypeException;
  41 import com.sun.nio.sctp.SctpChannel;
  42 import com.sun.nio.sctp.SctpServerChannel;
  43 import static java.lang.System.out;
  44 import static java.lang.System.err;
  45 
  46 /**
  47  * Tests connect, finishConnect, isConnectionPending,
  48  * getRemoteAddresses and association.
  49  */
  50 public class Connect {
  51 
  52     void test(String[] args) {
  53         if (!Util.isSCTPSupported()) {
  54             out.println("SCTP protocol is not supported");
  55             out.println("Test cannot be run");
  56             return;
  57         }
  58 
  59         doTest();
  60     }
  61 
  62     void doTest() {
  63         SctpChannel channel = null;
  64         SctpServerChannel ssc = null;
  65 
  66         try {
  67             /* Create a server channel to connect to */
  68             ssc = SctpServerChannel.open().bind(null);
  69             Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
  70             if (addrs.isEmpty())
  71                 debug("addrs should not be empty");
  72             final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();
  73 
  74             channel = SctpChannel.open();
  75 
  76             /* TEST 0.5 Verify default values for new/unconnected channel */
  77             check(channel.getRemoteAddresses().isEmpty(),
  78                     "non empty set for unconnected channel");
  79             check(channel.association() == null,
  80                     "non-null association for unconnected channel");
  81             check(!channel.isConnectionPending(),
  82                     "should not have a connection pending");
  83 
  84             /* TEST 1: non-blocking connect */
  85             channel.configureBlocking(false);
  86             if (channel.connect(peerAddress) != true) {
  87                 debug("non-blocking connect did not immediately succeed");
  88                 check(channel.isConnectionPending(),
  89                         "should return true for isConnectionPending");
  90                 try {
  91                     channel.connect(peerAddress);
  92                     fail("should have thrown ConnectionPendingException");
  93                 } catch (ConnectionPendingException cpe) {
  94                     pass();
  95                 } catch (IOException ioe) {
  96                     unexpected(ioe);
  97                 }
  98                 channel.configureBlocking(true);
  99                 check(channel.finishConnect(),
 100                         "finishConnect should have returned true");
 101             }
 102 
 103             ssc.accept();
 104             ssc.close();
 105 
 106             /* TEST 1.5 Verify after connect */
 107             check(!channel.getRemoteAddresses().isEmpty(),
 108                     "empty set for connected channel");
 109             check(channel.association() != null,
 110                     "null association for connected channel");
 111             check(!channel.isConnectionPending(),
 112                     "pending connection for connected channel");
 113 
 114             /* TEST 2: Verify AlreadyConnectedException thrown */
 115             try {
 116                 channel.connect(peerAddress);
 117                 fail("should have thrown AlreadyConnectedException");
 118             } catch (AlreadyConnectedException unused) {
 119                 pass();
 120             }  catch (IOException ioe) {
 121                 unexpected(ioe);
 122             }
 123 
 124             /* TEST 2.5: Verify AlreadyConnectedException thrown */
 125             try {
 126                 channel.connect(peerAddress, 5, 5);
 127                 fail("should have thrown AlreadyConnectedException");
 128             } catch (AlreadyConnectedException unused) {
 129                 pass();
 130             }  catch (IOException ioe) {
 131                 unexpected(ioe);
 132             }
 133 
 134             /* TEST 3: UnresolvedAddressException */
 135             channel.close();
 136             channel = SctpChannel.open();
 137             InetSocketAddress unresolved =
 138                     InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
 139             try {
 140                 channel.connect(unresolved);
 141                 fail("should have thrown UnresolvedAddressException");
 142             } catch (UnresolvedAddressException unused) {
 143                 pass();
 144             }  catch (IOException ioe) {
 145                 unexpected(ioe);
 146             }
 147 
 148             /* TEST 4: UnsupportedAddressTypeException */
 149             SocketAddress unsupported = new UnsupportedSocketAddress();
 150             try {
 151                 channel.connect(unsupported);
 152                 fail("should have thrown UnsupportedAddressTypeException");
 153             } catch (UnsupportedAddressTypeException unused) {
 154                 pass();
 155             }  catch (IOException ioe) {
 156                 unexpected(ioe);
 157             }
 158 
 159             /* TEST 5: ClosedChannelException */
 160             channel.close();
 161             final SctpChannel closedChannel = channel;
 162             testCCE(new Callable<Void>() {
 163                 public Void call() throws IOException {
 164                     closedChannel.connect(peerAddress); return null; } });
 165 
 166             /* TEST 5.5 getRemoteAddresses */
 167             testCCE(new Callable<Void>() {
 168                 public Void call() throws IOException {
 169                     closedChannel.getRemoteAddresses(); return null; } });
 170             testCCE(new Callable<Void>() {
 171                 public Void call() throws IOException {
 172                     closedChannel.association(); return null; } });
 173             check(!channel.isConnectionPending(),
 174                     "pending connection for closed channel");
 175 
 176             /* Run some more finishConnect tests */
 177 
 178             /* TEST 6: NoConnectionPendingException */
 179             channel = SctpChannel.open();
 180             try {
 181                 channel.finishConnect();
 182                 fail("should have thrown NoConnectionPendingException");
 183             } catch (NoConnectionPendingException unused) {
 184                 pass();
 185             }  catch (IOException ioe) {
 186                 unexpected(ioe);
 187             }
 188 
 189             /* TEST 7: ClosedChannelException */
 190             channel.close();
 191             final SctpChannel cceChannel = channel;
 192             testCCE(new Callable<Void>() {
 193                 public Void call() throws IOException {
 194                     cceChannel.finishConnect(); return null; } });
 195 
 196             /* TEST 8: IOException: Connection refused. Exercises handleSocketError.
 197              *         Assumption: no sctp socket listening on 3456 */
 198             SocketAddress addr = new InetSocketAddress("localhost", 3456);
 199             channel = SctpChannel.open();
 200             try {
 201                 channel.connect(addr);
 202                 fail("should have thrown ConnectException: Connection refused");
 203             } catch (IOException ioe) {
 204                 pass();
 205             }
 206 
 207         } catch (IOException ioe) {
 208             unexpected(ioe);
 209         } finally {
 210             try { if (channel != null) channel.close(); }
 211             catch (IOException unused) {}
 212             try { if (ssc != null) ssc.close(); }
 213             catch (IOException unused) {}
 214         }
 215     }
 216 
 217     class UnsupportedSocketAddress extends SocketAddress { }
 218 
 219     void testCCE(Callable callable) {
 220         try {
 221             callable.call();
 222             fail("should have thrown ClosedChannelException");
 223         } catch (ClosedChannelException cce) {
 224            pass();
 225         } catch (Exception ioe) {
 226             unexpected(ioe);
 227         }
 228     }
 229 
 230         //--------------------- Infrastructure ---------------------------
 231     boolean debug = true;
 232     volatile int passed = 0, failed = 0;
 233     void pass() {passed++;}
 234     void fail() {failed++; Thread.dumpStack();}
 235     void fail(String msg) {System.err.println(msg); fail();}
 236     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 237     void check(boolean cond) {if (cond) pass(); else fail();}
 238     void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
 239     void debug(String message) {if(debug) { System.out.println(message); }  }
 240     public static void main(String[] args) throws Throwable {
 241         Class<?> k = new Object(){}.getClass().getEnclosingClass();
 242         try {k.getMethod("instanceMain",String[].class)
 243                 .invoke( k.newInstance(), (Object) args);}
 244         catch (Throwable e) {throw e.getCause();}}
 245     public void instanceMain(String[] args) throws Throwable {
 246         try {test(args);} catch (Throwable t) {unexpected(t);}
 247         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 248         if (failed > 0) throw new AssertionError("Some tests failed");}
 249 
 250 }