|
| 1 | +<?php |
| 2 | +// Suppress unexpected errors from output and exit early as a failure when encountered. |
| 3 | +set_error_handler(function ($errno, $errstr, $errfile, $errline) { |
| 4 | + exit(1); |
| 5 | +}); |
| 6 | + |
| 7 | +// Need this to find the package's own files (since it isn't installing itself). |
| 8 | +spl_autoload_register(function ($Class) { |
| 9 | + $Class = explode('\\', $Class, 3); |
| 10 | + $Count = count($Class); |
| 11 | + if ($Count !== 3 || $Class[0] !== 'phpMussel' || $Class[1] !== 'Core') { |
| 12 | + return; |
| 13 | + } |
| 14 | + $Class = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $Class[2]); |
| 15 | + $Try = __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $Class . '.php'; |
| 16 | + if (is_readable($Try)) { |
| 17 | + require $Try; |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +$Autoloader = __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
| 22 | +if (!is_readable($Autoloader)) { |
| 23 | + exit(2); |
| 24 | +} |
| 25 | +require $Autoloader; |
| 26 | + |
| 27 | +// Path to all tests data. |
| 28 | +$TestsPath = __DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR; |
| 29 | + |
| 30 | +// Fetch the signatures needed for testing the scanner. |
| 31 | +$ZipObj = new \ZipArchive(); |
| 32 | +if ($ZipObj->open($TestsPath . 'signatures.zip') === TRUE) { |
| 33 | + $ZipObj->extractTo($TestsPath . 'signatures' . DIRECTORY_SEPARATOR); |
| 34 | + $ZipObj->close(); |
| 35 | + unset($ZipObj); |
| 36 | + $SigPath = $TestsPath . 'signatures'; |
| 37 | +} else { |
| 38 | + exit(3); |
| 39 | +} |
| 40 | + |
| 41 | +$Samples = $TestsPath . 'samples'; |
| 42 | + |
| 43 | +$Config = $TestsPath . 'phpmussel.yml'; |
| 44 | +if (!is_readable($Config) || !is_readable($Samples) || !is_readable($SigPath)) { |
| 45 | + exit(4); |
| 46 | +} |
| 47 | + |
| 48 | +$Loader = new \phpMussel\Core\Loader($Config, '', '', $SigPath); |
| 49 | +$Scanner = new \phpMussel\Core\Scanner($Loader); |
| 50 | + |
| 51 | +// Expected results from scanning the phpMussel test samples. |
| 52 | +$Expected = [ |
| 53 | + '1043d8e6c0deb7f7264952a163cbfe9f724251064f9c9d2ccbb3996ea79ebe1c:20882:pdf_standard_testfile.pdf' => 'Detected phpMussel-Testfile.PDF.Standard (pdf_standard_testfile.pdf)!', |
| 54 | + '14fb5b708076142cf38131ccc3827ff0a0ff28db1ee5db4583432cadafc8a4bf:658:ole_testfile.bin' => 'Detected phpMussel-Testfile.OLE.Standard (ole_testfile.bin)!', |
| 55 | + '4b4e349e8103d105b8dd0f5fce5ec9be0b263d203597e87abf3644089aea095f:19:hash_testfile_md5.txt' => 'Detected phpMussel-Testfile.HASH.MD5 (hash_testfile_md5.txt)!', |
| 56 | + '8b4413ceca5ba8b33f1af7d1ce82a108f26be2e3de9241ca9969ea47214a180a:5632:pe_sectional_testfile.exe' => 'Detected phpMussel-Testfile.PE.Sectional (pe_sectional_testfile.exe)!', |
| 57 | + '8e39388e6e605902d1192aecc5ea77f9a62547eb164562266c0060cf52cb6ec9:653:general_standard_testfile.txt' => 'Detected phpMussel-Testfile.General.Standard (general_standard_testfile.txt)!', |
| 58 | + 'a00178f9d85e56c8067c5d6c234a48afd6631c9e3c90fe0717f4b7330360ef3b:5632:exe_standard_testfile.exe' => 'Detected phpMussel-Testfile.EXE.Standard (exe_standard_testfile.exe)!', |
| 59 | + 'bf059f3112049d7299f9dc39397fe721c560e790611bfdc163adadbebb4e9ca9:13:hello.txt' => '', |
| 60 | + 'c845b950f38399ae7fe4b3107cab5b46ac7c3e184dddfec97d4d164c00cb584a:491:coex_testfile.rtf' => 'Detected phpMussel-Testfile.CoEx (coex_testfile.rtf)!', |
| 61 | + 'c8ff1888b2802f8824a59191d4ad0a7f5261840541044ca5313fd4ca0962063b:20:hash_testfile_sha1.txt' => 'Detected phpMussel-Testfile.HASH.SHA1 (hash_testfile_sha1.txt)!', |
| 62 | + 'd188d46c87f2174c78ed4aaf8b0d24bfafc684c789df36572110355f59443ff7:632:graphics_standard_testfile.gif' => 'Detected phpMussel-Testfile.Graphics.Standard (graphics_standard_testfile.gif)!', |
| 63 | + 'd1e1ec9461e107beee203d2c7f909d0dab026046a89d5b9a84bece02b5b93ca9:31662:swf_standard_testfile.swf' => 'Detected phpMussel-Testfile.SWF.Standard (swf_standard_testfile.swf)!', |
| 64 | + 'd45d5d9df433aefeacaece6162b835e6474d6fcb707d24971322ec429707c58f:185:encrypted.zip' => 'Detected encrypted archive; Encrypted archives not permitted (encrypted.zip)!', |
| 65 | + 'dcacac499064454218823fbabff7e09b5b011c0c877ee6f215f35bffb195b6e9:654:ascii_standard_testfile.txt' => 'Detected phpMussel-Testfile.ASCII.Standard (ascii_standard_testfile.txt)!', |
| 66 | + 'f90054161ed9c4ffcda720769cb1c563eb0fd0e770004db352c4e225522e9a93:22:hash_testfile_sha256.txt' => 'Detected phpMussel-Testfile.HASH.SHA256 (hash_testfile_sha256.txt)!', |
| 67 | + 'fbb49f897c8f8310f6c5ecacbd541d6873b18c7119ba71688d1bcdd3d7ea98fe:1488:html_standard_testfile.html' => 'Detected phpMussel-Testfile.HTML.Standard (html_standard_testfile.html)!', |
| 68 | +]; |
| 69 | + |
| 70 | +// Test scanning against the standard phpMussel test samples. |
| 71 | +$Actual = $Scanner->scan($Samples, 3); |
| 72 | +ksort($Actual); |
| 73 | +if ($Actual !== $Expected) { |
| 74 | + exit(5); |
| 75 | +} |
| 76 | + |
| 77 | +restore_error_handler(); |
| 78 | + |
| 79 | +// All tests passed. |
| 80 | +exit(0); |
0 commit comments