AndroidResourceOutputs: fix ZipEntry paths

Make sure that ZipEntry paths always use forward
slashes, even on Windows. Also add a test.

See https://github.com/bazelbuild/bazel/issues/3264

Change-Id: I4508e46dde49cd44c8e3792017d0d280a51dc565
PiperOrigin-RevId: 161500049
diff --git a/src/test/java/com/google/devtools/build/android/AndroidResourceOutputsTest.java b/src/test/java/com/google/devtools/build/android/AndroidResourceOutputsTest.java
new file mode 100644
index 0000000..9c10944
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/android/AndroidResourceOutputsTest.java
@@ -0,0 +1,68 @@
+// Copyright 2017 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.
+package com.google.devtools.build.android;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.jimfs.Jimfs;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link AndroidResourceOutputsTest}. */
+@RunWith(JUnit4.class)
+public class AndroidResourceOutputsTest {
+  private Path tmp;
+
+  @Before
+  public void setUp() throws Exception {
+    tmp =
+        Files.createTempDirectory(
+            Jimfs.newFileSystem().getRootDirectories().iterator().next(),
+            getClass().getSimpleName());
+  }
+
+  @Test
+  public void testZipEntryPaths() throws Exception {
+    Path output = tmp.resolve("actual.zip");
+    Files.createDirectories(tmp.resolve("foo/bar"));
+    Files.write(tmp.resolve("foo/data1.txt"), "hello".getBytes(Charset.defaultCharset()));
+    Files.write(tmp.resolve("foo/bar/data2.txt"), "world".getBytes(Charset.defaultCharset()));
+
+    try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(output))) {
+      AndroidResourceOutputs.ZipBuilderVisitor visitor =
+          new AndroidResourceOutputs.ZipBuilderVisitor(zout, tmp.resolve("foo"), "some/prefix");
+      Files.walkFileTree(tmp.resolve("foo"), visitor);
+      visitor.writeEntries();
+    }
+
+    List<String> entries = new ArrayList<>();
+    try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(output))) {
+      ZipEntry entry = null;
+      while ((entry = zin.getNextEntry()) != null) {
+        entries.add(entry.getName());
+      }
+    }
+    assertThat(entries).containsExactly("some/prefix/data1.txt", "some/prefix/bar/data2.txt");
+  }
+}
diff --git a/src/test/java/com/google/devtools/build/android/BUILD b/src/test/java/com/google/devtools/build/android/BUILD
index ab7dd89..77bb8df 100644
--- a/src/test/java/com/google/devtools/build/android/BUILD
+++ b/src/test/java/com/google/devtools/build/android/BUILD
@@ -12,6 +12,18 @@
 )
 
 java_test(
+    name = "AndroidResourceOutputsTest",
+    size = "small",
+    srcs = ["AndroidResourceOutputsTest.java"],
+    deps = [
+        "//src/tools/android/java/com/google/devtools/build/android:android_builder_lib",
+        "//third_party:jimfs",
+        "//third_party:junit4",
+        "//third_party:truth",
+    ],
+)
+
+java_test(
     name = "RClassGeneratorActionTest",
     size = "small",
     srcs = ["RClassGeneratorActionTest.java"],
diff --git a/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java b/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
index a9668ca..e18f466 100644
--- a/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
+++ b/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
@@ -24,7 +24,6 @@
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.nio.file.attribute.FileTime;
 import java.util.Collections;
 import java.util.List;
@@ -136,23 +135,23 @@
       Iterable<String> entries = getZipFilenames(zipEntries);
       assertThat(entries)
           .containsExactly(
-              Paths.get("com/google/foo/R$attr.class").toString(),
-              Paths.get("com/google/foo/R$id.class").toString(),
-              Paths.get("com/google/foo/R$string.class").toString(),
-              Paths.get("com/google/foo/R.class").toString(),
-              Paths.get("com/google/bar/R$attr.class").toString(),
-              Paths.get("com/google/bar/R$drawable.class").toString(),
-              Paths.get("com/google/bar/R.class").toString(),
-              Paths.get("com/google/app/R$attr.class").toString(),
-              Paths.get("com/google/app/R$drawable.class").toString(),
-              Paths.get("com/google/app/R$id.class").toString(),
-              Paths.get("com/google/app/R$integer.class").toString(),
-              Paths.get("com/google/app/R$string.class").toString(),
-              Paths.get("com/google/app/R.class").toString(),
-              Paths.get("META-INF/MANIFEST.MF").toString());
+              "com/google/foo/R$attr.class",
+              "com/google/foo/R$id.class",
+              "com/google/foo/R$string.class",
+              "com/google/foo/R.class",
+              "com/google/bar/R$attr.class",
+              "com/google/bar/R$drawable.class",
+              "com/google/bar/R.class",
+              "com/google/app/R$attr.class",
+              "com/google/app/R$drawable.class",
+              "com/google/app/R$id.class",
+              "com/google/app/R$integer.class",
+              "com/google/app/R$string.class",
+              "com/google/app/R.class",
+              "META-INF/MANIFEST.MF");
     }
   }
-  
+
   @Test
   public void withNoBinaryAndLibraries() throws Exception {
     Path libFooManifest =
@@ -192,14 +191,14 @@
       Iterable<String> entries = getZipFilenames(zipEntries);
       assertThat(entries)
           .containsExactly(
-              Paths.get("com/google/foo/R$attr.class").toString(),
-              Paths.get("com/google/foo/R$id.class").toString(),
-              Paths.get("com/google/foo/R$string.class").toString(),
-              Paths.get("com/google/foo/R.class").toString(),
-              Paths.get("com/google/bar/R$attr.class").toString(),
-              Paths.get("com/google/bar/R$drawable.class").toString(),
-              Paths.get("com/google/bar/R.class").toString(),
-              Paths.get("META-INF/MANIFEST.MF").toString());
+              "com/google/foo/R$attr.class",
+              "com/google/foo/R$id.class",
+              "com/google/foo/R$string.class",
+              "com/google/foo/R.class",
+              "com/google/bar/R$attr.class",
+              "com/google/bar/R$drawable.class",
+              "com/google/bar/R.class",
+              "META-INF/MANIFEST.MF");
     }
   }
 
@@ -236,13 +235,13 @@
       Iterable<String> entries = getZipFilenames(zipEntries);
       assertThat(entries)
           .containsExactly(
-              Paths.get("com/google/app/R$attr.class").toString(),
-              Paths.get("com/google/app/R$drawable.class").toString(),
-              Paths.get("com/google/app/R$id.class").toString(),
-              Paths.get("com/google/app/R$integer.class").toString(),
-              Paths.get("com/google/app/R$string.class").toString(),
-              Paths.get("com/google/app/R.class").toString(),
-              Paths.get("META-INF/MANIFEST.MF").toString());
+              "com/google/app/R$attr.class",
+              "com/google/app/R$drawable.class",
+              "com/google/app/R$id.class",
+              "com/google/app/R$integer.class",
+              "com/google/app/R$string.class",
+              "com/google/app/R.class",
+              "META-INF/MANIFEST.MF");
     }
   }
 
@@ -259,7 +258,7 @@
     try (ZipFile zip = new ZipFile(jarPath.toFile())) {
       List<? extends ZipEntry> zipEntries = Collections.list(zip.entries());
       Iterable<String> entries = getZipFilenames(zipEntries);
-      assertThat(entries).containsExactly(Paths.get("META-INF/MANIFEST.MF").toString());
+      assertThat(entries).containsExactly("META-INF/MANIFEST.MF");
     }
   }
 
@@ -301,13 +300,13 @@
       Iterable<String> entries = getZipFilenames(zipEntries);
       assertThat(entries)
           .containsExactly(
-              Paths.get("com/google/foo/R$string.class").toString(),
-              Paths.get("com/google/foo/R.class").toString(),
-              Paths.get("com/custom/er/R$attr.class").toString(),
-              Paths.get("com/custom/er/R$integer.class").toString(),
-              Paths.get("com/custom/er/R$string.class").toString(),
-              Paths.get("com/custom/er/R.class").toString(),
-              Paths.get("META-INF/MANIFEST.MF").toString());
+              "com/google/foo/R$string.class",
+              "com/google/foo/R.class",
+              "com/custom/er/R$attr.class",
+              "com/custom/er/R$integer.class",
+              "com/custom/er/R$string.class",
+              "com/custom/er/R.class",
+              "META-INF/MANIFEST.MF");
     }
   }
 
@@ -337,9 +336,7 @@
     try (ZipFile zip = new ZipFile(jarPath.toFile())) {
       List<? extends ZipEntry> zipEntries = Collections.list(zip.entries());
       Iterable<String> entries = getZipFilenames(zipEntries);
-      assertThat(entries)
-          .containsExactly(
-              Paths.get("META-INF/MANIFEST.MF").toString());
+      assertThat(entries).containsExactly("META-INF/MANIFEST.MF");
     }
   }
 
diff --git a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java
index 472e71f..608309f 100644
--- a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java
+++ b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java
@@ -160,7 +160,7 @@
     protected void addEntry(Path file, byte[] content) throws IOException {
       String prefix = directoryPrefix != null ? (directoryPrefix + "/") : "";
       String relativeName = root.relativize(file).toString();
-      ZipEntry entry = new ZipEntry(prefix + relativeName);
+      ZipEntry entry = new ZipEntry((prefix + relativeName).replace('\\', '/'));
       entry.setMethod(storageMethod);
       entry.setTime(normalizeTime(relativeName));
       entry.setSize(content.length);