File: ExtractFilesTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (265 lines) | stat: -rw-r--r-- 9,124 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8335912
 * @summary test extract jar files overwrite existing files behavior
 * @library /test/lib
 * @modules jdk.jartool
 * @build jdk.test.lib.Platform
 *        jdk.test.lib.util.FileUtils
 * @run junit/othervm ExtractFilesTest
 */

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;

import jdk.test.lib.util.FileUtils;

 @TestInstance(Lifecycle.PER_CLASS)
 public class ExtractFilesTest {
    private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
        .orElseThrow(() ->
            new RuntimeException("jar tool not found")
        );

    private final String nl = System.lineSeparator();
    private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    private final PrintStream out = new PrintStream(baos);

    @BeforeAll
    public void setupJar() throws IOException {
        mkdir("test1 test2");
        echo("testfile1", "test1/testfile1");
        echo("testfile2", "test2/testfile2");
        jar("cf test.jar -C test1 . -C test2 .");
        rm("test1 test2");
    }

    @AfterAll
    public void cleanup() {
        rm("test.jar");
    }

    /**
     * Regular clean extract with expected output.
     */
    @Test
    public void testExtract() throws IOException {
        jar("xvf test.jar");
        println();
        String output = "  created: META-INF/" + nl +
                " inflated: META-INF/MANIFEST.MF" + nl +
                " inflated: testfile1" + nl +
                " inflated: testfile2" + nl;
        rm("META-INF testfile1 testfile2");
        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
    }

    /**
     * Extract should overwrite existing file as default behavior.
     */
    @Test
    public void testOverwrite() throws IOException {
        touch("testfile1");
        jar("xvf test.jar");
        println();
        String output = "  created: META-INF/" + nl +
                " inflated: META-INF/MANIFEST.MF" + nl +
                " inflated: testfile1" + nl +
                " inflated: testfile2" + nl;
        Assertions.assertEquals("testfile1", cat("testfile1"));
        rm("META-INF testfile1 testfile2");
        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
    }

    /**
     * Extract with legacy style option `k` should preserve existing files.
     */
    @Test
    public void testKeptOldFile() throws IOException {
        touch("testfile1");
        jar("xkvf test.jar");
        println();
        String output = "  created: META-INF/" + nl +
                " inflated: META-INF/MANIFEST.MF" + nl +
                "  skipped: testfile1 exists" + nl +
                " inflated: testfile2" + nl;
        Assertions.assertEquals("", cat("testfile1"));
        Assertions.assertEquals("testfile2", cat("testfile2"));
        rm("META-INF testfile1 testfile2");
        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
    }

    /**
     * Extract with gnu style -k should preserve existing files.
     */
    @Test
    public void testGnuOptionsKeptOldFile() throws IOException {
        touch("testfile1 testfile2");
        jar("-x -k -v -f test.jar");
        println();
        String output = "  created: META-INF/" + nl +
                " inflated: META-INF/MANIFEST.MF" + nl +
                "  skipped: testfile1 exists" + nl +
                "  skipped: testfile2 exists" + nl;
        Assertions.assertEquals("", cat("testfile1"));
        Assertions.assertEquals("", cat("testfile2"));
        rm("META-INF testfile1 testfile2");
        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
    }

    /**
     * Extract with gnu style long option --keep-old-files should preserve existing files.
     */
    @Test
    public void testGnuLongOptionsKeptOldFile() throws IOException {
        touch("testfile2");
        jar("-x --keep-old-files -v -f test.jar");
        println();
        String output = "  created: META-INF/" + nl +
                " inflated: META-INF/MANIFEST.MF" + nl +
                " inflated: testfile1" + nl +
                "  skipped: testfile2 exists" + nl;
        Assertions.assertEquals("testfile1", cat("testfile1"));
        Assertions.assertEquals("", cat("testfile2"));
        rm("META-INF testfile1 testfile2");
        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
    }

    /**
     * Test jar will issue warning when use keep option in non-extraction mode.
     */
    @Test
    public void testWarningOnInvalidKeepOption() throws IOException {
        var err = jar("tkf test.jar");
        println();

        String output = "META-INF/" + nl +
                "META-INF/MANIFEST.MF" + nl +
                "testfile1" + nl +
                "testfile2" + nl;

        Assertions.assertArrayEquals(baos.toByteArray(), output.getBytes());
        Assertions.assertEquals("Warning: The --keep-old-files/-k/k option is not valid with current usage, will be ignored." + nl, err);
    }

    private Stream<Path> mkpath(String... args) {
        return Arrays.stream(args).map(d -> Path.of(".", d.split("/")));
    }

    private void mkdir(String cmdline) {
        System.out.println("mkdir -p " + cmdline);
        mkpath(cmdline.split(" +")).forEach(p -> {
            try {
                Files.createDirectories(p);
            } catch (IOException x) {
                throw new UncheckedIOException(x);
            }
        });
    }

    private void touch(String cmdline) {
        System.out.println("touch " + cmdline);
        mkpath(cmdline.split(" +")).forEach(p -> {
            try {
                Files.createFile(p);
            } catch (IOException x) {
                throw new UncheckedIOException(x);
            }
        });
    }

    private void echo(String text, String path) {
        System.out.println("echo '" + text + "' > " + path);
        try {
            var p = Path.of(".", path.split("/"));
            Files.writeString(p, text);
        } catch (IOException x) {
            throw new UncheckedIOException(x);
        }
    }

    private String cat(String path) {
        System.out.println("cat " + path);
        try {
            return Files.readString(Path.of(path));
        } catch (IOException x) {
            throw new UncheckedIOException(x);
        }
    }

    private void rm(String cmdline) {
        System.out.println("rm -rf " + cmdline);
        mkpath(cmdline.split(" +")).forEach(p -> {
            try {
                if (Files.isDirectory(p)) {
                    FileUtils.deleteFileTreeWithRetry(p);
                } else {
                    FileUtils.deleteFileIfExistsWithRetry(p);
                }
            } catch (IOException x) {
                throw new UncheckedIOException(x);
            }
        });
    }

    private String jar(String cmdline) throws IOException {
        System.out.println("jar " + cmdline);
        baos.reset();

        // the run method catches IOExceptions, we need to expose them
        ByteArrayOutputStream baes = new ByteArrayOutputStream();
        PrintStream err = new PrintStream(baes);
        PrintStream saveErr = System.err;
        System.setErr(err);
        try {
            int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));
            if (rc != 0) {
                throw new IOException(baes.toString());
            }
        } finally {
            System.setErr(saveErr);
        }
        return baes.toString();
    }

    private void println() throws IOException {
        System.out.println(new String(baos.toByteArray()));
    }
}