aboutsummaryrefslogtreecommitdiff
path: root/sharedtest/src/main/java/com/google/archivepatcher/shared/UnitTestZipArchive.java
blob: 228a3f310faf37bbe1ae7a5b88ec7c625ac79c95 (plain)
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
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.archivepatcher.shared;

import org.junit.Assert;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * A testing construct that provides a well-known archive and metadata about it. The archive
 * contains four files, one each at compression levels 0 (stored), 1 (fastest), 6 (default), and 9
 * (maximum compression). Two of the files have comments in the central directory, two do not. Each
 * has unique content with a distinct CRC32. The archive has had its dates normalized, so the date
 * and time will be the beginning of the epoch. The goal is to provide a reasonably robust test for
 * the logic in MinimalZipParser, but other unit test code also uses this functionality to construct
 * contrived data for testing. Exotic stuff like extra data padded at the beginning or in-between
 * entries, zip64 support and so on are not present; the goal is not to exhaustively test compliance
 * with the zip spec, but rather to ensure that the code works with most common zip files that are
 * likely to be encountered in the real world.
 */
public class UnitTestZipArchive {
  /**
   * The data for the first entry in the zip file, compressed at level 1. Has no comment.
   */
  public static final UnitTestZipEntry entry1 =
      makeUnitTestZipEntry(
          "file1", // path / filename
          1, // compression level
          "This is the content of file 1, at level 1. No comment.",
          null); // comment

  /**
   * The data for the second entry in the zip file, compressed at level 6. Has no comment.
   */
  public static final UnitTestZipEntry entry2 =
      makeUnitTestZipEntry(
          "file2", // path / filename
          6, // compression level
          "Here is some content for file 2, at level 6. No comment.",
          null); // comment

  /**
   * The data for the third entry in the zip file, compressed at level 9. Has a comment.
   */
  public static final UnitTestZipEntry entry3 =
      makeUnitTestZipEntry(
          "file3", // path / filename
          9, // compression level
          "And some other content for file 3, at level 9. With comment.",
          "COMMENT3"); // comment

  /**
   * The data for the fourth entry in the zip file, stored (uncompressed / level 0). Has a comment.
   */
  public static final UnitTestZipEntry entry4 =
      makeUnitTestZipEntry(
          "file4", // path / filename
          0, // compression level
          "File 4 data here, this is stored uncompressed. With comment.",
          "COMMENT4"); // comment

  /**
   * Invokes {@link #makeUnitTestZipEntry(String, int, boolean, String, String)} with nowrap=true.
   * @param path the file path
   * @param level the level the entry is compressed with
   * @param contentPrefix the content prefix - the corpus body will be appended to this value to
   * produce the final content for the entry
   * @param comment the comment to add to the file in the central directory, if any
   * @return the newly created entry
   */
  public static final UnitTestZipEntry makeUnitTestZipEntry(
      String path, int level, String contentPrefix, String comment) {
    return makeUnitTestZipEntry(path, level, true, contentPrefix, comment);
  }

  /**
   * Makes a unit test entry using the specified parameters <em>plus</em> the corpus from
   * {@link DefaultDeflateCompatibilityWindow#getCorpus()} to provide enough data for an accurate
   * level identification.
   * @param path the file path
   * @param level the level the entry is compressed with
   * @param nowrap the value for the nowrap flag
   * @param contentPrefix the content prefix - the corpus body will be appended to this value to
   * produce the final content for the entry
   * @param comment the comment to add to the file in the central directory, if any
   * @return the newly created entry
   */
  public static final UnitTestZipEntry makeUnitTestZipEntry(
      String path, int level, boolean nowrap, String contentPrefix, String comment) {
    String corpusText;
    try {
      corpusText = new String(new DefaultDeflateCompatibilityWindow().getCorpus(), "US-ASCII");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("System doesn't support US-ASCII", e);
    }
    return new UnitTestZipEntry(path, level, nowrap, contentPrefix + corpusText, comment);
  }

  /**
   * All of the entries in the zip file, in the order in which their local entries appear in the
   * file.
   */
  public static final List<UnitTestZipEntry> allEntriesInFileOrder =
      Collections.unmodifiableList(
          Arrays.asList(new UnitTestZipEntry[] {entry1, entry2, entry3, entry4}));

  // At class load time, ensure that it is safe to use this class for other tests.
  static {
    try {
      verifyTestZip(makeTestZip());
    } catch (Exception e) {
      throw new RuntimeException("Core sanity test 1 has failed, unit tests are unreliable", e);
    }
  }

  /**
   * Make a test ZIP file in memory and return it as a byte array. The ZIP contains the entries
   * described by {@link #entry1}, {@link #entry2}, {@link #entry3}, and {@link #entry4}. In
   * general, unit tests should use this data for all testing.
   * @return the zip file described above, as a byte array
   */
  public static byte[] makeTestZip() {
    return makeTestZip(allEntriesInFileOrder);
  }

  /**
   * Make an arbitrary zip archive in memory using the specified entries.
   * @param entriesInFileOrder the entries
   * @return the zip file described above, as a byte array
   */
  public static byte[] makeTestZip(List<UnitTestZipEntry> entriesInFileOrder) {
    try {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      ZipOutputStream zipOut = new ZipOutputStream(buffer);
      for (UnitTestZipEntry unitTestEntry : entriesInFileOrder) {
        ZipEntry zipEntry = new ZipEntry(unitTestEntry.path);
        zipOut.setLevel(unitTestEntry.level);
        CRC32 crc32 = new CRC32();
        byte[] uncompressedContent = unitTestEntry.getUncompressedBinaryContent();
        crc32.update(uncompressedContent);
        zipEntry.setCrc(crc32.getValue());
        zipEntry.setSize(uncompressedContent.length);
        if (unitTestEntry.level == 0) {
          zipOut.setMethod(ZipOutputStream.STORED);
          zipEntry.setCompressedSize(uncompressedContent.length);
        } else {
          zipOut.setMethod(ZipOutputStream.DEFLATED);
        }
        // Normalize MSDOS date/time fields to zero for reproducibility.
        zipEntry.setTime(0);
        if (unitTestEntry.comment != null) {
          zipEntry.setComment(unitTestEntry.comment);
        }
        zipOut.putNextEntry(zipEntry);
        zipOut.write(unitTestEntry.getUncompressedBinaryContent());
        zipOut.closeEntry();
      }
      zipOut.close();
      return buffer.toByteArray();
    } catch (IOException e) {
      // Should not happen as this is all in memory
      throw new RuntimeException("Unable to generate test zip!", e);
    }
  }

  /**
   * Verifies the test zip file created by {@link #makeTestZip()} or for sanity, so that the rest of
   * the tests can safely rely upon them. The outputs may be slightly different from platform to
   * platform due to, e.g., filesystem differences that affect the choice of string encoding or
   * filesystem attributes that are preserved (eg, NTFS versus POSIX).
   * @param data the data to verify
   * @throws Exception if verification fails
   */
  private static void verifyTestZip(byte[] data) throws Exception {
    ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(data));
    for (int x = 0; x < allEntriesInFileOrder.size(); x++) {
      ZipEntry zipEntry = zipIn.getNextEntry();
      checkEntry(zipEntry, zipIn);
      zipIn.closeEntry();
    }
    Assert.assertNull(zipIn.getNextEntry());
    zipIn.close();
  }

  /**
   * Save the test archive to a file.
   * @param file the file to write to
   * @throws IOException if unable to write the file
   */
  public static void saveTestZip(File file) throws IOException {
    FileOutputStream out = new FileOutputStream(file);
    out.write(makeTestZip());
    out.flush();
    out.close();
  }

  /**
   * Check that the specified entry is one of the test entries and that its content matches the
   * expected content. If this is the entry that is uncompressed, also asserts that it is in fact
   * uncompressed.
   * @param entry the entry to check
   * @param zipIn the input stream to read from
   * @throws IOException if anything goes wrong
   */
  private static void checkEntry(ZipEntry entry, ZipInputStream zipIn) throws IOException {
    // NB: File comments cannot be verified because the comments are in the central directory, which
    // is later in the stream.
    for (UnitTestZipEntry testEntry : allEntriesInFileOrder) {
      if (testEntry.path.equals(entry.getName())) {
        if (testEntry.level == 0) {
          // This entry should be uncompressed. So the "compressed" size should be the same as the
          // uncompressed size.
          Assert.assertEquals(0, entry.getMethod());
          Assert.assertEquals(
              testEntry.getUncompressedBinaryContent().length, entry.getCompressedSize());
        }
        ByteArrayOutputStream uncompressedData = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int numRead = 0;
        while ((numRead = zipIn.read(buffer)) >= 0) {
          uncompressedData.write(buffer, 0, numRead);
        }
        Assert.assertArrayEquals(
            testEntry.getUncompressedBinaryContent(), uncompressedData.toByteArray());
        return;
      }
    }
    Assert.fail("entry unknown: " + entry.getName());
  }
}