5 * @package akeebaengine
6 * @copyright Copyright (c)2006-2020 Nicholas K. Dionysopoulos / Akeeba Ltd
7 * @license GNU General Public License version 3, or later
10 namespace Akeeba\MiniTest\Test;
12 use Akeeba\Engine\Postproc\Connector\S3v4\Connector;
15 abstract class AbstractTest
19 const HUNDRED_KB = 102400;
21 const SIX_HUNDRED_KB = 614400;
23 const ONE_MB = 1048576;
25 const FIVE_MB = 5242880;
27 const SIX_MB = 6291456;
29 const TEN_MB = 10485760;
31 const ELEVEN_MB = 11534336;
33 const BLOCK_SIZE = 1048576;
35 const FILE_HASHING_ALGORITHM = 'sha256';
37 public static function setup(Connector $s3, array $options): void
39 // Runs before any test
42 public static function teardown(Connector $s3, array $options): void
44 // Runs after all tests are finished
48 * Creates a file with random data and returns its file path.
50 * The random data in the file repeats every $blockSize bytes when $reuseBlock is true.
52 * @param int $size Size in files
54 * @param int $blockSize
55 * @param bool $reuseBlock
57 * @return string The full path to the temporary file.
59 protected static function createFile(int $size = AbstractTest::SIX_HUNDRED_KB, int $blockSize = self::BLOCK_SIZE, bool $reuseBlock = true)
61 $tempFilePath = tempnam(self::getTempFolder(), 'as3');
63 if ($tempFilePath === false)
65 throw new RuntimeException("Cannot create a temporary file.");
68 $fp = @fopen($tempFilePath, 'wb', false);
72 throw new RuntimeException("Cannot write to the temporary file.");
75 $blockSize = self::BLOCK_SIZE;
76 $lastBlockSize = $size % $blockSize;
77 $wholeBlocks = (int) (($size - $lastBlockSize) / $blockSize);
78 $blockData = self::getRandomData();
80 for ($i = 0; $i < $wholeBlocks; $i++)
82 fwrite($fp, $blockData);
86 $blockData = self::getRandomData($blockSize);
90 if ($lastBlockSize > 0)
92 fwrite($fp, $blockData, $lastBlockSize);
102 * Get a writeable temporary folder
106 protected static function getTempFolder(): string
108 $tempPath = sys_get_temp_dir();
110 if (!is_writable($tempPath))
112 $tempPath = __DIR__ . '/tmp';
114 if (!is_dir($tempPath))
116 @mkdir($tempPath, 0755, true);
120 if (!is_writable($tempPath))
122 throw new RuntimeException("Cannot get a writeable temporary path.");
129 * Checks that two files are of equal length and contents
131 * @param string $referenceFilePath The known, reference file
132 * @param string $unknownFilePath The file we want to verify is the same as the reference file
136 protected static function areFilesEqual(string $referenceFilePath, string $unknownFilePath): bool
138 if (!file_exists($referenceFilePath) || !file_exists($unknownFilePath))
143 if (!is_file($referenceFilePath) || !is_file($unknownFilePath))
148 if (!is_readable($referenceFilePath) || !is_readable($unknownFilePath))
153 if (@filesize($referenceFilePath) !== @filesize($unknownFilePath))
158 return hash_file(self::FILE_HASHING_ALGORITHM, $referenceFilePath) === hash_file(self::FILE_HASHING_ALGORITHM, $unknownFilePath);
162 * Checks that two strings are of equal length and contents
164 * @param string $referenceString The known, reference file
165 * @param string $unknownString The file we want to verify is the same as the reference file
169 protected static function areStringsEqual(string $referenceString, string $unknownString): bool
171 return $referenceString === $unknownString;
175 * Returns random data of the specific size in bytes
177 * @param int $length How many bytes of random data to return
179 * @return string Your random data
181 protected static function getRandomData(int $length = self::BLOCK_SIZE): string
185 if (substr(strtolower(PHP_OS), 0, 7) !== 'windows')
187 $fpRandom = @fopen('/dev/urandom', 'r');
188 if ($fpRandom !== false)
190 $blockData = @fread($fpRandom, $length);
195 if (empty($blockData) && function_exists('random_bytes'))
199 $blockData = random_bytes($length);
201 catch (\Exception $e)
207 if (empty($blockData) && function_exists('openssl_random_pseudo_bytes'))
209 $blockData = openssl_random_pseudo_bytes($length);
212 if (empty($blockData) && function_exists('mcrypt_create_iv'))
214 $blockData = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
216 if (empty($blockData))
218 $blockData = mcrypt_create_iv($length, MCRYPT_RAND);
222 if (empty($blockData))
224 for ($i = 0; $i < $length; $i++)
226 $blockData .= ord(mt_rand(0, 255));
233 protected static function assert(bool $condition, string $message): void
240 throw new RuntimeException($message);