1 /* 2 * Copyright (c) 2015, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.nashorn.internal.runtime.linker.test; 26 27 import static org.testng.Assert.assertEquals; 28 29 import jdk.nashorn.internal.runtime.linker.NameCodec; 30 import org.testng.annotations.Test; 31 32 /** 33 * Test for jdk.nashorn.intenal.runtime.linker.NameCodec.java. This test is 34 * derived from BytecodeNameTest.java from (older) mlvm code @ 35 * http://hg.openjdk.java.net/mlvm/mlvm/file/tip/netbeans/meth/test/sun/invoke/util/BytecodeNameTest.java 36 * 37 * @bug 8141285: NameCode should pass tests from BytecodeNameTest.java 38 */ 39 public class NameCodecTest { 40 41 static String[][] SAMPLES = { 42 // mangled, source 43 {"foo", "foo"}, 44 {"ba\\r", "ba\\r"}, 45 {"\\=ba\\-%z", "ba\\%z"}, 46 {"\\=ba\\--z", "ba\\-z"}, 47 {"=\\=", "=\\="}, 48 {"\\==\\|\\=", "=/\\="}, 49 {"\\|\\=", "/\\="}, 50 {"\\=ba\\!", "ba:"}, 51 {"\\|", "/"}, 52 {"\\", "\\"}, 53 {"\\\\%", "\\$"}, 54 {"\\\\", "\\\\"}, 55 {"\\=", ""} 56 57 }; 58 59 static final String DANGEROUS_CHARS = "\\/.;:$[]<>"; 60 static final String REPLACEMENT_CHARS = "-|,?!%{}^_"; 61 62 static String[][] canonicalSamples() { 63 int ndc = DANGEROUS_CHARS.length(); 64 String[][] res = new String[2 * ndc][]; 65 for (int i = 0; i < ndc; i++) { 66 char dc = DANGEROUS_CHARS.charAt(i); 67 char rc = REPLACEMENT_CHARS.charAt(i); 68 if (dc == '\\') { 69 res[2 * i + 0] = new String[]{"\\-%", "\\%"}; 70 } else { 71 res[2 * i + 0] = new String[]{"\\" + rc, "" + dc}; 72 } 73 res[2 * i + 1] = new String[]{"" + rc, "" + rc}; 74 } 75 return res; 76 } 77 78 @Test 79 public void testEncode() { 80 System.out.println("testEncode"); 81 testEncode(SAMPLES); 82 testEncode(canonicalSamples()); 83 } 84 85 private void testEncode(String[][] samples) { 86 for (String[] sample : samples) { 87 String s = sample[1]; 88 String expResult = sample[0]; 89 String result = NameCodec.encode(s); 90 if (!result.equals(expResult)) { 91 System.out.println(s + " => " + result + " != " + expResult); 92 } 93 assertEquals(expResult, result); 94 } 95 } 96 97 @Test 98 public void testDecode() { 99 System.out.println("testDecode"); 100 testDecode(SAMPLES); 101 testDecode(canonicalSamples()); 102 } 103 104 private void testDecode(String[][] samples) { 105 for (String[] sample : samples) { 106 String s = sample[0]; 107 String expResult = sample[1]; 108 String result = NameCodec.decode(s); 109 assertEquals(expResult, result); 110 } 111 } 112 }