73 lines
4.3 KiB
PHP
73 lines
4.3 KiB
PHP
<?php
|
|
|
|
use App\Services\BudgetDistributionArtifact;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
beforeEach(function () {
|
|
$proof = json_decode(file_get_contents(base_path('docs/proofs/rq01-distribution-artifact.json')), true, 32, JSON_THROW_ON_ERROR);
|
|
$this->artifactDirectory = storage_path('framework/testing/distribution-integrity-'.Str::uuid());
|
|
mkdir($this->artifactDirectory.'/src', 0700, true);
|
|
foreach ($proof['files'] as $file => $hash) {
|
|
copy(base_path($proof['path'].'/'.$file), $this->artifactDirectory.'/'.$file);
|
|
}
|
|
$this->artifactManifest = $this->artifactDirectory.'/manifest.json';
|
|
$this->artifactData = ['files' => $proof['files']];
|
|
file_put_contents($this->artifactManifest, json_encode($this->artifactData, JSON_THROW_ON_ERROR));
|
|
$this->artifactPin = hash_file('sha256', $this->artifactManifest);
|
|
});
|
|
|
|
afterEach(function () {
|
|
foreach (glob($this->artifactDirectory.'/src/*') as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($this->artifactDirectory.'/src');
|
|
foreach (glob($this->artifactDirectory.'/*') as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($this->artifactDirectory);
|
|
});
|
|
|
|
test('frozen standalone budget release verifies all seven files without executing its PHP', function () {
|
|
BudgetDistributionArtifact::verify($this->artifactDirectory, $this->artifactManifest, $this->artifactPin);
|
|
expect(config('budget-distributions.enabled'))->toBeFalse();
|
|
});
|
|
|
|
test('budget release rejects changed executable files before loading them', function (string $file) {
|
|
file_put_contents($this->artifactDirectory.'/'.$file, "\n// changed", FILE_APPEND);
|
|
expect(fn () => BudgetDistributionArtifact::verify($this->artifactDirectory, $this->artifactManifest, $this->artifactPin))
|
|
->toThrow(LogicException::class, 'BUDGET_DISTRIBUTION_FILE_MISMATCH');
|
|
})->with(['bootstrap.php', 'src/BudgetControl.php', 'src/BudgetDistributions.php', 'src/BudgetDistributionPlan.php']);
|
|
|
|
test('budget release rejects a changed manifest even when its replacement lists current hashes', function () {
|
|
$this->artifactData['files']['src/BudgetControl.php'] = str_repeat('0', 64);
|
|
file_put_contents($this->artifactManifest, json_encode($this->artifactData, JSON_THROW_ON_ERROR));
|
|
expect(fn () => BudgetDistributionArtifact::verify($this->artifactDirectory, $this->artifactManifest, $this->artifactPin))
|
|
->toThrow(LogicException::class, 'BUDGET_DISTRIBUTION_MANIFEST_MISMATCH');
|
|
});
|
|
|
|
test('budget release rejects incomplete inventories and missing trust anchors', function () {
|
|
unset($this->artifactData['files']['src/BudgetDistributions.php']);
|
|
file_put_contents($this->artifactManifest, json_encode($this->artifactData, JSON_THROW_ON_ERROR));
|
|
expect(fn () => BudgetDistributionArtifact::verify($this->artifactDirectory, $this->artifactManifest, hash_file('sha256', $this->artifactManifest)))
|
|
->toThrow(LogicException::class, 'BUDGET_DISTRIBUTION_INVENTORY_MISMATCH')
|
|
->and(fn () => BudgetDistributionArtifact::verify($this->artifactDirectory, $this->artifactManifest, null))
|
|
->toThrow(LogicException::class, 'BUDGET_DISTRIBUTION_ARTIFACT_CONFIGURATION');
|
|
});
|
|
|
|
test('host boots the independently pinned release and rejects changed bootstrap before it can execute', function () {
|
|
$environment = ['APP_ENV' => 'testing', 'COMMON_BUDGET_PATH' => $this->artifactDirectory,
|
|
'SGC_BUDGET_DISTRIBUTIONS_ENABLED' => 'true', 'SGC_BUDGET_DISTRIBUTIONS_MANIFEST' => $this->artifactManifest,
|
|
'SGC_BUDGET_DISTRIBUTIONS_MANIFEST_SHA256' => $this->artifactPin];
|
|
$process = new Process([PHP_BINARY, base_path('tests/Concurrency/budget-distribution-artifact-boot.php')], base_path(), $environment, timeout: 30);
|
|
$process->run();
|
|
expect($process->isSuccessful())->toBeTrue();
|
|
$result = json_decode(trim($process->getOutput()), true, 32, JSON_THROW_ON_ERROR);
|
|
expect($result['runtime'])->toBe(realpath($this->artifactDirectory.'/src/BudgetControl.php'));
|
|
file_put_contents($this->artifactDirectory.'/bootstrap.php', '<?php file_put_contents(__DIR__."/executed", "yes");');
|
|
$process->run();
|
|
expect($process->isSuccessful())->toBeFalse()
|
|
->and($process->getOutput().$process->getErrorOutput())->toContain('BUDGET_DISTRIBUTION_FILE_MISMATCH')
|
|
->and(file_exists($this->artifactDirectory.'/executed'))->toBeFalse();
|
|
});
|