139 lines
8.3 KiB
PHP
139 lines
8.3 KiB
PHP
<?php
|
|
|
|
use App\Services\StandaloneArtifact;
|
|
use App\Services\StandaloneRuntime;
|
|
use Illuminate\Database\Connection;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Database\Schema\Builder as SchemaBuilder;
|
|
|
|
beforeEach(function () {
|
|
$this->artifactParent = realpath(__DIR__.'/../../storage/framework/testing');
|
|
expect($this->artifactParent)->not->toBeFalse();
|
|
$this->artifactRoot = $this->artifactParent.'/standalone-artifact-'.bin2hex(random_bytes(6));
|
|
mkdir($this->artifactRoot, 0700);
|
|
$this->manifest = ['contract' => StandaloneArtifact::CONTRACT, 'product' => 'compras', 'version' => 1,
|
|
'capabilities' => ['capital-register'], 'files' => []];
|
|
foreach (StandaloneArtifact::PACKAGES as $package) {
|
|
mkdir($this->artifactRoot.'/'.$package, 0700);
|
|
$bytes = '<?php throw new RuntimeException("VERIFIER_MUST_NOT_EXECUTE_PACKAGE");';
|
|
file_put_contents($this->artifactRoot.'/'.$package.'/bootstrap.php', $bytes);
|
|
$this->manifest['files'][] = ['file' => $package.'/bootstrap.php', 'sha256' => hash('sha256', $bytes)];
|
|
}
|
|
$this->writeArtifactManifest = function (): string {
|
|
$bytes = json_encode($this->manifest, JSON_THROW_ON_ERROR);
|
|
file_put_contents($this->artifactRoot.'/manifest.json', $bytes);
|
|
|
|
return hash('sha256', $bytes);
|
|
};
|
|
$this->artifactPin = ($this->writeArtifactManifest)();
|
|
});
|
|
|
|
afterEach(function () {
|
|
$root = realpath($this->artifactRoot);
|
|
if ($root === false || dirname($root) !== $this->artifactParent || ! preg_match('/^standalone-artifact-[a-f0-9]{12}$/D', basename($root))) {
|
|
throw new LogicException('Owned artifact fixture cleanup refused.');
|
|
}
|
|
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $file) {
|
|
$file->isDir() && ! $file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname());
|
|
}
|
|
rmdir($root);
|
|
});
|
|
|
|
test('standalone artifact verifies the complete portable package without executing it', function () {
|
|
$result = (new StandaloneArtifact)->verify($this->artifactRoot, $this->artifactPin);
|
|
expect($result['manifest']['product'])->toBe('compras')
|
|
->and($result['manifest']['capabilities'])->toBe(['capital-register']);
|
|
foreach (StandaloneArtifact::PACKAGES as $package) {
|
|
expect(realpath($result['paths'][$package]))->toBe(realpath($this->artifactRoot.'/'.$package));
|
|
}
|
|
});
|
|
|
|
test('standalone artifact rejects changed source bytes and unlisted files before execution', function (string $mutation) {
|
|
if ($mutation === 'changed') {
|
|
file_put_contents($this->artifactRoot.'/payables/bootstrap.php', '<?php echo "ALTERED";');
|
|
} else {
|
|
file_put_contents($this->artifactRoot.'/payables/extra.php', '<?php echo "UNLISTED";');
|
|
}
|
|
expect(fn () => (new StandaloneArtifact)->verify($this->artifactRoot, $this->artifactPin))->toThrow(LogicException::class);
|
|
})->with(['changed', 'unlisted']);
|
|
|
|
test('standalone artifact rejects incompatible metadata duplicate and escaping identities', function (string $mutation) {
|
|
match ($mutation) {
|
|
'product' => $this->manifest['product'] = 'sgf',
|
|
'version' => $this->manifest['version'] = 2,
|
|
'contract' => $this->manifest['contract'] = 'QA-UNPUBLISHED',
|
|
'duplicate-file' => $this->manifest['files'][] = $this->manifest['files'][0],
|
|
'escaping-file' => $this->manifest['files'][0]['file'] = 'budget-control/../../outside.php',
|
|
'absolute-file' => $this->manifest['files'][0]['file'] = 'C:/outside.php',
|
|
'malformed-file' => $this->manifest['files'][0] = 'budget-control/bootstrap.php',
|
|
'duplicate-capability' => $this->manifest['capabilities'][] = 'capital-register',
|
|
'invalid-capability' => $this->manifest['capabilities'][] = ['capital-register'],
|
|
'missing-package' => $this->manifest['files'][0]['file'] = 'budget-control/other.php',
|
|
};
|
|
if ($mutation === 'missing-package') {
|
|
rename($this->artifactRoot.'/budget-control/bootstrap.php', $this->artifactRoot.'/budget-control/other.php');
|
|
}
|
|
$pin = ($this->writeArtifactManifest)();
|
|
expect(fn () => (new StandaloneArtifact)->verify($this->artifactRoot, $pin))->toThrow(LogicException::class);
|
|
})->with(['product', 'version', 'contract', 'duplicate-file', 'escaping-file', 'absolute-file', 'malformed-file', 'duplicate-capability', 'invalid-capability', 'missing-package']);
|
|
|
|
test('standalone artifact requires the caller pin even for an otherwise valid package', function () {
|
|
expect(fn () => (new StandaloneArtifact)->verify($this->artifactRoot, str_repeat('0', 64)))->toThrow(LogicException::class, 'COMPRAS_RUNTIME_MANIFEST_HASH');
|
|
});
|
|
|
|
test('standalone composition rejects capabilities absent from its pinned package', function (mixed $features) {
|
|
$runtime = new StandaloneRuntime(['enabled' => true, 'root' => $this->artifactRoot,
|
|
'manifest_sha256' => $this->artifactPin, 'installation_id' => '2998ee93-322b-4bd9-9c7b-c16725056a99',
|
|
'features' => $features], new StandaloneArtifact);
|
|
expect(fn () => $runtime->artifact())->toThrow(LogicException::class);
|
|
})->with([
|
|
'unsupported enabled feature' => [['private-audit' => true]],
|
|
'truthy string' => [['capital-register' => 'true']],
|
|
'malformed feature collection' => ['capital-register'],
|
|
]);
|
|
|
|
test('standalone composition accepts explicitly disabled unavailable capabilities', function () {
|
|
$runtime = new StandaloneRuntime(['enabled' => true, 'root' => $this->artifactRoot,
|
|
'manifest_sha256' => $this->artifactPin, 'installation_id' => '2998ee93-322b-4bd9-9c7b-c16725056a99',
|
|
'features' => ['capital-register' => true, 'private-audit' => false]], new StandaloneArtifact);
|
|
expect($runtime->artifact()['manifest']['capabilities'])->toBe(['capital-register']);
|
|
});
|
|
|
|
test('standalone feature checks the installation receipt before permitting availability', function (string $mutation) {
|
|
$settings = ['enabled' => true, 'root' => $this->artifactRoot, 'manifest_sha256' => $this->artifactPin,
|
|
'installation_id' => '2998ee93-322b-4bd9-9c7b-c16725056a99', 'features' => ['capital-register' => true]];
|
|
$db = Mockery::mock(Connection::class);
|
|
$db->shouldReceive('getDriverName')->once()->andReturn('pgsql');
|
|
$db->shouldReceive('selectOne')->once()->andReturn((object) ['database_name' => 'purchasing_company',
|
|
'role_name' => 'purchasing_app', 'database_oid' => '45000', 'server_address' => '192.0.2.10',
|
|
'server_port' => 5432, 'elevated' => 0]);
|
|
$schema = Mockery::mock(SchemaBuilder::class);
|
|
$db->shouldReceive('getSchemaBuilder')->once()->andReturn($schema);
|
|
$schema->shouldReceive('hasTable')->with('compras_runtime_installations')->once()->andReturn($mutation !== 'schema');
|
|
if ($mutation !== 'schema') {
|
|
$query = Mockery::mock(Builder::class);
|
|
$db->shouldReceive('table')->with('compras_runtime_installations')->once()->andReturn($query);
|
|
$query->shouldReceive('where')->with('id', $settings['installation_id'])->once()->andReturnSelf();
|
|
$receipt = (object) ['product' => 'compras', 'contract' => StandaloneArtifact::CONTRACT,
|
|
'manifest_sha256' => $this->artifactPin, 'database_binding_sha256' => hash('sha256', json_encode([
|
|
'product' => 'compras', 'database' => 'purchasing_company', 'oid' => '45000', 'role' => 'purchasing_app',
|
|
'address' => '192.0.2.10', 'port' => 5432,
|
|
], JSON_THROW_ON_ERROR))];
|
|
match ($mutation) {
|
|
'product' => $receipt->product = 'sgf',
|
|
'contract' => $receipt->contract = 'OLD',
|
|
'manifest' => $receipt->manifest_sha256 = str_repeat('0', 64),
|
|
'database' => $receipt->database_binding_sha256 = str_repeat('0', 64),
|
|
default => null,
|
|
};
|
|
$query->shouldReceive('first')->once()->andReturn($mutation === 'missing' ? null : $receipt);
|
|
}
|
|
$runtime = new StandaloneRuntime($settings, new StandaloneArtifact);
|
|
if ($mutation === 'valid') {
|
|
expect($runtime->feature('capital-register', $db))->toBeTrue();
|
|
} else {
|
|
expect(fn () => $runtime->feature('capital-register', $db))->toThrow(LogicException::class,
|
|
$mutation === 'schema' ? 'COMPRAS_RUNTIME_INSTALLATION_MISSING' : 'COMPRAS_RUNTIME_INSTALLATION_MISMATCH');
|
|
}
|
|
})->with(['valid', 'schema', 'missing', 'product', 'contract', 'manifest', 'database']);
|