aboutsummaryrefslogtreecommitdiff
path: root/generator/src/test/java/com/google/archivepatcher/generator/DefaultDeflateCompressionDivinerTest.java
blob: c712a2bec6fbc2b537e4ba5a698cba0dbdd41893 (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
// 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 com.google.archivepatcher.generator.DefaultDeflateCompressionDiviner.DivinationResult;
import com.google.archivepatcher.shared.ByteArrayInputStreamFactory;
import com.google.archivepatcher.shared.DefaultDeflateCompatibilityWindow;
import com.google.archivepatcher.shared.DeflateCompressor;
import com.google.archivepatcher.shared.JreDeflateParameters;
import com.google.archivepatcher.shared.UnitTestZipArchive;
import com.google.archivepatcher.shared.UnitTestZipEntry;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
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 DefaultDeflateCompressionDiviner}.
 */
@RunWith(JUnit4.class)
@SuppressWarnings("javadoc")
public class DefaultDeflateCompressionDivinerTest {
  /**
   * The object under test.
   */
  private DefaultDeflateCompressionDiviner diviner = null;

  /**
   * Test delivery written to the file.
   */
  private byte[] testData = null;

  @Before
  public void setup() {
    testData = new DefaultDeflateCompatibilityWindow().getCorpus();
    diviner = new DefaultDeflateCompressionDiviner();
  }

  /**
   * Deflates the test delivery using the specified parameters, storing them in a temp file and
   * returns the temp file created.
   * @param parameters the parameters to use for deflating
   * @return the temp file with the delivery
   */
  private byte[] deflate(JreDeflateParameters parameters) throws IOException {
    DeflateCompressor compressor = new DeflateCompressor();
    compressor.setNowrap(parameters.nowrap);
    compressor.setStrategy(parameters.strategy);
    compressor.setCompressionLevel(parameters.level);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    compressor.compress(new ByteArrayInputStream(testData), buffer);
    return buffer.toByteArray();
  }

  @Test
  public void testDivineDeflateParameters_JunkData() throws IOException {
    final byte[] junk = new byte[] {0, 1, 2, 3, 4};
    Assert.assertNull(diviner.divineDeflateParameters(new ByteArrayInputStreamFactory(junk)));
  }

  @Test
  public void testDivineDeflateParameters_AllValidSettings() throws IOException {
    for (boolean nowrap : new boolean[] {true, false}) {
      for (int strategy : new int[] {0, 1, 2}) {
        for (int level : new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}) {
          JreDeflateParameters trueParameters = JreDeflateParameters.of(level, strategy, nowrap);
          final byte[] buffer = deflate(trueParameters);
          JreDeflateParameters divinedParameters =
              diviner.divineDeflateParameters(new ByteArrayInputStreamFactory(buffer));
          Assert.assertNotNull(divinedParameters);
          // TODO(andrewhayden) make *CERTAIN 100%( that strategy doesn't matter for level < 4.
          if (strategy == 1 && level <= 3) {
            // Strategy 1 produces identical output at levels 1, 2 and 3.
            Assert.assertEquals(
                /*expected=*/ JreDeflateParameters.of(level, 0, nowrap),
                /*actual=*/ divinedParameters);
          } else if (strategy == 2) {
            // All levels are the same with strategy 2.
            // TODO: Assert only one test gets done for this, should be the first level always.
            Assert.assertEquals(nowrap, divinedParameters.nowrap);
            Assert.assertEquals(strategy, divinedParameters.strategy);
          } else {
            Assert.assertEquals(trueParameters, divinedParameters);
          }
        } // End of iteration on level
      } // End of iteration on strategy
    } // End of iteration on nowrap
  }

  @Test
  public void testDivineDeflateParameters_File() throws IOException {
    File tempFile = File.createTempFile("ddcdt", "tmp");
    tempFile.deleteOnExit();
    try {
      UnitTestZipArchive.saveTestZip(tempFile);
      List<DivinationResult> results = diviner.divineDeflateParameters(tempFile);
      Assert.assertEquals(UnitTestZipArchive.allEntriesInFileOrder.size(), results.size());
      for (int x = 0; x < results.size(); x++) {
        UnitTestZipEntry expected = UnitTestZipArchive.allEntriesInFileOrder.get(x);
        DivinationResult actual = results.get(x);
        Assert.assertEquals(expected.path, actual.minimalZipEntry.getFileName());
        int expectedLevel = expected.level;
        if (expectedLevel > 0) {
          // Compressed entry
          Assert.assertTrue(actual.minimalZipEntry.isDeflateCompressed());
          Assert.assertNotNull(actual.divinedParameters);
          Assert.assertEquals(expectedLevel, actual.divinedParameters.level);
          Assert.assertEquals(0, actual.divinedParameters.strategy);
          Assert.assertTrue(actual.divinedParameters.nowrap);
        } else {
          // Uncompressed entry
          Assert.assertFalse(actual.minimalZipEntry.isDeflateCompressed());
          Assert.assertNull(actual.divinedParameters);
        }
      }
    } finally {
      try {
        tempFile.delete();
      } catch (Exception ignoreD) {
        // Nothing
      }
    }
  }
}