Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/ParseUtil_4922813.java
38841 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 492281325@summary Check the new impl of encodePath will not cause regression26@key randomness27*/2829import java.util.BitSet;30import java.io.File;31import java.util.Random;32import sun.net.www.ParseUtil;3334public class ParseUtil_4922813 {35public static void main(String[] argv) throws Exception {3637int num = 400;38while (num-- >= 0) {39String source = getTestSource();40String ec = sun.net.www.ParseUtil.encodePath(source);41String v117 = ParseUtil_V117.encodePath(source);42if (!ec.equals(v117)) {43throw new RuntimeException("Test Failed for : \n"44+ " source =<"45+ getUnicodeString(source)46+ ">");47}4849}50}5152static int maxCharCount = 200;53static int maxCodePoint = 0x10ffff;54static Random random;55static String getTestSource() {56if (random == null) {57long seed = System.currentTimeMillis();58random = new Random(seed);59}60String source = "";61int i = 0;62int count = random.nextInt(maxCharCount) + 1;63while (i < count) {64int codepoint = random.nextInt(127);65source = source + String.valueOf((char)codepoint);6667codepoint = random.nextInt(0x7ff);68source = source + String.valueOf((char)codepoint);6970codepoint = random.nextInt(maxCodePoint);71source = source + new String(Character.toChars(codepoint));7273i += 3;74}75return source;76}7778static String getUnicodeString(String s){79String unicodeString = "";80for(int j=0; j< s.length(); j++){81unicodeString += "0x"+ Integer.toString(s.charAt(j), 16);82}83return unicodeString;84}85}86class ParseUtil_V117 {87static BitSet encodedInPath;88static {89encodedInPath = new BitSet(256);9091// Set the bits corresponding to characters that are encoded in the92// path component of a URI.9394// These characters are reserved in the path segment as described in95// RFC2396 section 3.3.96encodedInPath.set('=');97encodedInPath.set(';');98encodedInPath.set('?');99encodedInPath.set('/');100101// These characters are defined as excluded in RFC2396 section 2.4.3102// and must be escaped if they occur in the data part of a URI.103encodedInPath.set('#');104encodedInPath.set(' ');105encodedInPath.set('<');106encodedInPath.set('>');107encodedInPath.set('%');108encodedInPath.set('"');109encodedInPath.set('{');110encodedInPath.set('}');111encodedInPath.set('|');112encodedInPath.set('\\');113encodedInPath.set('^');114encodedInPath.set('[');115encodedInPath.set(']');116encodedInPath.set('`');117118// US ASCII control characters 00-1F and 7F.119for (int i=0; i<32; i++)120encodedInPath.set(i);121encodedInPath.set(127);122}123/**124* Constructs an encoded version of the specified path string suitable125* for use in the construction of a URL.126*127* A path separator is replaced by a forward slash. The string is UTF8128* encoded. The % escape sequence is used for characters that are above129* 0x7F or those defined in RFC2396 as reserved or excluded in the path130* component of a URL.131*/132public static String encodePath(String path) {133StringBuffer sb = new StringBuffer();134int n = path.length();135for (int i=0; i<n; i++) {136char c = path.charAt(i);137if (c == File.separatorChar)138sb.append('/');139else {140if (c <= 0x007F) {141if (encodedInPath.get(c))142escape(sb, c);143else144sb.append(c);145} else if (c > 0x07FF) {146escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));147escape(sb, (char)(0x80 | ((c >> 6) & 0x3F)));148escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));149} else {150escape(sb, (char)(0xC0 | ((c >> 6) & 0x1F)));151escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));152}153}154}155return sb.toString();156}157158/**159* Appends the URL escape sequence for the specified char to the160* specified StringBuffer.161*/162private static void escape(StringBuffer s, char c) {163s.append('%');164s.append(Character.forDigit((c >> 4) & 0xF, 16));165s.append(Character.forDigit(c & 0xF, 16));166}167}168169170