aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/ijar/test
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-03-31 10:07:39 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-31 10:08:51 -0700
commitdf7731f13e58e719d0b0a703a0b53f2bc1e2d795 (patch)
tree0fc5736e046090f0a58dd05beb2f4d2eb2cd9012 /third_party/ijar/test
parentd5527767b08cbc77a0a71948dcf55ce7814ebbb0 (diff)
Make ijar support not stripping the class data.
This is done by adding a --[no]strip_classes flag to ijar. PiperOrigin-RevId: 191184258
Diffstat (limited to 'third_party/ijar/test')
-rw-r--r--third_party/ijar/test/BUILD49
-rw-r--r--third_party/ijar/test/IjarTests.java101
-rw-r--r--third_party/ijar/test/jar-with-manifest-and-target-label.jarbin0 -> 3338 bytes
-rw-r--r--third_party/ijar/test/jar-with-manifest.jarbin0 -> 3312 bytes
-rw-r--r--third_party/ijar/test/jar-without-manifest.jarbin0 -> 2952 bytes
5 files changed, 150 insertions, 0 deletions
diff --git a/third_party/ijar/test/BUILD b/third_party/ijar/test/BUILD
index 62b6edd3a3..816ae1221d 100644
--- a/third_party/ijar/test/BUILD
+++ b/third_party/ijar/test/BUILD
@@ -110,6 +110,46 @@ genrule(
)
genrule(
+ name = "interface_ijar_testlib_nostrip",
+ srcs = [":ijar_testlib"],
+ outs = ["interface_ijar_testlib_nostrip.jar"],
+ cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
+ tools = ["//third_party/ijar"],
+)
+
+genrule(
+ name = "jar_with_manifest_nostrip",
+ srcs = ["jar-with-manifest.jar"],
+ outs = ["jar-with-manifest-nostrip.jar"],
+ cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
+ tools = ["//third_party/ijar"],
+)
+
+genrule(
+ name = "jar_with_manifest_and_target_label_nostrip",
+ srcs = ["jar-with-manifest-and-target-label.jar"],
+ outs = ["jar-with-manifest-and-target-label-nostrip.jar"],
+ cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
+ tools = ["//third_party/ijar"],
+)
+
+genrule(
+ name = "jar_without_manifest_nostrip",
+ srcs = ["jar-without-manifest.jar"],
+ outs = ["jar-without-manifest-nostrip.jar"],
+ cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
+ tools = ["//third_party/ijar"],
+)
+
+genrule(
+ name = "jar_without_manifest_nostrip_idempotence",
+ srcs = ["jar-without-manifest-nostrip.jar"],
+ outs = ["jar-without-manifest-nostrip-idempotence.jar"],
+ cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
+ tools = ["//third_party/ijar"],
+)
+
+genrule(
name = "empty_with_target_label",
srcs = [":empty_zip.jar"],
outs = ["empty_with_target_label.jar"],
@@ -194,10 +234,19 @@ java_test(
"PrivateNestedClass.java",
"UseDeprecatedParts.java",
"UseRestrictedAnnotation.java",
+ "jar-with-manifest.jar",
+ "jar-with-manifest-and-target-label.jar",
+ "jar-without-manifest.jar",
"package-info.java",
":empty_with_target_label",
+ ":ijar_testlib",
":interface_ijar_testlib",
+ ":interface_ijar_testlib_nostrip",
":interface_ijar_testlib_with_target_label",
+ ":jar_with_manifest_and_target_label_nostrip",
+ ":jar_with_manifest_nostrip",
+ ":jar_without_manifest_nostrip",
+ ":jar_without_manifest_nostrip_idempotence",
":liblocal_and_anonymous_lib.jar",
":local_and_anonymous-interface.jar",
":module_info-interface.jar",
diff --git a/third_party/ijar/test/IjarTests.java b/third_party/ijar/test/IjarTests.java
index f88616f469..f8e487ff64 100644
--- a/third_party/ijar/test/IjarTests.java
+++ b/third_party/ijar/test/IjarTests.java
@@ -25,6 +25,9 @@ import com.google.common.io.ByteStreams;
import com.google.devtools.build.java.bazel.BazelJavaCompiler;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
@@ -309,4 +312,102 @@ public class IjarTests {
LocalDateTime.of(2010, 1, 1, 0, 0, 0).atZone(ZoneOffset.systemDefault()).toInstant());
}
}
+
+ // Tests --nostrip_jar with a jar that already has a manifest, but no target label
+ @Test
+ public void testNoStripJarWithManifest() throws Exception {
+ JarFile original = new JarFile("third_party/ijar/test/jar-with-manifest.jar");
+ JarFile stripped = new JarFile("third_party/ijar/test/jar-with-manifest-nostrip.jar");
+ try {
+ ImmutableList<String> strippedEntries =
+ stripped.stream().map(JarEntry::getName).collect(toImmutableList());
+ assertThat(strippedEntries.get(0)).isEqualTo("META-INF/");
+ assertThat(strippedEntries.get(1)).isEqualTo("META-INF/MANIFEST.MF");
+ Manifest manifest = stripped.getManifest();
+ Attributes attributes = manifest.getMainAttributes();
+ assertThat(attributes.getValue("Manifest-Version")).isEqualTo("1.0");
+ // Created-By was already in manifest, doesn't get overwritten
+ assertThat(attributes.getValue("Created-By")).isEqualTo("test-code");
+ assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");
+ assertNonManifestFilesBitIdentical(original, stripped);
+ } finally {
+ original.close();
+ stripped.close();
+ }
+ }
+
+ // Tests --nostrip_jar with a jar that already has a manifest with a target label
+ @Test
+ public void testNoStripJarWithManifestAndTargetLabel() throws Exception {
+ JarFile original = new JarFile("third_party/ijar/test/jar-with-manifest-and-target-label.jar");
+ JarFile stripped =
+ new JarFile("third_party/ijar/test/jar-with-manifest-and-target-label-nostrip.jar");
+ try {
+ ImmutableList<String> strippedEntries =
+ stripped.stream().map(JarEntry::getName).collect(toImmutableList());
+ assertThat(strippedEntries.get(0)).isEqualTo("META-INF/");
+ assertThat(strippedEntries.get(1)).isEqualTo("META-INF/MANIFEST.MF");
+ Manifest manifest = stripped.getManifest();
+ Attributes attributes = manifest.getMainAttributes();
+ assertThat(attributes.getValue("Manifest-Version")).isEqualTo("1.0");
+ // Created-By was already in manifest, doesn't get overwritten
+ assertThat(attributes.getValue("Created-By")).isEqualTo("test-code");
+ assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");
+ assertNonManifestFilesBitIdentical(original, stripped);
+ } finally {
+ original.close();
+ stripped.close();
+ }
+ }
+
+ // Tests --nostrip_jar with a jar that didn't already have a manifest
+ @Test
+ public void testNoStripJarWithoutManifest() throws Exception {
+ JarFile original = new JarFile("third_party/ijar/test/jar-without-manifest.jar");
+ JarFile stripped = new JarFile("third_party/ijar/test/jar-without-manifest-nostrip.jar");
+ try {
+ ImmutableList<String> strippedEntries =
+ stripped.stream().map(JarEntry::getName).collect(toImmutableList());
+ assertThat(strippedEntries.get(0)).isEqualTo("META-INF/");
+ assertThat(strippedEntries.get(1)).isEqualTo("META-INF/MANIFEST.MF");
+ Manifest manifest = stripped.getManifest();
+ Attributes attributes = manifest.getMainAttributes();
+ assertThat(attributes.getValue("Manifest-Version")).isEqualTo("1.0");
+ assertThat(attributes.getValue("Created-By")).isEqualTo("bazel");
+ assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");
+ assertNonManifestFilesBitIdentical(original, stripped);
+ } finally {
+ original.close();
+ stripped.close();
+ }
+ }
+
+ // Tests idempotence of --nostrip
+ @Test
+ public void testNoStripIdempotence() throws Exception {
+ byte[] original =
+ Files.readAllBytes(Paths.get("third_party/ijar/test/jar-without-manifest-nostrip.jar"));
+ byte[] stripped =
+ Files.readAllBytes(
+ Paths.get("third_party/ijar/test/jar-without-manifest-nostrip-idempotence.jar"));
+ assertThat(original).isEqualTo(stripped);
+ }
+
+ private static void assertNonManifestFilesBitIdentical(JarFile original, JarFile stripped)
+ throws IOException {
+ // Make sure that all other files came across bitwise equal
+ for (String classEntry :
+ original
+ .stream()
+ .map(JarEntry::getName)
+ .filter(name -> !name.equals("META-INF/MANIFEST.MF"))
+ .collect(toImmutableList())) {
+ ZipEntry originalEntry = original.getEntry(classEntry);
+ ZipEntry strippedEntry = stripped.getEntry(classEntry);
+ InputStream originalStream = original.getInputStream(originalEntry);
+ InputStream strippedStream = stripped.getInputStream(strippedEntry);
+ assertThat(ByteStreams.toByteArray(strippedStream))
+ .isEqualTo(ByteStreams.toByteArray(originalStream));
+ }
+ }
}
diff --git a/third_party/ijar/test/jar-with-manifest-and-target-label.jar b/third_party/ijar/test/jar-with-manifest-and-target-label.jar
new file mode 100644
index 0000000000..aab7c701f2
--- /dev/null
+++ b/third_party/ijar/test/jar-with-manifest-and-target-label.jar
Binary files differ
diff --git a/third_party/ijar/test/jar-with-manifest.jar b/third_party/ijar/test/jar-with-manifest.jar
new file mode 100644
index 0000000000..7191935efb
--- /dev/null
+++ b/third_party/ijar/test/jar-with-manifest.jar
Binary files differ
diff --git a/third_party/ijar/test/jar-without-manifest.jar b/third_party/ijar/test/jar-without-manifest.jar
new file mode 100644
index 0000000000..d2bd18d006
--- /dev/null
+++ b/third_party/ijar/test/jar-without-manifest.jar
Binary files differ