1 /*
   2  * Copyright (c) 2007, 2017, 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 4640544 8044773
  26  * @summary Unit test to check SocketChannel setOption/getOption/options
  27  *          methods.
  28  * @requires !vm.graal.enabled
  29  * @run main SocketOptionTests
  30  * @run main/othervm --limit-modules=java.base SocketOptionTests
  31  */
  32 
  33 import java.nio.*;
  34 import java.nio.channels.*;
  35 import java.net.*;
  36 import java.io.IOException;
  37 import java.util.*;
  38 import static java.net.StandardSocketOptions.*;
  39 
  40 public class SocketOptionTests {
  41 
  42     static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)
  43         throws IOException
  44     {
  45         Object value = sc.getOption(name);
  46         if (!value.equals(expectedValue))
  47             throw new RuntimeException("value not as expected");
  48     }
  49 
  50     public static void main(String[] args) throws IOException {
  51         SocketChannel sc = SocketChannel.open();
  52 
  53         // check supported options
  54         Set<SocketOption<?>> options = sc.supportedOptions();
  55         List<? extends SocketOption> expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF,
  56             SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, TCP_NODELAY);
  57         for (SocketOption opt: expected) {
  58             if (!options.contains(opt))
  59                 throw new RuntimeException(opt.name() + " should be supported");
  60         }
  61 
  62         // check specified defaults
  63         int linger = sc.<Integer>getOption(SO_LINGER);
  64         if (linger >= 0)
  65             throw new RuntimeException("initial value of SO_LINGER should be < 0");
  66         checkOption(sc, SO_KEEPALIVE, false);
  67         checkOption(sc, TCP_NODELAY, false);
  68 
  69         // allowed to change when not bound
  70         sc.setOption(SO_KEEPALIVE, true);
  71         checkOption(sc, SO_KEEPALIVE, true);
  72         sc.setOption(SO_KEEPALIVE, false);
  73         checkOption(sc, SO_KEEPALIVE, false);
  74         sc.setOption(SO_SNDBUF, 128*1024);      // can't check
  75         sc.setOption(SO_RCVBUF, 256*1024);      // can't check
  76         int before, after;
  77         before = sc.getOption(SO_SNDBUF);
  78         after = sc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF);
  79         if (after < before)
  80             throw new RuntimeException("setOption caused SO_SNDBUF to decrease");
  81         before = sc.getOption(SO_RCVBUF);
  82         after = sc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
  83         if (after < before)
  84             throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
  85         sc.setOption(SO_REUSEADDR, true);
  86         checkOption(sc, SO_REUSEADDR, true);
  87         sc.setOption(SO_REUSEADDR, false);
  88         checkOption(sc, SO_REUSEADDR, false);
  89         sc.setOption(SO_LINGER, 10);
  90         linger = sc.<Integer>getOption(SO_LINGER);
  91         if (linger < 1)
  92             throw new RuntimeException("expected linger to be enabled");
  93         sc.setOption(SO_LINGER, -1);
  94         linger = sc.<Integer>getOption(SO_LINGER);
  95         if (linger >= 0)
  96             throw new RuntimeException("expected linger to be disabled");
  97         sc.setOption(TCP_NODELAY, true);
  98         checkOption(sc, TCP_NODELAY, true);
  99         sc.setOption(TCP_NODELAY, false);       // can't check
 100 
 101         // bind socket
 102         sc.bind(new InetSocketAddress(0));
 103 
 104         // allow to change when bound
 105         sc.setOption(SO_KEEPALIVE, true);
 106         checkOption(sc, SO_KEEPALIVE, true);
 107         sc.setOption(SO_KEEPALIVE, false);
 108         checkOption(sc, SO_KEEPALIVE, false);
 109 
 110         sc.setOption(SO_LINGER, 10);
 111         linger = sc.<Integer>getOption(SO_LINGER);
 112         if (linger < 1)
 113             throw new RuntimeException("expected linger to be enabled");
 114         sc.setOption(SO_LINGER, -1);
 115         linger = sc.<Integer>getOption(SO_LINGER);
 116         if (linger >= 0)
 117             throw new RuntimeException("expected linger to be disabled");
 118         sc.setOption(TCP_NODELAY, true);        // can't check
 119         sc.setOption(TCP_NODELAY, false);       // can't check
 120 
 121         // NullPointerException
 122         try {
 123             sc.setOption(null, "value");
 124             throw new RuntimeException("NullPointerException not thrown");
 125         } catch (NullPointerException x) {
 126         }
 127         try {
 128             sc.getOption(null);
 129             throw new RuntimeException("NullPointerException not thrown");
 130         } catch (NullPointerException x) {
 131         }
 132 
 133         // ClosedChannelException
 134         sc.close();
 135         try {
 136             sc.setOption(TCP_NODELAY, true);
 137             throw new RuntimeException("ClosedChannelException not thrown");
 138         } catch (ClosedChannelException x) {
 139         }
 140     }
 141 }