Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implement of md5 and sha1 for MockAbsoluteFile #2436

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,29 @@ private fun MockServerRemoteFile.toMockAbsFile(
sha1: ByteArray = byteArrayOf()
): AbsoluteFile {
val parent = this.parent.toMockAbsFolder(files)
// todo md5 and sha
return MockAbsoluteFile(
sha1,
md5,
files,
parent,
this.id,
this.name,
parent.absolutePath.removeSuffix("/") + "/" + this.name
)
return if (md5.isEmpty() || sha1.isEmpty()) {
asExternalResource().use { res ->
MockAbsoluteFile(
if (sha1.isEmpty()) res.sha1 else sha1,
if (md5.isEmpty()) res.md5 else md5,
files,
parent,
this.id,
this.name,
parent.absolutePath.removeSuffix("/") + "/" + this.name
)
}
} else {
MockAbsoluteFile(
sha1,
md5,
files,
parent,
this.id,
this.name,
parent.absolutePath.removeSuffix("/") + "/" + this.name
)
}
}

internal open class MockAbsoluteFolder(
Expand Down
32 changes: 32 additions & 0 deletions mirai-core-mock/test/AbsoluteFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import net.mamoe.mirai.mock.internal.serverfs.MockServerFileSystemImpl
import net.mamoe.mirai.mock.utils.simpleMemberInfo
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.cast
import net.mamoe.mirai.utils.md5
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.nio.file.FileSystem
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

Expand Down Expand Up @@ -97,4 +99,34 @@ internal class AbsoluteFileTest : MockBotTestBase() {
assertEquals(true, file.exists())
assertNotEquals(null, folder.resolveFiles("test.txt").firstOrNull())
}

@Test
fun testMD5() = runTest {
val bytes = "test".toByteArray()
val file = bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
assertContentEquals(bytes.md5(), file.md5)
}

@Test
fun testMD5WithResolve() = runTest {
val bytes = "test".toByteArray()
bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
val file = files.root.resolveFiles("/test.txt").toList()
assertEquals(1, file.size)
assertContentEquals(bytes.md5(), file[0].md5)
}

@Test
fun testMD5WithIDResolve() = runTest {
val bytes = "test".toByteArray()
val absFile = bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
val file = files.root.resolveFileById(absFile.id, true)!!
assertContentEquals(bytes.md5(), file.md5)
}
}