1/*
2 * Copyright (c) 2002, 2003, 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 * @author Valerie Peng
27 * @bug 4716213
28 * @bug 4797850
29 * @modules java.base/sun.security.util
30 * @summary Verify that expand(String, boolean) does not encode if
31 * the value is a valid URI with a scheme (it is already encoded),
32 * i.e. avoid double encoding.
33 */
34import java.io.File;
35import sun.security.util.*;
36
37public class ExpandAndEncode {
38
39    private static String tests[] = {
40        "${env.test1}/test.jar",
41        "file:${env.test2}/test.jar",
42        "file:/foo/${env.test3}",
43        "file:/foo/${env.test4}"
44    };
45
46    private static String answers[] = {
47        "http://my%20home/test.jar",
48        "file:/my%20home/test.jar",
49        "file:/foo/bar%23foobar",
50        "file:/foo/goofy:bar%23foobar"
51    };
52
53    private static boolean checkAnswer(String result, int i) {
54        if (result.equals(answers[i])) {
55            return true;
56        } else {
57            System.out.println("Test#" + i + ": expected " + answers[i]);
58            System.out.println("Test#" + i + ": got " + result);
59            return false;
60        }
61    }
62
63    public static void main(String[] args) throws Exception {
64
65        boolean status = true;
66
67        System.setProperty("env.test1", "http://my%20home");
68        System.setProperty("env.test2", File.separator + "my home");
69        System.setProperty("env.test3", "bar#foobar");
70        System.setProperty("env.test4", "goofy:bar#foobar");
71
72        for (int i=0; i < tests.length; i++) {
73            String result = PropertyExpander.expand(tests[i], true);
74            status &= checkAnswer(result, i);
75        }
76
77        if (status) {
78            System.out.println("PASSED");
79        } else {
80            throw new Exception("FAILED");
81        }
82    }
83}
84