diff --git a/app/Helpers/Upload.php b/app/Helpers/Upload.php index 0d505b4..e6c1e2c 100644 --- a/app/Helpers/Upload.php +++ b/app/Helpers/Upload.php @@ -2,11 +2,16 @@ namespace app\Helpers; +use Exception; use Illuminate\Support\Facades\Storage; class Upload { public static function generateFilePath(string $token) { + if (strlen($token) < 5) { + throw new Exception('Invalid token'); + } + $path = 'files/'; for ($i = 0; $i < 3; $i++) { $letter = substr($token, $i, 1); @@ -33,7 +38,7 @@ class Upload { return false; } - public static function humanFilesize(float $size, $precision = 2) + public static function humanFilesize($size, $precision = 2) { if ($size > 0) { $size = (int) $size; @@ -152,10 +157,10 @@ class Upload { // A-B format if (strpos($range, '-') !== false) { list($lower, $upper) = explode('-', $range, 2); - $lower_dec = (float)sprintf("%u",ip2long($lower)); - $upper_dec = (float)sprintf("%u",ip2long($upper)); - $ip_dec = (float)sprintf("%u",ip2long($ip)); - return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); + $lower_dec = (float)sprintf("%u", ip2long($lower)); + $upper_dec = (float)sprintf("%u", ip2long($upper)); + $ip_dec = (float)sprintf("%u", ip2long($ip)); + return ( ($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec) ); } return false; diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php index 41170fa..8d433ee 100644 --- a/app/Http/Controllers/UploadController.php +++ b/app/Http/Controllers/UploadController.php @@ -144,8 +144,6 @@ class UploadController extends Controller 'error' => $e->getMessage() ], 500); } - - } } diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index 486dc27..ba4c4b3 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -9,15 +9,5 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; class ExampleTest extends TestCase { - /** - * A basic test example. - * - * @return void - */ - public function testBasicTest() - { - $response = $this->get('/'); - $response->assertStatus(200); - } } diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php index 5663bb4..faf7563 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/ExampleTest.php @@ -2,19 +2,86 @@ namespace Tests\Unit; +use Upload; +use Exception; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ExampleTest extends TestCase { - /** - * A basic test example. - * - * @return void - */ - public function testBasicTest() + + public function testPhpVersion() { - $this->assertTrue(true); + $this->assertTrue(version_compare(phpversion(), '5.6.4', '>=')); + } + + public function testOpenSSL() { + $this->assertTrue(extension_loaded('openssl')); + } + + public function testPDO() { + $this->assertTrue(extension_loaded('pdo')); + } + + public function testMbString() { + $this->assertTrue(extension_loaded('mbstring')); + } + + public function testTokenizer() { + $this->assertTrue(extension_loaded('tokenizer')); + } + + public function testXml() { + $this->assertTrue(extension_loaded('xml')); + } + + public function testJson() { + $this->assertTrue(extension_loaded('json')); + } + + public function testZip() { + $this->assertTrue(extension_loaded('zip')); + } + + public function testCheckIpRange() { + $ips = [ + true => [ + '192.168.0.10' => '192.168.0.*', + '10.0.2.1' => '10.0.2.0-10.0.2.10', + '191.57.177.24' => '191.57.177.0/24', + '127.0.0.1' => '127.0.0.1' + + ], + + false => [ + '192.168.2.10' => '192.168.0.*', + '10.0.2.11' => '10.0.2.0-10.0.2.10', + '191.57.171.24' => '191.57.177.0/24', + '127.0.0.1' => '127.0.0.2' + ] + ]; + + foreach ($ips as $assert => $tests) { + foreach ($tests as $ip => $range) { + if ($assert == true) { + $this->assertTrue(Upload::isValidIp($ip, $range), 'Test should be OK but is not (IP: '.$ip.', range '.$range.')'); + } + else { + $this->assertFalse(Upload::isValidIp($ip, $range), 'Test should not be OK but is it (IP: '.$ip.', range '.$range.')'); + } + } + } + } + + /** + * @expectedException Exception + */ + function testFilePath() { + upload::generateFilePath('abc'); + } + + function testUploadFilesize() { + $this->assertTrue(is_numeric(Upload::fileMaxSize()), 'Max filesize should be a numeric'); } }