test/jdk/tools/jar/UpdateJar.java
author lancea
Tue, 29 Oct 2019 14:22:18 -0400
changeset 58845 e492513d3630
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file Reviewed-by: clanger, bpb, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     1
/*
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     4
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     8
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    13
 * accompanied this code).
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    14
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    18
 *
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    21
 * questions.
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    22
 */
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    23
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    24
/**
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    25
 * @test
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    26
 * @bug 7175845
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    27
 * @modules jdk.jartool
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    28
 * @summary jar -uf should not change file permission
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    29
 */
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    30
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    31
import java.io.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    32
import java.nio.file.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    33
import java.nio.file.attribute.*;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    34
import java.util.Set;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    35
import java.util.spi.ToolProvider;
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    36
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    37
public class UpdateJar {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    38
    private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    39
        .orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    40
            new RuntimeException("jar tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    41
        );
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    42
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    43
    private static void cleanup(String... fnames) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    44
        for (String fname : fnames) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    45
            Files.deleteIfExists(Paths.get(fname));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    46
        }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    47
    }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    48
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    49
    public static void realMain(String[] args) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    50
        if (!System.getProperty("os.name").startsWith("Windows")) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    51
            String jar = "testUpdateJar.jar";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    52
            String e0  = "testUpdateJar_entry0.txt";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    53
            String e1  = "testUpdateJar_entry1.txt";
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    54
            cleanup(jar, e0, e1);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    55
            try {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    56
                try (FileOutputStream fos0 = new FileOutputStream(e0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    57
                     FileOutputStream fos1 = new FileOutputStream(e1)) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    58
                    fos0.write(0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    59
                    fos1.write(0);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    60
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    61
                String[] jarArgs = new String[] {"cfM0", jar, e0};
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    62
                if (JAR_TOOL.run(System.out, System.err, jarArgs) != 0) {
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    63
                    fail("Could not create jar file.");
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    64
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    65
                Set<PosixFilePermission> pm = Files.getPosixFilePermissions(Paths.get(jar));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    66
                jarArgs = new String[] {"uf", jar, e1};
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    67
                if (JAR_TOOL.run(System.out, System.err, jarArgs) != 0) {
13154
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    68
                    fail("Could not create jar file.");
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    69
                }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    70
                equal(pm, Files.getPosixFilePermissions(Paths.get(jar)));
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    71
            } finally {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    72
                cleanup(jar, e0, e1);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    73
            }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    74
        }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    75
    }
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    76
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    77
    //--------------------- Infrastructure ---------------------------
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    78
    static volatile int passed = 0, failed = 0;
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    79
    static void pass() {passed++;}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    80
    static void fail() {failed++; Thread.dumpStack();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    81
    static void fail(String msg) {System.out.println(msg); fail();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    82
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    83
    static void check(boolean cond) {if (cond) pass(); else fail();}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    84
    static void equal(Object x, Object y) {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    85
        if (x == null ? y == null : x.equals(y)) pass();
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    86
        else fail(x + " not equal to " + y);}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    87
    public static void main(String[] args) throws Throwable {
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    88
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    89
        System.out.println("\nPassed = " + passed + " failed = " + failed);
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    90
        if (failed > 0) throw new AssertionError("Some tests failed");}
9e8a04d28ded 7175845: jar uf changes file permissions unexpectedly
sherman
parents:
diff changeset
    91
}