tarantool-php /
client
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of the tarantool/client package. |
||
| 5 | * |
||
| 6 | * (c) Eugene Leonovich <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace Tarantool\Client\Tests; |
||
| 15 | |||
| 16 | final class GreetingDataProvider |
||
| 17 | { |
||
| 18 | private const DEFAULT_SERVER_VERSION = '2.2.2'; |
||
| 19 | private const SALT_SIZE_BYTES = 20; |
||
| 20 | |||
| 21 | public static function provideGreetingsWithInvalidServerName() : iterable |
||
| 22 | { |
||
| 23 | return [ |
||
| 24 | [''], |
||
| 25 | ["\n"], |
||
| 26 | ['1'], |
||
| 27 | ['tarantool'], |
||
| 28 | ['Tarantoo'], |
||
| 29 | ['Тарантул'], |
||
| 30 | [str_repeat('2', 63)."\n"], |
||
| 31 | [str_repeat('3', 63)."\n3"], |
||
| 32 | [str_repeat('4', 63)."\n\n"], |
||
| 33 | [str_repeat('5', 63)."\n\n\n"], |
||
| 34 | [str_repeat('6', 63)."\n".str_repeat('6', 63)."\n"], |
||
| 35 | ]; |
||
| 36 | } |
||
| 37 | |||
| 38 | public static function provideGreetingsWithInvalidSalt() : iterable |
||
| 39 | { |
||
| 40 | return [ |
||
| 41 | [str_pad('Tarantool', 63, '1')."\n"], |
||
| 42 | [str_pad('Tarantool', 63, '2')."\n2"], |
||
| 43 | [str_pad('Tarantool', 63, '3')."\n\n"], |
||
| 44 | [str_pad('Tarantool', 63, '4')."\n\n\n"], |
||
| 45 | [str_pad('Tarantool', 63, '5')."\nтутсолинеттутсолинеттутсолинеттутсолинеттутсолинеттутсолинеттут\n"], |
||
| 46 | ]; |
||
| 47 | } |
||
| 48 | |||
| 49 | public static function provideValidGreetings() : iterable |
||
| 50 | { |
||
| 51 | return [ |
||
| 52 | [self::generateGreetingFromSalt('12345678901234567890'), '12345678901234567890'], |
||
| 53 | ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function generateGreeting(string $version = self::DEFAULT_SERVER_VERSION) : string |
||
| 57 | { |
||
| 58 | return self::generateGreetingFromSalt(self::generateSalt(), $version); |
||
| 59 | } |
||
| 60 | |||
| 61 | public static function generateGreetingFromSalt(string $salt, string $version = self::DEFAULT_SERVER_VERSION) : string |
||
| 62 | { |
||
| 63 | $uuid = self::generateUuid4(); |
||
| 64 | |||
| 65 | $greeting = str_pad("Tarantool $version (Binary) $uuid", 63, ' ')."\n"; |
||
| 66 | $greeting .= str_pad(base64_encode($salt.str_repeat('_', 12)), 63, ' ')."\n"; |
||
| 67 | |||
| 68 | return $greeting; |
||
| 69 | } |
||
| 70 | |||
| 71 | private static function generateSalt() : string |
||
| 72 | { |
||
| 73 | return random_bytes(self::SALT_SIZE_BYTES); |
||
| 74 | } |
||
| 75 | |||
| 76 | private static function generateUuid4() : string |
||
| 77 | { |
||
| 78 | $data = random_bytes(16); |
||
| 79 | $data[6] = \chr(\ord($data[6]) & 0x0f | 0x40); |
||
| 80 | $data[8] = \chr(\ord($data[8]) & 0x3f | 0x80); |
||
| 81 | |||
| 82 | return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 83 | } |
||
| 84 | } |
||
| 85 |