aboutsummaryrefslogtreecommitdiff
path: root/generator/src/test/java/com/google/archivepatcher/generator/MinimalZipEntryTest.java
blob: d0bde74439b8cf3a72f2709b32fafbb516788e53 (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
255
256
257
258
259
260
261
262
263
264
// 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.generator;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link MinimalZipEntry}. */
@RunWith(JUnit4.class)
@SuppressWarnings("javadoc")
public class MinimalZipEntryTest {
  private static final int COMPRESSION_METHOD = 8; // (meaning deflate)
  private static final long CRC32 = 123;
  private static final long COMPRESSED_SIZE = 456;
  private static final long UNCOMPRESSED_SIZE = 789;
  private static final byte[] FILE_NAME_BYTES = new byte[] {'f', 'o', 'o', '.', 'b', 'a', 'r'};
  private static final boolean GENERAL_PURPOSE_BIT_FLAG_11 = true; // (meaning file name is UTF8)
  private static final long FILE_OFFSET_OF_LOCAL_ENTRY = 1337;
  private static final long FILE_OFFSET_OF_COMPRESSED_DATA = 2674;

  private MinimalZipEntry defaultEntry;
  private MinimalZipEntry clonedDefaultEntry;
  private MinimalZipEntry alteredCompressionMethod;
  private MinimalZipEntry alteredCrc32;
  private MinimalZipEntry alteredCompressedSize;
  private MinimalZipEntry alteredUncompressedSize;
  private MinimalZipEntry alteredFileNameBytes;
  private MinimalZipEntry alteredGeneralPurposeBitFlag11;
  private MinimalZipEntry alteredOffsetOfLocalEntry;
  private MinimalZipEntry alteredFileOffsetOfCompressedData;
  private List<MinimalZipEntry> allMutations;

  @Before
  public void setup() throws Exception {
    defaultEntry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    clonedDefaultEntry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredCompressionMethod =
        new MinimalZipEntry(
            COMPRESSION_METHOD - 1,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredCrc32 =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32 - 1,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredCompressedSize =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE - 1,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredUncompressedSize =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE - 1,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredFileNameBytes =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            new byte[] {'x'},
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredGeneralPurposeBitFlag11 =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            !GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredOffsetOfLocalEntry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY - 1);
    alteredFileOffsetOfCompressedData =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    alteredFileOffsetOfCompressedData.setFileOffsetOfCompressedData(FILE_OFFSET_OF_COMPRESSED_DATA);
    allMutations =
        Collections.unmodifiableList(
            Arrays.asList(
                alteredCompressionMethod,
                alteredCrc32,
                alteredCompressedSize,
                alteredUncompressedSize,
                alteredFileNameBytes,
                alteredGeneralPurposeBitFlag11,
                alteredOffsetOfLocalEntry,
                alteredFileOffsetOfCompressedData));
  }

  @Test
  public void testGetFileName() throws Exception {
    // Make a string with some chars that are from DOS ANSI art days, these chars have different
    // binary representations in UTF8 and Cp437. We use light, medium, and dark "shade" characters
    // (0x2591, 0x2592, 0x2593 respectively) for this purpose. Go go ANSI art!
    // https://en.wikipedia.org/wiki/Code_page_437
    // https://en.wikipedia.org/wiki/Block_Elements
    String fileName = new String("\u2591\u2592\u2593AWESOME\u2593\u2592\u2591");
    byte[] utf8Bytes = fileName.getBytes("UTF8");
    byte[] cp437Bytes = fileName.getBytes("Cp437");
    Assert.assertFalse(Arrays.equals(utf8Bytes, cp437Bytes)); // For test sanity

    MinimalZipEntry utf8Entry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            utf8Bytes,
            true /* utf8 */,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    Assert.assertArrayEquals(utf8Bytes, utf8Entry.getFileNameBytes());
    String fileNameFromUtf8Bytes = utf8Entry.getFileName();
    Assert.assertEquals(fileName, fileNameFromUtf8Bytes);

    MinimalZipEntry cp437Entry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            cp437Bytes,
            false /* cp437 */,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    Assert.assertArrayEquals(cp437Bytes, cp437Entry.getFileNameBytes());
    String fileNameFromCp437Bytes = cp437Entry.getFileName();
    Assert.assertEquals(fileName, fileNameFromCp437Bytes);
  }

  @Test
  public void testIsDeflateCompressed() {
    // Compression method == 8, and uncompressed size != compressed size
    Assert.assertTrue(defaultEntry.isDeflateCompressed());
    // Compression method == 8, but uncompressed size == compressed size (ie, STOR'ed entry)
    MinimalZipEntry stored =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            1000,
            1000,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    Assert.assertFalse(stored.isDeflateCompressed());
    // Compression method != 8 (obviously not deflate)
    Assert.assertFalse(alteredCompressionMethod.isDeflateCompressed());
  }

  @Test
  @SuppressWarnings("EqualsIncompatibleType") // For ErrorProne
  public void testEquals() {
    Assert.assertEquals(defaultEntry, defaultEntry);
    MinimalZipEntry clonedDefaultEntry =
        new MinimalZipEntry(
            COMPRESSION_METHOD,
            CRC32,
            COMPRESSED_SIZE,
            UNCOMPRESSED_SIZE,
            FILE_NAME_BYTES,
            GENERAL_PURPOSE_BIT_FLAG_11,
            FILE_OFFSET_OF_LOCAL_ENTRY);
    Assert.assertEquals(defaultEntry, clonedDefaultEntry);
    for (MinimalZipEntry mutation : allMutations) {
      Assert.assertNotEquals(defaultEntry, mutation);
    }
    Assert.assertFalse(defaultEntry.equals(null));
    Assert.assertFalse(defaultEntry.equals("foo"));
  }

  @Test
  public void testHashCode() {
    Set<MinimalZipEntry> hashSet = new HashSet<>();
    hashSet.add(defaultEntry);
    hashSet.add(clonedDefaultEntry);
    Assert.assertEquals(1, hashSet.size());
    hashSet.addAll(allMutations);
    Assert.assertEquals(1 + allMutations.size(), hashSet.size());
  }

  @Test
  public void testGetters() {
    Assert.assertEquals(COMPRESSED_SIZE, defaultEntry.getCompressedSize());
    Assert.assertEquals(COMPRESSION_METHOD, defaultEntry.getCompressionMethod());
    Assert.assertEquals(CRC32, defaultEntry.getCrc32OfUncompressedData());
    Assert.assertArrayEquals(FILE_NAME_BYTES, defaultEntry.getFileNameBytes());
    Assert.assertEquals(FILE_OFFSET_OF_LOCAL_ENTRY, defaultEntry.getFileOffsetOfLocalEntry());
    Assert.assertEquals(GENERAL_PURPOSE_BIT_FLAG_11, defaultEntry.getGeneralPurposeFlagBit11());
    Assert.assertEquals(UNCOMPRESSED_SIZE, defaultEntry.getUncompressedSize());

    // Special one, only alteredFileOffsetOfCompressedData has this field set...
    Assert.assertEquals(-1, defaultEntry.getFileOffsetOfCompressedData());
    Assert.assertEquals(
        FILE_OFFSET_OF_COMPRESSED_DATA,
        alteredFileOffsetOfCompressedData.getFileOffsetOfCompressedData());
  }
}