Path: blob/master/test/jdk/java/net/URLEncoder/SurrogatePairs.java
41152 views
/*1* Copyright (c) 2001, 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/*24* @test25* @bug 439670826* @summary Test URL encoder and decoder on a string that contains27* surrogate pairs.28*29*/3031import java.io.*;32import java.net.*;3334/*35* Surrogate pairs are two character Unicode sequences where the first36* character lies in the range [d800, dbff] and the second character lies37* in the range [dc00, dfff]. They are used as an escaping mechanism to add38* 1M more characters to Unicode.39*/40public class SurrogatePairs {4142static String[] testStrings = {"\uD800\uDC00",43"\uD800\uDFFF",44"\uDBFF\uDC00",45"\uDBFF\uDFFF",46"1\uDBFF\uDC00",47"@\uDBFF\uDC00",48"\uDBFF\uDC001",49"\uDBFF\uDC00@",50"\u0101\uDBFF\uDC00",51"\uDBFF\uDC00\u0101"52};5354static String[] correctEncodings = {"%F0%90%80%80",55"%F0%90%8F%BF",56"%F4%8F%B0%80",57"%F4%8F%BF%BF",58"1%F4%8F%B0%80",59"%40%F4%8F%B0%80",60"%F4%8F%B0%801",61"%F4%8F%B0%80%40",62"%C4%81%F4%8F%B0%80",63"%F4%8F%B0%80%C4%81"64};6566public static void main(String[] args) throws Exception {6768for (int i=0; i < testStrings.length; i++) {69test(testStrings[i], correctEncodings[i]);70}71}7273private static void test(String str, String correctEncoding)74throws Exception {7576System.out.println("Unicode bytes of test string are: "77+ getHexBytes(str));7879String encoded = URLEncoder.encode(str, "UTF-8");8081System.out.println("URLEncoding is: " + encoded);8283if (encoded.equals(correctEncoding))84System.out.println("The encoding is correct!");85else {86throw new Exception("The encoding is incorrect!" +87" It should be " + correctEncoding);88}8990String decoded = URLDecoder.decode(encoded, "UTF-8");9192System.out.println("Unicode bytes for URLDecoding are: "93+ getHexBytes(decoded));9495if (str.equals(decoded))96System.out.println("The decoding is correct");97else {98throw new Exception("The decoded is not equal to the original");99}100System.out.println("---");101}102103private static String getHexBytes(String s) throws Exception {104StringBuffer sb = new StringBuffer();105for (int i = 0; i < s.length(); i++) {106107int a = s.charAt(i);108int b1 = (a >>8) & 0xff;109int b2 = (byte)a;110int b11 = (b1>>4) & 0x0f;111int b12 = b1 & 0x0f;112int b21 = (b2 >>4) & 0x0f;113int b22 = b2 & 0x0f;114115sb.append(Integer.toHexString(b11));116sb.append(Integer.toHexString(b12));117sb.append(Integer.toHexString(b21));118sb.append(Integer.toHexString(b22));119sb.append(' ');120}121return sb.toString();122}123124}125126127