Improve store install storage checks and recovery; adaptive parallel Epic installs (#710)

Space checks before Epic, GOG and Amazon installs (measured zero vs unknown), clear failure reasons with retry/clear, resume-aware checks, and safe per-file replacement so a failed or cancelled update never breaks an installed game.

Epic installs stream chunks straight into staged files in parallel (most bytes written once, a small budgeted cache for shared chunks, about 1x game size instead of 2x), pace updates by free space, and keep the lookahead, batch and bounded Java writers as hidden alternatives.

Co-authored-by: Juan Marti Mercado <juanma@microsoft.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
This commit is contained in:
Juan MartíandThe412Banner authored and GitHub committed 2026-10-13 22:29:23 -04:00
1 parent 34969ccf44
commit 0d64ad8c23
34 files changed
+6777 -233

No files matched your search

@@ -11,6 +11,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
@@ -55,4 +56,87 @@ class StoreInstallRootTest {
assertTrue(dir.absolutePath.startsWith(app.cacheDir.absolutePath))
assertEquals("a_b_c", dir.name)
}
@Test fun estimatedBytesNeededUsesKnownInstallSizeForAllStores() {
val item = CatalogItem(Store.EPIC, "game1", "Game 1", imageUrl = null, owned = true, sizeBytes = 10_000_000L)
assertEquals(10_000_000L, StoreInstallRoot.estimatedBytesNeeded(item))
val gogItem = CatalogItem(Store.GOG, "game2", "Game 2", imageUrl = null, owned = true, sizeBytes = 10_000_000L)
assertEquals(10_000_000L, StoreInstallRoot.estimatedBytesNeeded(gogItem))
}
@Test fun checkStorageReportsEnoughWhenSpaceIsUnknownOrAvailable() {
val zeroItem = CatalogItem(Store.EPIC, "game1", "Game 1", imageUrl = null, owned = true, sizeBytes = 0L)
val check = StoreInstallRoot.checkStorage(app, zeroItem, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
}
@Test fun availableBytesUsesNearestExistingAncestorWithoutCreatingDirectories() {
val existing = File(app.filesDir, "storage-probe").apply { mkdirs() }
val missing = File(existing, "not-created/another-level/game")
ShadowStatFs.registerStats(existing.path, 1024, 100, 100)
assertTrue(StoreInstallRoot.availableBytes(missing) > 0L)
assertFalse(File(existing, "not-created").exists())
}
@Test fun epicDefersFullSizeChecksToItsManifestDeltaForResume() {
val item = CatalogItem(Store.EPIC, "large", "Large", imageUrl = null, owned = true, sizeBytes = Long.MAX_VALUE)
val check = StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
assertEquals(Long.MAX_VALUE, check.requiredBytes)
}
@Test fun gogAndAmazonResumeChecksUseTheExistingVolumeAndDeferRemainingWork() {
val card = File(app.filesDir, "resume-card").apply { mkdirs() }
SessionPrefs.setGameStorage(app, card.path, "Card")
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "resume-${store.id}", "Partial ${store.id}", imageUrl = null, sizeBytes = Long.MAX_VALUE)
val folder = File(StoreInstallRoot.storeDir(File(card, "Games"), store), StoreInstallRoot.folderName(item.title, item.id)).apply { mkdirs() }
StoreInstalls.begin(folder, store, item.id, item.title, null, null)
val check = StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
assertEquals(StoreInstallRoot.availableBytes(folder), check.availableBytes)
assertEquals(folder, StoreInstallRoot.folderFor(app, store, item.id, item.title))
}
}
@Test fun freshGogAndAmazonStillRejectInsufficientCatalogSpace() {
StoreInstallRoot.internalRoot(app).mkdirs()
ShadowStatFs.registerStats(StoreInstallRoot.internalRoot(app).path, 1024, 100, 100)
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "too-large-${store.id}", "Too large ${store.id}", imageUrl = null, sizeBytes = Long.MAX_VALUE)
assertFalse(StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app)).enough)
}
}
@Test fun measuredZeroRejectsFreshGogAndAmazonRatherThanBeingUnknown() {
val root = File(app.filesDir, "zero-space").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val missing = File(root, "not-created/game")
assertEquals(0L, StoreInstallRoot.availableBytes(missing))
assertFalse(File(root, "not-created").exists())
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "zero-${store.id}", "Zero ${store.id}", imageUrl = null, sizeBytes = 1L)
val check = StoreInstallRoot.checkStorage(app, item, missing)
assertFalse(check.enough)
assertEquals(0L, check.availableBytes)
}
}
@Test fun failedProbesAreUnknownAndDoNotCreateOrWriteToTargets() {
val root = File(app.filesDir, "read-only-probe").apply { mkdirs() }
val missing = File(root, "not-created/game")
val fail: (File) -> Long = { throw java.io.IOException("unavailable") }
assertEquals(-1L, StoreInstallRoot.probeAvailableBytes(missing, fail, fail))
assertEquals(-1L, StoreInstallRoot.probeAvailableBytes(missing, fail, { 0L }))
assertEquals(42L, StoreInstallRoot.probeAvailableBytes(missing, fail, { 42L }))
assertEquals(0L, StoreInstallRoot.probeAvailableBytes(missing, { 0L }, fail))
assertFalse(File(root, "not-created").exists())
assertTrue(root.listFiles().orEmpty().isEmpty())
assertFalse(StoreInstallRoot.insufficientSpace(1L, -1L))
assertTrue(StoreInstallRoot.insufficientSpace(1L, 0L))
assertFalse(StoreInstallRoot.insufficientSpace(1L, 1L))
assertFalse(StoreInstallRoot.insufficientSpace(0L, 0L))
}
}
@@ -0,0 +1,43 @@
package com.droiddeck.launcher.stores.amazon
import java.io.File
import java.security.MessageDigest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
import com.droiddeck.launcher.stores.StoreInstallRoot
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class AmazonResumeSpaceTest {
@Test fun remainingWorkRejectsMeasuredZeroButNotUnknownSpace() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "amazon-zero").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val files = listOf(AmazonManifest.ManifestFile("missing", 4, 0, null))
val needed = AmazonDownloadManager.remainingInstallBytes(files, root)
assertEquals(4L, needed)
assertTrue(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.availableBytes(root)))
assertFalse(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.SPACE_UNKNOWN))
}
@Test fun remainingSpaceExcludesVerifiedFilesButIncludesCorruptAndPartialFiles() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "amazon-resume").apply { mkdirs() }
val data = byteArrayOf(1, 2, 3, 4)
val hash = MessageDigest.getInstance("SHA-256").digest(data)
val files = listOf("good", "corrupt", "partial", "missing").map {
AmazonManifest.ManifestFile(it, 4, 0, hash)
}
File(root, "good").writeBytes(data)
File(root, "corrupt").writeBytes(ByteArray(4))
File(root, "partial").writeBytes(byteArrayOf(1))
assertEquals(12L, AmazonDownloadManager.remainingInstallBytes(files, root))
for (file in files) File(root, file.unixPath()).writeBytes(data)
assertEquals(0L, AmazonDownloadManager.remainingInstallBytes(files, root))
}
}
@@ -0,0 +1,140 @@
package com.droiddeck.launcher.stores.download
import android.content.Context
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.stores.*
import java.io.File
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.json.JSONArray
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class DownloadQueueRetryTest {
private val app get() = RuntimeEnvironment.getApplication()
@After fun cleanRows() { DownloadQueue.dismissFinished(); StoreSizes.known.clear() }
private fun awaitFailure(key: String, error: String) {
val until = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)
while (System.nanoTime() < until) {
val entry = DownloadQueue.entry(key)
if (entry?.state == DownloadState.FAILED && entry.error == error) return
Thread.sleep(10)
}
fail("Expected failed real job: ${DownloadQueue.entry(key)}")
}
@Test fun storageRetryRechecksThenRunsTheRealBackendAndNormalRetryStillWorks() {
StoreInstallRoot.internalRoot(app).mkdirs()
ShadowStatFs.registerStats(StoreInstallRoot.internalRoot(app).path, 1024, 100, 100)
val item = CatalogItem(Store.GOG, "preflight-retry", "Preflight retry", imageUrl = null, sizeBytes = Long.MAX_VALUE)
val runs = AtomicInteger()
var lastTarget: File? = null
val previous = StoresState.backend(Store.GOG)
val backend = object : StoreBackend {
override val store = Store.GOG
override fun signIn(context: Context) {}
override fun signOut(context: Context) {}
override fun syncLibrary(context: Context, force: Boolean) {}
override fun loadShelves(context: Context, force: Boolean) {}
override fun uninstall(context: Context, game: InstalledStoreGame) {}
override fun install(context: Context, item: CatalogItem, root: File) {
lastTarget = root
DownloadQueue.enqueue(context, DownloadEntry(item.store, item.id, item.title)) {
object : DownloadQueue.DownloadJob {
override fun run(handle: DownloadQueue.JobHandle): String? {
throw IllegalStateException("backend-${runs.incrementAndGet()}")
}
override fun cancel(deleteFiles: Boolean) {}
}
}
}
}
StoresState.register(backend)
try {
StoresState.install(app, item)
assertEquals(DownloadState.FAILED, DownloadQueue.entry(item.key)?.state)
DownloadQueue.retry(app, item.key)
assertEquals(DownloadState.FAILED, DownloadQueue.entry(item.key)?.state)
assertEquals(0, runs.get())
StoreSizes.known[item.key] = 1
DownloadQueue.retry(app, item.key)
awaitFailure(item.key, "backend-1")
DownloadQueue.retry(app, item.key)
awaitFailure(item.key, "backend-2")
DownloadQueue.clear(app, item.key)
assertNull(DownloadQueue.entry(item.key))
// A card resume (including the restored paused path) must retain its real target.
val card = File(app.filesDir, "retry-card").apply { mkdirs() }
SessionPrefs.setGameStorage(app, card.path, "Card")
val target = File(card, "Games")
val folder = File(StoreInstallRoot.storeDir(target, item.store), StoreInstallRoot.folderName(item.title, item.id)).apply { mkdirs() }
StoreInstalls.begin(folder, item.store, item.id, item.title, null, null)
StoreSizes.known[item.key] = Long.MAX_VALUE
StoresState.unfinished = StoreInstallRoot.unfinished(app)
DownloadQueue.holdPaused(item.key)
StoresState.requestInstall(app, item)
assertEquals(target, lastTarget)
assertEquals(DownloadState.PAUSED, DownloadQueue.entry(item.key)?.state)
assertEquals(2, runs.get())
val saved = File(app.filesDir, "store-queue.json")
val until = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)
var correctSavedRoot = false
while (!correctSavedRoot && System.nanoTime() < until) {
correctSavedRoot = runCatching {
val rows = JSONArray(saved.readText())
(0 until rows.length()).any { i ->
val row = rows.getJSONObject(i)
row.getJSONObject("item").getString("id") == item.id &&
row.getString("root") == target.absolutePath && row.getBoolean("paused")
}
}.getOrDefault(false)
if (!correctSavedRoot) Thread.sleep(10)
}
assertTrue("Paused saved queue must remember the existing card root", correctSavedRoot)
DownloadQueue.clear(app, item.key)
} finally {
if (previous != null) StoresState.register(previous)
SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "")
StoresState.unfinished = emptyList()
}
}
@Test fun preflightClearRemovesTheRowWithoutRunningItsRetry() {
val entry = DownloadEntry(Store.AMAZON, "clear-preflight", "Clear")
DownloadQueue.recordFailed(app, entry, "storage") { fail("Clear must not retry") }
DownloadQueue.clear(app, entry.key)
assertNull(DownloadQueue.entry(entry.key))
}
@Test fun heldPausedBackendRestoresAndResumesWithoutAPlaceholder() {
val entry = DownloadEntry(Store.GOG, "restored-paused", "Paused")
val ran = CountDownLatch(1)
DownloadQueue.holdPaused(entry.key)
DownloadQueue.enqueue(app, entry) {
object : DownloadQueue.DownloadJob {
override fun run(handle: DownloadQueue.JobHandle): String? {
ran.countDown()
throw IllegalStateException("restored")
}
override fun cancel(deleteFiles: Boolean) {}
}
}
assertEquals(DownloadState.PAUSED, DownloadQueue.entry(entry.key)?.state)
assertEquals(1L, ran.count)
DownloadQueue.resume(app, entry.key)
assertTrue(ran.await(5, TimeUnit.SECONDS))
awaitFailure(entry.key, "restored")
}
}
@@ -0,0 +1,107 @@
package com.droiddeck.launcher.stores.epic
import java.io.File
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicAdaptivePlanTest {
@get:Rule val temp = TemporaryFolder()
private val mib = 1024L * 1024
private val gib = 1024 * mib
private fun file(name: String, size: Long) = EpicDownloadManager.FileInfo().apply {
filename = name
var left = size
while (left > 0) {
val part = minOf(left, Int.MAX_VALUE.toLong())
parts.add(EpicDownloadManager.ChunkPart().apply { this.size = part.toInt() })
left -= part
}
}
@Test fun netPeakCreditsEachOldCopyAtItsSwapAndLargestReplacedIgnoresFreshFiles() {
val files = listOf(file("a", 10 * gib), file("b", 2 * gib), file("c", 4 * gib))
val old = mapOf("a" to 9 * gib, "c" to 5 * gib)
// Running Σ(new − old): +1, +3, +2 GiB.
assertEquals(3 * gib, EpicDownloadManager.settledNetPeak(files, old))
assertEquals(10 * gib, EpicDownloadManager.largestReplacedFile(files, old))
assertEquals(16 * gib, EpicDownloadManager.settledNetPeak(files, emptyMap()))
assertEquals(0L, EpicDownloadManager.largestReplacedFile(files, emptyMap()))
assertEquals(0L, EpicDownloadManager.largestReplacedFile(files, mapOf("b" to 0L)))
}
@Test fun cacheBudgetIsHalfTheHeadroomCappedAndDefaultsWhenSpaceIsUnknown() {
assertEquals(EpicDownloadManager.ADAPTIVE_CACHE_DEFAULT_BYTES, EpicDownloadManager.adaptiveCacheBudget(-1, 0))
assertEquals(EpicDownloadManager.ADAPTIVE_CACHE_MAX_BYTES, EpicDownloadManager.adaptiveCacheBudget(100 * gib, 10 * gib))
assertEquals(0L, EpicDownloadManager.adaptiveCacheBudget(gib, gib))
val free = 1000 * mib
assertEquals((free - 400 * mib - EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES) / 2, EpicDownloadManager.adaptiveCacheBudget(free, 400 * mib))
}
@Test fun freshInstallsHaveNoOverlapLimit() {
val plan = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 30 * gib, true, 10 * gib)
assertNull(plan.error)
assertEquals(-1L, plan.overlapBudget)
assertEquals(20 * gib, plan.gameNeed)
}
@Test fun updateOverlapBudgetIsTheLeftoverAfterNetGrowthAndCache() {
// Card install, cache on internal: the card's leftover after the net growth is the budget.
val roomy = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 20 * gib, true, 10 * gib)
assertNull(roomy.error)
assertFalse(roomy.cacheBesideGame)
assertEquals(19 * gib, roomy.overlapBudget)
assertEquals(20 * gib, roomy.gameNeed)
// Tight: floored at the largest replaced file, one at a time.
val tight = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 5 * gib, true, 10 * gib)
assertNull(tight.error)
assertEquals(4 * gib, tight.overlapBudget)
// Not even one replaced file fits: the shortfall is named.
val short = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 4 * gib, true, 10 * gib)
assertTrue(short.error!!.contains("game's storage"))
assertTrue(short.error!!.contains("short by 1.0 GB"))
// Unknown free space: one replaced file at a time.
assertEquals(4 * gib, EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, -1, true, -1).overlapBudget)
}
@Test fun cacheBesideTheGameComesOutOfTheOverlapBudget() {
val plan = EpicDownloadManager.planAdaptiveSpace(gib, 2 * gib, 10 * gib, false, -1)
assertNull(plan.error)
assertTrue(plan.cacheBesideGame)
assertTrue(plan.cacheBudget > 0)
assertEquals(10 * gib - gib - plan.cacheBudget - EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES, plan.overlapBudget)
assertEquals(10 * gib, plan.gameNeed)
}
@Test fun fullInternalStorageMovesTheCacheBesideTheGameInsteadOfFailing() {
val plan = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 30 * gib, true, 100 * mib)
assertNull(plan.error)
assertTrue(plan.cacheBesideGame)
assertEquals(0L, plan.scratchNeed)
assertTrue(plan.cacheBudget > 0)
assertEquals(20 * gib + plan.cacheBudget + EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES, plan.gameNeed)
}
@Test fun ownedStagingIsRemovedOnlyWhenItHoldsJustItsMarker() {
val root = temp.newFolder("game")
val staging = EpicDownloadManager.recoverOwnedStaging(root, emptyList())
File(staging, "keep.stage").writeText("x")
EpicDownloadManager.releaseOwnedStaging(root)
assertTrue(File(staging, "owner").exists())
File(staging, "keep.stage").delete()
EpicDownloadManager.releaseOwnedStaging(root)
assertFalse(staging.exists())
// A directory without the marker is not ours.
staging.mkdir()
File(staging, "owner").writeText("someone else")
EpicDownloadManager.releaseOwnedStaging(root)
assertTrue(staging.exists())
}
}
@@ -0,0 +1,646 @@
package com.droiddeck.launcher.stores.epic
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstalls
import java.io.File
import java.net.InetAddress
import java.net.ServerSocket
import java.net.SocketException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.security.MessageDigest
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.net.Socket
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicBoundedInstallTest {
@get:Rule val temp = TemporaryFolder()
private lateinit var server: ServerSocket
private lateinit var worker: Thread
private lateinit var manifest: EpicDownloadManager.Manifest
private lateinit var root: File
private val requests = AtomicInteger()
private val reject = AtomicBoolean()
private val rejectGuid = AtomicInteger()
private val handlers = Executors.newCachedThreadPool()
private val sockets = ConcurrentHashMap.newKeySet<Socket>()
private var requestBarrier: CountDownLatch? = null
private var deliveryGate: CountDownLatch? = null
private var bytes = ByteArray(256 * 1024) { (it % 251).toByte() }
@Before fun setUp() {
root = temp.newFolder("game")
ShadowStatFs.registerStats(root.path, 1_000_000_000, 1_000_000_000, 1_000_000_000)
server = ServerSocket(0, 8, InetAddress.getByName("127.0.0.1"))
worker = Thread {
while (!server.isClosed) {
val socket = try { server.accept() } catch (e: SocketException) {
if (server.isClosed) break else throw e
}
sockets.add(socket)
handlers.submit {
try {
socket.use {
it.soTimeout = 5000
val reader = it.getInputStream().bufferedReader()
val request = reader.readLine()
while (!reader.readLine().isNullOrEmpty()) { }
requests.incrementAndGet()
requestBarrier?.let { barrier ->
barrier.countDown()
check(barrier.await(5, TimeUnit.SECONDS)) { "Requests did not overlap" }
}
val out = it.getOutputStream()
val rejectedGuid = rejectGuid.get()
if (reject.get() || (rejectedGuid != 0 && request.contains("_%08X".format(rejectedGuid)))) {
out.write("HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray())
} else {
val header = ByteBuffer.allocate(41).order(ByteOrder.LITTLE_ENDIAN)
.putInt(0xB1FE3AA2.toInt()).putInt(1).putInt(41).putInt(bytes.size)
.put(ByteArray(24)).put(0).array()
out.write("HTTP/1.1 200 OK\r\nContent-Length: ${header.size + bytes.size}\r\nConnection: close\r\n\r\n".toByteArray())
out.write(header)
out.flush()
deliveryGate?.await(5, TimeUnit.SECONDS)
out.write(bytes)
}
out.flush()
}
} catch (_: java.io.IOException) {
// Cancellation disconnects an owned request while this fixture serves it.
} finally { sockets.remove(socket) }
}
}
}.apply { start() }
val chunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(1, 0, 0, 0)
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest = EpicDownloadManager.Manifest().apply {
chunkDir = "chunks"
uniqueChunks = arrayListOf(chunk)
cdnUrls = listOf(EpicDownloadManager.CdnUrl("http://127.0.0.1:${server.localPort}", "", ""))
files = arrayListOf(gameFile("game.bin", 0, bytes.size / 2), gameFile("game.bin.part", bytes.size / 2, bytes.size / 2))
}
}
@After fun tearDown() {
deliveryGate?.countDown()
server.close()
worker.join(6000)
sockets.forEach { it.close() }
handlers.shutdownNow()
assertTrue(handlers.awaitTermination(6, TimeUnit.SECONDS))
assertFalse(worker.isAlive)
}
private fun sha(data: ByteArray) = MessageDigest.getInstance("SHA-1").digest(data)
private fun gameFile(name: String, offset: Int, size: Int) = EpicDownloadManager.FileInfo().apply {
filename = name
sha1 = sha(bytes.copyOfRange(offset, offset + size))
parts.add(EpicDownloadManager.ChunkPart().apply {
guid = intArrayOf(1, 0, 0, 0)
this.offset = offset
this.size = size
})
}
private fun install(cancel: AtomicBoolean = AtomicBoolean(), callback: EpicDownloadManager.Callback =
EpicDownloadManager.Callback { _, _ -> }, cache: String = "") =
EpicDownloadManager.installManifest(manifest, root.path, cache, null, cancel, callback)
private fun oldInstall() {
File(root, "game.bin").writeBytes(byteArrayOf(9, 8, 7))
File(root, "game.bin.part").writeBytes(byteArrayOf(6, 5, 4))
StoreGameSidecar(Store.EPIC, "game", "Game", "game.bin", installVersion = "old").write(root)
}
private fun assertOldInstallIntact() {
assertArrayEquals(byteArrayOf(9, 8, 7), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals("old", StoreGameSidecar.read(root)?.installVersion)
assertTrue(StoreGameSidecar.read(root)?.isInstalled == true)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun networkFailureAfterAcceptedPreflightPreservesInstalledFilesAndRecord() {
oldInstall()
reject.set(true)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(requests.get() > 0)
assertOldInstallIntact()
}
@Test fun hashFailureAfterAcceptedPreflightPreservesInstalledFilesAndRecord() {
oldInstall()
manifest.files.first().sha1 = ByteArray(20) { 1 }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertOldInstallIntact()
}
@Test fun cancellationWhileStagingPreservesInstalledFilesAndRecord() {
oldInstall()
val cancel = AtomicBoolean()
assertNull(install(cancel, EpicDownloadManager.Callback { message, _ ->
if (message.startsWith("Downloading chunks (")) cancel.set(true)
}))
assertOldInstallIntact()
}
@Test fun laterFailureRetainsVerifiedSwapsAndMarksMixedUpdateResumable() {
oldInstall()
manifest.files.last().sha1 = ByteArray(20) { 1 }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
val record = StoreGameSidecar.read(root)!!
assertEquals(StoreGameSidecar.STATE_UPDATING, record.state)
assertFalse(record.isInstalled)
assertEquals("", record.installVersion)
StoreInstalls.begin(root, Store.EPIC, "game", "Game", null, null)
assertEquals("game.bin", StoreGameSidecar.read(root)?.exe)
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
}
@Test fun laterNetworkFailurePreservesTheUnreplacedOldFile() {
oldInstall()
val secondChunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(2, 0, 0, 0)
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest.uniqueChunks.add(secondChunk)
manifest.files.last().parts.single().guid = secondChunk.guid
// Prefetch may fail early, but replacement is still ordered by the writer.
rejectGuid.set(2)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun cancellationAfterFirstSwapLeavesAResumableMixedUpdate() {
oldInstall()
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items == 1) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
// Registration, not an individual file swap, restores the installed state.
assertFalse(StoreGameSidecar.read(root)!!.isInstalled)
}
@Test fun atomicRenameFailurePreservesDestinationAndCleansStagingWithoutDeleteFallback() {
val destination = File(root, "game.bin").apply { mkdir() }
File(destination, "old-content").writeText("keep")
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("atomically replace"))
assertEquals("keep", File(destination, "old-content").readText())
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun safeStagingSpaceBoundaryRejectsWithoutReclaimingAllOldOutputs() {
// The first staged half-file must coexist with the shared chunk and BOTH old outputs.
val old = ByteArray(bytes.size / 2) { 9 }
File(root, "game.bin").writeBytes(old)
File(root, "game.bin.part").writeBytes(old)
val peak = bytes.size + old.size
ShadowStatFs.registerStats(root.path, 1024, peak / 4096 - 1, peak / 4096 - 1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals(0, requests.get())
assertArrayEquals(old, File(root, "game.bin").readBytes())
assertArrayEquals(old, File(root, "game.bin.part").readBytes())
ShadowStatFs.registerStats(root.path, 1024, peak / 4096, peak / 4096)
assertNotNull(install())
}
@Test fun zeroSpaceRejectsPendingFilesWhileUnknownSpaceIsNotTreatedAsZero() {
oldInstall()
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertOldInstallIntact()
assertEquals(0, requests.get())
assertTrue(com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(1, 0))
assertFalse(com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(1, -1))
}
@Test fun zeroScratchSpaceRejectsBeforeFetchWithoutTouchingInstalledOutputs() {
oldInstall()
val cache = temp.newFolder("full-scratch")
ShadowStatFs.registerStats(cache.path, 1024, 0, 0)
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache.path) }
assertTrue(error.message.orEmpty().contains("Not enough internal space"))
assertOldInstallIntact()
assertEquals(0, requests.get())
}
@Test fun installsSharedChunksAndPreservesLegitimatePartFilename() {
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
assertEquals(1, requests.get())
assertFalse(File(root, ".chunks").exists())
assertNotNull(install())
assertEquals("Verified files must not be downloaded again", 1, requests.get())
}
private fun oversizedUpdate() {
val file = manifest.files[0]
val part = file.parts[0]
file.parts.clear()
repeat(100_000) {
file.parts.add(EpicDownloadManager.ChunkPart().apply {
guid = part.guid
size = Int.MAX_VALUE
})
}
}
@Test fun insufficientInstallSpacePreservesExistingFilesOnBothCacheLayouts() {
val oldGame = byteArrayOf(9, 8, 7)
val oldPart = byteArrayOf(6, 5, 4)
File(root, "game.bin").writeBytes(oldGame)
File(root, "game.bin.part").writeBytes(oldPart)
oversizedUpdate()
assertTrue(manifest.files[0].fileSize() > root.usableSpace)
for (cache in listOf("", temp.newFolder("scratch-rejected").path)) {
if (cache.isNotEmpty()) ShadowStatFs.registerStats(cache, 1_000_000_000, 1_000_000_000, 1_000_000_000)
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache) }
assertTrue(error.message.orEmpty().contains("Not enough free space"))
assertArrayEquals(oldGame, File(root, "game.bin").readBytes())
assertArrayEquals(oldPart, File(root, "game.bin.part").readBytes())
}
assertEquals(0, requests.get())
}
@Test fun acceptedUpdateReplacesOutdatedFilesWithVerifiedOutputs() {
oldInstall()
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun pauseKeepsCompletedFilesAndResumeReusesSharedCache() {
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items == 1) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertTrue(File(root, "game.bin").exists())
assertFalse(File(root, "game.bin.part").exists())
assertEquals(1, requests.get())
assertNotNull(install())
assertEquals(1, requests.get())
}
@Test fun cancellationDuringChunkDeliveryRemovesIncompleteOutput() {
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {
if (message.startsWith("Downloading chunks (")) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertFalse(File(root, "game.bin").exists())
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
}
@Test fun fileHashFailureIsExplicitAndRetryRepairsIt() {
val expected = manifest.files[0].sha1
manifest.files[0].sha1 = ByteArray(20) { 1 }
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("SHA-1 mismatch"))
assertFalse(File(root, "game.bin").exists())
manifest.files[0].sha1 = expected
assertNotNull(install())
}
@Test fun corruptCacheIsRefetchedAndScratchCacheIsRemovedOnSuccess() {
val cache = temp.newFolder("scratch")
ShadowStatFs.registerStats(cache.path, 1_000_000_000, 1_000_000_000, 1_000_000_000)
File(cache, manifest.uniqueChunks[0].guidStr()).writeBytes(ByteArray(bytes.size))
assertNotNull(install(cache = cache.path))
assertEquals(1, requests.get())
assertFalse(cache.exists())
}
@Test fun networkFailureLeavesNoCompletedFileAndCanBeRetried() {
reject.set(true)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertFalse(File(root, "game.bin").exists())
reject.set(false)
assertNotNull(install())
}
@Test fun boundedProgressNeverReentersDownloadAfterEnteringInstall() {
val events = mutableListOf<String>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {
if (message.startsWith("Downloading chunks")) events.add("download")
if (message.startsWith("Writing")) events.add("install")
}
override fun onBytes(done: Long, total: Long, speedBps: Long) { events.add("download") }
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "install") {
assertEquals(total, done)
assertEquals(itemsTotal, items)
events.add("install")
}
}
}
assertNotNull(install(callback = callback))
val firstInstall = events.indexOf("install")
assertTrue(firstInstall > 0)
assertFalse(events.drop(firstInstall).contains("download"))
}
@Test fun sharedChunksDoNotCompleteProgressBeforeTheRemainingFilesAreWritten() {
val samples = mutableListOf<Pair<Long, Long>>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onBytes(done: Long, total: Long, speedBps: Long) { samples.add(done to total) }
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items < itemsTotal) assertTrue(samples.last().first < samples.last().second)
}
}
assertNotNull(install(callback = callback))
assertEquals(listOf(0L, bytes.size / 2L, bytes.size.toLong()), samples.map { it.first })
assertTrue(samples.all { it.second == bytes.size.toLong() })
}
@Test fun oversizedSharedChunkStaysPinnedAcrossPrefetchedFilesWithoutChangingStages() {
bytes = ByteArray((EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 1024).toInt()) { (it % 251).toByte() }
manifest.uniqueChunks.single().apply {
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest.files = (0..2).map { gameFile("part-$it.bin", it * 1024, 1024) }
val chunk = manifest.uniqueChunks.single()
val estimatedPeak = EpicDownloadManager.estimatePeakAdditionalBytes(
manifest.files,
mapOf(chunk.guidStr() to chunk),
emptyMap(),
true,
)
assertEquals(EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 4 * 1024L, estimatedPeak)
val samples = mutableListOf<Long>()
val stages = mutableListOf<String>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onBytes(done: Long, total: Long, speedBps: Long) {
assertEquals(3072L, total)
assertFalse(stages.contains("install"))
samples.add(done)
}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
stages.add(stage)
if (stage == "stream" && items < itemsTotal) assertTrue(samples.last() < total)
}
}
assertNotNull(install(callback = callback))
assertEquals(1, requests.get())
assertEquals(listOf(0L, 1024L, 2048L, 3072L), samples)
assertEquals("install", stages.last())
repeat(3) { assertArrayEquals(bytes.copyOfRange(it * 1024, (it + 1) * 1024), File(root, "part-$it.bin").readBytes()) }
}
@Test fun hogwartsSizedSequentialPlanFitsAvailableSpaceWithoutFullChunkCache() {
val gib = 1024L * 1024 * 1024
val file = EpicDownloadManager.FileInfo()
val chunks = LinkedHashMap<String, EpicDownloadManager.ChunkInfo>()
repeat(72) { index ->
val chunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(index + 1, 0, 0, 0)
windowSize = gib.toInt()
}
chunks[chunk.guidStr()] = chunk
file.parts.add(EpicDownloadManager.ChunkPart().apply {
guid = chunk.guid
size = chunk.windowSize
})
}
val peak = EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), true)
assertEquals(73 * gib, peak)
assertTrue(peak < 96 * gib)
}
@Test fun plannersReclaimEachOldFileOnlyAfterItsOwnVerifiedSwap() {
val chunks = manifest.uniqueChunks.associateBy { it.guidStr() }
val half = bytes.size / 2L
val old = mapOf("game.bin" to half, "game.bin.part" to 100 * half)
assertEquals(bytes.size + half, EpicDownloadManager.estimatePeakAdditionalBytes(manifest.files, chunks, emptyMap(), true, old))
assertEquals(half, EpicDownloadManager.estimateStagedFilePeak(manifest.files, old))
assertEquals(2 * half, EpicDownloadManager.estimateStagedFilePeak(manifest.files, emptyMap()))
assertEquals(bytes.size.toLong(), EpicDownloadManager.estimatePeakAdditionalBytes(manifest.files, chunks, emptyMap(), false, old))
}
private fun distinctFiles(count: Int) {
val template = manifest.uniqueChunks.single()
manifest.uniqueChunks = (1..count).map { id ->
EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(id, 0, 0, 0)
windowSize = template.windowSize
fileSize = template.fileSize
sha1 = template.sha1
}
}
manifest.files = manifest.uniqueChunks.mapIndexed { index, chunk ->
gameFile("file-$index.bin", 0, bytes.size).apply { parts.single().guid = chunk.guid }
}
}
@Test fun prefetchOverlapsEightSinglePartFilesButNeverFetchesTheWholeManifest() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
val cancel = AtomicBoolean()
var sawWindow = false
assertNull(install(cancel, EpicDownloadManager.Callback { message, _ ->
if (message.startsWith("Downloading chunks (")) {
assertEquals(8, requests.get())
val cacheBytes = File(root, ".chunks").listFiles()!!.sumOf { it.length() }
assertTrue(cacheBytes <= 8L * bytes.size)
sawWindow = true
cancel.set(true)
}
}))
assertTrue("An eight-request barrier cannot pass with a sequential downloader", sawWindow)
assertEquals(8, requests.get())
assertTrue(root.walk().none { it.name.endsWith(".stage") || it.name.endsWith(".part") })
}
@Test fun parallelWindowSpaceThresholdIsCheckedBeforeStartingAnyRequest() {
distinctFiles(20)
val cache = temp.newFolder("parallel-scratch")
val peak = EpicDownloadManager.estimatePeakAdditionalBytes(
manifest.files, manifest.uniqueChunks.associateBy { it.guidStr() }, emptyMap(), false,
)
assertEquals(8L * bytes.size, peak)
ShadowStatFs.registerStats(cache.path, 1024, peak.toInt() / 4096 - 1, peak.toInt() / 4096 - 1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache.path) }
assertEquals(0, requests.get())
ShadowStatFs.registerStats(cache.path, 1024, peak.toInt() / 4096, peak.toInt() / 4096)
requestBarrier = CountDownLatch(8)
assertNotNull(install(cache = cache.path))
assertEquals(20, requests.get())
manifest.files.forEach { assertArrayEquals(bytes, File(root, it.filename).readBytes()) }
}
@Test fun cancellationDuringParallelNetworkReadsJoinsWorkersBeforeReturning() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
deliveryGate = CountDownLatch(1)
val cancel = AtomicBoolean()
val outcome = java.util.concurrent.atomic.AtomicReference<Any?>()
val installer = Thread {
try { outcome.set(install(cancel)) } catch (e: Throwable) { outcome.set(e) }
}.apply { start() }
try {
assertTrue(requestBarrier!!.await(5, TimeUnit.SECONDS))
cancel.set(true)
installer.join(10_000)
assertFalse("Installer must disconnect and join all download workers", installer.isAlive)
assertNull(outcome.get())
val cache = File(root, ".chunks")
val snapshot = cache.listFiles()?.associate { it.name to it.length() }.orEmpty()
deliveryGate!!.countDown()
Thread.sleep(150)
assertEquals(snapshot, cache.listFiles()?.associate { it.name to it.length() }.orEmpty())
assertTrue(snapshot.keys.none { it.endsWith(".part") })
assertTrue(root.walk().none { it.name.endsWith(".stage") })
assertEquals(8, requests.get())
} finally {
cancel.set(true)
deliveryGate!!.countDown()
installer.join(10_000)
}
}
@Test fun failedParallelFetchJoinsOtherNetworkReadersBeforeReturning() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
deliveryGate = CountDownLatch(1)
rejectGuid.set(1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals(8, requests.get())
val cache = File(root, ".chunks")
val snapshot = cache.listFiles()?.associate { it.name to it.length() }.orEmpty()
deliveryGate!!.countDown()
Thread.sleep(150)
assertEquals(snapshot, cache.listFiles()?.associate { it.name to it.length() }.orEmpty())
assertTrue(snapshot.keys.none { it.endsWith(".part") })
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun ownedOrphanStagingIsRecoveredBeforeTheSpaceProbeWithoutTouchingManifestFiles() {
val staging = EpicDownloadManager.recoverOwnedStaging(root, manifest.files)
File(staging, "native-123-0.stage").writeBytes(ByteArray(bytes.size))
val legitimate = File(root, ".droiddeck-epic-old.stage").apply { writeText("keep") }
oldInstall()
val peak = 2 * bytes.size
ShadowStatFs.registerStats(root.path, 1024, peak / 4096, peak / 4096)
assertNotNull(install())
assertEquals("keep", legitimate.readText())
// The orphan was recovered, and a finished install removes the then-empty owned namespace.
assertFalse(staging.exists())
}
@Test fun stagingRecoveryRefusesUnownedAndManifestOwnedNamespaces() {
val staging = File(root, EpicDownloadManager.STAGING_DIRECTORY).apply { mkdir() }
val untouched = File(staging, "legitimate.stage").apply { writeText("keep") }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("keep", untouched.readText())
File(staging, "owner").writeText("DroidDeck Epic staging v1\n")
manifest.files.first().filename = "${EpicDownloadManager.STAGING_DIRECTORY}/legitimate.stage"
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("keep", untouched.readText())
assertEquals(0, requests.get())
}
@Test fun loadedNativeAbiFailureIsExplicitAndNeverFallsBackToJavaDownloads() {
val field = com.droiddeck.launcher.stores.StoresNative::class.java.getDeclaredField("loaded")
field.isAccessible = true
field.set(null, true)
try {
oldInstall()
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("Native Epic install failed"))
assertEquals(0, requests.get())
assertOldInstallIntact()
} finally {
com.droiddeck.launcher.stores.StoresNative.forget()
}
}
@Test fun stagingRecoveryRejectsSymlinkNamespaceOwnerAndEntriesWithoutDeletingOutsideData() {
val outside = temp.newFolder("outside-staging")
val externalStage = File(outside, "keep.stage").apply { writeText("keep") }
val externalOwner = File(outside, "owner").apply { writeText("DroidDeck Epic staging v1\n") }
val staging = File(root, EpicDownloadManager.STAGING_DIRECTORY)
try {
java.nio.file.Files.createSymbolicLink(staging.toPath(), outside.toPath())
} catch (e: java.nio.file.FileSystemException) {
org.junit.Assume.assumeNoException("Host does not permit symbolic links", e)
}
try {
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("Unsafe Epic staging"))
assertEquals("keep", externalStage.readText())
java.nio.file.Files.delete(staging.toPath())
staging.mkdir()
java.nio.file.Files.createSymbolicLink(File(staging, "owner").toPath(), externalOwner.toPath())
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
java.nio.file.Files.delete(File(staging, "owner").toPath())
File(staging, "owner").writeText("DroidDeck Epic staging v1\n")
val localStage = File(staging, "local.stage").apply { writeText("local") }
java.nio.file.Files.createSymbolicLink(File(staging, "linked.stage").toPath(), externalStage.toPath())
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("local", localStage.readText())
assertEquals("keep", externalStage.readText())
assertEquals(0, requests.get())
} finally {
if (java.nio.file.Files.isSymbolicLink(staging.toPath())) java.nio.file.Files.delete(staging.toPath())
for (name in listOf("owner", "linked.stage")) {
val entry = File(staging, name).toPath()
if (java.nio.file.Files.isSymbolicLink(entry)) java.nio.file.Files.delete(entry)
}
}
}
}
@@ -1,8 +1,10 @@
package com.droiddeck.launcher.stores.epic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -11,6 +13,140 @@ import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicJsonManifestTest {
@Test
fun boundedInstallPeakReservesParallelIncomingChunksBeforeWriting() {
val first = chunk(1)
val second = chunk(2)
val file = file(part(1, 0, 100), part(2, 0, 100))
val chunks = mapOf(first.guidStr() to first, second.guidStr() to second)
assertEquals(300L, EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), true))
assertEquals(200L, EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), false))
}
@Test
fun sharedCachedChunkIsReclaimedOnlyAfterItsLastPart() {
val shared = chunk(1)
val files = listOf(file(part(1, 0, 60)), file(part(1, 60, 40)))
assertEquals(
100L,
EpicDownloadManager.estimatePeakAdditionalBytes(
files, mapOf(shared.guidStr() to shared), mapOf(shared.guidStr() to 100L), true,
),
)
assertEquals(
200L,
EpicDownloadManager.estimatePeakAdditionalBytes(
files, mapOf(shared.guidStr() to shared), emptyMap(), true,
),
)
}
@Test
fun sharedChunksWithinTheWindowStayPinnedWithinTheCacheCeiling() {
val first = chunk(1).apply { windowSize = 40 * 1024 * 1024 }
val second = chunk(2).apply { windowSize = 40 * 1024 * 1024 }
val files = listOf(
file(part(1, 0, 40 * 1024 * 1024)),
file(part(2, 0, 40 * 1024 * 1024)),
file(part(1, 0, 40 * 1024 * 1024)),
file(part(2, 0, 40 * 1024 * 1024)),
)
val chunks = mapOf(first.guidStr() to first, second.guidStr() to second)
val gamePeak = EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), true)
val cachePeak = EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false)
assertEquals(200L * 1024 * 1024, gamePeak)
assertTrue(cachePeak <= EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 40L * 1024 * 1024)
}
@Test fun scheduleDeduplicatesWindowAndEvictsOnlyUnpinnedReusableChunks() {
val size = 40L * 1024 * 1024
val chunks = (1..12).map { chunk(it).apply { windowSize = size.toInt() } }
.associateBy { it.guidStr() }
val files = (1..12).map { file(part(it, 0, 1)) } + (1..12).map { file(part(it, 0, 1)) }
val schedule = EpicDownloadManager.PrefetchSchedule(files, chunks, emptyMap())
assertEquals(chunks.keys.take(8), schedule.fill())
assertEquals(emptyList<String>(), schedule.fill())
assertEquals(8 * size, schedule.bytes())
assertEquals(emptyList<String>(), schedule.consume())
assertEquals(listOf(chunks.keys.elementAt(8)), schedule.fill())
assertEquals(9 * size, schedule.bytes())
assertEquals(listOf(chunks.keys.first()), schedule.consume())
var refetchedFirst = false
repeat(22) {
val fetched = schedule.fill()
if (chunks.keys.first() in fetched) refetchedFirst = true
assertTrue(schedule.bytes() <= EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 8 * size)
val released = schedule.consume()
assertTrue(released.none { it in schedule.window() })
}
assertTrue(refetchedFirst)
assertEquals(0L, schedule.bytes())
assertEquals(9 * size, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false))
}
@Test fun cachedAndOldOutputBytesAreCreditedOnlyWhenTheScheduleReleasesThem() {
val chunks = (1..8).map { chunk(it) }.associateBy { it.guidStr() }
val files = (1..8).map { id -> file(part(id, 0, 100)).apply { filename = "file-$id" } }
val existing = chunks.keys.associateWith { 100L }
val old = files.associate { it.filename to 100L }
assertEquals(800L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false))
assertEquals(0L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, existing, false))
assertEquals(900L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), true, old))
assertEquals(100L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, existing, true, old))
}
@Test
fun resumeCacheDropsCorruptAndUnneededEntriesButKeepsVerifiedChunks() {
val dir = java.io.File(System.getProperty("user.dir"), "build/epic-cache-test-${System.nanoTime()}")
assertTrue(dir.mkdirs())
try {
val bytes = ByteArray(100) { (it % 251).toByte() }
val expectedSha = java.security.MessageDigest.getInstance("SHA-1").digest(bytes)
val valid = chunk(1).apply { sha1 = expectedSha }
val corrupt = chunk(2).apply { sha1 = expectedSha }
java.io.File(dir, valid.guidStr()).writeBytes(bytes)
java.io.File(dir, corrupt.guidStr()).writeBytes(ByteArray(100) { 1 })
java.io.File(dir, "orphan").writeBytes(bytes)
val retained = EpicDownloadManager.prepareBoundedCache(dir, listOf(valid, corrupt))
assertEquals(mapOf(valid.guidStr() to 100L), retained)
assertTrue(java.io.File(dir, valid.guidStr()).exists())
assertFalse(java.io.File(dir, corrupt.guidStr()).exists())
assertFalse(java.io.File(dir, "orphan").exists())
} finally {
EpicDownloadManager.deleteDir(dir)
}
}
@Test
fun referenceCountsIncludeRepeatedPartsSoCancelledFilesCanBeReplanned() {
val pending = listOf(file(part(1, 0, 20), part(1, 20, 20)), file(part(1, 40, 60)))
assertEquals(3, EpicDownloadManager.chunkReferences(pending)["00000001000000000000000000000000"])
}
private fun chunk(id: Int) = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(id, 0, 0, 0)
windowSize = 100
fileSize = 100
}
private fun part(id: Int, offset: Int, size: Int) = EpicDownloadManager.ChunkPart().apply {
guid = intArrayOf(id, 0, 0, 0)
this.offset = offset
this.size = size
}
private fun file(vararg parts: EpicDownloadManager.ChunkPart) = EpicDownloadManager.FileInfo().apply {
filename = "test.bin"
this.parts.addAll(parts)
}
@Test
fun blobNumbersAreDecimalBytesLeastSignificantFirst() {
assertEquals(1L, EpicDownloadManager.blobToNum("001000000000"))
@@ -0,0 +1,43 @@
package com.droiddeck.launcher.stores.gog
import java.io.File
import java.security.MessageDigest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
import com.droiddeck.launcher.stores.StoreInstallRoot
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class GogResumeSpaceTest {
@Test fun remainingWorkRejectsMeasuredZeroButNotUnknownSpace() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-zero").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val files = listOf(GogDownloadManager.DepotFile("missing").apply { totalSize = 4 })
val needed = GogDownloadManager.remainingInstallBytes(files, root)
assertEquals(4L, needed)
assertTrue(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.availableBytes(root)))
assertFalse(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.SPACE_UNKNOWN))
}
@Test fun remainingSpaceExcludesVerifiedFilesButIncludesCorruptAndPartialFiles() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-resume").apply { mkdirs() }
val data = byteArrayOf(1, 2, 3, 4)
val hash = MessageDigest.getInstance("MD5").digest(data).joinToString("") { "%02x".format(it) }
val files = listOf("good", "corrupt", "partial", "missing").map {
GogDownloadManager.DepotFile(it).apply { totalSize = 4; md5 = hash }
}
File(root, "good").writeBytes(data)
File(root, "corrupt").writeBytes(ByteArray(4))
File(root, "partial").writeBytes(byteArrayOf(1))
assertEquals(12L, GogDownloadManager.remainingInstallBytes(files, root))
for (file in files) File(root, file.relativePath).writeBytes(data)
assertEquals(0L, GogDownloadManager.remainingInstallBytes(files, root))
}
}