|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Helper; |
| 10 | + |
| 11 | +function is_uploaded_file($filename) { |
| 12 | + return file_exists($filename); |
| 13 | +} |
| 14 | + |
| 15 | +namespace OCA\Libresign\Tests\Unit\Helper; |
| 16 | + |
| 17 | +use InvalidArgumentException; |
| 18 | +use OCA\Libresign\Helper\FileUploadHelper; |
| 19 | +use OCP\IL10N; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class FileUploadHelperTest extends TestCase { |
| 24 | + private FileUploadHelper $helper; |
| 25 | + private IL10N&MockObject $l10n; |
| 26 | + private string $tempFile; |
| 27 | + |
| 28 | + protected function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->l10n = $this->createMock(IL10N::class); |
| 32 | + $this->l10n->method('t') |
| 33 | + ->willReturnCallback(fn ($text) => $text); |
| 34 | + |
| 35 | + $this->helper = new FileUploadHelper($this->l10n); |
| 36 | + |
| 37 | + $this->tempFile = tempnam(sys_get_temp_dir(), 'upload_test_'); |
| 38 | + file_put_contents($this->tempFile, 'test content'); |
| 39 | + } |
| 40 | + |
| 41 | + protected function tearDown(): void { |
| 42 | + if (file_exists($this->tempFile)) { |
| 43 | + @unlink($this->tempFile); |
| 44 | + } |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testValidateUploadedFileSuccess(): void { |
| 49 | + $uploadedFile = [ |
| 50 | + 'tmp_name' => $this->tempFile, |
| 51 | + 'error' => UPLOAD_ERR_OK, |
| 52 | + 'size' => filesize($this->tempFile), |
| 53 | + ]; |
| 54 | + |
| 55 | + $this->helper->validateUploadedFile($uploadedFile); |
| 56 | + |
| 57 | + $this->assertTrue(file_exists($this->tempFile)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testValidateUploadedFileWithUploadError(): void { |
| 61 | + $uploadedFile = [ |
| 62 | + 'tmp_name' => $this->tempFile, |
| 63 | + 'error' => UPLOAD_ERR_INI_SIZE, |
| 64 | + 'size' => 0, |
| 65 | + ]; |
| 66 | + |
| 67 | + $this->expectException(InvalidArgumentException::class); |
| 68 | + $this->expectExceptionMessage('Invalid file provided'); |
| 69 | + |
| 70 | + try { |
| 71 | + $this->helper->validateUploadedFile($uploadedFile); |
| 72 | + } finally { |
| 73 | + $this->assertFalse(file_exists($this->tempFile), 'File should be deleted after error'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function testValidateUploadedFileNotActuallyUploaded(): void { |
| 78 | + $nonExistentFile = sys_get_temp_dir() . '/non_existent_file_' . time(); |
| 79 | + |
| 80 | + $uploadedFile = [ |
| 81 | + 'tmp_name' => $nonExistentFile, |
| 82 | + 'error' => UPLOAD_ERR_OK, |
| 83 | + 'size' => 100, |
| 84 | + ]; |
| 85 | + |
| 86 | + $this->expectException(InvalidArgumentException::class); |
| 87 | + $this->expectExceptionMessage('Invalid file provided'); |
| 88 | + |
| 89 | + $this->helper->validateUploadedFile($uploadedFile); |
| 90 | + } |
| 91 | + |
| 92 | + public function testValidateUploadedFileTooBig(): void { |
| 93 | + $uploadedFile = [ |
| 94 | + 'tmp_name' => $this->tempFile, |
| 95 | + 'error' => UPLOAD_ERR_OK, |
| 96 | + 'size' => \OCP\Util::uploadLimit() + 1, |
| 97 | + ]; |
| 98 | + |
| 99 | + $this->expectException(InvalidArgumentException::class); |
| 100 | + $this->expectExceptionMessage('File is too big'); |
| 101 | + |
| 102 | + try { |
| 103 | + $this->helper->validateUploadedFile($uploadedFile); |
| 104 | + } finally { |
| 105 | + $this->assertFalse(file_exists($this->tempFile), 'File should be deleted when too big'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public function testReadUploadedFileSuccess(): void { |
| 110 | + $expectedContent = 'test file content'; |
| 111 | + file_put_contents($this->tempFile, $expectedContent); |
| 112 | + |
| 113 | + $uploadedFile = [ |
| 114 | + 'tmp_name' => $this->tempFile, |
| 115 | + ]; |
| 116 | + |
| 117 | + $content = $this->helper->readUploadedFile($uploadedFile); |
| 118 | + |
| 119 | + $this->assertEquals($expectedContent, $content); |
| 120 | + } |
| 121 | + |
| 122 | + public function testReadUploadedFileNotReadable(): void { |
| 123 | + $uploadedFile = [ |
| 124 | + 'tmp_name' => '/path/that/does/not/exist.txt', |
| 125 | + ]; |
| 126 | + |
| 127 | + $this->expectException(InvalidArgumentException::class); |
| 128 | + $this->expectExceptionMessage('Cannot read file'); |
| 129 | + |
| 130 | + $this->helper->readUploadedFile($uploadedFile); |
| 131 | + } |
| 132 | + |
| 133 | + public function testValidateUploadedFileWithForbiddenName(): void { |
| 134 | + $forbiddenFile = sys_get_temp_dir() . '/test<file>.txt'; |
| 135 | + |
| 136 | + if (@file_put_contents($forbiddenFile, 'test') === false) { |
| 137 | + $this->markTestSkipped('Cannot create file with forbidden characters on this OS'); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $uploadedFile = [ |
| 142 | + 'tmp_name' => $forbiddenFile, |
| 143 | + 'error' => UPLOAD_ERR_OK, |
| 144 | + 'size' => filesize($forbiddenFile), |
| 145 | + ]; |
| 146 | + |
| 147 | + try { |
| 148 | + $this->helper->validateUploadedFile($uploadedFile); |
| 149 | + @unlink($forbiddenFile); |
| 150 | + } catch (InvalidArgumentException $e) { |
| 151 | + $this->assertEquals('Invalid file provided', $e->getMessage()); |
| 152 | + $this->assertFalse(file_exists($forbiddenFile)); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments