]> git.mxchange.org Git - friendica-addons.git/blob - s3_storage/vendor/akeeba/s3/minitest/Test/AbstractTest.php
8470e26076ada91997aa3ce77e00f4ce3e610e42
[friendica-addons.git] / s3_storage / vendor / akeeba / s3 / minitest / Test / AbstractTest.php
1 <?php
2 /**
3  * Akeeba Engine
4  *
5  * @package   akeebaengine
6  * @copyright Copyright (c)2006-2020 Nicholas K. Dionysopoulos / Akeeba Ltd
7  * @license   GNU General Public License version 3, or later
8  */
9
10 namespace Akeeba\MiniTest\Test;
11
12 use Akeeba\Engine\Postproc\Connector\S3v4\Connector;
13 use RuntimeException;
14
15 abstract class AbstractTest
16 {
17         const TEN_KB = 10240;
18
19         const HUNDRED_KB = 102400;
20
21         const SIX_HUNDRED_KB = 614400;
22
23         const ONE_MB = 1048576;
24
25         const FIVE_MB = 5242880;
26
27         const SIX_MB = 6291456;
28
29         const TEN_MB = 10485760;
30
31         const ELEVEN_MB = 11534336;
32
33         const BLOCK_SIZE = 1048576;
34
35         const FILE_HASHING_ALGORITHM = 'sha256';
36
37         public static function setup(Connector $s3, array $options): void
38         {
39                 // Runs before any test
40         }
41
42         public static function teardown(Connector $s3, array $options): void
43         {
44                 // Runs after all tests are finished
45         }
46
47         /**
48          * Creates a file with random data and returns its file path.
49          *
50          * The random data in the file repeats every $blockSize bytes when $reuseBlock is true.
51          *
52          * @param   int   $size  Size in files
53          *
54          * @param   int   $blockSize
55          * @param   bool  $reuseBlock
56          *
57          * @return  string  The full path to the temporary file.
58          */
59         protected static function createFile(int $size = AbstractTest::SIX_HUNDRED_KB, int $blockSize = self::BLOCK_SIZE, bool $reuseBlock = true)
60         {
61                 $tempFilePath = tempnam(self::getTempFolder(), 'as3');
62
63                 if ($tempFilePath === false)
64                 {
65                         throw new RuntimeException("Cannot create a temporary file.");
66                 }
67
68                 $fp = @fopen($tempFilePath, 'wb', false);
69
70                 if ($fp === false)
71                 {
72                         throw new RuntimeException("Cannot write to the temporary file.");
73                 }
74
75                 $blockSize     = self::BLOCK_SIZE;
76                 $lastBlockSize = $size % $blockSize;
77                 $wholeBlocks   = (int) (($size - $lastBlockSize) / $blockSize);
78                 $blockData     = self::getRandomData();
79
80                 for ($i = 0; $i < $wholeBlocks; $i++)
81                 {
82                         fwrite($fp, $blockData);
83
84                         if (!$reuseBlock)
85                         {
86                                 $blockData = self::getRandomData($blockSize);
87                         }
88                 }
89
90                 if ($lastBlockSize > 0)
91                 {
92                         fwrite($fp, $blockData, $lastBlockSize);
93                 }
94
95
96                 fclose($fp);
97
98                 return $tempFilePath;
99         }
100
101         /**
102          * Get a writeable temporary folder
103          *
104          * @return  string
105          */
106         protected static function getTempFolder(): string
107         {
108                 $tempPath = sys_get_temp_dir();
109
110                 if (!is_writable($tempPath))
111                 {
112                         $tempPath = __DIR__ . '/tmp';
113
114                         if (!is_dir($tempPath))
115                         {
116                                 @mkdir($tempPath, 0755, true);
117                         }
118                 }
119
120                 if (!is_writable($tempPath))
121                 {
122                         throw new RuntimeException("Cannot get a writeable temporary path.");
123                 }
124
125                 return $tempPath;
126         }
127
128         /**
129          * Checks that two files are of equal length and contents
130          *
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
133          *
134          * @return  bool
135          */
136         protected static function areFilesEqual(string $referenceFilePath, string $unknownFilePath): bool
137         {
138                 if (!file_exists($referenceFilePath) || !file_exists($unknownFilePath))
139                 {
140                         return false;
141                 }
142
143                 if (!is_file($referenceFilePath) || !is_file($unknownFilePath))
144                 {
145                         return false;
146                 }
147
148                 if (!is_readable($referenceFilePath) || !is_readable($unknownFilePath))
149                 {
150                         return false;
151                 }
152
153                 if (@filesize($referenceFilePath) !== @filesize($unknownFilePath))
154                 {
155                         return false;
156                 }
157
158                 return hash_file(self::FILE_HASHING_ALGORITHM, $referenceFilePath) === hash_file(self::FILE_HASHING_ALGORITHM, $unknownFilePath);
159         }
160
161         /**
162          * Checks that two strings are of equal length and contents
163          *
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
166          *
167          * @return  bool
168          */
169         protected static function areStringsEqual(string $referenceString, string $unknownString): bool
170         {
171                 return $referenceString === $unknownString;
172         }
173
174         /**
175          * Returns random data of the specific size in bytes
176          *
177          * @param   int  $length  How many bytes of random data to return
178          *
179          * @return  string  Your random data
180          */
181         protected static function getRandomData(int $length = self::BLOCK_SIZE): string
182         {
183                 $blockData = '';
184
185                 if (substr(strtolower(PHP_OS), 0, 7) !== 'windows')
186                 {
187                         $fpRandom = @fopen('/dev/urandom', 'r');
188                         if ($fpRandom !== false)
189                         {
190                                 $blockData = @fread($fpRandom, $length);
191                                 @fclose($fpRandom);
192                         }
193                 }
194
195                 if (empty($blockData) && function_exists('random_bytes'))
196                 {
197                         try
198                         {
199                                 $blockData = random_bytes($length);
200                         }
201                         catch (\Exception $e)
202                         {
203                                 $blockData = '';
204                         }
205                 }
206
207                 if (empty($blockData) && function_exists('openssl_random_pseudo_bytes'))
208                 {
209                         $blockData = openssl_random_pseudo_bytes($length);
210                 }
211
212                 if (empty($blockData) && function_exists('mcrypt_create_iv'))
213                 {
214                         $blockData = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
215
216                         if (empty($blockData))
217                         {
218                                 $blockData = mcrypt_create_iv($length, MCRYPT_RAND);
219                         }
220                 }
221
222                 if (empty($blockData))
223                 {
224                         for ($i = 0; $i < $length; $i++)
225                         {
226                                 $blockData .= ord(mt_rand(0, 255));
227                         }
228                 }
229
230                 return $blockData;
231         }
232
233         protected static function assert(bool $condition, string $message): void
234         {
235                 if ($condition)
236                 {
237                         return;
238                 }
239
240                 throw new RuntimeException($message);
241         }
242 }