< prev index next >

test/jdk/java/nio/channels/unixdomain/SocketOptions.java

Print this page

  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  */

 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());

  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  */

 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());
< prev index next >