1/*
2 * Copyright (c) 2016, 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
24import jdk.test.lib.process.ProcessTools;
25
26import java.io.File;
27import java.io.FilePermission;
28import java.nio.file.Files;
29import java.nio.file.Paths;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.stream.Collectors;
33import java.util.stream.IntStream;
34
35/**
36 * @test
37 * @bug 8170364
38 * @summary FilePermission path modified during merge
39 * @library /test/lib
40 */
41
42public class MergeName {
43
44    public static final String[] ALL_ACTIONS
45            = {"read", "write", "execute", "delete"};
46
47    public static void main(String[] args) throws Exception {
48        if (args.length == 0) {
49            test("p1", "read", "write", "delete", "execute");
50            test("p2", "read,write", "delete,execute");
51            test("p3", "read,write,delete", "execute");
52            test("p4", "read,write,delete,execute");
53        } else {
54            SecurityManager sm = System.getSecurityManager();
55            for (String arg : args) {
56                // Use bits to create powerset of ALL_ACTIONS
57                IntStream.range(1, 16)
58                        .mapToObj(n -> IntStream.range(0, 4)
59                                .filter(x -> (n & (1 << x)) != 0)
60                                .mapToObj(x -> ALL_ACTIONS[x])
61                                .collect(Collectors.joining(",")))
62                        .forEach(a -> sm.checkPermission(
63                                new FilePermission(arg, a)));
64            }
65        }
66    }
67
68    private static void test(String file, String... actions) throws Exception {
69        List<String> content = new ArrayList<>();
70        content.add("grant {");
71        for (String action : actions) {
72            content.add("   permission java.io.FilePermission " +
73                    "\"x\", \"" +action + "\";");
74        }
75        content.add("};");
76        Files.write(Paths.get(file), content);
77        ProcessTools.executeTestJvm("-Djava.security.manager",
78                "-Djava.security.policy=" + file,
79                "MergeName",
80                "x",
81                new File(System.getProperty("user.dir"), "x").getPath())
82            .shouldHaveExitValue(0);
83    }
84}
85