test/jdk/sun/net/www/ParseUtil_4922813.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 30046
diff changeset
    25
 * @bug 4922813
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 30046
diff changeset
    26
 * @summary Check the new impl of encodePath will not cause regression
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 30046
diff changeset
    27
 * @modules java.base/sun.net.www
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    28
   @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ParseUtil_4922813 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        int num = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        while (num-- >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            String source = getTestSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            String ec = sun.net.www.ParseUtil.encodePath(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            String v117 = ParseUtil_V117.encodePath(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            if (!ec.equals(v117)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                throw new RuntimeException("Test Failed for : \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                           + "   source  =<"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                           + getUnicodeString(source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                           + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int maxCharCount = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static int maxCodePoint = 0x10ffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static Random random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static String getTestSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            long seed = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            random = new Random(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        String source = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        int count = random.nextInt(maxCharCount) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        while (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            int codepoint = random.nextInt(127);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            source = source + String.valueOf((char)codepoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            codepoint = random.nextInt(0x7ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            source = source + String.valueOf((char)codepoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            codepoint = random.nextInt(maxCodePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            source = source + new String(Character.toChars(codepoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            i += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static String getUnicodeString(String s){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String unicodeString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for(int j=0; j< s.length(); j++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
             unicodeString += "0x"+ Integer.toString(s.charAt(j), 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return unicodeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
class ParseUtil_V117 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static BitSet encodedInPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        encodedInPath = new BitSet(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Set the bits corresponding to characters that are encoded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // path component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // These characters are reserved in the path segment as described in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // RFC2396 section 3.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        encodedInPath.set('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        encodedInPath.set(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        encodedInPath.set('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        encodedInPath.set('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // These characters are defined as excluded in RFC2396 section 2.4.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // and must be escaped if they occur in the data part of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        encodedInPath.set('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        encodedInPath.set(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        encodedInPath.set('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        encodedInPath.set('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        encodedInPath.set('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        encodedInPath.set('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        encodedInPath.set('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        encodedInPath.set('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        encodedInPath.set('|');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        encodedInPath.set('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        encodedInPath.set('^');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        encodedInPath.set('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        encodedInPath.set(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        encodedInPath.set('`');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // US ASCII control characters 00-1F and 7F.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        for (int i=0; i<32; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            encodedInPath.set(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        encodedInPath.set(127);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Constructs an encoded version of the specified path string suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * for use in the construction of a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A path separator is replaced by a forward slash. The string is UTF8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * encoded. The % escape sequence is used for characters that are above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * 0x7F or those defined in RFC2396 as reserved or excluded in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * component of a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static String encodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int n = path.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i=0; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            char c = path.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (c == File.separatorChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                sb.append('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (c <= 0x007F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    if (encodedInPath.get(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        escape(sb, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                } else if (c > 0x07FF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    escape(sb, (char)(0x80 | ((c >>  6) & 0x3F)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    escape(sb, (char)(0x80 | ((c >>  0) & 0x3F)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    escape(sb, (char)(0xC0 | ((c >>  6) & 0x1F)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    escape(sb, (char)(0x80 | ((c >>  0) & 0x3F)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Appends the URL escape sequence for the specified char to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * specified StringBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static void escape(StringBuffer s, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        s.append('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        s.append(Character.forDigit((c >> 4) & 0xF, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        s.append(Character.forDigit(c & 0xF, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}