Skip to content

Commit

Permalink
Closes mozilla-mobile#10157 Remove any data urls from the download db
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Apr 28, 2021
1 parent c428c50 commit 56fa896
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 56fa896

Please sign in to comment.