1 /*
  2  * Copyright (c) 2020, 2024, 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, actual " + s1 +
 76                                                " but expected value from property user.name is " + s2);
 77                 }
 78 
 79                 // Try setting the option: Read only
 80 
 81                 try {
 82                     c.setOption(SO_PEERCRED, p);
 83                     throw new RuntimeException("should have thrown SocketException");
 84                 } catch (SocketException e) {}
 85             }
 86         } finally {
 87             if (addr != null)
 88                 Files.deleteIfExists(addr.getPath());
 89         }
 90 
 91         // Try getting from unconnected socket
 92 
 93         try (var c = SocketChannel.open(StandardProtocolFamily.UNIX)) {
 94             try {
 95                 p = c.getOption(SO_PEERCRED);
 96                 System.out.println(p.user());
 97                 throw new RuntimeException("should have thrown SocketException");
 98             } catch (SocketException e) {}
 99         }
100 
101         // Try getting from ServerSocketChannel
102 
103         try (var server = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
104             try {
105                 p = server.getOption(SO_PEERCRED);
106                 System.out.println(p.user());
107                 throw new RuntimeException("should have thrown USE");
108             } catch (UnsupportedOperationException e) {}
109         }
110     }
111 
112     static boolean supported() {
113         try {
114             SocketChannel.open(StandardProtocolFamily.UNIX).close();
115         } catch (UnsupportedOperationException e) {
116             return false;
117         } catch (Exception e) {
118             return true; // continue test to see what problem is
119         }
120         return true;
121     }
122 
123     @SuppressWarnings("unchecked")
124     public static void test(NetworkChannel chan) throws IOException {
125         System.out.println("Checking: " + chan.getClass());
126         Set<SocketOption<?>> supported = chan.supportedOptions();
127         for (SocketOption<?> option : supported) {
128             String name = option.name();
129             System.out.println("Checking option " + name);
130             if (option.type() == Boolean.class) {
131                 chan.setOption((SocketOption<Boolean>)option, true);
132                 chan.setOption((SocketOption<Boolean>)option, false);
133                 chan.getOption(option);
134             } else if (option.type() == Integer.class) {
135                 chan.setOption((SocketOption<Integer>)option, 10);
136                 chan.getOption(option);
137             }
138         }
139     }
140 }