diff options
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java | 23 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/bazel/repository/MavenJarFunctionTest.java | 19 |
2 files changed, 31 insertions, 11 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java index 595e0cb991..64cdfdf14a 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java @@ -30,32 +30,33 @@ import javax.annotation.Nullable; public class RepositoryCache { /** The types of cache keys used. */ - public static enum KeyType { + public enum KeyType { SHA1("SHA-1", "\\p{XDigit}{40}", "sha1", Hashing.sha1()), SHA256("SHA-256", "\\p{XDigit}{64}", "sha256", Hashing.sha256()); private final String stringRepr; private final String regexp; - private final String cacheDirName; + private final String hashName; + @SuppressWarnings("ImmutableEnumChecker") private final HashFunction hashFunction; - KeyType(String stringRepr, String regexp, String cacheDirName, HashFunction hashFunction) { + KeyType(String stringRepr, String regexp, String hashName, HashFunction hashFunction) { this.stringRepr = stringRepr; this.regexp = regexp; - this.cacheDirName = cacheDirName; + this.hashName = hashName; this.hashFunction = hashFunction; } - public boolean isValid(String checksum) { - return checksum.matches(regexp); + public boolean isValid(@Nullable String checksum) { + return checksum != null && checksum.matches(regexp); } - public String getDirectoryName() { - return cacheDirName; + public Path getCachePath(Path parentDirectory) { + return parentDirectory.getChild(hashName); } - public Path getCachePath(Path parentDirectory) { - return parentDirectory.getChild(cacheDirName); + public Hasher newHasher() { + return hashFunction.newHasher(); } @Override @@ -205,7 +206,7 @@ public class RepositoryCache { * @throws IOException */ public static String getChecksum(KeyType keyType, Path path) throws IOException { - Hasher hasher = keyType.hashFunction.newHasher(); + Hasher hasher = keyType.newHasher(); byte[] byteBuffer = new byte[BUFFER_SIZE]; try (InputStream stream = path.getInputStream()) { int numBytesRead = stream.read(byteBuffer); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/MavenJarFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/MavenJarFunctionTest.java index 61e8023e27..8b90c612df 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/MavenJarFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/MavenJarFunctionTest.java @@ -93,4 +93,23 @@ public class MavenJarFunctionTest extends BuildViewTestCase { assertThat(expected.getMessage()).contains("Failed to fetch Maven dependency:"); } } + + @Test + public void testNoSha1WithCache() throws Exception { + Rule rule = scratchRule("external", "foo", + "maven_jar(", + " name = 'foo',", + " artifact = 'x:y:z:1.1',", + ")"); + RepositoryCache cache = Mockito.mock(RepositoryCache.class); + Mockito.when(cache.isEnabled()).thenReturn(true); + MavenDownloader downloader = new MavenDownloader(cache); + try { + downloader.download( + "foo", WorkspaceAttributeMapper.of(rule), scratch.dir("/whatever"), TEST_SERVER); + fail("Expected failure to fetch artifact because of nonexistent server."); + } catch (IOException expected) { + assertThat(expected.getMessage()).contains("Failed to fetch Maven dependency:"); + } + } } |