1/*
2 * Copyright (c) 2006, 2013, 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 6374379
27 * @library ../../../../lib/testlibrary
28 * @summary Verify that we can read zip file names > 255 chars long
29 */
30
31import java.io.*;
32import java.util.jar.*;
33import java.util.zip.*;
34import java.util.Stack;
35import jdk.testlibrary.FileUtils;
36
37public class ReadLongZipFileName {
38    private static String entryName = "testFile.txt";;
39
40    public static void realMain(String args[]) {
41        String longDirName = "abcdefghijklmnopqrstuvwx"; // 24 chars.
42        String jarFileName = "areallylargejarfilename.jar";    // 27 chars.
43        File file = null;
44        File myJarFile = null;
45        int currentFileLength = 0;
46        int minRequiredLength = 600; // long enough to definitely fail.
47        Stack<File> directories = new Stack<File>();
48
49        String filename = "." + File.separator;
50        try {
51            // Create a directory structure long enough that the filename will
52            // put us over the minRequiredLength.
53            do {
54                filename = filename + longDirName + File.separator;
55                file = new File(filename);
56                file.mkdir();
57                currentFileLength = file.getCanonicalPath().length();
58                directories.push(file);
59            } while (currentFileLength < (minRequiredLength - jarFileName.length()));
60
61            // Create a new Jar file: use jar instead of zip to make sure long
62            // names work for both zip and jar subclass.
63            filename = filename + jarFileName;
64            JarOutputStream out = new JarOutputStream(
65                new BufferedOutputStream(
66                    new FileOutputStream(filename.toString())));
67            out.putNextEntry(new JarEntry(entryName));
68            out.write(1);
69            out.close();
70            myJarFile = new File(filename.toString());
71            currentFileLength = myJarFile.getCanonicalPath().length();
72            if (!myJarFile.exists()) {
73                fail("Jar file does not exist.");
74            }
75        } catch (IOException e) {
76            unexpected(e, "Problem creating the Jar file.");
77        }
78
79        try {
80            JarFile readJarFile = new JarFile(myJarFile);
81            JarEntry je = readJarFile.getJarEntry(entryName);
82            check(je != null);
83            DataInputStream dis = new DataInputStream(
84                readJarFile.getInputStream(je));
85            byte val = dis.readByte();
86            check(val == 1);
87            try {
88                dis.readByte();
89                fail("Read past expected EOF");
90            } catch (IOException e) {
91                pass();
92            }
93            readJarFile.close();
94            pass("Opened Jar file for reading with a name " + currentFileLength
95                 + " characters long");
96        } catch (IOException e) {
97            unexpected(e, "Test failed - problem reading the Jar file back in.");
98        }
99
100        if (myJarFile != null) {
101            check(myJarFile.delete());
102        }
103
104        while (! directories.empty()) {
105            File f = directories.pop();
106            try {
107                FileUtils.deleteFileWithRetry(f.toPath());
108            } catch (IOException e) {
109                unexpected(e, "Fail to clean up directory, " + f);
110                break;
111            }
112        }
113    }
114
115    //--------------------- Infrastructure ---------------------------
116    static volatile int passed = 0, failed = 0;
117    static void pass() {passed++;}
118    static void pass(String msg) {System.out.println(msg); passed++;}
119    static void fail() {failed++; Thread.dumpStack();}
120    static void fail(String msg) {System.out.println(msg); fail();}
121    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
122    static void unexpected(Throwable t, String msg) {
123        System.out.println(msg); failed++; t.printStackTrace();}
124    static void check(boolean cond) {if (cond) pass(); else fail();}
125    static void equal(Object x, Object y) {
126        if (x == null ? y == null : x.equals(y)) pass();
127        else fail(x + " not equal to " + y);}
128    public static void main(String[] args) throws Throwable {
129        try {realMain(args);} catch (Throwable t) {unexpected(t);}
130        System.out.println("\nPassed = " + passed + " failed = " + failed);
131        if (failed > 0) throw new AssertionError("Some tests failed");}
132}
133