From a15cfde0fcbdb29dafdb9ebe39fe53c8da4073be Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 25 Aug 2024 02:30:36 +0200 Subject: Combine tests from both local and samba Most the tests test the Tree implementation and thus should work on all such implementations. Current exception is symlinks which Samba backend doesn't (currently?) support. Improve the Samba remove methods to better match the expected behavior. --- .../org/the_jk/cleversync/samba/SambaTreeTest.kt | 178 +++++++-------------- 1 file changed, 59 insertions(+), 119 deletions(-) (limited to 'libs/samba/src/test') diff --git a/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt index 61c8da7..fecd746 100644 --- a/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt +++ b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt @@ -12,9 +12,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.shadows.ShadowLooper -import org.the_jk.cleversync.io.Directory +import org.the_jk.cleversync.TreeAbstractTest import org.the_jk.cleversync.io.samba.SambaCredentials -import org.the_jk.cleversync.safeValue import java.io.File import java.nio.charset.StandardCharsets import java.nio.file.Files @@ -22,14 +21,18 @@ import java.util.concurrent.TimeUnit @Config(manifest=Config.NONE) @RunWith(RobolectricTestRunner::class) -class SambaTreeTest { +class SambaTreeTest : TreeAbstractTest() { @Before fun setUpTest() { assertThat(shareDir.listFiles()).isEmpty() + + tree = SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow() } @After fun tearDownTest() { + tree.close() + for (file in shareDir.listFiles()!!) { if (file.isDirectory) { file.deleteRecursively() @@ -47,157 +50,94 @@ class SambaTreeTest { } } - @Test - fun listEmptyRoot() { - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.list() - assertThat(content.directories).isEmpty() - assertThat(content.files).isEmpty() - assertThat(content.links).isEmpty() - } - } - - @Test - fun listEmptyRootLive() { - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.liveList().safeValue() - assertThat(content?.directories).isEmpty() - assertThat(content?.files).isEmpty() - assertThat(content?.links).isEmpty() - } - } - @Test fun listRootWithSymlink() { File(shareDir, "dir").mkdir() File(shareDir, "file").writeText("foo") Files.createSymbolicLink(File(shareDir, "link").toPath(), File("file").toPath()) - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.list() - - assertThat(content.directories).hasSize(1) - assertThat(content.directories[0].name).isEqualTo("dir") - assertThat(content.files).hasSize(2) - if (content.files[0].name == "file") { - assertThat(content.files[0].name).isEqualTo("file") - assertThat(content.files[0].size).isEqualTo(3UL) - assertThat(content.files[1].name).isEqualTo("link") - assertThat(content.files[1].size).isEqualTo(3UL) - } else { - assertThat(content.files[0].name).isEqualTo("link") - assertThat(content.files[0].size).isEqualTo(3UL) - assertThat(content.files[1].name).isEqualTo("file") - assertThat(content.files[1].size).isEqualTo(3UL) - } - // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now - assertThat(content.links).isEmpty() + val content = tree.list() + + assertThat(content.directories).hasSize(1) + assertThat(content.directories[0].name).isEqualTo("dir") + assertThat(content.files).hasSize(2) + if (content.files[0].name == "file") { + assertThat(content.files[0].name).isEqualTo("file") + assertThat(content.files[0].size).isEqualTo(3UL) + assertThat(content.files[1].name).isEqualTo("link") + assertThat(content.files[1].size).isEqualTo(3UL) + } else { + assertThat(content.files[0].name).isEqualTo("link") + assertThat(content.files[0].size).isEqualTo(3UL) + assertThat(content.files[1].name).isEqualTo("file") + assertThat(content.files[1].size).isEqualTo(3UL) } + // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now + assertThat(content.links).isEmpty() } @Test - fun readFile() { + fun readExistingFile() { File(shareDir, "file").writeText("hello world") - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val file = root.openFile("file") - assertThat(file?.name).isEqualTo("file") - assertThat(file?.size).isEqualTo(11UL) + val file = tree.openFile("file") + assertThat(file?.name).isEqualTo("file") + assertThat(file?.size).isEqualTo(11UL) - file?.read().use { input -> - assertThat(input?.readAllBytes()?.toString(StandardCharsets.UTF_8)).isEqualTo("hello world") - } + file?.read().use { input -> + assertThat(input?.readAllBytes()?.toString(StandardCharsets.UTF_8)).isEqualTo("hello world") + } - file?.read().use { input -> - assertThat(input?.available()).isEqualTo(11) - assertThat(input?.markSupported()).isTrue() - val buffer = ByteArray(10) - assertThat(input?.read(buffer, 5, 5)).isEqualTo(5) - input?.mark(100) - assertThat(buffer.sliceArray(5..<10).toString(StandardCharsets.UTF_8)).isEqualTo("hello") - assertThat(input?.read(buffer)).isEqualTo(6) - assertThat(buffer.sliceArray(0..<6).toString(StandardCharsets.UTF_8)).isEqualTo(" world") - input?.reset() - assertThat(input?.read(buffer, 3, 5)).isEqualTo(5) - assertThat(buffer.sliceArray(3..<8).toString(StandardCharsets.UTF_8)).isEqualTo(" worl") - } + file?.read().use { input -> + assertThat(input?.available()).isEqualTo(11) + assertThat(input?.markSupported()).isTrue() + val buffer = ByteArray(10) + assertThat(input?.read(buffer, 5, 5)).isEqualTo(5) + input?.mark(100) + assertThat(buffer.sliceArray(5..<10).toString(StandardCharsets.UTF_8)).isEqualTo("hello") + assertThat(input?.read(buffer)).isEqualTo(6) + assertThat(buffer.sliceArray(0..<6).toString(StandardCharsets.UTF_8)).isEqualTo(" world") + input?.reset() + assertThat(input?.read(buffer, 3, 5)).isEqualTo(5) + assertThat(buffer.sliceArray(3..<8).toString(StandardCharsets.UTF_8)).isEqualTo(" worl") } } @Test - fun writeFile() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val file = root.createFile("file") - assertThat(file.name).isEqualTo("file") - - file.write().writer().use { output -> - output.write("hello world") - } - - assertThat(file.size).isEqualTo(11UL) - } + override fun createFile() { + super.createFile() - assertThat(File(shareDir, "file").readText()).isEqualTo("hello world") + assertThat(File(shareDir, "foo").readBytes()).isEqualTo(byteArrayOf(1, 2, 3, 4)) } @Test - fun overwriteFile() { - File(shareDir, "file").writeText("hello world") - - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val file = root.modifiableOpenFile("file") - assertThat(file?.name).isEqualTo("file") - assertThat(file?.size).isEqualTo(11UL) - - file?.write().use { output -> - val buffer = "foobar".toByteArray(StandardCharsets.UTF_8) - output?.write(buffer, 0, 1) - output?.write(buffer, 1, 2) - output?.write(buffer, 3, 3) - } - - assertThat(file?.size).isEqualTo(6UL) - } + override fun overwriteFile() { + super.overwriteFile() - assertThat(File(shareDir, "file").readText()).isEqualTo("foobar") + assertThat(File(shareDir, "foo").readBytes()).isEqualTo(byteArrayOf(127, 1)) } @Test - fun createDirectory() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val foo = root.createDirectory("foo") - assertThat(foo.name).isEqualTo("foo") - val fooContent = foo.list() - assertThat(fooContent.directories).isEmpty() - assertThat(fooContent.files).isEmpty() - assertThat(fooContent.links).isEmpty() - val content = root.list() - assertThat(content.directories).contains(foo) - assertThat(content.files).isEmpty() - assertThat(content.links).isEmpty() - } + override fun createDirectory() { + super.createDirectory() assertThat(File(shareDir, "foo").isDirectory).isTrue() } @Test(timeout = 10000) - fun observeCreateDirectory() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val content = root.liveList() - var dir: Directory? = null - content.observeForever { - if (it.directories.size == 1) dir = it.directories[0] - } - root.createDirectory("foo") - while (dir == null) { - ShadowLooper.idleMainLooper(10, TimeUnit.SECONDS) - } - assertThat(dir?.name).isEqualTo("foo") - } + override fun observeCreateDirectory() { + super.observeCreateDirectory() assertThat(File(shareDir, "foo").isDirectory).isTrue() } + // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now + override fun supportSymlinks() = false + + override fun idle() { + ShadowLooper.idleMainLooper(10, TimeUnit.SECONDS) + } + companion object { private lateinit var uri: String private lateinit var credentials: SambaCredentials -- cgit v1.2.3-70-g09d2