From 56fa89637875c2437579af541706a0ed7e1cb635 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Wed, 28 Apr 2021 14:27:11 -0400 Subject: [PATCH] Closes #10157 Remove any data urls from the download db --- .../downloads/OnDeviceDownloadStorageTest.kt | 37 +++++++++++++++++++ .../feature/downloads/db/DownloadsDatabase.kt | 12 +++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/components/feature/downloads/src/androidTest/java/mozilla/components/feature/downloads/OnDeviceDownloadStorageTest.kt b/components/feature/downloads/src/androidTest/java/mozilla/components/feature/downloads/OnDeviceDownloadStorageTest.kt index 4737a33b2f9..739f15e31c3 100644 --- a/components/feature/downloads/src/androidTest/java/mozilla/components/feature/downloads/OnDeviceDownloadStorageTest.kt +++ b/components/feature/downloads/src/androidTest/java/mozilla/components/feature/downloads/OnDeviceDownloadStorageTest.kt @@ -126,6 +126,43 @@ class OnDeviceDownloadStorageTest { } } + @Test + fun migrate3to4() { + helper.createDatabase(MIGRATION_TEST_DB, 3).apply { + // A data url download + execSQL( + "INSERT INTO " + + "downloads " + + "(id, url, file_name, content_type,content_length,status,destination_directory,created_at) " + + "VALUES " + + "(1,'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==','file_name','content_type',1,1,'destination_directory',1)" + ) + // A normal url download + execSQL( + "INSERT INTO " + + "downloads " + + "(id, url, file_name, content_type,content_length,status,destination_directory,created_at) " + + "VALUES " + + "(2,'url','file_name','content_type',1,1,'destination_directory',1)" + ) + } + + val dbVersion4 = helper.runMigrationsAndValidate(MIGRATION_TEST_DB, 4, true, Migrations.migration_3_4) + + dbVersion4.query("SELECT * FROM downloads").use { cursor -> + assertEquals(2, cursor.count) + + cursor.moveToFirst() + // Data url must be removed from download 1. + assertEquals("", cursor.getString(cursor.getColumnIndexOrThrow("url"))) + + cursor.moveToNext() + + // The download 1 must keep its url. + assertEquals("url", cursor.getString(cursor.getColumnIndexOrThrow("url"))) + } + } + @Test fun testAddingDownload() = runBlockingTest { val download1 = createMockDownload("1", "url1") diff --git a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/db/DownloadsDatabase.kt b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/db/DownloadsDatabase.kt index 840891793bb..0aa6f399c12 100644 --- a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/db/DownloadsDatabase.kt +++ b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/db/DownloadsDatabase.kt @@ -17,7 +17,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase /** * Internal database for saving downloads. */ -@Database(entities = [DownloadEntity::class], version = 3) +@Database(entities = [DownloadEntity::class], version = 4) @TypeConverters(StatusConverter::class) internal abstract class DownloadsDatabase : RoomDatabase() { abstract fun downloadDao(): DownloadDao @@ -36,7 +36,8 @@ internal abstract class DownloadsDatabase : RoomDatabase() { "mozac_downloads_database" ).addMigrations( Migrations.migration_1_2, - Migrations.migration_2_3 + Migrations.migration_2_3, + Migrations.migration_3_4 ).build().also { instance = it } @@ -64,6 +65,13 @@ internal object Migrations { database.execSQL("ALTER TABLE temp_downloads RENAME TO downloads") } } + + val migration_3_4 = object : Migration(3, 4) { + override fun migrate(database: SupportSQLiteDatabase) { + // Clear any data urls. + database.execSQL("UPDATE downloads SET url='' WHERE url LIKE 'data:%' ") + } + } } @Suppress("unused")