1 /* 2 * Copyright (c) 2001, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4396708 27 * @summary Test URL encoder and decoder on a string that contains 28 * surrogate pairs. 29 * 30 */ 31 package test.java.net.URLEncoder; 32 33 import java.io.*; 34 import java.net.*; 35 import org.testng.annotations.Test; 36 import org.testng.Assert; 37 38 /* 39 * Surrogate pairs are two character Unicode sequences where the first 40 * character lies in the range [d800, dbff] and the second character lies 41 * in the range [dc00, dfff]. They are used as an escaping mechanism to add 42 * 1M more characters to Unicode. 43 */ 44 public class SurrogatePairs { 45 46 static String[] testStrings = {"\uD800\uDC00", 47 "\uD800\uDFFF", 48 "\uDBFF\uDC00", 49 "\uDBFF\uDFFF", 50 "1\uDBFF\uDC00", 51 "@\uDBFF\uDC00", 52 "\uDBFF\uDC001", 53 "\uDBFF\uDC00@", 54 "\u0101\uDBFF\uDC00", 55 "\uDBFF\uDC00\u0101" 56 }; 57 58 static String[] correctEncodings = {"%F0%90%80%80", 59 "%F0%90%8F%BF", 60 "%F4%8F%B0%80", 61 "%F4%8F%BF%BF", 62 "1%F4%8F%B0%80", 63 "%40%F4%8F%B0%80", 64 "%F4%8F%B0%801", 65 "%F4%8F%B0%80%40", 66 "%C4%81%F4%8F%B0%80", 67 "%F4%8F%B0%80%C4%81" 68 }; 69 70 @Test testSurrogatePairs()71 public void testSurrogatePairs() throws Exception { 72 for (int i=0; i < testStrings.length; i++) { 73 test(testStrings[i], correctEncodings[i]); 74 } 75 } 76 test(String str, String correctEncoding)77 private static void test(String str, String correctEncoding) 78 throws Exception { 79 String encoded = URLEncoder.encode(str, "UTF-8"); 80 81 Assert.assertEquals(encoded, correctEncoding); 82 83 String decoded = URLDecoder.decode(encoded, "UTF-8"); 84 85 Assert.assertEquals(str, decoded); 86 } 87 88 }