Path: blob/master/test/jdk/sun/net/www/ParseUtil_4922813.java
41152 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* @modules java.base/sun.net.www27@key randomness28*/2930import java.util.BitSet;31import java.io.File;32import java.util.Random;33import sun.net.www.ParseUtil;3435public class ParseUtil_4922813 {36public static void main(String[] argv) throws Exception {3738int num = 400;39while (num-- >= 0) {40String source = getTestSource();41String ec = sun.net.www.ParseUtil.encodePath(source);42String v117 = ParseUtil_V117.encodePath(source);43if (!ec.equals(v117)) {44throw new RuntimeException("Test Failed for : \n"45+ " source =<"46+ getUnicodeString(source)47+ ">");48}4950}51}5253static int maxCharCount = 200;54static int maxCodePoint = 0x10ffff;55static Random random;56static String getTestSource() {57if (random == null) {58long seed = System.currentTimeMillis();59random = new Random(seed);60}61String source = "";62int i = 0;63int count = random.nextInt(maxCharCount) + 1;64while (i < count) {65int codepoint = random.nextInt(127);66source = source + String.valueOf((char)codepoint);6768codepoint = random.nextInt(0x7ff);69source = source + String.valueOf((char)codepoint);7071codepoint = random.nextInt(maxCodePoint);72source = source + new String(Character.toChars(codepoint));7374i += 3;75}76return source;77}7879static String getUnicodeString(String s){80String unicodeString = "";81for(int j=0; j< s.length(); j++){82unicodeString += "0x"+ Integer.toString(s.charAt(j), 16);83}84return unicodeString;85}86}87class ParseUtil_V117 {88static BitSet encodedInPath;89static {90encodedInPath = new BitSet(256);9192// Set the bits corresponding to characters that are encoded in the93// path component of a URI.9495// These characters are reserved in the path segment as described in96// RFC2396 section 3.3.97encodedInPath.set('=');98encodedInPath.set(';');99encodedInPath.set('?');100encodedInPath.set('/');101102// These characters are defined as excluded in RFC2396 section 2.4.3103// and must be escaped if they occur in the data part of a URI.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(']');117encodedInPath.set('`');118119// US ASCII control characters 00-1F and 7F.120for (int i=0; i<32; i++)121encodedInPath.set(i);122encodedInPath.set(127);123}124/**125* Constructs an encoded version of the specified path string suitable126* for use in the construction of a URL.127*128* A path separator is replaced by a forward slash. The string is UTF8129* encoded. The % escape sequence is used for characters that are above130* 0x7F or those defined in RFC2396 as reserved or excluded in the path131* component of a URL.132*/133public static String encodePath(String path) {134StringBuffer sb = new StringBuffer();135int n = path.length();136for (int i=0; i<n; i++) {137char c = path.charAt(i);138if (c == File.separatorChar)139sb.append('/');140else {141if (c <= 0x007F) {142if (encodedInPath.get(c))143escape(sb, c);144else145sb.append(c);146} else if (c > 0x07FF) {147escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));148escape(sb, (char)(0x80 | ((c >> 6) & 0x3F)));149escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));150} else {151escape(sb, (char)(0xC0 | ((c >> 6) & 0x1F)));152escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));153}154}155}156return sb.toString();157}158159/**160* Appends the URL escape sequence for the specified char to the161* specified StringBuffer.162*/163private static void escape(StringBuffer s, char c) {164s.append('%');165s.append(Character.forDigit((c >> 4) & 0xF, 16));166s.append(Character.forDigit(c & 0xF, 16));167}168}169170171