1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-12-02 06:28:06 +00:00

Use JSON for settings imports/exports

This commit is contained in:
Stypox
2024-03-27 12:31:16 +01:00
parent 6afdbd6fd3
commit d8423499dc
8 changed files with 292 additions and 164 deletions

View File

@@ -1,6 +1,7 @@
package org.schabi.newpipe.settings
import android.content.SharedPreferences
import com.grack.nanojson.JsonParser
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
@@ -18,6 +19,7 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.Mockito.withSettings
import org.mockito.junit.MockitoJUnitRunner
import org.schabi.newpipe.settings.export.BackupFileLocator
import org.schabi.newpipe.settings.export.ImportExportManager
import org.schabi.newpipe.streams.io.StoredFileHelper
import us.shandian.giga.io.FileStream
@@ -33,21 +35,19 @@ class ImportExportManagerTest {
private val classloader = ImportExportManager::class.java.classLoader!!
}
private lateinit var fileLocator: NewPipeFileLocator
private lateinit var fileLocator: BackupFileLocator
private lateinit var storedFileHelper: StoredFileHelper
@Before
fun setupFileLocator() {
fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
fileLocator = Mockito.mock(BackupFileLocator::class.java, withSettings().stubOnly())
storedFileHelper = Mockito.mock(StoredFileHelper::class.java, withSettings().stubOnly())
}
@Test
fun `The settings must be exported successfully in the correct format`() {
val db = File(classloader.getResource("settings/newpipe.db")!!.file)
val newpipeSettings = File.createTempFile("newpipe_", "")
`when`(fileLocator.db).thenReturn(db)
`when`(fileLocator.settings).thenReturn(newpipeSettings)
val expectedPreferences = mapOf("such pref" to "much wow")
val sharedPreferences =
@@ -60,7 +60,7 @@ class ImportExportManagerTest {
val zipFile = ZipFile(output)
val entries = zipFile.entries().toList()
assertEquals(2, entries.size)
assertEquals(3, entries.size)
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual ->
db.inputStream().use { expected ->
@@ -72,26 +72,11 @@ class ImportExportManagerTest {
val actualPreferences = ObjectInputStream(actual).readObject()
assertEquals(expectedPreferences, actualPreferences)
}
}
@Test
fun `Settings file must be deleted`() {
val settings = File.createTempFile("newpipe_", "")
`when`(fileLocator.settings).thenReturn(settings)
ImportExportManager(fileLocator).deleteSettingsFile()
assertFalse(settings.exists())
}
@Test
fun `Deleting settings file must do nothing if none exist`() {
val settings = File("non_existent")
`when`(fileLocator.settings).thenReturn(settings)
ImportExportManager(fileLocator).deleteSettingsFile()
assertFalse(settings.exists())
zipFile.getInputStream(entries.first { it.name == "preferences.json" }).use { actual ->
val actualPreferences = JsonParser.`object`().from(actual)
assertEquals(expectedPreferences, actualPreferences)
}
}
@Test
@@ -156,38 +141,29 @@ class ImportExportManagerTest {
@Test
fun `Contains setting must return true if a settings file exists in the zip`() {
val settings = File.createTempFile("newpipe_", "")
`when`(fileLocator.settings).thenReturn(settings)
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)
assertTrue(contains)
assertTrue(ImportExportManager(fileLocator).exportHasSerializedPrefs(storedFileHelper))
}
@Test
fun `Contains setting must return false if a no settings file exists in the zip`() {
val settings = File.createTempFile("newpipe_", "")
`when`(fileLocator.settings).thenReturn(settings)
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)
assertFalse(contains)
assertFalse(ImportExportManager(fileLocator).exportHasSerializedPrefs(storedFileHelper))
}
@Test
fun `Preferences must be set from the settings file`() {
val settings = File(classloader.getResource("settings/newpipe.settings")!!.path)
`when`(fileLocator.settings).thenReturn(settings)
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
val preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
val editor = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(preferences.edit()).thenReturn(editor)
`when`(editor.commit()).thenReturn(true)
ImportExportManager(fileLocator).loadSharedPreferences(preferences)
ImportExportManager(fileLocator).loadSerializedPrefs(storedFileHelper, preferences)
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
verify(editor, atLeastOnce()).putString(anyString(), anyString())
@@ -196,19 +172,15 @@ class ImportExportManagerTest {
@Test
fun `Importing preferences with a serialization injected class should fail`() {
val settings = File.createTempFile("newpipe_", "")
`when`(fileLocator.settings).thenReturn(settings)
val emptyZip = File(classloader.getResource("settings/vulnerable_serialization.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
Assume.assumeTrue(ImportExportManager(fileLocator).extractSettings(storedFileHelper))
val preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
val editor = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(preferences.edit()).thenReturn(editor)
assertThrows(ClassNotFoundException::class.java) {
ImportExportManager(fileLocator).loadSharedPreferences(preferences)
ImportExportManager(fileLocator).loadSerializedPrefs(storedFileHelper, preferences)
}
}
}