Files
nextcloud-server-mirror/tests/lib/Files/FileInfoTest.php
Stephen CuppettandClaude Opus 4.6 b6fddabf6c fix(encryption): Address review feedback and fix zero-byte size consistency
Apply come-nc's reviewer suggestions on Scanner.php:
- Simplify cached unencrypted_size skip condition to explicit == 0 check
- Remove redundant !isset guard on new-file path

Fix remaining > 0 / !== 0 guards that treated zero as "no unencrypted_size":
- FileInfo constructor: use encrypted flag for rawSize (getSize(false) path)
- FileInfo::addSubEntry: use encrypted flag for sub-mount accumulation
- Cache::calculateFolderSizeInner: use encrypted column instead of > 0

Add tests covering zero-byte encrypted file scenarios:
- FileInfoTest: getSize(true/false) for zero-byte encrypted files
- CacheTest: getUnencryptedSize and calculateFolderSize with encrypted=1,size=0
- EncryptionTest: verifyUnencryptedSize data provider cases for header-only files

Assisted-by: ClaudeCode:claude-opus-4-6
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Cuppett <scuppett@redhat.com>
2026-07-21 20:43:38 +02:00

86 lines
2.2 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Files;
use OC\Files\FileInfo;
use OC\Files\Mount\HomeMountPoint;
use OC\Files\Mount\MountPoint;
use OC\Files\Storage\Home;
use OC\Files\Storage\Temporary;
use OCP\IConfig;
use OCP\IUser;
use Test\TestCase;
use Test\Traits\UserTrait;
class FileInfoTest extends TestCase {
use UserTrait;
private $config;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->createUser('foo', 'foo');
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
}
private function makeFileInfo(array $data): FileInfo {
$storage = new Temporary();
return new FileInfo('', $storage, '', $data, new MountPoint($storage, '/foo/files'));
}
public function testGetSizeEncryptedZeroByte(): void {
$info = $this->makeFileInfo(['encrypted' => true, 'size' => 8192, 'unencrypted_size' => 0]);
// Both paths must report the true plaintext size (0), not the on-disk encrypted size (8192)
$this->assertSame(0, $info->getSize(true));
$this->assertSame(0, $info->getSize(false));
}
public function testGetSizeEncryptedNonZero(): void {
$info = $this->makeFileInfo(['encrypted' => true, 'size' => 16384, 'unencrypted_size' => 5000]);
$this->assertSame(5000, $info->getSize(true));
$this->assertSame(5000, $info->getSize(false));
}
public function testGetSizeNonEncrypted(): void {
$info = $this->makeFileInfo(['encrypted' => false, 'size' => 100]);
$this->assertSame(100, $info->getSize(true));
$this->assertSame(100, $info->getSize(false));
}
public function testIsMountedHomeStorage(): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('foo');
$user->method('getHome')
->willReturn('foo');
$storage = new Home(['user' => $user]);
$fileInfo = new FileInfo(
'',
$storage,
'',
[],
new HomeMountPoint($user, $storage, '/foo/files')
);
$this->assertFalse($fileInfo->isMounted());
}
public function testIsMountedNonHomeStorage(): void {
$storage = new Temporary();
$fileInfo = new FileInfo(
'',
$storage,
'',
[],
new MountPoint($storage, '/foo/files/bar')
);
$this->assertTrue($fileInfo->isMounted());
}
}