-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSessionTest.php
More file actions
741 lines (652 loc) · 23.8 KB
/
Copy pathSessionTest.php
File metadata and controls
741 lines (652 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<?php
namespace tests;
use flight\Session;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class SessionTest extends TestCase
{
/** @var string Temporary directory for session files */
private string $tempDir;
/** @var string Encryption key for testing (must be 32 bytes for AES-256) */
private string $encryptionKey = 'test-encryption-key-32-bytes-long';
/**
* Set up the test environment by creating a temporary directory.
*/
protected function setUp(): void
{
// Create a unique temporary directory for session files
$this->tempDir = sys_get_temp_dir() . '/flight_session_test_' . uniqid();
if (!is_dir($this->tempDir)) {
mkdir($this->tempDir, 0700);
}
// Ensure no active session exists from a previous test
if (session_status() === PHP_SESSION_ACTIVE) {
session_destroy();
}
}
/**
* Clean up the temporary directory after each test.
*/
protected function tearDown(): void
{
// Close any active session
if (session_status() === PHP_SESSION_ACTIVE) {
session_destroy();
}
// Remove the temporary directory and its contents
$this->deleteDirectory($this->tempDir);
}
/**
* Helper method to recursively delete a directory.
*/
private function deleteDirectory(string $dir): void
{
if (is_dir($dir) === false) {
return;
}
$files = glob($dir . '/*');
foreach ($files as $file) {
if (is_file($file) === true) {
unlink($file);
} elseif (is_dir($file) === true) {
$this->deleteDirectory($file);
}
}
rmdir($dir);
}
/**
* Test that the constructor creates the session directory.
*/
public function testConstructorCreatesDirectory(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'start_session' => false,
'test_mode' => true
]);
$this->assertDirectoryExists($this->tempDir);
}
/**
* Test setting, getting, deleting, and clearing session data.
*/
public function testSetGetDeleteClear(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true
]); // No auto-commit
$session->set('key', 'value');
$this->assertEquals('value', $session->get('key'));
$session->delete('key');
$this->assertNull($session->get('key'));
$session->set('key1', 'value1');
$session->set('key2', 'value2');
$session->clear();
$this->assertNull($session->get('key1'));
$this->assertNull($session->get('key2'));
}
/**
* Test reading and writing session data without encryption.
*/
public function testReadWriteWithoutEncryption(): void
{
// First session instance to write data
$session1 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]); // No encryption, no auto-commit
$session1->set('key', 'value');
$session1->commit();
// Simulate a new request with a second instance
$session2 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]);
$this->assertEquals('value', $session2->get('key'));
}
/**
* Test reading and writing session data with encryption.
*/
public function testReadWriteWithEncryption(): void
{
// First session instance with encryption
$session1 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]);
$session1->set('key', 'secret');
$session1->commit();
// Simulate a new request with encryption
$session2 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]);
$this->assertEquals('secret', $session2->get('key'));
}
/**
* Test that auto-commit saves session data on shutdown.
*/
public function testAutoCommit(): void
{
// Session with auto-commit enabled
$session1 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => true,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]);
$session1->set('key', 'value');
// No manual commit; simulate shutdown by calling commit() manually
$session1->commit();
// Simulate a new request
$session2 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'test_session_id1234'
]);
$this->assertEquals('value', $session2->get('key'));
}
/**
* Test garbage collection of expired session files.
*/
public function testGarbageCollection(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'start_session' => false,
'test_mode' => true
]);
$file = $this->tempDir . '/sess_testfile';
// Create an expired session file
file_put_contents($file, 'Ptest'); // Simple content with 'P' prefix
touch($file, time() - 3600); // Set file to 1 hour old
$result = $session->gc(1800); // Max lifetime of 30 minutes
$this->assertEquals(1, $result); // Expect 1 file deleted
$this->assertFileDoesNotExist($file);
}
public function testGarbageCollectionWithEmptyDirectory(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'start_session' => false,
'test_mode' => true
]);
// Ensure the directory is empty
$result = $session->gc(1800); // Max lifetime of 30 minutes
$this->assertEquals(0, $result); // No files to delete
}
public function testGarbageCollectionWithNoExpiredFiles(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'start_session' => false,
'test_mode' => true
]);
$file = $this->tempDir . '/sess_testfile';
// Create a fresh session file
file_put_contents($file, 'Ptest');
touch($file, time()); // File is not expired
$result = $session->gc(1800); // Max lifetime of 30 minutes
$this->assertEquals(0, $result); // No files deleted
$this->assertFileExists($file);
}
public function testGarbageCollectionWithInvalidPath(): void
{
// Use a non-existent subdirectory to simulate failure
$session = new Session([
'save_path' => $this->tempDir . '/nonexistent',
'start_session' => false,
'test_mode' => true
]);
$result = $session->gc(1800);
$this->assertEquals(0, $result); // Expect 0 on failure
}
public function testGarbageCollectionWithNonIntegerMaxLifetime(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'start_session' => false,
'test_mode' => true
]);
$file = $this->tempDir . '/sess_testfile';
// Create an expired session file
file_put_contents($file, 'Ptest');
touch($file, time() - 3600); // 1 hour old
$result = $session->gc('1800'); // Pass a string instead of int
$this->assertEquals(1, $result); // Should still work due to PHP's type juggling
$this->assertFileDoesNotExist($file);
}
/**
* Test regenerating the session ID while preserving data.
*/
public function testRegenerateSessionId(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true
]);
$oldId = $session->id();
$session->set('key', 'value');
$session->commit();
$session->regenerate();
$newId = $session->id();
$this->assertNotEquals($oldId, $newId);
$this->assertEquals('value', $session->get('key'));
}
/**
* Test the open and close methods which are required by the SessionHandlerInterface.
*/
public function testOpenAndClose(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
// Using reflection to access these methods since they're normally called by PHP internally
$reflector = new ReflectionClass($session);
$openMethod = $reflector->getMethod('open');
$openMethod->setAccessible(true);
$this->assertTrue($openMethod->invoke($session, $this->tempDir, 'PHPSESSID'));
$closeMethod = $reflector->getMethod('close');
$closeMethod->setAccessible(true);
$this->assertTrue($closeMethod->invoke($session));
}
/**
* Test the destroy method to ensure it removes session data.
*/
public function testDestroy(): void
{
$sessionId = 'test_destroy_session';
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true,
'test_session_id' => $sessionId
]);
// Create session data
$session->set('key', 'value');
$session->commit();
// Verify the file exists
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
$this->assertFileExists($sessionFile);
// Use reflection to access destroy method
$reflector = new ReflectionClass($session);
$destroyMethod = $reflector->getMethod('destroy');
$destroyMethod->setAccessible(true);
$result = $destroyMethod->invoke($session, $sessionId);
$this->assertTrue($result);
$this->assertFileDoesNotExist($sessionFile);
// Check that internal data was cleared
$this->assertNull($session->get('key'));
}
/**
* Test reading from empty or invalid session files.
*/
public function testReadWithInvalidContent(): void
{
$sessionId = 'test_invalid_content';
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
// Create empty file
file_put_contents($sessionFile, '');
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true,
'test_session_id' => $sessionId
]);
// Data should be empty array when file is empty
$this->assertNull($session->get('any_key'));
// Try with invalid content too short for proper format
file_put_contents($sessionFile, 'X');
$session2 = new Session([
'save_path' => $this->tempDir,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$this->assertNull($session2->get('any_key'));
}
/**
* Test mismatch between encryption state and file prefix.
*/
public function testReadWithPrefixMismatch(): void
{
$sessionId = 'test_prefix_mismatch';
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
// Create file with E prefix but we'll read without encryption key
$data = serialize(['key' => 'value']);
file_put_contents($sessionFile, 'E' . str_repeat('0', 16) . 'dummy_encrypted_data');
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true,
'test_session_id' => $sessionId
]);
// Data should be empty when prefix doesn't match encryption state
$this->assertNull($session->get('key'));
// Now try P prefix with encryption
file_put_contents($sessionFile, 'P' . serialize(['key' => 'value']));
$session2 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => $this->encryptionKey,
'test_mode' => true,
'test_session_id' => $sessionId
]);
// Data should be empty when prefix doesn't match encryption state
$this->assertNull($session2->get('key'));
}
/**
* Test write method with encryption failure.
*/
public function testWriteWithEncryptionFailure(): void
{
// We need to mock openssl_encrypt to simulate failure
$sessionId = 'test_encryption_failure';
// Create a partial mock of the Session class
$session = $this->getMockBuilder(Session::class)
->setConstructorArgs([
[
'save_path' => $this->tempDir,
'encryption_key' => 'invalid_key_for_test',
'test_mode' => true,
'test_session_id' => $sessionId,
'serialization' => 'php'
]
])
->onlyMethods(['encryptData'])
->getMock();
// Set up the mock to simulate encryption failure
$session->method('encryptData')->willReturn(false);
// Use reflection to make encryptData accessible and inject it
$reflector = new ReflectionClass(Session::class);
$writeMethod = $reflector->getMethod('write');
$writeMethod->setAccessible(true);
// Set some data and mark as changed
$session->set('key', 'value');
// Write should return false when encryption fails
$result = $writeMethod->invoke($session, $sessionId, 'data');
$this->assertFalse($result);
}
/**
* Test write method when no changes were made.
*/
public function testWriteWithNoChanges(): void
{
$sessionId = 'test_no_changes';
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$session->set('key', 'value');
$session->commit(); // Commit to mark as changed
$result = $session->write($sessionId, '');
$this->assertTrue($result);
}
public function testGetAll(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'encryption_key' => null,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true
]);
$session->set('key1', 'value1');
$session->set('key2', 'value2');
$expectedData = [
'key1' => 'value1',
'key2' => 'value2'
];
$this->assertEquals($expectedData, $session->getAll());
}
/**
* Test reading and writing session data with JSON serialization (default, no encryption).
*/
public function testReadWriteJsonDefault(): void
{
$sessionId = 'json_default_id';
$session1 = new Session([
'save_path' => $this->tempDir,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$session1->set('foo', 'bar');
$session1->commit();
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
$fileContents = file_get_contents($sessionFile);
$this->assertSame('J', $fileContents[0]);
$data = json_decode(substr($fileContents, 1), true);
$this->assertEquals(['foo' => 'bar'], $data);
$session2 = new Session([
'save_path' => $this->tempDir,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$this->assertEquals('bar', $session2->get('foo'));
}
/**
* Test reading and writing session data with JSON serialization and encryption.
*/
public function testReadWriteJsonWithEncryption(): void
{
$sessionId = 'json_enc_id';
$session1 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$session1->set('foo', 'secret');
$session1->commit();
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
$fileContents = file_get_contents($sessionFile);
$this->assertSame('F', $fileContents[0]);
$iv = substr($fileContents, 1, 16);
$encrypted = substr($fileContents, 17);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->encryptionKey, 0, $iv);
$this->assertNotFalse($decrypted);
$data = json_decode($decrypted, true);
$this->assertEquals(['foo' => 'secret'], $data);
// Ensure encrypted data is not trivially readable
$this->assertStringNotContainsString('secret', $encrypted);
$session2 = new Session([
'save_path' => $this->tempDir,
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$this->assertEquals('secret', $session2->get('foo'));
}
/**
* Test reading and writing session data with PHP serialization (legacy, no encryption).
*/
public function testReadWritePhpSerialization(): void
{
$sessionId = 'php_plain_id';
$session1 = new Session([
'save_path' => $this->tempDir,
'serialization' => 'php',
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$session1->set('foo', 'bar');
$session1->commit();
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
$fileContents = file_get_contents($sessionFile);
$this->assertSame('P', $fileContents[0]);
$data = unserialize(substr($fileContents, 1));
$this->assertEquals(['foo' => 'bar'], $data);
$session2 = new Session([
'save_path' => $this->tempDir,
'serialization' => 'php',
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$this->assertEquals('bar', $session2->get('foo'));
}
/**
* Test reading and writing session data with PHP serialization and encryption (legacy).
*/
public function testReadWritePhpWithEncryption(): void
{
$sessionId = 'php_enc_id';
$session1 = new Session([
'save_path' => $this->tempDir,
'serialization' => 'php',
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$session1->set('foo', 'secret');
$session1->commit();
$sessionFile = $this->tempDir . '/sess_' . $sessionId;
$fileContents = file_get_contents($sessionFile);
$this->assertSame('E', $fileContents[0]);
$iv = substr($fileContents, 1, 16);
$encrypted = substr($fileContents, 17);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->encryptionKey, 0, $iv);
$this->assertNotFalse($decrypted);
$data = unserialize($decrypted);
$this->assertEquals(['foo' => 'secret'], $data);
// Ensure encrypted data is not trivially readable
$this->assertStringNotContainsString('secret', $encrypted);
$session2 = new Session([
'save_path' => $this->tempDir,
'serialization' => 'php',
'encryption_key' => $this->encryptionKey,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => $sessionId
]);
$this->assertEquals('secret', $session2->get('foo'));
}
/**
* Test that storing an object with JSON serialization throws an exception.
*/
public function testJsonSerializationRejectsObjects(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'auto_commit' => false,
'start_session' => false,
'test_mode' => true,
'test_session_id' => 'json_obj_id'
]);
$this->expectException(\InvalidArgumentException::class);
$session->set('obj', new \stdClass());
$session->commit();
}
public function testInvalidSerializationThrows(): void
{
$this->expectException(\InvalidArgumentException::class);
new Session([
'save_path' => $this->tempDir,
'serialization' => 'invalid',
'test_mode' => true
]);
}
public function testRegenerateInTestModeChangesId(): void
{
$session = new Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
$oldId = $session->id();
$session->regenerate();
$newId = $session->id();
$this->assertNotEquals($oldId, $newId);
$this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $newId);
}
public function testAssertNoObjectsWithDeepArrayNoObjects(): void
{
$session = new \flight\Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
// Should not throw
$session->set('deep', [[['foo' => 'bar'], ['baz' => 123]], []]);
$session->commit();
$this->assertEquals([[['foo' => 'bar'], ['baz' => 123]], []], $session->get('deep'));
}
public function testAssertNoObjectsWithObjectNested(): void
{
$session = new \flight\Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
$nested = [[['foo' => new \stdClass()]]];
$this->expectException(\InvalidArgumentException::class);
$session->set('deep', $nested);
$session->commit();
}
public function testAssertNoObjectsWithEmptyArray(): void
{
$session = new \flight\Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
$session->set('empty', []);
$session->commit();
$this->assertEquals([], $session->get('empty'));
}
public function testAssertNoObjectsWithObjectInMixedArray(): void
{
$session = new \flight\Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
$mixed = ['a' => 1, 'b' => [2, new \stdClass()]];
$this->expectException(\InvalidArgumentException::class);
$session->set('mixed', $mixed);
$session->commit();
}
public function testAssertNoObjectsWithPrimitive(): void
{
$session = new \flight\Session([
'save_path' => $this->tempDir,
'test_mode' => true
]);
$session->set('primitive', 42);
$session->commit();
$this->assertEquals(42, $session->get('primitive'));
}
}