Skip to content

Commit 38b6e9a

Browse files
committed
Add ChecksumCalculator tests
1 parent 82758c7 commit 38b6e9a

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package helium314.keyboard.latin.utils
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
import java.io.ByteArrayInputStream
6+
import java.io.File
7+
import java.nio.file.Files
8+
9+
class ChecksumCalculatorTest {
10+
11+
@Test
12+
fun checksum_emptyInputStream_returnsExpectedHash() {
13+
val emptyStream = ByteArrayInputStream(ByteArray(0))
14+
val result = ChecksumCalculator.checksum(emptyStream)
15+
// SHA-256 for empty string
16+
assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", result)
17+
}
18+
19+
@Test
20+
fun checksum_stringInputStream_returnsExpectedHash() {
21+
val stream = ByteArrayInputStream("hello world".toByteArray())
22+
val result = ChecksumCalculator.checksum(stream)
23+
// SHA-256 for "hello world"
24+
assertEquals("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", result)
25+
}
26+
27+
@Test
28+
fun checksum_largeInputStream_returnsExpectedHash() {
29+
// Create an input larger than 8192 buffer to exercise the while loop
30+
val largeData = ByteArray(10000) { it.toByte() }
31+
val stream = ByteArrayInputStream(largeData)
32+
val result = ChecksumCalculator.checksum(stream)
33+
34+
// Let's compute the hash separately or use a known one. We can use MessageDigest directly here to verify
35+
val digester = java.security.MessageDigest.getInstance("SHA-256")
36+
val expectedDigest = digester.digest(largeData)
37+
val s = java.lang.StringBuilder()
38+
for (i in expectedDigest.indices) {
39+
s.append(String.format("%1$02x", expectedDigest[i]))
40+
}
41+
val expected = s.toString()
42+
43+
assertEquals(expected, result)
44+
}
45+
46+
@Test
47+
fun checksum_file_returnsExpectedHash() {
48+
val tempFile = Files.createTempFile("test", ".txt").toFile()
49+
try {
50+
tempFile.writeText("file content")
51+
val result = ChecksumCalculator.checksum(tempFile)
52+
// SHA-256 for "file content"
53+
assertEquals("e0ac3601005dfa1864f5392aabaf7d898b1b5bab854f1acb4491bcd806b76b0c", result)
54+
} finally {
55+
tempFile.delete()
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)