1 /*
   2  * Copyright (c) 2019, 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 /*
  25  * @test
  26  * @bug 8224477
  27  * @summary Basic test for java.net.SocketImpl implSpec
  28  * @run testng TestImplSpec
  29  */
  30 
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.net.InetAddress;
  35 import java.net.SocketAddress;
  36 import java.net.SocketImpl;
  37 import java.net.SocketOption;
  38 import java.util.Set;
  39 import org.testng.annotations.Test;
  40 import static java.lang.Boolean.*;
  41 import static java.net.StandardSocketOptions.*;
  42 import static org.testng.Assert.assertEquals;
  43 import static org.testng.Assert.expectThrows;
  44 
  45 public class TestImplSpec {
  46 
  47     static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
  48 
  49     @Test
  50     public void socketImpl() {
  51         DelegatingSocketImpl dsi = new DelegatingSocketImpl();
  52 
  53         assertEquals(dsi.supportedOptions().size(), 0);
  54 
  55         expectThrows(UOE, () -> dsi.setOption(null, null));
  56         expectThrows(UOE, () -> dsi.setOption(null, 1));
  57         expectThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100));
  58         expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
  59         expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE));
  60         expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE));
  61         expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE));
  62         expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
  63 
  64         expectThrows(UOE, () -> dsi.getOption(null));
  65         expectThrows(UOE, () -> dsi.getOption(SO_RCVBUF));
  66         expectThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE));
  67         expectThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT));
  68     }
  69 
  70     static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {
  71         @Override public String name() { return "FAKE_SOCK_OPT"; }
  72         @Override public Class<Boolean> type() { return Boolean.class; }
  73     };
  74 
  75     // A SocketImpl that delegates the three new-style socket option
  76     // methods to the default java.net.SocketImpl implementation.
  77     static class DelegatingSocketImpl extends SocketImpl {
  78 
  79         @Override
  80         public <T> void setOption(SocketOption<T> name, T value) throws IOException {
  81             super.setOption(name, value);
  82         }
  83 
  84         @Override
  85         public Set<SocketOption<?>> supportedOptions() {
  86             return super.supportedOptions();
  87         }
  88 
  89         @Override
  90         public <T> T getOption(SocketOption<T> name) throws IOException {
  91             return super.getOption(name);
  92         }
  93 
  94         // --
  95 
  96         @Override protected void create(boolean stream) { }
  97         @Override protected void connect(String host, int port) { }
  98         @Override protected void connect(InetAddress address, int port) { }
  99         @Override protected void connect(SocketAddress address, int timeout)  { }
 100         @Override protected void bind(InetAddress host, int port) { }
 101         @Override protected void listen(int backlog) { }
 102         @Override protected void accept(SocketImpl s)  { }
 103         @Override protected InputStream getInputStream() { return null; }
 104         @Override protected OutputStream getOutputStream() { return null; }
 105         @Override protected int available() { return 0; }
 106         @Override protected void close() { }
 107         @Override protected void sendUrgentData(int data)  { }
 108         @Override public void setOption(int optID, Object value) { }
 109         @Override public Object getOption(int optID) { return null; }
 110     }
 111 }