@@ -8,20 +8,20 @@ |
||
8 | 8 | |
9 | 9 | class OpenSSLCryptoTest extends SapphireTest |
10 | 10 | { |
11 | - /** |
|
12 | - * @requires extension openssl |
|
13 | - */ |
|
14 | - public function testIntegrity() |
|
15 | - { |
|
16 | - $key = random_bytes(8); |
|
17 | - $salt = random_bytes(16); |
|
11 | + /** |
|
12 | + * @requires extension openssl |
|
13 | + */ |
|
14 | + public function testIntegrity() |
|
15 | + { |
|
16 | + $key = random_bytes(8); |
|
17 | + $salt = random_bytes(16); |
|
18 | 18 | |
19 | - $handler = new OpenSSLCrypto($key, $salt); |
|
19 | + $handler = new OpenSSLCrypto($key, $salt); |
|
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
22 | - $data = random_bytes(1024 * 4); |
|
21 | + for ($i=0; $i<1000; ++$i) { |
|
22 | + $data = random_bytes(1024 * 4); |
|
23 | 23 | |
24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | - } |
|
26 | - } |
|
24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | + } |
|
26 | + } |
|
27 | 27 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | |
19 | 19 | $handler = new OpenSSLCrypto($key, $salt); |
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
21 | + for ($i = 0; $i < 1000; ++$i) { |
|
22 | 22 | $data = random_bytes(1024 * 4); |
23 | 23 | |
24 | 24 | $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
@@ -8,20 +8,20 @@ |
||
8 | 8 | |
9 | 9 | class McryptCryptoTest extends SapphireTest |
10 | 10 | { |
11 | - /** |
|
12 | - * @requires extension mcrypt |
|
13 | - */ |
|
14 | - public function testIntegrity() |
|
15 | - { |
|
16 | - $key = random_bytes(8); |
|
17 | - $salt = random_bytes(16); |
|
11 | + /** |
|
12 | + * @requires extension mcrypt |
|
13 | + */ |
|
14 | + public function testIntegrity() |
|
15 | + { |
|
16 | + $key = random_bytes(8); |
|
17 | + $salt = random_bytes(16); |
|
18 | 18 | |
19 | - $handler = new McryptCrypto($key, $salt); |
|
19 | + $handler = new McryptCrypto($key, $salt); |
|
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
22 | - $data = random_bytes(1024 * 4); |
|
21 | + for ($i=0; $i<1000; ++$i) { |
|
22 | + $data = random_bytes(1024 * 4); |
|
23 | 23 | |
24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | - } |
|
26 | - } |
|
24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | + } |
|
26 | + } |
|
27 | 27 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | |
19 | 19 | $handler = new McryptCrypto($key, $salt); |
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
21 | + for ($i = 0; $i < 1000; ++$i) { |
|
22 | 22 | $data = random_bytes(1024 * 4); |
23 | 23 | |
24 | 24 | $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
@@ -11,38 +11,38 @@ |
||
11 | 11 | */ |
12 | 12 | trait BinaryDataJsonCodec |
13 | 13 | { |
14 | - /** |
|
15 | - * Encode binary data into ASCII string (a subset of UTF-8) |
|
16 | - * |
|
17 | - * @param string $data This is a binary blob |
|
18 | - * |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - public function binary_data_json_encode($data) { |
|
22 | - return json_encode([ |
|
23 | - self::class, |
|
24 | - base64_encode($data) |
|
25 | - ]); |
|
26 | - } |
|
14 | + /** |
|
15 | + * Encode binary data into ASCII string (a subset of UTF-8) |
|
16 | + * |
|
17 | + * @param string $data This is a binary blob |
|
18 | + * |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + public function binary_data_json_encode($data) { |
|
22 | + return json_encode([ |
|
23 | + self::class, |
|
24 | + base64_encode($data) |
|
25 | + ]); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Decode ASCII string into original binary data (a php string) |
|
30 | - * |
|
31 | - * @param string $text |
|
32 | - * |
|
33 | - * @param null|string |
|
34 | - */ |
|
35 | - public function binary_data_json_decode($text) { |
|
36 | - $struct = json_decode($text, true, 2); |
|
28 | + /** |
|
29 | + * Decode ASCII string into original binary data (a php string) |
|
30 | + * |
|
31 | + * @param string $text |
|
32 | + * |
|
33 | + * @param null|string |
|
34 | + */ |
|
35 | + public function binary_data_json_decode($text) { |
|
36 | + $struct = json_decode($text, true, 2); |
|
37 | 37 | |
38 | - if (!is_array($struct) || count($struct) !== 2) { |
|
39 | - return null; |
|
40 | - } |
|
38 | + if (!is_array($struct) || count($struct) !== 2) { |
|
39 | + return null; |
|
40 | + } |
|
41 | 41 | |
42 | - if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
43 | - return null; |
|
44 | - } |
|
42 | + if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
43 | + return null; |
|
44 | + } |
|
45 | 45 | |
46 | - return base64_decode($struct[1]); |
|
47 | - } |
|
46 | + return base64_decode($struct[1]); |
|
47 | + } |
|
48 | 48 | } |
@@ -12,96 +12,96 @@ |
||
12 | 12 | |
13 | 13 | class DatabaseStore extends BaseStore |
14 | 14 | { |
15 | - use BinaryDataJsonCodec; |
|
16 | - |
|
17 | - /** |
|
18 | - * Determine if the DB is ready to use. |
|
19 | - * |
|
20 | - * @return bool |
|
21 | - * @throws Exception |
|
22 | - */ |
|
23 | - protected function isDatabaseReady() |
|
24 | - { |
|
25 | - // Such as during setup of testsession prior to DB connection. |
|
26 | - if (!DB::is_active()) { |
|
27 | - return false; |
|
28 | - } |
|
29 | - |
|
30 | - // If we have a DB of the wrong type then complain |
|
31 | - if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
32 | - throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
33 | - } |
|
34 | - |
|
35 | - // Prevent freakout during dev/build |
|
36 | - return ClassInfo::hasTable('HybridSessionDataObject'); |
|
37 | - } |
|
38 | - |
|
39 | - public function open($save_path, $name) |
|
40 | - { |
|
41 | - // no-op |
|
42 | - } |
|
43 | - |
|
44 | - public function close() |
|
45 | - { |
|
46 | - // no-op |
|
47 | - } |
|
48 | - |
|
49 | - public function read($session_id) |
|
50 | - { |
|
51 | - if (!$this->isDatabaseReady()) { |
|
52 | - return null; |
|
53 | - } |
|
54 | - |
|
55 | - $query = sprintf( |
|
56 | - 'SELECT "Data" FROM "HybridSessionDataObject" |
|
15 | + use BinaryDataJsonCodec; |
|
16 | + |
|
17 | + /** |
|
18 | + * Determine if the DB is ready to use. |
|
19 | + * |
|
20 | + * @return bool |
|
21 | + * @throws Exception |
|
22 | + */ |
|
23 | + protected function isDatabaseReady() |
|
24 | + { |
|
25 | + // Such as during setup of testsession prior to DB connection. |
|
26 | + if (!DB::is_active()) { |
|
27 | + return false; |
|
28 | + } |
|
29 | + |
|
30 | + // If we have a DB of the wrong type then complain |
|
31 | + if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
32 | + throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
33 | + } |
|
34 | + |
|
35 | + // Prevent freakout during dev/build |
|
36 | + return ClassInfo::hasTable('HybridSessionDataObject'); |
|
37 | + } |
|
38 | + |
|
39 | + public function open($save_path, $name) |
|
40 | + { |
|
41 | + // no-op |
|
42 | + } |
|
43 | + |
|
44 | + public function close() |
|
45 | + { |
|
46 | + // no-op |
|
47 | + } |
|
48 | + |
|
49 | + public function read($session_id) |
|
50 | + { |
|
51 | + if (!$this->isDatabaseReady()) { |
|
52 | + return null; |
|
53 | + } |
|
54 | + |
|
55 | + $query = sprintf( |
|
56 | + 'SELECT "Data" FROM "HybridSessionDataObject" |
|
57 | 57 | WHERE "SessionID" = \'%s\' AND "Expiry" >= %s', |
58 | - Convert::raw2sql($session_id), |
|
59 | - $this->getNow() |
|
60 | - ); |
|
58 | + Convert::raw2sql($session_id), |
|
59 | + $this->getNow() |
|
60 | + ); |
|
61 | 61 | |
62 | - $result = DB::query($query); |
|
62 | + $result = DB::query($query); |
|
63 | 63 | |
64 | - if ($result && $result->numRecords()) { |
|
65 | - $data = $result->first(); |
|
66 | - $decoded = $this->binary_data_json_decode($data['Data']); |
|
67 | - return is_null($decoded) ? $data['Data'] : $decoded; |
|
68 | - } |
|
69 | - } |
|
64 | + if ($result && $result->numRecords()) { |
|
65 | + $data = $result->first(); |
|
66 | + $decoded = $this->binary_data_json_decode($data['Data']); |
|
67 | + return is_null($decoded) ? $data['Data'] : $decoded; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - public function write($session_id, $session_data) |
|
72 | - { |
|
73 | - if (!$this->isDatabaseReady()) { |
|
74 | - return false; |
|
75 | - } |
|
71 | + public function write($session_id, $session_data) |
|
72 | + { |
|
73 | + if (!$this->isDatabaseReady()) { |
|
74 | + return false; |
|
75 | + } |
|
76 | 76 | |
77 | - $expiry = $this->getNow() + $this->getLifetime(); |
|
77 | + $expiry = $this->getNow() + $this->getLifetime(); |
|
78 | 78 | |
79 | - DB::query($str = sprintf( |
|
80 | - 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
79 | + DB::query($str = sprintf( |
|
80 | + 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
81 | 81 | VALUES (\'%1$s\', %2$u, \'%3$s\') |
82 | 82 | ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'', |
83 | - Convert::raw2sql($session_id), |
|
84 | - $expiry, |
|
85 | - Convert::raw2sql($this->binary_data_json_encode($session_data)) |
|
86 | - )); |
|
87 | - |
|
88 | - return true; |
|
89 | - } |
|
90 | - |
|
91 | - public function destroy($session_id) |
|
92 | - { |
|
93 | - // NOP |
|
94 | - } |
|
95 | - |
|
96 | - public function gc($maxlifetime) |
|
97 | - { |
|
98 | - if (!$this->isDatabaseReady()) { |
|
99 | - return; |
|
100 | - } |
|
101 | - |
|
102 | - DB::query(sprintf( |
|
103 | - 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
104 | - $this->getNow() |
|
105 | - )); |
|
106 | - } |
|
83 | + Convert::raw2sql($session_id), |
|
84 | + $expiry, |
|
85 | + Convert::raw2sql($this->binary_data_json_encode($session_data)) |
|
86 | + )); |
|
87 | + |
|
88 | + return true; |
|
89 | + } |
|
90 | + |
|
91 | + public function destroy($session_id) |
|
92 | + { |
|
93 | + // NOP |
|
94 | + } |
|
95 | + |
|
96 | + public function gc($maxlifetime) |
|
97 | + { |
|
98 | + if (!$this->isDatabaseReady()) { |
|
99 | + return; |
|
100 | + } |
|
101 | + |
|
102 | + DB::query(sprintf( |
|
103 | + 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
104 | + $this->getNow() |
|
105 | + )); |
|
106 | + } |
|
107 | 107 | } |
@@ -7,5 +7,5 @@ |
||
7 | 7 | |
8 | 8 | class BinaryDataJsonCodecImplementation implements TestOnly |
9 | 9 | { |
10 | - use BinaryDataJsonCodec; |
|
10 | + use BinaryDataJsonCodec; |
|
11 | 11 | } |
@@ -11,31 +11,31 @@ |
||
11 | 11 | |
12 | 12 | class DatabaseStoreTest extends AbstractTest |
13 | 13 | { |
14 | - protected function setUp() |
|
15 | - { |
|
16 | - parent::setUp(); |
|
14 | + protected function setUp() |
|
15 | + { |
|
16 | + parent::setUp(); |
|
17 | 17 | |
18 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
19 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
20 | - } |
|
21 | - } |
|
18 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
19 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
20 | + } |
|
21 | + } |
|
22 | 22 | |
23 | - protected function getStore() |
|
24 | - { |
|
25 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
26 | - $store->setKey(uniqid()); |
|
23 | + protected function getStore() |
|
24 | + { |
|
25 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
26 | + $store->setKey(uniqid()); |
|
27 | 27 | |
28 | - return $store; |
|
29 | - } |
|
28 | + return $store; |
|
29 | + } |
|
30 | 30 | |
31 | - public function testDataCodecIntegrity() |
|
32 | - { |
|
33 | - $codec = new BinaryDataJsonCodecImplementation; |
|
31 | + public function testDataCodecIntegrity() |
|
32 | + { |
|
33 | + $codec = new BinaryDataJsonCodecImplementation; |
|
34 | 34 | |
35 | - for ($i=0; $i<1000; ++$i) { |
|
36 | - $data = random_bytes(1024 * 4); |
|
35 | + for ($i=0; $i<1000; ++$i) { |
|
36 | + $data = random_bytes(1024 * 4); |
|
37 | 37 | |
38 | - $this->assertEquals($data, $codec->binary_data_json_decode($codec->binary_data_json_encode($data))); |
|
39 | - } |
|
40 | - } |
|
38 | + $this->assertEquals($data, $codec->binary_data_json_decode($codec->binary_data_json_encode($data))); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | } |
@@ -32,7 +32,7 @@ |
||
32 | 32 | { |
33 | 33 | $codec = new BinaryDataJsonCodecImplementation; |
34 | 34 | |
35 | - for ($i=0; $i<1000; ++$i) { |
|
35 | + for ($i = 0; $i < 1000; ++$i) { |
|
36 | 36 | $data = random_bytes(1024 * 4); |
37 | 37 | |
38 | 38 | $this->assertEquals($data, $codec->binary_data_json_decode($codec->binary_data_json_encode($data))); |