1/* 2 * Copyright 2012 Google, Inc. 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 8056934 27 * @summary Check ability to read zip files created by python zipfile 28 * implementation, which fails to write optional (but recommended) data 29 * descriptor signatures. Repro scenario is a Java -> Python -> Java round trip: 30 * - ZipOutputStream creates zip file with DEFLATED entries and data 31 * descriptors with optional signature "PK0x0708". 32 * - Python reads those entries, preserving the 0x08 flag byte 33 * - Python outputs those entries with data descriptors lacking the 34 * optional signature. 35 * - ZipInputStream cannot handle the missing signature 36 * 37 * No way to adapt the technique in this test to get a ZIP64 zip file 38 * without data descriptors was found. 39 * 40 * @ignore This test has brittle dependencies on an external working python. 41 */ 42 43import java.io.*; 44import java.util.zip.*; 45 46public class DataDescriptorSignatureMissing { 47 void printStream(InputStream is) throws IOException { 48 Reader r = new InputStreamReader(is); 49 StringBuilder sb = new StringBuilder(); 50 char[] buf = new char[1024]; 51 int n; 52 while ((n = r.read(buf)) > 0) { 53 sb.append(buf, 0, n); 54 } 55 System.out.print(sb); 56 } 57 58 int entryCount(File zipFile) throws IOException { 59 try (FileInputStream fis = new FileInputStream(zipFile); 60 ZipInputStream zis = new ZipInputStream(fis)) { 61 for (int count = 0;; count++) 62 if (zis.getNextEntry() == null) 63 return count; 64 } 65 } 66 67 void test(String[] args) throws Throwable { 68 if (! new File("/usr/bin/python").canExecute()) 69 return; 70 71 // Create a java zip file with DEFLATED entries and data 72 // descriptors with signatures. 73 final File in = new File("in.zip"); 74 final File out = new File("out.zip"); 75 final int count = 3; 76 try (FileOutputStream fos = new FileOutputStream(in); 77 ZipOutputStream zos = new ZipOutputStream(fos)) { 78 for (int i = 0; i < count; i++) { 79 ZipEntry ze = new ZipEntry("hello.python" + i); 80 ze.setMethod(ZipEntry.DEFLATED); 81 zos.putNextEntry(ze); 82 zos.write(new byte[10]); 83 zos.closeEntry(); 84 } 85 } 86 87 // Copy the zip file using python's zipfile module 88 String[] python_program_lines = { 89 "import os", 90 "import zipfile", 91 "input_zip = zipfile.ZipFile('in.zip', mode='r')", 92 "output_zip = zipfile.ZipFile('out.zip', mode='w')", 93 "count08 = 0", 94 "for input_info in input_zip.infolist():", 95 " output_info = input_info", 96 " if output_info.flag_bits & 0x08 == 0x08:", 97 " count08 += 1", 98 " output_zip.writestr(output_info, input_zip.read(input_info))", 99 "output_zip.close()", 100 "if count08 == 0:", 101 " raise ValueError('Expected to see entries with 0x08 flag_bits set')", 102 }; 103 StringBuilder python_program_builder = new StringBuilder(); 104 for (String line : python_program_lines) 105 python_program_builder.append(line).append('\n'); 106 String python_program = python_program_builder.toString(); 107 String[] cmdline = { "/usr/bin/python", "-c", python_program }; 108 ProcessBuilder pb = new ProcessBuilder(cmdline); 109 pb.redirectErrorStream(true); 110 Process p = pb.start(); 111 printStream(p.getInputStream()); 112 p.waitFor(); 113 equal(p.exitValue(), 0); 114 115 File pythonZipFile = new File("out.zip"); 116 check(pythonZipFile.exists()); 117 118 equal(entryCount(in), 119 entryCount(out)); 120 121 // We expect out to be identical to in, except for the removal of 122 // the optional data descriptor signatures. 123 final int SIG_LENGTH = 4; // length of a zip signature - PKxx 124 equal(in.length(), 125 out.length() + SIG_LENGTH * count); 126 127 in.delete(); 128 out.delete(); 129 } 130 131 //--------------------- Infrastructure --------------------------- 132 volatile int passed = 0, failed = 0; 133 void pass() {passed++;} 134 void fail() {failed++; Thread.dumpStack();} 135 void fail(String msg) {System.err.println(msg); fail();} 136 void unexpected(Throwable t) {failed++; t.printStackTrace();} 137 void check(boolean cond) {if (cond) pass(); else fail();} 138 void equal(Object x, Object y) { 139 if (x == null ? y == null : x.equals(y)) pass(); 140 else fail(x + " not equal to " + y);} 141 public static void main(String[] args) throws Throwable { 142 new DataDescriptorSignatureMissing().instanceMain(args);} 143 public void instanceMain(String[] args) throws Throwable { 144 try {test(args);} catch (Throwable t) {unexpected(t);} 145 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 146 if (failed > 0) throw new AssertionError("Some tests failed");} 147} 148