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 package test.java.net.UnixDomainSocketAddress; 24 25 /* 26 * @test 27 * @summary Test UnixDomainSocketAddress constructor 28 * @library /test/lib 29 * @run testng/othervm LengthTest 30 */ 31 32 import org.testng.annotations.DataProvider; 33 import org.testng.annotations.Test; 34 35 import static java.lang.System.out; 36 import static java.net.StandardProtocolFamily.UNIX; 37 38 import java.net.UnixDomainSocketAddress; 39 import java.io.IOException; 40 import java.nio.channels.SocketChannel; 41 import java.nio.file.Path; 42 import org.testng.annotations.Test; 43 import org.testng.Assert; 44 45 public class LengthTest { 46 final int namelen = 100; // length close to max 47 48 @DataProvider(name = "strings") strings()49 public Object[][] strings() { 50 if (namelen == -1) 51 return new Object[][] {new String[]{""}}; 52 53 return new Object[][]{ 54 {""}, 55 {new String(new char[100]).replaceAll("\0", "x")}, 56 {new String(new char[namelen]).replaceAll("\0", "x")}, 57 {new String(new char[namelen-1]).replaceAll("\0", "x")}, 58 }; 59 } 60 61 @Test(dataProvider = "strings") expectPass(String s)62 public void expectPass(String s) { 63 var addr = UnixDomainSocketAddress.of(s); 64 Assert.assertTrue(addr.getPath().toString().equals(s), "getPathName.equals(s)"); 65 var p = Path.of(s); 66 addr = UnixDomainSocketAddress.of(p); 67 Assert.assertTrue(addr.getPath().equals(p), "getPath.equals(p)"); 68 } 69 70 @Test expectNPE()71 public void expectNPE() { 72 try { 73 String s = null; 74 UnixDomainSocketAddress.of(s); 75 throw new RuntimeException("Expected NPE"); 76 } catch (NullPointerException npe) { 77 out.println("\tCaught expected exception: " + npe); 78 } 79 try { 80 Path p = null; 81 UnixDomainSocketAddress.of(p); 82 throw new RuntimeException("Expected NPE"); 83 } catch (NullPointerException npe) { 84 out.println("\tCaught expected exception: " + npe); 85 } 86 } 87 }