54 lines
2.7 KiB
PHP
54 lines
2.7 KiB
PHP
<?php
|
|
|
|
use App\Services\InventoryConversionRuntime;
|
|
|
|
test('inventory archive validates exact entry names independently of insertion order', function () {
|
|
$path = sys_get_temp_dir().'/compras-inventory-entries-'.bin2hex(random_bytes(8)).'.zip';
|
|
$zip = new ZipArchive;
|
|
try {
|
|
expect($zip->open($path, ZipArchive::CREATE | ZipArchive::EXCL))->toBeTrue();
|
|
foreach (['src/ReceiptMovementWriter.php', 'src/QuantityConversion.php', 'bootstrap.php', 'manifest.json'] as $name) {
|
|
$zip->addFromString($name, 'synthetic archive entry');
|
|
}
|
|
$zip->close();
|
|
$zip->open($path);
|
|
InventoryConversionRuntime::assertArchiveEntries($zip);
|
|
expect($zip->numFiles)->toBe(4);
|
|
$zip->addFromString('../unexpected.php', 'synthetic forbidden entry');
|
|
expect(fn () => InventoryConversionRuntime::assertArchiveEntries($zip))->toThrow(LogicException::class, 'COMPRAS_INVENTORY_ARCHIVE_ENTRIES');
|
|
$zip->deleteName('../unexpected.php');
|
|
$zip->deleteName('bootstrap.php');
|
|
expect(fn () => InventoryConversionRuntime::assertArchiveEntries($zip))->toThrow(LogicException::class, 'COMPRAS_INVENTORY_ARCHIVE_ENTRIES');
|
|
} finally {
|
|
$zip->close();
|
|
if (is_file($path)) {
|
|
unlink($path);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('inventory candidate rejects changed component bytes and manifests before loading PHP', function () {
|
|
$source = dirname(__DIR__, 2).'/packages/inventory-write/releases/0.2.0-rc.1';
|
|
$root = sys_get_temp_dir().'/compras-inventory-artifact-'.bin2hex(random_bytes(8));
|
|
mkdir($root.'/src', 0700, true);
|
|
$files = ['manifest.json', 'bootstrap.php', 'src/QuantityConversion.php', 'src/ReceiptMovementWriter.php'];
|
|
try {
|
|
foreach ($files as $file) {
|
|
copy($source.'/'.$file, $root.'/'.$file);
|
|
}
|
|
InventoryConversionRuntime::verifyArtifact($root);
|
|
expect(hash_file('sha256', $root.'/manifest.json'))->toBe(InventoryConversionRuntime::MANIFEST_SHA256);
|
|
file_put_contents($root.'/src/QuantityConversion.php', "\n// altered", FILE_APPEND);
|
|
expect(fn () => InventoryConversionRuntime::verifyArtifact($root))->toThrow(LogicException::class, 'COMPRAS_INVENTORY_FILE:src/QuantityConversion.php');
|
|
copy($source.'/src/QuantityConversion.php', $root.'/src/QuantityConversion.php');
|
|
file_put_contents($root.'/manifest.json', "\n", FILE_APPEND);
|
|
expect(fn () => InventoryConversionRuntime::verifyArtifact($root))->toThrow(LogicException::class, 'COMPRAS_INVENTORY_MANIFEST');
|
|
} finally {
|
|
foreach ($files as $file) {
|
|
unlink($root.'/'.$file);
|
|
}
|
|
rmdir($root.'/src');
|
|
rmdir($root);
|
|
}
|
|
});
|