summaryrefslogtreecommitdiff
path: root/libs/io/src/test/java/org/the_jk
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-11-10 15:47:01 +0100
committerJoel Klinghed <the_jk@spawned.biz>2024-11-10 15:47:01 +0100
commit71e9a88cca01050200489ca716928f3b9c3177b7 (patch)
tree8e7df664153a73c28df975d7b568dcdf6dc8e9ad /libs/io/src/test/java/org/the_jk
parent7b82ec0afe0049dfad85e89f3d42f64176c0c9fa (diff)
Add verifier
Used to check if target files have the expected hash. Using a memory cache to not have to read source each time but falls back to reading source if needed.
Diffstat (limited to 'libs/io/src/test/java/org/the_jk')
-rw-r--r--libs/io/src/test/java/org/the_jk/cleversync/io/HasherTest.kt42
-rw-r--r--libs/io/src/test/java/org/the_jk/cleversync/io/VerifierLocalTest.kt19
2 files changed, 61 insertions, 0 deletions
diff --git a/libs/io/src/test/java/org/the_jk/cleversync/io/HasherTest.kt b/libs/io/src/test/java/org/the_jk/cleversync/io/HasherTest.kt
new file mode 100644
index 0000000..02fd79a
--- /dev/null
+++ b/libs/io/src/test/java/org/the_jk/cleversync/io/HasherTest.kt
@@ -0,0 +1,42 @@
+package org.the_jk.cleversync.io
+
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import java.io.ByteArrayOutputStream
+import java.nio.charset.Charset
+
+@Suppress("MaxLineLength")
+class HasherTest {
+ @Test
+ fun empty() {
+ check("", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
+ check("", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
+ check("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
+ check("", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b")
+ check("", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
+ }
+
+ @Test
+ fun known() {
+ check("The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12")
+ check("The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3")
+ check("The quick brown fox jumps over the lazy dog", "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525")
+ check("The quick brown fox jumps over the lazy dog.", "619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c")
+ }
+
+ private companion object {
+ fun check(input: String, hash: String) {
+ val format = Hasher.getFormat(hash)
+ input.toByteArray().inputStream().use {
+ assertThat(Hasher.hash(it, format)).isEqualTo(hash)
+ }
+ val byteStream = ByteArrayOutputStream()
+ val hashStream = Hasher.wrap(byteStream, format)
+ hashStream.use {
+ it.write(input.toByteArray())
+ }
+ assertThat(hashStream.hash()).isEqualTo(hash)
+ assertThat(byteStream.toByteArray().toString(Charset.defaultCharset())).isEqualTo(input)
+ }
+ }
+}
diff --git a/libs/io/src/test/java/org/the_jk/cleversync/io/VerifierLocalTest.kt b/libs/io/src/test/java/org/the_jk/cleversync/io/VerifierLocalTest.kt
new file mode 100644
index 0000000..3576d15
--- /dev/null
+++ b/libs/io/src/test/java/org/the_jk/cleversync/io/VerifierLocalTest.kt
@@ -0,0 +1,19 @@
+package org.the_jk.cleversync.io
+
+import org.junit.Rule
+import org.junit.rules.TemporaryFolder
+import org.the_jk.cleversync.local.LocalTreeFactory
+
+class VerifierLocalTest : BaseVerifierTest() {
+ @Rule
+ @JvmField
+ val temp = TemporaryFolder()
+
+ override fun source(): ModifiableTree {
+ return LocalTreeFactory.modifiableTree(temp.newFolder("source").toPath())
+ }
+
+ override fun target(): ModifiableTree {
+ return LocalTreeFactory.modifiableTree(temp.newFolder("target").toPath())
+ }
+}