feat: rewrite the Epic and Amazon store modules (#802)

Both store modules are rewritten from a protocol-level spec: new Rust engines behind a small JNI surface (nativeCall/nativeInstall) and thin Kotlin facades, with the launcher's own user agent.

Epic: sign-in carries over, library, manifest download with byte-exact verification, assembly, uninstall, launch arguments, achievements.
Amazon: sign-in carries over, library, manifest download with per-file hash checks, path checks, executable detection from fuel.json, uninstall.
On-disk install layout is unchanged, so existing installs keep working.

Store game pages now take controller focus on their lead hero action (Install, Uninstall, Pause, Resume, Get, Buy), fall back to Back, and keep D-pad focus inside the page while it covers the library.
This commit is contained in:
The412Banner authored and GitHub committed 2026-10-14 07:20:06 -04:00
1 parent a849556f3f
commit 7ad3c60efe
105 files changed
+10975 -13233

No files matched your search

@@ -1,43 +0,0 @@
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))
}
}
@@ -1,107 +0,0 @@
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())
}
}
@@ -1,646 +0,0 @@
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)
}
}
}
}
@@ -0,0 +1,86 @@
package com.droiddeck.launcher.stores.epic
import com.droiddeck.launcher.stores.CredentialCipher
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreAccounts
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.security.SecureRandom
import javax.crypto.spec.SecretKeySpec
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicCredentialStoreTest {
private val app get() = RuntimeEnvironment.getApplication()
private val key = SecretKeySpec(ByteArray(32).also { SecureRandom().nextBytes(it) }, "AES")
@Before fun setUp() { StoreAccounts.keys = CredentialCipher.KeyProvider { key }; StoreAccounts.keystoreFailed = false }
@After fun tearDown() {
StoreAccounts.clear(app, Store.EPIC)
StoreAccounts.keys = CredentialCipher.Keystore; StoreAccounts.keystoreFailed = false
}
private val existing = JSONObject().put("access_token", "AT").put("refresh_token", "RT").put("account_id", "acc")
.put("display_name", "Someone").put("expires_at", System.currentTimeMillis() + 3_600_000L)
@Test fun anExistingSignInIsRead() {
StoreAccounts.write(app, Store.EPIC, existing)
val c = EpicCredentialStore.load(app)!!
assertEquals("AT", c.accessToken)
assertEquals("RT", c.refreshToken)
assertEquals("acc", c.accountId)
assertEquals("Someone", c.displayName)
assertTrue(EpicCredentialStore.isLoggedIn(app))
assertFalse(EpicCredentialStore.expired(app))
assertEquals("AT", EpicCredentialStore.getValidAccessToken(app))
assertEquals("Someone", StoreAccounts.signedInAs(app, Store.EPIC))
}
@Test fun aRefreshAnswerKeepsWhatItOmits() {
StoreAccounts.write(app, Store.EPIC, existing)
EpicCredentialStore.save(app, EpicCredentialStore.TokenResult("AT2", null, null, null, 5L))
val c = EpicCredentialStore.load(app)!!
assertEquals("AT2", c.accessToken)
assertEquals("RT", c.refreshToken)
assertEquals("acc", c.accountId)
assertEquals("Someone", c.displayName)
assertTrue(EpicCredentialStore.expired(app))
val stored = StoreAccounts.read(app, Store.EPIC)!!
for (k in listOf("access_token", "refresh_token", "account_id", "display_name", "expires_at")) assertTrue(k, stored.has(k))
}
@Test fun aNewSignInReplacesTheOldAccount() {
StoreAccounts.write(app, Store.EPIC, existing)
EpicCredentialStore.save(app, EpicCredentialStore.TokenResult("NEW", null, "acc2", "Other", 9L), merge = false)
val c = EpicCredentialStore.load(app)!!
assertNull(c.refreshToken)
assertEquals("acc2", c.accountId)
assertFalse(StoreAccounts.read(app, Store.EPIC)!!.has("refresh_token"))
}
@Test fun signedOutAndUnusableAnswers() {
assertNull(EpicCredentialStore.load(app))
assertNull(EpicCredentialStore.getValidAccessToken(app))
assertFalse(EpicCredentialStore.expired(app))
assertNull(EpicCredentialStore.TokenResult.from(JSONObject().put("error", "x")))
assertNull(EpicCredentialStore.TokenResult.from(JSONObject().put("refresh_token", "x")))
StoreAccounts.write(app, Store.EPIC, JSONObject().put("access_token", "").put("account_id", "a"))
assertNull(EpicCredentialStore.load(app))
}
@Test fun anExpiringTokenWithoutRefreshTokenIsReturnedAsIs() {
StoreAccounts.write(app, Store.EPIC, JSONObject().put("access_token", "OLD").put("expires_at", 1L))
assertEquals("OLD", EpicCredentialStore.getValidAccessToken(app))
assertTrue(EpicCredentialStore.expired(app))
}
}
@@ -1,190 +0,0 @@
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
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"))
assertEquals(0x100L, EpicDownloadManager.blobToNum("000001000000"))
assertEquals(0x0807_0605_0403_0201L, EpicDownloadManager.blobToNum("001002003004005006007008"))
assertEquals(-1L, EpicDownloadManager.blobToNum("255255255255255255255255"))
assertEquals(7L, EpicDownloadManager.blobToNum("7"))
assertEquals(0L, EpicDownloadManager.blobToNum(""))
}
@Test
fun aBlobIsDigitsOnly() {
assertThrows(NumberFormatException::class.java) { EpicDownloadManager.blobToNum("0x1") }
assertThrows(NumberFormatException::class.java) { EpicDownloadManager.blobToNum("-05") }
}
@Test
fun jsonManifestUsesBlobs() {
val json = """{
"ManifestFileVersion": "006000000000",
"LaunchExeString": "Game\\Binaries\\Game.exe",
"ChunkHashList": { "0000000100000002000000030000000F": "001002003004005006007008" },
"DataGroupList": { "0000000100000002000000030000000F": "007" },
"ChunkFilesizeList": { "0000000100000002000000030000000F": "001002000000000000000000" },
"FileManifestList": [
{ "Filename": "a.bin",
"FileChunkParts": [ { "Guid": "0000000100000002000000030000000F", "Offset": "001000000000", "Size": "000000016000" } ] }
]
}"""
val m = EpicDownloadManager.parseManifest(json.toByteArray())
assertNotNull(m)
assertEquals("ChunksV3", m.chunkDir)
assertEquals("Game/Binaries/Game.exe", m.launchExe)
val c = m.uniqueChunks.single()
assertEquals(0x0201L, c.fileSize)
assertEquals("ChunksV3/07/0807060504030201_0000000100000002000000030000000F.chunk", c.getPath(m.chunkDir))
val part = m.files.single().parts.single()
assertEquals(1, part.offset)
assertEquals(1_048_576, part.size)
}
}
@@ -0,0 +1,95 @@
package com.droiddeck.launcher.stores.epic
import com.droiddeck.launcher.stores.Store
import org.json.JSONArray
import org.json.JSONObject
import org.junit.After
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
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicLibraryTest {
private val app get() = RuntimeEnvironment.getApplication()
@After fun tearDown() { EpicPrefs.get(app).edit().clear().commit() }
@Test fun cacheRoundTripsInTheStoredShape() {
val game = EpicGame("Fig", "ns", "cat", "Fig Game", artCover = "tall", artSquare = "wide", developer = "Dev",
description = "Text", installSize = 42L, canRunOffline = false, releaseDate = "2024-01-01", publisher = "Pub")
val text = EpicLibrary.toCacheJson(listOf(game))
val o = JSONArray(text).getJSONObject(0)
for (key in listOf("appName", "namespace", "catalogItemId", "title", "artCover", "artSquare", "developer",
"description", "version", "installSize", "canRunOffline", "releaseDate", "publisher")) assertTrue(key, o.has(key))
assertEquals(listOf(game), EpicLibrary.fromCacheJson(text))
}
@Test fun readingSkipsDedupsSortsAndCapsSizes() {
val text = JSONArray()
.put(JSONObject().put("appName", "b").put("title", "beta").put("installSize", (1L shl 41)))
.put(JSONObject().put("title", "no app name"))
.put(JSONObject().put("appName", "a").put("title", "Alpha"))
.put(JSONObject().put("appName", "b").put("title", "beta again"))
.toString()
val games = EpicLibrary.fromCacheJson(text)
assertEquals(listOf("a", "b"), games.map { it.appName })
assertEquals(0L, games[1].installSize)
assertTrue(games[0].canRunOffline)
assertTrue(EpicLibrary.fromCacheJson("not json").isEmpty())
}
@Test fun catalogItemMapping() {
EpicPrefs.get(app).edit().putLong("size_Fig", 1234L).putString("release_Old", "2001").commit()
val item = EpicLibrary.toCatalogItem(app, EpicGame("Fig", "ns", "cat", "Fig & Co", artCover = "tall", description = "<p>Hi</p>", installSize = 9L))
assertEquals(Store.EPIC, item.store)
assertEquals("Fig", item.id)
assertEquals("tall", item.imageUrl)
assertEquals("tall", item.tallImageUrl)
assertEquals(1234L, item.sizeBytes)
assertEquals("Hi", item.description)
assertTrue(item.owned)
assertEquals("https://store.epicgames.com/en-US/browse?q=Fig+%26+Co", item.storeUrl)
assertEquals(mapOf("namespace" to "ns", "catalogItemId" to "cat", "version" to ""), item.extra)
val old = EpicLibrary.toCatalogItem(app, EpicGame("Old", "ns", "c", "Old", artSquare = "wide", installSize = 9L))
assertEquals("2001", old.releaseDate)
assertEquals(9L, old.sizeBytes)
assertEquals("wide", old.tallImageUrl)
}
@Test fun dlcAreHiddenUnlessEverythingIsDlc() {
val base = EpicGame("base", "ns", "c1", "Base")
val dlc = EpicGame("dlc", "ns", "c2", "Add-on", isDlc = true)
assertEquals(listOf("base"), EpicLibrary.visible(listOf(dlc, base)).map { it.appName })
assertEquals(listOf("dlc"), EpicLibrary.visible(listOf(dlc)).map { it.appName })
}
@Test fun syncedGamesKeepCachedValuesWhenDetailsAreMissing() {
val record = JSONObject().put("appName", "Fig").put("namespace", "ns").put("catalogItemId", "cat")
val before = EpicGame("Fig", "ns", "cat", "Cached Title", artCover = "c", version = "v1", installSize = 7L)
val kept = EpicLibrary.gameFrom(record, null, before)
assertEquals("Cached Title", kept.title)
assertEquals("v1", kept.version)
assertEquals(7L, kept.installSize)
val fresh = EpicLibrary.gameFrom(record, JSONObject().put("title", "New").put("dlc", true).put("canRunOffline", false), before)
assertEquals("New", fresh.title)
assertTrue(fresh.isDlc)
assertFalse(fresh.canRunOffline)
assertEquals("Fig", EpicLibrary.gameFrom(record, null, null).title)
}
@Test fun signOutForgetsOnlyTheLibrary() {
EpicPrefs.get(app).edit().putString(EpicLibrary.KEY_CACHE, "[]").putLong(EpicLibrary.KEY_SYNCED_AT, 1L)
.putString("cloud_template_Fig", "{AppData}/x").commit()
EpicBackend.signOut(app)
val prefs = EpicPrefs.get(app)
assertFalse(prefs.contains(EpicLibrary.KEY_CACHE))
assertFalse(prefs.contains(EpicLibrary.KEY_SYNCED_AT))
assertTrue(prefs.contains("cloud_template_Fig"))
}
}
@@ -0,0 +1,30 @@
package com.droiddeck.launcher.stores.epic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
class EpicSaveTemplateTest {
@Test fun rootsAreCaseInsensitive() {
val p = EpicSaveTemplate.parse("{appdata}\\Game\\Saved", "acc", "app")!!
assertEquals(EpicSaveTemplate.Root.LOCAL_APPDATA, p.root)
assertEquals("/Game/Saved", p.rest)
assertEquals(EpicSaveTemplate.Root.LOCAL_APPDATA, EpicSaveTemplate.parse("{LocalAppData}/x", "a", "b")!!.root)
assertEquals(EpicSaveTemplate.Root.ROAMING_APPDATA, EpicSaveTemplate.parse("{RoamingAppData}/x", "a", "b")!!.root)
assertEquals(EpicSaveTemplate.Root.DOCUMENTS, EpicSaveTemplate.parse("{UserDir}/x", "a", "b")!!.root)
assertEquals(EpicSaveTemplate.Root.SAVED_GAMES, EpicSaveTemplate.parse("{UserSavedGames}/x", "a", "b")!!.root)
assertEquals(EpicSaveTemplate.Root.USER_PROFILE, EpicSaveTemplate.parse("{UserProfile}/x", "a", "b")!!.root)
assertEquals(EpicSaveTemplate.Root.INSTALL_DIR, EpicSaveTemplate.parse("{InstallDir}/x", "a", "b")!!.root)
}
@Test fun placeholdersAreFilledLiterally() {
val p = EpicSaveTemplate.parse("{UserSavedGames}/S/{epicid}/{APPNAME}/x", "a\$1", "B")!!
assertEquals("/S/a\$1/B/x", p.rest)
}
@Test fun unknownOrMissingRootIsNull() {
assertNull(EpicSaveTemplate.parse("{Nope}/x", "a", "b"))
assertNull(EpicSaveTemplate.parse("Saves/x", "a", "b"))
assertNull(EpicSaveTemplate.parse("", "a", "b"))
}
}
@@ -0,0 +1,58 @@
package com.droiddeck.launcher.stores.epic
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreShelves
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicShelvesTest {
private fun item(id: String, items: String, mature: Boolean? = null, tags: List<String> = emptyList()) = JSONObject()
.put("id", id).put("title", "T$id").put("image", "w").put("tall", "t").put("tags", "Action").put("free", false)
.put("hasPrice", true).put("final", "$5").put("orig", "$10").put("disc", 50).put("url", "u").put("dev", "D")
.put("pub", "P").put("rel", "2024").put("desc", "<b>x</b>")
.put("extra", JSONObject().put("namespace", "ns").put("items", items))
.apply { if (mature != null) put("mature", mature) else put("allTags", JSONArray(tags)) }
@Test fun nativeAnswerAndPersistedCopyReadTheSame() {
val answer = JSONObject().put("new", JSONArray().put(item("o1", "c1", tags = listOf("Mature"))))
.put("deals", JSONArray()).put("free", JSONArray()).put("trending", JSONArray().put(item("o2", "c2")))
val shelves = EpicShelves.fromJson(answer.toString())!!
assertEquals(1, shelves.whatsNew.size)
assertTrue(shelves.whatsNew[0].mature)
assertEquals("x", shelves.whatsNew[0].description)
assertEquals(50, shelves.whatsNew[0].discountPercent)
val again = EpicShelves.fromJson(EpicShelves.toJson(shelves))!!
assertEquals(shelves.whatsNew, again.whatsNew)
assertEquals(shelves.trending, again.trending)
}
@Test fun allEmptyIsAbsent() {
val empty = JSONObject().put("new", JSONArray()).put("deals", JSONArray()).put("free", JSONArray()).put("trending", JSONArray())
assertNull(EpicShelves.fromJson(empty.toString()))
assertNull(EpicShelves.fromJson(null))
}
@Test fun ownedShelfEntriesBecomeTheLibraryItem() {
val shelf = EpicShelves.fromJson(JSONObject().put("free", JSONArray().put(item("offer", "x,c2", mature = false))).toString())!!
val owned = CatalogItem(Store.EPIC, "FigApp", "Fig", imageUrl = null, owned = true, developer = "",
publisher = "Lib Pub", extra = mapOf("namespace" to "ns", "catalogItemId" to "c2"))
val marked = EpicShelves.markOwned(shelf, listOf(owned))
val entry = marked.free.single()
assertEquals("FigApp", entry.id)
assertTrue(entry.owned)
assertEquals("w", entry.imageUrl)
assertEquals("D", entry.developer)
assertEquals("Lib Pub", entry.publisher)
assertEquals(StoreShelves().isEmpty, EpicShelves.markOwned(StoreShelves(), listOf(owned)).isEmpty)
}
}