markTestSkipped('The zip extension is not loaded.'); } Storage::fake('launcher'); }); /** * Builds a CI-style release zip and returns its path. * * @param array $manifestOverrides * @param array $exes File name => contents. */ function makeLauncherReleaseZip( array $manifestOverrides = [], array $exes = ['DeathgardenLauncher-net11.0-windows.exe' => 'net11 exe bytes', 'DeathgardenLauncher-net9.0-windows.exe' => 'net9 exe bytes'], string $signingKey = LAUNCHER_TEST_KEY, string $folder = '', ): string { $builds = collect($exes)->mapWithKeys(fn (string $contents, string $file) => [ Str::match('/-(net[\d.]+-windows)\.exe$/', $file) => ['file' => $file, 'size' => strlen($contents), 'sha256' => hash('sha256', $contents)], ])->all(); $manifest = json_encode(array_merge([ 'product' => 'deathgarden-launcher', 'channel' => 'stable', 'version' => '1.200.1', 'notes' => 'Test update', 'published' => '2026-11-11T16:40:12Z', 'builds' => $builds, ], $manifestOverrides), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); openssl_sign($manifest, $derSignature, $signingKey, OPENSSL_ALGO_SHA256); $path = tempnam(sys_get_temp_dir(), 'launcher-release').'.zip'; $zip = new ZipArchive; $zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE); $zip->addFromString($folder.'launcher-manifest.json', $manifest); $zip->addFromString($folder.'launcher-manifest.json.sig', base64_encode(derSignatureToP1363($derSignature))); foreach ($exes as $file => $contents) { $zip->addFromString($folder.$file, $contents); } $zip->close(); return $path; } function derSignatureToP1363(string $der): string { $offset = 2; $integers = []; for ($i = 0; $i < 2; $i++) { $length = ord($der[$offset + 1]); $integers[] = str_pad(ltrim(substr($der, $offset + 2, $length), "\x00"), 32, "\x00", STR_PAD_LEFT); $offset += 2 + $length; } return $integers[0].$integers[1]; } function publicKeyBase64(string $privateKey): string { $pem = openssl_pkey_get_details(openssl_pkey_get_private($privateKey))['key']; return preg_replace('/-----[^-]+-----|\s/', '', $pem); } function publishLauncherRelease(string $zipPath, array $trustedPublicKeys = []): LauncherRelease { return (new LauncherReleasePublisher($trustedPublicKeys))->publish($zipPath); } function expectReleaseRejected(string $zipPath, string $expectedMessage, array $trustedPublicKeys = []): void { try { publishLauncherRelease($zipPath, $trustedPublicKeys); } catch (ValidationException $exception) { expect($exception->errors()['release'][0])->toContain($expectedMessage); return; } test()->fail('The release was not rejected.'); } test('a valid release zip is published and served byte for byte to the launcher', function () { $zipPath = makeLauncherReleaseZip(); $release = publishLauncherRelease($zipPath); expect($release->version)->toBe('1.200.1') ->and(LauncherRelease::current()->version)->toBe('1.200.1'); $zip = new ZipArchive; $zip->open($zipPath); $manifestResponse = $this->get('/launcher/launcher-manifest.json'); $manifestResponse->assertOk()->assertHeader('Content-Type', 'application/json'); expect($manifestResponse->streamedContent())->toBe($zip->getFromName('launcher-manifest.json')) ->and($this->get('/launcher/launcher-manifest.json.sig')->streamedContent())->toBe($zip->getFromName('launcher-manifest.json.sig')); $exeResponse = $this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe'); $exeResponse->assertOk(); expect($exeResponse->streamedContent())->toBe('net11 exe bytes') ->and($exeResponse->headers->get('Cache-Control'))->toContain('no-cache'); }); test('exe downloads can be resumed with a range request', function () { publishLauncherRelease(makeLauncherReleaseZip()); $response = $this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe', ['Range' => 'bytes=6-']); $response->assertStatus(206)->assertHeader('Content-Range', 'bytes 6-14/15'); expect($response->streamedContent())->toBe('exe bytes'); }); test('only files listed in the current manifest are served', function () { publishLauncherRelease(makeLauncherReleaseZip()); $this->get('/launcher/current')->assertNotFound(); $this->get('/launcher/other.exe')->assertNotFound(); $this->get('/launcher/..%2Fcurrent')->assertNotFound(); }); test('launcher files are not found before any release is published', function () { $this->get('/launcher/launcher-manifest.json')->assertNotFound(); $this->get('/download-launcher')->assertNotFound(); }); test('a zip with the release inside a single top level folder is accepted', function () { expect(publishLauncherRelease(makeLauncherReleaseZip(folder: 'launcher-1.200.1/'))->version)->toBe('1.200.1'); }); test('exes named differently from the manifest are matched by size and hash and stored under the manifest name', function () { $zipPath = makeLauncherReleaseZip(); $zip = new ZipArchive; $zip->open($zipPath); $zip->renameName('DeathgardenLauncher-net11.0-windows.exe', 'DeathgardenLauncher_1.200.1.0-net11.0.exe'); $zip->close(); publishLauncherRelease($zipPath); expect($this->get('/launcher/DeathgardenLauncher-net11.0-windows.exe')->streamedContent())->toBe('net11 exe bytes'); }); test('a release whose exe does not match the manifest is rejected and the current release is kept', function () { publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0'])); $zipPath = makeLauncherReleaseZip(['version' => '1.200.0']); $zip = new ZipArchive; $zip->open($zipPath); $zip->addFromString('DeathgardenLauncher-net9.0-windows.exe', 'NET9 EXE BYTES'); $zip->close(); expectReleaseRejected($zipPath, 'does not match the size or SHA-256'); expect(LauncherRelease::current()->version)->toBe('1.100.0') ->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.100.0']); }); test('invalid manifests are rejected', function (array $manifestOverrides, string $expectedMessage) { expectReleaseRejected(makeLauncherReleaseZip($manifestOverrides), $expectedMessage); expect(LauncherRelease::current())->toBeNull(); })->with([ 'wrong product' => [['product' => 'something-else'], 'product must be'], 'bad version' => [['version' => '../1.0'], 'version must look like'], 'no builds' => [['builds' => []], 'lists no builds'], 'path in file name' => [['builds' => ['net11.0-windows' => ['file' => '../evil.exe', 'size' => 1, 'sha256' => str_repeat('a', 64)]]], 'invalid file name'], ]); test('the same zip cannot be published twice', function () { $zipPath = makeLauncherReleaseZip(); publishLauncherRelease($zipPath); expectReleaseRejected($zipPath, 'already been published as version 1.200.1'); }); test('a version cannot be published twice even with different files', function () { publishLauncherRelease(makeLauncherReleaseZip(['notes' => 'First build'])); expectReleaseRejected(makeLauncherReleaseZip(['notes' => 'Rebuilt']), 'Version 1.200.1 has already been published'); }); test('the manifest signature is verified when trusted keys are configured', function () { $trustedKeys = [publicKeyBase64(LAUNCHER_TEST_KEY)]; expectReleaseRejected(makeLauncherReleaseZip(signingKey: LAUNCHER_OTHER_TEST_KEY), 'does not match any trusted key', $trustedKeys); $publisher = new LauncherReleasePublisher($trustedKeys); $publisher->publish(makeLauncherReleaseZip()); expect($publisher->warnings)->toBe([]); }); test('trusted keys are read from config when the publisher is resolved from the container', function () { config(['services.launcher.update_public_keys' => [publicKeyBase64(LAUNCHER_TEST_KEY)]]); $publisher = app(LauncherReleasePublisher::class); $publisher->publish(makeLauncherReleaseZip()); expect($publisher->warnings)->toBe([]); }); test('a non stable channel and an older version only produce warnings', function () { publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.1'])); $publisher = new LauncherReleasePublisher; $publisher->publish(makeLauncherReleaseZip(['version' => '1.95.0', 'channel' => 'test'])); expect(LauncherRelease::current()->version)->toBe('1.95.0') ->and(implode("\n", $publisher->warnings)) ->toContain('channel is "test"') ->toContain('older than 1.200.1') ->toContain('LAUNCHER_UPDATE_PUBLIC_KEYS'); }); test('an older release can be made current again', function () { publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0'])); publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.0'])); expect(LauncherRelease::all()->pluck('version')->all())->toBe(['1.200.0', '1.100.0']); LauncherRelease::findOrFail('1.100.0')->makeCurrent(); expect(LauncherRelease::current()->version)->toBe('1.100.0'); }); test('the download page serves the newest framework build with a versioned name', function () { publishLauncherRelease(makeLauncherReleaseZip()); $response = $this->get('/download-launcher'); $response->assertOk()->assertDownload('Deathgarden Launcher_1.200.1.exe'); expect($response->streamedContent())->toBe('net11 exe bytes'); }); test('the legacy launcher version endpoint returns the current release version', function () { publishLauncherRelease(makeLauncherReleaseZip()); expect($this->get('/api/v1/launcher-version')->getContent())->toBe('"1.200.1"'); }); test('a release can be deleted but the current release cannot', function () { publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.100.0'])); publishLauncherRelease(makeLauncherReleaseZip(['version' => '1.200.0'])); expect(LauncherRelease::findOrFail('1.200.0')->delete())->toBeFalse() ->and(LauncherRelease::findOrFail('1.100.0')->delete())->toBeTrue() ->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.200.0']) ->and(Storage::disk('launcher')->exists('releases/1.100.0'))->toBeFalse(); }); test('only the newest releases are kept and the oldest are removed on publish', function () { foreach (['1.1.0', '1.2.0', '1.3.0', '1.4.0', '1.5.0'] as $version) { publishLauncherRelease(makeLauncherReleaseZip(['version' => $version])); } $publisher = new LauncherReleasePublisher; $publisher->publish(makeLauncherReleaseZip(['version' => '1.6.0'])); expect($publisher->prunedVersions)->toBe(['1.1.0']) ->and(LauncherRelease::all()->pluck('version')->all())->toBe(['1.6.0', '1.5.0', '1.4.0', '1.3.0', '1.2.0']); }); test('pruning never removes the current release, even when it is the oldest', function () { foreach (['1.2.0', '1.3.0', '1.4.0', '1.5.0', '1.6.0'] as $version) { publishLauncherRelease(makeLauncherReleaseZip(['version' => $version])); } $publisher = new LauncherReleasePublisher; $publisher->publish(makeLauncherReleaseZip(['version' => '1.1.0'])); expect(LauncherRelease::current()->version)->toBe('1.1.0') ->and($publisher->prunedVersions)->toBe(['1.2.0']) ->and(LauncherRelease::all())->toHaveCount(LauncherRelease::MAX_RELEASES); }); /** * Uploads a release zip to the CI publish endpoint the way the launcher workflow does (multipart, no Accept header). */ function uploadLauncherReleaseFromCi(string $zipPath, ?string $token = 'ci-publish-token'): TestResponse { return test()->call('POST', '/api/v1/launcher/releases', files: ['release' => new UploadedFile($zipPath, 'release.zip', 'application/zip', null, true)], server: $token === null ? [] : ['HTTP_AUTHORIZATION' => "Bearer {$token}"], ); } test('ci publishes a release with the publish token and gets json back', function () { config(['services.launcher.publish_token' => 'ci-publish-token']); uploadLauncherReleaseFromCi(makeLauncherReleaseZip()) ->assertCreated() ->assertJson(['version' => '1.200.1', 'channel' => 'stable', 'pruned_versions' => []]); expect(LauncherRelease::current()->version)->toBe('1.200.1'); }); test('ci gets a json validation error when the release is rejected', function () { config(['services.launcher.publish_token' => 'ci-publish-token']); uploadLauncherReleaseFromCi(makeLauncherReleaseZip())->assertCreated(); uploadLauncherReleaseFromCi(makeLauncherReleaseZip()) ->assertUnprocessable() ->assertJsonValidationErrors(['release' => 'already been published']); }); test('ci publishing is refused without the right token', function (?string $configuredToken, ?string $sentToken, int $expectedStatus) { config(['services.launcher.publish_token' => $configuredToken]); uploadLauncherReleaseFromCi(makeLauncherReleaseZip(), $sentToken)->assertStatus($expectedStatus); expect(LauncherRelease::current())->toBeNull(); })->with([ 'wrong token' => ['ci-publish-token', 'wrong-token', 401], 'missing token' => ['ci-publish-token', null, 401], 'publishing disabled' => [null, 'ci-publish-token', 403], 'publishing disabled with empty token' => ['', '', 403], ]);