Validate snapshot configuration fingerprints
This commit is contained in:
@@ -34,9 +34,14 @@ import org.oxycblt.musikr.tag.interpret.PreSong
|
||||
internal object LibrarySnapshotDocument {
|
||||
private const val MAX_SONGS = 100_000
|
||||
|
||||
fun write(output: OutputStream, songs: List<PreSong>) {
|
||||
fun write(
|
||||
output: OutputStream,
|
||||
songs: List<PreSong>,
|
||||
fingerprint: LibrarySnapshotFingerprint,
|
||||
) {
|
||||
LibrarySnapshotCodec.writeHeader(output)
|
||||
val checkedOutput = CheckedOutputStream(output, CRC32())
|
||||
LibrarySnapshotFingerprintCodec.write(checkedOutput, fingerprint)
|
||||
LibrarySnapshotCodec.writeCount(checkedOutput, songs.size, MAX_SONGS)
|
||||
for (song in songs) {
|
||||
LibrarySnapshotCodec.writePreSong(checkedOutput, song)
|
||||
@@ -46,6 +51,7 @@ internal object LibrarySnapshotDocument {
|
||||
|
||||
suspend fun read(
|
||||
input: InputStream,
|
||||
expectedFingerprint: LibrarySnapshotFingerprint,
|
||||
storedCoverDirectory: File,
|
||||
context: Context,
|
||||
storedPlaylists: StoredPlaylists,
|
||||
@@ -53,6 +59,10 @@ internal object LibrarySnapshotDocument {
|
||||
): MutableLibrary {
|
||||
LibrarySnapshotCodec.readHeader(input)
|
||||
val checkedInput = CheckedInputStream(input, CRC32())
|
||||
val fingerprint = LibrarySnapshotFingerprintCodec.read(checkedInput)
|
||||
if (fingerprint != expectedFingerprint) {
|
||||
throw IOException("Snapshot fingerprint mismatch")
|
||||
}
|
||||
val songCount = LibrarySnapshotCodec.readCount(checkedInput, MAX_SONGS)
|
||||
val songs = ArrayList<PreSong>(songCount)
|
||||
repeat(songCount) {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotFingerprint.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
data class LibrarySnapshotFingerprint(
|
||||
val sourceMode: String,
|
||||
val sourceRoots: List<String>,
|
||||
val excludedRoots: List<String>,
|
||||
val withHidden: Boolean,
|
||||
val excludeNonMusic: Boolean,
|
||||
val intelligentSorting: Boolean,
|
||||
val separators: String,
|
||||
val coverMode: String,
|
||||
)
|
||||
|
||||
internal object LibrarySnapshotFingerprintCodec {
|
||||
private const val MAX_ROOT_IDENTITIES = 1_000
|
||||
|
||||
fun write(output: OutputStream, value: LibrarySnapshotFingerprint) {
|
||||
LibrarySnapshotCodec.writeString(output, value.sourceMode)
|
||||
writeStrings(output, value.sourceRoots)
|
||||
writeStrings(output, value.excludedRoots)
|
||||
LibrarySnapshotCodec.writeBoolean(output, value.withHidden)
|
||||
LibrarySnapshotCodec.writeBoolean(output, value.excludeNonMusic)
|
||||
LibrarySnapshotCodec.writeBoolean(output, value.intelligentSorting)
|
||||
LibrarySnapshotCodec.writeString(output, value.separators)
|
||||
LibrarySnapshotCodec.writeString(output, value.coverMode)
|
||||
}
|
||||
|
||||
fun read(input: InputStream): LibrarySnapshotFingerprint {
|
||||
val sourceMode = LibrarySnapshotCodec.readString(input)
|
||||
val sourceRoots = readStrings(input)
|
||||
val excludedRoots = readStrings(input)
|
||||
val withHidden = LibrarySnapshotCodec.readBoolean(input)
|
||||
val excludeNonMusic = LibrarySnapshotCodec.readBoolean(input)
|
||||
val intelligentSorting = LibrarySnapshotCodec.readBoolean(input)
|
||||
val separators = LibrarySnapshotCodec.readString(input)
|
||||
val coverMode = LibrarySnapshotCodec.readString(input)
|
||||
return LibrarySnapshotFingerprint(
|
||||
sourceMode,
|
||||
sourceRoots,
|
||||
excludedRoots,
|
||||
withHidden,
|
||||
excludeNonMusic,
|
||||
intelligentSorting,
|
||||
separators,
|
||||
coverMode,
|
||||
)
|
||||
}
|
||||
|
||||
private fun writeStrings(output: OutputStream, values: List<String>) {
|
||||
LibrarySnapshotCodec.writeCount(output, values.size, MAX_ROOT_IDENTITIES)
|
||||
values.forEach { LibrarySnapshotCodec.writeString(output, it) }
|
||||
}
|
||||
|
||||
private fun readStrings(input: InputStream): List<String> {
|
||||
return List(LibrarySnapshotCodec.readCount(input, MAX_ROOT_IDENTITIES)) {
|
||||
LibrarySnapshotCodec.readString(input)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,20 @@ import org.robolectric.annotation.Config
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [30])
|
||||
class LibrarySnapshotDocumentTest {
|
||||
private companion object {
|
||||
val TEST_FINGERPRINT =
|
||||
LibrarySnapshotFingerprint(
|
||||
"TEST",
|
||||
emptyList(),
|
||||
emptyList(),
|
||||
withHidden = false,
|
||||
excludeNonMusic = false,
|
||||
intelligentSorting = true,
|
||||
separators = "",
|
||||
coverMode = "AS_IS",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun document_roundTrip_preservesCompleteGraphAndLivePlaylist() {
|
||||
val storedCoverDirectory =
|
||||
@@ -111,11 +125,12 @@ class LibrarySnapshotDocumentTest {
|
||||
}
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotDocument.write(output, listOf(first, second, third))
|
||||
LibrarySnapshotDocument.write(output, listOf(first, second, third), TEST_FINGERPRINT)
|
||||
val restored =
|
||||
runBlocking {
|
||||
LibrarySnapshotDocument.read(
|
||||
ByteArrayInputStream(output.toByteArray()),
|
||||
TEST_FINGERPRINT,
|
||||
storedCoverDirectory,
|
||||
context,
|
||||
storedPlaylists,
|
||||
@@ -146,6 +161,7 @@ class LibrarySnapshotDocumentTest {
|
||||
for (count in listOf(-1, 100_001)) {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeHeader(output)
|
||||
LibrarySnapshotFingerprintCodec.write(output, TEST_FINGERPRINT)
|
||||
LibrarySnapshotCodec.writeInt(output, count)
|
||||
|
||||
val error =
|
||||
@@ -170,7 +186,7 @@ class LibrarySnapshotDocumentTest {
|
||||
val storedPlaylists = mockk<StoredPlaylists>()
|
||||
coEvery { storedPlaylists.read() } returns emptyList()
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotDocument.write(output, listOf(song))
|
||||
LibrarySnapshotDocument.write(output, listOf(song), TEST_FINGERPRINT)
|
||||
val bytes = output.toByteArray()
|
||||
val restored =
|
||||
readDocument(
|
||||
@@ -232,6 +248,7 @@ class LibrarySnapshotDocumentTest {
|
||||
) = runBlocking {
|
||||
LibrarySnapshotDocument.read(
|
||||
input,
|
||||
TEST_FINGERPRINT,
|
||||
storedCoverDirectory,
|
||||
RuntimeEnvironment.getApplication(),
|
||||
storedPlaylists,
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Auxio Project
|
||||
* LibrarySnapshotFingerprintTest.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.musikr.snapshot
|
||||
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.mockk
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.nio.file.Files
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.playlist.interpret.PlaylistInterpreter
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [30])
|
||||
class LibrarySnapshotFingerprintTest {
|
||||
@Test
|
||||
fun matchingFingerprint_documentRestoresSuccessfully() {
|
||||
val directory = Files.createTempDirectory("auxio-fingerprint-match").toFile().apply { deleteOnExit() }
|
||||
val fingerprint = fingerprint()
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotDocument.write(output, emptyList(), fingerprint)
|
||||
|
||||
val restored = readDocument(output.toByteArray(), fingerprint, directory)
|
||||
|
||||
assertTrue(restored.empty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sourceModeOrRootChange_isRejectedAsFingerprintMismatch() {
|
||||
val directory = Files.createTempDirectory("auxio-fingerprint-source").toFile().apply { deleteOnExit() }
|
||||
val stored = fingerprint()
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotDocument.write(output, emptyList(), stored)
|
||||
val bytes = output.toByteArray()
|
||||
val changedConfigurations =
|
||||
listOf(
|
||||
stored.copy(sourceMode = "MEDIA_STORE"),
|
||||
stored.copy(sourceRoots = listOf("content://tree/Other")),
|
||||
stored.copy(excludedRoots = listOf("content://tree/Development/Music/Other")),
|
||||
)
|
||||
|
||||
for (expected in changedConfigurations) {
|
||||
val error =
|
||||
assertThrows(IOException::class.java) {
|
||||
readDocument(bytes, expected, directory)
|
||||
}
|
||||
assertEquals("Snapshot fingerprint mismatch", error.message)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sortingOrSeparatorChange_isRejectedAsFingerprintMismatch() {
|
||||
val directory = Files.createTempDirectory("auxio-fingerprint-names").toFile().apply { deleteOnExit() }
|
||||
val stored = fingerprint()
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotDocument.write(output, emptyList(), stored)
|
||||
val bytes = output.toByteArray()
|
||||
val changedConfigurations =
|
||||
listOf(
|
||||
stored.copy(intelligentSorting = false),
|
||||
stored.copy(separators = ";&"),
|
||||
)
|
||||
|
||||
for (expected in changedConfigurations) {
|
||||
val error =
|
||||
assertThrows(IOException::class.java) {
|
||||
readDocument(bytes, expected, directory)
|
||||
}
|
||||
assertEquals("Snapshot fingerprint mismatch", error.message)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun coverModeChange_isRejectedAsFingerprintMismatch() {
|
||||
val directory = Files.createTempDirectory("auxio-fingerprint-covers").toFile().apply { deleteOnExit() }
|
||||
val stored = fingerprint()
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotDocument.write(output, emptyList(), stored)
|
||||
val bytes = output.toByteArray()
|
||||
val changedModes = listOf("OFF", "SAVE_SPACE", "HIGH_QUALITY", "AS_IS")
|
||||
|
||||
for (mode in changedModes) {
|
||||
val error =
|
||||
assertThrows(IOException::class.java) {
|
||||
readDocument(bytes, stored.copy(coverMode = mode), directory)
|
||||
}
|
||||
assertEquals("Snapshot fingerprint mismatch", error.message)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mismatchedFingerprint_storeFallsBackAndPreservesUnrelatedCache() {
|
||||
val root = Files.createTempDirectory("auxio-fingerprint-store").toFile().apply { deleteOnExit() }
|
||||
val directory = File(root, "snapshots")
|
||||
val cache = File(root, "tag-cache.db").apply { writeText("preserve-tags") }
|
||||
val stored = fingerprint()
|
||||
val store = LibrarySnapshotStore(directory)
|
||||
store.replace(
|
||||
write = { LibrarySnapshotDocument.write(it, emptyList(), stored) },
|
||||
verify = { readStream(it, stored, directory) },
|
||||
)
|
||||
|
||||
val invalid =
|
||||
store.load {
|
||||
readStream(it, stored.copy(sourceMode = "MEDIA_STORE"), directory)
|
||||
}
|
||||
val valid = store.load { readStream(it, stored, directory) }
|
||||
|
||||
assertNull(invalid)
|
||||
assertEquals("preserve-tags", cache.readText())
|
||||
assertTrue(valid?.empty() == true)
|
||||
assertEquals("preserve-tags", cache.readText())
|
||||
}
|
||||
|
||||
private fun readDocument(
|
||||
bytes: ByteArray,
|
||||
expectedFingerprint: LibrarySnapshotFingerprint,
|
||||
directory: File,
|
||||
) = readStream(ByteArrayInputStream(bytes), expectedFingerprint, directory)
|
||||
|
||||
private fun readStream(
|
||||
input: InputStream,
|
||||
expectedFingerprint: LibrarySnapshotFingerprint,
|
||||
directory: File,
|
||||
) = runBlocking {
|
||||
val storedPlaylists = mockk<StoredPlaylists>()
|
||||
coEvery { storedPlaylists.read() } returns emptyList()
|
||||
LibrarySnapshotDocument.read(
|
||||
input,
|
||||
expectedFingerprint,
|
||||
directory,
|
||||
RuntimeEnvironment.getApplication(),
|
||||
storedPlaylists,
|
||||
mockk<PlaylistInterpreter>(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun fingerprint() =
|
||||
LibrarySnapshotFingerprint(
|
||||
sourceMode = "SAF",
|
||||
sourceRoots = listOf("content://tree/Development/Music"),
|
||||
excludedRoots = listOf("content://tree/Development/Music/Excluded"),
|
||||
withHidden = true,
|
||||
excludeNonMusic = false,
|
||||
intelligentSorting = true,
|
||||
separators = ",;",
|
||||
coverMode = "BALANCED",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user