|
15 | 15 | import java.security.NoSuchAlgorithmException; |
16 | 16 | import java.util.Base64; |
17 | 17 | import java.util.concurrent.atomic.AtomicInteger; |
| 18 | +import java.util.stream.IntStream; |
18 | 19 | import org.hamcrest.MatcherAssert; |
19 | 20 | import org.hamcrest.Matchers; |
20 | 21 | import org.junit.jupiter.api.Test; |
@@ -158,4 +159,63 @@ void writesCorrectShaHash(@Mktmp final Path temp) throws IOException, NoSuchAlgo |
158 | 159 | ) |
159 | 160 | ); |
160 | 161 | } |
| 162 | + |
| 163 | + @Test |
| 164 | + void generatesCorrectHashForLargeFile( |
| 165 | + @Mktmp final Path temp |
| 166 | + ) throws IOException, NoSuchAlgorithmException { |
| 167 | + final var cache = temp.resolve("cache"); |
| 168 | + Files.createDirectories(cache); |
| 169 | + final var source = temp.resolve("large.txt"); |
| 170 | + final int lines = 100_000; |
| 171 | + final StringBuilder builder = new StringBuilder(lines * 10); |
| 172 | + IntStream.range(0, lines).forEach( |
| 173 | + i -> builder.append("Line ").append(i).append('\n') |
| 174 | + ); |
| 175 | + final String content = builder.toString(); |
| 176 | + Files.writeString(source, content, StandardCharsets.UTF_8); |
| 177 | + final var target = temp.resolve("out.txt"); |
| 178 | + final var tail = source.getFileName(); |
| 179 | + new Cache(cache, p -> content).apply(source, target, tail); |
| 180 | + MatcherAssert.assertThat( |
| 181 | + "SHA-256 hash file has incorrect content for large file", |
| 182 | + Files.readString( |
| 183 | + cache.resolve(String.format("%s.sha256", tail)), |
| 184 | + StandardCharsets.UTF_8 |
| 185 | + ), |
| 186 | + Matchers.equalTo( |
| 187 | + Base64.getEncoder().encodeToString( |
| 188 | + MessageDigest.getInstance("SHA-256") |
| 189 | + .digest(content.getBytes(StandardCharsets.UTF_8)) |
| 190 | + ) |
| 191 | + ) |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + void generatesCorrectHashForTinyFile( |
| 197 | + @Mktmp final Path temp |
| 198 | + ) throws IOException, NoSuchAlgorithmException { |
| 199 | + final var cache = temp.resolve("cache"); |
| 200 | + Files.createDirectories(cache); |
| 201 | + final var source = temp.resolve("tiny.txt"); |
| 202 | + final String content = "x"; |
| 203 | + Files.writeString(source, content, StandardCharsets.UTF_8); |
| 204 | + final var target = temp.resolve("out.txt"); |
| 205 | + final var tail = source.getFileName(); |
| 206 | + new Cache(cache, p -> content).apply(source, target, tail); |
| 207 | + MatcherAssert.assertThat( |
| 208 | + "SHA-256 hash file has incorrect content for tiny file", |
| 209 | + Files.readString( |
| 210 | + cache.resolve(String.format("%s.sha256", tail)), |
| 211 | + StandardCharsets.UTF_8 |
| 212 | + ), |
| 213 | + Matchers.equalTo( |
| 214 | + Base64.getEncoder().encodeToString( |
| 215 | + MessageDigest.getInstance("SHA-256") |
| 216 | + .digest(content.getBytes(StandardCharsets.UTF_8)) |
| 217 | + ) |
| 218 | + ) |
| 219 | + ); |
| 220 | + } |
161 | 221 | } |
0 commit comments