1 /*
  2  * Copyright (c) 2020, 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 8245194
 27  * @run main/othervm SocketOptions
 28  */
 29 
 30 import java.io.IOException;
 31 import java.net.*;
 32 import java.nio.ByteBuffer;
 33 import java.nio.channels.*;
 34 import java.nio.file.Files;
 35 import java.nio.file.Path;
 36 import java.util.Set;
 37 import jdk.net.UnixDomainPrincipal;
 38 import static jdk.net.ExtendedSocketOptions.SO_PEERCRED;
 39 
 40 /**
 41  * Check that all supported options can actually be set and got
 42  */
 43 public class SocketOptions {
 44 
 45     public static void main(String args[]) throws Exception {
 46         if (!supported()) {
 47             System.out.println("Unix domain channels not supported");
 48             return;
 49         }
 50         test(ServerSocketChannel.open(StandardProtocolFamily.UNIX));
 51         test(SocketChannel.open(StandardProtocolFamily.UNIX));
 52         testPeerCred();
 53     }
 54 
 55     static void testPeerCred() throws Exception {
 56         UnixDomainSocketAddress addr = null;
 57         UnixDomainPrincipal p;
 58         try (ServerSocketChannel s = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
 59             s.bind(null);
 60             addr = (UnixDomainSocketAddress)s.getLocalAddress();
 61             try (SocketChannel c = SocketChannel.open(addr)) {
 62                 if (!c.supportedOptions().contains(SO_PEERCRED)) {
 63                     return;
 64                 }
 65                 Files.deleteIfExists(addr.getPath());
 66                 p = c.getOption(SO_PEERCRED);
 67                 String s1 = p.user().getName();
 68                 System.out.println(s1);
 69                 System.out.println(p.group().getName());
 70                 String s2 = System.getProperty("user.name");
 71 
 72                 // Check returned user name
 73 
 74                 if (!s1.equals(s2)) {
 75                     throw new RuntimeException("wrong username");
 76                 }
 77 
 78                 // Try setting the option: Read only
 79 
 80                 try {
 81                     c.setOption(SO_PEERCRED, p);
 82                     throw new RuntimeException("should have thrown SocketException");
 83                 } catch (SocketException e) {}
 84             }
 85         } finally {
 86             if (addr != null)
 87                 Files.deleteIfExists(addr.getPath());
 88         }
 89 
 90         // Try getting from unconnected socket
 91 
 92         try (var c = SocketChannel.open(StandardProtocolFamily.UNIX)) {
 93             try {
 94                 p = c.getOption(SO_PEERCRED);
 95                 System.out.println(p.user());
 96                 throw new RuntimeException("should have thrown SocketException");
 97             } catch (SocketException e) {}
 98         }
 99 
100         // Try getting from ServerSocketChannel
101 
102         try (var server = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
103             try {
104                 p = server.getOption(SO_PEERCRED);
105                 System.out.println(p.user());
106                 throw new RuntimeException("should have thrown USE");
107             } catch (UnsupportedOperationException e) {}
108         }
109     }
110 
111     static boolean supported() {
112         try {
113             SocketChannel.open(StandardProtocolFamily.UNIX).close();
114         } catch (UnsupportedOperationException e) {
115             return false;
116         } catch (Exception e) {
117             return true; // continue test to see what problem is
118         }
119         return true;
120     }
121 
122     @SuppressWarnings("unchecked")
123     public static void test(NetworkChannel chan) throws IOException {
124         System.out.println("Checking: " + chan.getClass());
125         Set<SocketOption<?>> supported = chan.supportedOptions();
126         for (SocketOption<?> option : supported) {
127             String name = option.name();
128             System.out.println("Checking option " + name);
129             if (option.type() == Boolean.class) {
130                 chan.setOption((SocketOption<Boolean>)option, true);
131                 chan.setOption((SocketOption<Boolean>)option, false);
132                 chan.getOption(option);
133             } else if (option.type() == Integer.class) {
134                 chan.setOption((SocketOption<Integer>)option, 10);
135                 chan.getOption(option);
136             }
137         }
138     }
139 }