aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/ZipFileDataTest.java
blob: 4caa47e768eeb6788296a4cef48d8041069c267a (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
// Copyright 2015 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.devtools.build.zip;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.testing.NullPointerTester;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.zip.ZipException;

@RunWith(JUnit4.class)
public class ZipFileDataTest {
  @Rule public ExpectedException thrown = ExpectedException.none();

  private ZipFileData data;

  @Before public void setup() {
    data = new ZipFileData(UTF_8);
  }

  @Test public void testNulls() {
    NullPointerTester tester = new NullPointerTester();
    tester.testAllPublicConstructors(ZipFileData.class);
    tester.testAllPublicInstanceMethods(data);
  }

  @Test public void testCharset() {
    assertThat(new ZipFileData(UTF_8).getCharset()).isEqualTo(UTF_8);
  }

  @Test public void testComment() throws ZipException {
    data.setComment("foo");
    assertThat(data.getComment()).isEqualTo("foo");
  }

  @Test public void testSetComment_TooLong() throws ZipException {
    String comment = new String(new byte[0x100ff], UTF_8);
    thrown.expect(ZipException.class);
    thrown.expectMessage("File comment too long. Is 65791; max 65535.");
    data.setComment(comment);
  }

  @Test public void testSetComment_FromBytes() throws ZipException {
    String comment = "foo";
    byte[] bytes = comment.getBytes(UTF_8);
    data.setComment(bytes);
    assertThat(data.getComment()).isEqualTo(comment);
  }

  @Test public void testSetComment_FromBytes_TooLong() throws ZipException {
    byte[] comment = new byte[0x100ff];
    thrown.expect(ZipException.class);
    thrown.expectMessage("File comment too long. Is 65791; max 65535.");
    data.setComment(comment);
  }

  @Test public void testZip64Setting() {
    assertThat(data.isZip64()).isFalse();
    data.setCentralDirectorySize(0xffffffffL);
    assertThat(data.isZip64()).isFalse();
    data.setCentralDirectorySize(0x100000000L);
    assertThat(data.isZip64()).isTrue();

    data.setZip64(false);
    assertThat(data.isZip64()).isFalse();
    data.setCentralDirectoryOffset(0xffffffffL);
    assertThat(data.isZip64()).isFalse();
    data.setCentralDirectoryOffset(0x100000000L);
    assertThat(data.isZip64()).isTrue();

    data.setZip64(false);
    assertThat(data.isZip64()).isFalse();
    data.setExpectedEntries(0xffff);
    assertThat(data.isZip64()).isFalse();
    data.setExpectedEntries(0x10000L);
    assertThat(data.isZip64()).isTrue();

    data.setZip64(false);
    assertThat(data.isZip64()).isFalse();
    data.setZip64EndOfCentralDirectoryOffset(0);
    assertThat(data.isZip64()).isTrue();

    data.setZip64(false);
    assertThat(data.isZip64()).isFalse();
    ZipFileEntry template = new ZipFileEntry("template");
    for (int i = 0; i < 0xffff; i++) {
      ZipFileEntry entry = new ZipFileEntry(template);
      entry.setName("entry" + i);
      data.addEntry(entry);
    }
    assertThat(data.isZip64()).isFalse();
    data.addEntry(template);
    assertThat(data.isZip64()).isTrue();
  }

  @Test public void testSetZip64SetsMaybeZip64() {
    assertThat(data.isMaybeZip64()).isFalse();
    data.setZip64(true);
    assertThat(data.isMaybeZip64()).isTrue();
  }
}