aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/singlejar/combiners_test.cc
blob: effdf265bed216447cc83abf4c1da4ca60030789 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2016 The Bazel Authors. 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.

#include "src/tools/singlejar/combiners.h"

#include "src/tools/singlejar/input_jar.h"
#include "src/tools/singlejar/zip_headers.h"
#include "src/tools/singlejar/zlib_interface.h"
#include "gtest/gtest.h"

static const char kTag1Contents[] = "<tag1>Contents1</tag1>";
static const char kTag2Contents[] = "<tag2>Contents2</tag2>";
static const char kCombinedXmlContents[] =
    "<toplevel>\n<tag1>Contents1</tag1><tag2>Contents2</tag2></toplevel>\n";
static const char kConcatenatedContents[] =
    "<tag1>Contents1</tag1>\n<tag2>Contents2</tag2>";
const uint8_t kPoison = 0xFA;

// A test fixture is used because test case setup is needed.
class CombinersTest : public ::testing::Test {
 protected:
  static void SetUpTestCase() {
    ASSERT_EQ(0, chdir(getenv("TEST_TMPDIR")));
    ASSERT_TRUE(CreateFile("tag1.xml", kTag1Contents));
    ASSERT_TRUE(CreateFile("tag2.xml", kTag2Contents));
    ASSERT_EQ(0, system("zip -qm combiners.zip tag1.xml tag2.xml"));
  }

  static void TearDownTestCase() { system("rm -f xmls.zip"); }

  static bool CreateFile(const char *filename, const char *contents) {
    FILE *fp = fopen(filename, "wb");
    size_t contents_size = strlen(contents);
    if (fp == nullptr || fwrite(contents, contents_size, 1, fp) != 1 ||
        fclose(fp)) {
      perror(filename);
      return false;
    }
    return true;
  }

  static void TestJava8DesugarDepsChecker_HasDefaultMethods() {
    Java8DesugarDepsChecker checker([](const std::string &) { return false; },
                                    /*verbose=*/false);
    checker.has_default_methods_["a"] = true;
    checker.extended_interfaces_["c"] = {"b", "a"};

    // Induce cycle (shouldn't happen but make sure we don't crash)
    checker.extended_interfaces_["d"] = {"e"};
    checker.extended_interfaces_["e"] = {"d", "a"};

    EXPECT_TRUE(checker.HasDefaultMethods("a"));
    EXPECT_FALSE(checker.HasDefaultMethods("b"));
    EXPECT_TRUE(checker.HasDefaultMethods("c"));  // Transitivly through a
    EXPECT_TRUE(checker.HasDefaultMethods("d"));  // Transitivly through a
    EXPECT_FALSE(checker.error_);
  }

  static void TestJava8DesugarDepsChecker_OutputEntry() {
    bool checkedA = false;
    Java8DesugarDepsChecker checker(
        [&checkedA](const std::string &binary_name) {
          checkedA = true;
          return binary_name == "a$$CC.class";
        },
        /*verbose=*/false);
    checker.has_default_methods_["a"] = true;
    checker.extended_interfaces_["b"] = {"c", "d"};
    checker.extended_interfaces_["c"] = {"e"};
    checker.needed_deps_["a$$CC.class"] = "f";
    checker.missing_interfaces_["b"] = "g";
    EXPECT_EQ(nullptr, checker.OutputEntry(/*compress=*/true));
    EXPECT_TRUE(checkedA);

    // Make sure we checked b and its extended interfaces for default methods
    EXPECT_FALSE(checker.has_default_methods_.at("b"));  // should be cached
    EXPECT_FALSE(checker.has_default_methods_.at("c"));  // should be cached
    EXPECT_FALSE(checker.has_default_methods_.at("d"));  // should be cached
    EXPECT_FALSE(checker.has_default_methods_.at("e"));  // should be cached
    EXPECT_FALSE(checker.error_);
  }

  static void TestJava8DesugarDepsChecker_NeededDepMissing() {
    Java8DesugarDepsChecker checker([](const std::string &) { return false; },
                                    /*verbose=*/false,
                                    /*fail_on_error=*/false);
    checker.needed_deps_["a$$CC.class"] = "b";
    EXPECT_EQ(nullptr, checker.OutputEntry(/*compress=*/true));
    EXPECT_TRUE(checker.error_);
  }

  static void TestJava8DesugarDepsChecker_MissedDefaultMethods() {
    Java8DesugarDepsChecker checker([](const std::string &) { return true; },
                                    /*verbose=*/false,
                                    /*fail_on_error=*/false);
    checker.has_default_methods_["b"] = true;
    checker.extended_interfaces_["a"] = {"b", "a"};
    checker.missing_interfaces_["a"] = "g";
    EXPECT_EQ(nullptr, checker.OutputEntry(/*compress=*/true));
    EXPECT_TRUE(checker.error_);
  }
};

// Test Concatenator.
TEST_F(CombinersTest, ConcatenatorSmall) {
  InputJar input_jar;
  Concatenator concatenator("concat");
  ASSERT_TRUE(input_jar.Open("combiners.zip"));
  const LH *lh;
  const CDH *cdh;
  while ((cdh = input_jar.NextEntry(&lh))) {
    if (cdh->file_name_is("tag1.xml") || cdh->file_name_is("tag2.xml")) {
      ASSERT_TRUE(concatenator.Merge(cdh, lh));
    }
  }

  // Create output, verify Local Header contents.
  LH *entry = reinterpret_cast<LH *>(concatenator.OutputEntry(true));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_DEFLATED, entry->compression_method());
  uint64_t original_size = entry->uncompressed_file_size();
  uint64_t compressed_size = entry->compressed_file_size();
  EXPECT_EQ(strlen(kConcatenatedContents), original_size);
  EXPECT_LE(compressed_size, original_size);
  EXPECT_TRUE(entry->file_name_is("concat"));
  EXPECT_EQ(0, entry->extra_fields_length());

  // Decompress and check contents.
  Inflater inflater;
  inflater.DataToInflate(entry->data(), compressed_size);
  uint8_t buffer[256];
  memset(buffer, kPoison, sizeof(buffer));
  ASSERT_EQ(Z_STREAM_END, inflater.Inflate((buffer), sizeof(buffer)));
  EXPECT_EQ(kPoison, buffer[original_size]);
  EXPECT_EQ(kConcatenatedContents,
            std::string(reinterpret_cast<char *>(buffer), original_size));
  free(reinterpret_cast<void *>(entry));

  // And if we just copy instead of compress:
  entry = reinterpret_cast<LH *>(concatenator.OutputEntry(false));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_NO_COMPRESSION, entry->compression_method());
  original_size = entry->uncompressed_file_size();
  compressed_size = entry->compressed_file_size();
  EXPECT_EQ(compressed_size, original_size);
  EXPECT_EQ(
      kConcatenatedContents,
      std::string(reinterpret_cast<char *>(entry->data()), original_size));
  EXPECT_TRUE(entry->file_name_is("concat"));
  EXPECT_EQ(0, entry->extra_fields_length());
  free(reinterpret_cast<void *>(entry));
}

// Tests that Concatenator creates huge (>4GB original/compressed sizes)
// correctly. This test is slow.
TEST_F(CombinersTest, ConcatenatorHuge) {
  Concatenator concatenator("huge");

  // Append 5,000,000,000 bytes to the concatenator.
  const int kBufSize = 1000000;
  char *buf = reinterpret_cast<char *>(malloc(kBufSize));
  memset(buf, kPoison, kBufSize);
  for (int i = 0; i < 5000; ++i) {
    concatenator.Append(buf, kBufSize);
  }
  free(buf);

  // Now hope that we have enough memory :-)
  LH *entry = reinterpret_cast<LH *>(concatenator.OutputEntry(true));
  ASSERT_NE(nullptr, entry);
  ASSERT_TRUE(entry->is());
  ASSERT_EQ(20, entry->version());
  EXPECT_EQ(Z_DEFLATED, entry->compression_method());
  uint64_t original_size = entry->uncompressed_file_size();
  uint64_t compressed_size = entry->compressed_file_size();
  ASSERT_EQ(5000000000, original_size);
  ASSERT_LE(compressed_size, original_size);
  free(reinterpret_cast<void *>(entry));
}

// Test NullCombiner.
TEST_F(CombinersTest, NullCombiner) {
  NullCombiner null_combiner;
  ASSERT_TRUE(null_combiner.Merge(nullptr, nullptr));
  ASSERT_EQ(nullptr, null_combiner.OutputEntry(true));
  ASSERT_EQ(nullptr, null_combiner.OutputEntry(false));
}

// Test XmlCombiner.
TEST_F(CombinersTest, XmlCombiner) {
  InputJar input_jar;
  XmlCombiner xml_combiner("combined.xml", "toplevel");
  XmlCombiner xml_combiner2("combined2.xml", "toplevel");
  ASSERT_TRUE(input_jar.Open("combiners.zip"));
  const LH *lh;
  const CDH *cdh;
  while ((cdh = input_jar.NextEntry(&lh))) {
    if (cdh->file_name_is("tag1.xml") || cdh->file_name_is("tag2.xml")) {
      ASSERT_TRUE(xml_combiner.Merge(cdh, lh));
      ASSERT_TRUE(xml_combiner2.Merge(cdh, lh));
    }
  }

  // Create output, verify Local Header contents.
  LH *entry = reinterpret_cast<LH *>(xml_combiner.OutputEntry(true));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_DEFLATED, entry->compression_method());
  uint64_t original_size = entry->uncompressed_file_size();
  uint64_t compressed_size = entry->compressed_file_size();
  EXPECT_EQ(strlen(kCombinedXmlContents), original_size);
  EXPECT_LE(compressed_size, original_size);
  EXPECT_TRUE(entry->file_name_is("combined.xml"));
  EXPECT_EQ(0, entry->extra_fields_length());

  // Decompress and check contents.
  Inflater inflater;
  inflater.DataToInflate(entry->data(), compressed_size);
  uint8_t buffer[256];
  memset(buffer, kPoison, sizeof(buffer));
  ASSERT_EQ(Z_STREAM_END, inflater.Inflate((buffer), sizeof(buffer)));
  EXPECT_EQ(kPoison, buffer[original_size]);
  EXPECT_EQ(kCombinedXmlContents,
            std::string(reinterpret_cast<char *>(buffer), original_size));
  free(reinterpret_cast<void *>(entry));

  // And for the combiner that just copies out:
  entry = reinterpret_cast<LH *>(xml_combiner2.OutputEntry(false));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_NO_COMPRESSION, entry->compression_method());
  original_size = entry->uncompressed_file_size();
  compressed_size = entry->compressed_file_size();
  EXPECT_EQ(compressed_size, original_size);
  EXPECT_EQ(
      kCombinedXmlContents,
      std::string(reinterpret_cast<char *>(entry->data()), original_size));
  EXPECT_TRUE(entry->file_name_is("combined2.xml"));
  EXPECT_EQ(0, entry->extra_fields_length());
  free(reinterpret_cast<void *>(entry));
}

// Test PropertyCombiner.
TEST_F(CombinersTest, PropertyCombiner) {
  static char kProperties[] =
      "name=value\n"
      "name_str=value_str\n";
  PropertyCombiner property_combiner("properties");
  property_combiner.AddProperty("name", "value");
  property_combiner.AddProperty(std::string("name_str"),
                                std::string("value_str"));

  // Merge should not be called.
  ASSERT_FALSE(property_combiner.Merge(nullptr, nullptr));

  // Create output, verify Local Header contents.
  LH *entry = reinterpret_cast<LH *>(property_combiner.OutputEntry(true));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_DEFLATED, entry->compression_method());
  uint64_t original_size = entry->uncompressed_file_size();
  uint64_t compressed_size = entry->compressed_file_size();
  EXPECT_EQ(strlen(kProperties), original_size);
  EXPECT_LE(compressed_size, original_size);
  EXPECT_EQ("properties", entry->file_name_string());
  EXPECT_EQ(0, entry->extra_fields_length());

  // Decompress and check contents.
  Inflater inflater;
  inflater.DataToInflate(entry->data(), compressed_size);
  uint8_t buffer[256];
  memset(buffer, kPoison, sizeof(buffer));
  ASSERT_EQ(Z_STREAM_END, inflater.Inflate((buffer), sizeof(buffer)));
  EXPECT_EQ(kPoison, buffer[original_size]);
  EXPECT_EQ(kProperties,
            std::string(reinterpret_cast<char *>(buffer), original_size));
  free(reinterpret_cast<void *>(entry));

  // Create output, verify Local Header contents.
  entry = reinterpret_cast<LH *>(property_combiner.OutputEntry(false));
  EXPECT_TRUE(entry->is());
  EXPECT_EQ(20, entry->version());
  EXPECT_EQ(Z_NO_COMPRESSION, entry->compression_method());
  original_size = entry->uncompressed_file_size();
  compressed_size = entry->compressed_file_size();
  EXPECT_EQ(compressed_size, original_size);
  EXPECT_EQ(
      kProperties,
      std::string(reinterpret_cast<char *>(entry->data()), original_size));
  EXPECT_EQ("properties", entry->file_name_string());
  EXPECT_EQ(0, entry->extra_fields_length());
  free(reinterpret_cast<void *>(entry));
}

TEST_F(CombinersTest, Java8DesugarDepsChecker) {
  // Tests are instance methods of CombinersTest to avoid gUnit dep in .h file.
  TestJava8DesugarDepsChecker_HasDefaultMethods();
  TestJava8DesugarDepsChecker_OutputEntry();
  TestJava8DesugarDepsChecker_NeededDepMissing();
  TestJava8DesugarDepsChecker_MissedDefaultMethods();
}