addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('region'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('status'); $table->integer('user_id'); }); $schema->create('cloud_instance_backups', static function (Blueprint $table): void { $table->increments('id'); $table->string('backup_key'); $table->integer('source_instance_id'); $table->integer('source_host_id'); $table->integer('user_id'); $table->string('source_uuid'); $table->string('name'); $table->string('purpose')->default('portable'); $table->string('state'); $table->string('consistency'); $table->string('object_key'); $table->integer('object_size_bytes'); $table->integer('virtual_size_bytes'); $table->string('sha256'); $table->text('spec_json'); $table->text('sealed_storage_runtime'); $table->text('sealed_credential')->nullable(); $table->integer('storage_revision'); $table->integer('active_job_id')->nullable(); $table->string('last_operation_id')->nullable(); $table->string('error_code'); $table->integer('ready_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $host = new CloudHost(); $host->name = 'source-host'; $host->region = 'HKG'; $host->save(); $source = new CloudInstance(); $source->name = 'deleted-source'; $source->status = 'deleted'; $source->user_id = 77; $source->save(); for ($index = 1; $index <= 21; $index++) { $backup = new CloudInstanceBackup(); $backup->backup_key = str_pad((string) $index, 32, '0', STR_PAD_LEFT); $backup->source_instance_id = (int) $source->id; $backup->source_host_id = (int) $host->id; $backup->user_id = 77; $backup->source_uuid = '11111111-1111-4111-8111-111111111111'; $backup->name = 'backup-' . $index; $backup->state = 'failed'; $backup->consistency = 'crash'; $backup->object_key = 'virt/vm-backups/' . $index . '/system.qcow2'; $backup->object_size_bytes = 0; $backup->virtual_size_bytes = 0; $backup->sha256 = ''; $backup->spec_json = '{}'; $backup->sealed_storage_runtime = 'sealed'; $backup->sealed_credential = null; $backup->storage_revision = 1; $backup->active_job_id = null; $backup->last_operation_id = null; $backup->error_code = 'backup_failed'; $backup->ready_at = null; $backup->created_at = $backup->updated_at = 1700000000 + $index; $backup->save(); } $readOnly = [ 'user_id' => 9, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false, ]; $first = CloudBackupService::library($readOnly, ['page' => '1', 'per_page' => '20']); $assert(count($first['items']) === 20 && $first['pagination']['total'] === 21 && $first['pagination']['total_pages'] === 2 && $first['can_manage'] === false, 'Global backup library lost bounded portable-backup pagination.'); $item = $first['items'][0]; $assert(($item['source']['deleted'] ?? null) === true && ($item['source']['status'] ?? null) === 'deleted' && ($item['source']['name'] ?? null) === 'deleted-source' && ($item['can_delete'] ?? null) === false, 'A deleted source VM made its retained backup disappear or become writable.'); $second = CloudBackupService::library($readOnly, ['page' => '2', 'per_page' => '20']); $assert(count($second['items']) === 1 && $second['pagination']['from'] === 21 && $second['pagination']['to'] === 21, 'Backup library second-page bounds are incorrect.'); $other = $backup->replicate(); $other->user_id = 88; $other->source_instance_id = 999; $other->source_host_id = 999; $other->name = 'literal_%_backup'; $other->state = 'uncertain'; $other->backup_key = str_repeat('b', 32); $other->created_at = $other->updated_at = 1700000100; $other->save(); $deleted = $other->replicate(); $deleted->backup_key = str_repeat('c', 32); $deleted->state = 'deleted'; $deleted->created_at = $deleted->updated_at = 1700000101; $deleted->save(); $internal = $other->replicate(); $internal->backup_key = str_repeat('d', 32); $internal->purpose = 'migration'; $internal->created_at = $internal->updated_at = 1700000102; $internal->save(); $combined = CloudBackupService::library($readOnly, [ 'user_id' => '77', 'instance_id' => (string) $source->id, 'host_id' => (string) $host->id, 'state' => 'failed', 'q' => 'backup-2', 'page' => '2', ]); $assert($combined['pagination']['total'] === 3 && $combined['pagination']['page'] === 1 && count($combined['items']) === 3, 'Combined backup filters, total and clamped pagination disagree.'); $missingSource = CloudBackupService::library($readOnly, ['user_id' => '88', 'instance_id' => '999', 'host_id' => '999', 'state' => 'uncertain']); $assert($missingSource['pagination']['total'] === 1 && $missingSource['items'][0]['source']['deleted'] === true && $missingSource['can_manage'] === false, 'Filtering backups with missing source records hid retained data or expanded permissions.'); $literal = CloudBackupService::library($readOnly, ['q' => '_%_']); $assert($literal['pagination']['total'] === 1, 'Backup name search treated literal wildcard characters as SQL wildcards.'); $empty = CloudBackupService::library($readOnly, ['user_id' => '77', 'host_id' => '999']); $assert($empty['pagination']['total'] === 0 && $empty['pagination']['from'] === 0, 'Backup filters were ORed instead of combined.'); foreach ([['state' => 'deleted'], ['q' => ['bad']], ['host_id' => '-1'], ['user_id' => '1 OR 1=1'], ['q' => str_repeat('字', 81)], ['q' => "bad\0query"], ['unknown' => '1']] as $invalid) { try { CloudBackupService::library($readOnly, $invalid); throw new RuntimeException('An invalid backup filter was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === (isset($invalid['unknown']) ? 422 : 400), 'Invalid backup filter error semantics changed.'); } } try { CloudBackupService::library([ 'user_id' => 77, 'is_admin' => false, ], []); throw new RuntimeException('Non-administrator read was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === 403, 'Non-administrator backup-library read returned the wrong status.'); } echo "Portable backup global library pagination and deleted-source behavior passed.\n";