summaryrefslogtreecommitdiff
path: root/src/tests/session_encryption
diff options
context:
space:
mode:
authorjvoisin2019-01-14 19:29:25 +0000
committerGitHub2019-01-14 19:29:25 +0000
commite79f7e3bd992c7f0915ef9afe7afb6d79740527a (patch)
treef881c25694eb00da2331a9ab280ec1c24a5662ab /src/tests/session_encryption
parentc943db586ac46b686b49bdf61d8473e39dd93000 (diff)
Reorganize the testsuite
Splitting the testsuite in several components makes it easier to manage and comprehend. This was also needed some some tests aren't passing on Alpine Linux, but we still want to run as many of them as we can on this platform.
Diffstat (limited to 'src/tests/session_encryption')
-rw-r--r--src/tests/session_encryption/config/config_crypt_session.ini2
-rw-r--r--src/tests/session_encryption/config/config_crypt_session_simul.ini3
-rw-r--r--src/tests/session_encryption/crypt_session_corrupted_session.phpt30
-rw-r--r--src/tests/session_encryption/crypt_session_invalid.phpt24
-rw-r--r--src/tests/session_encryption/crypt_session_invalid_simul.phpt27
-rw-r--r--src/tests/session_encryption/crypt_session_read_uncrypt.phpt33
-rw-r--r--src/tests/session_encryption/crypt_session_valid.phpt27
-rw-r--r--src/tests/session_encryption/crypt_session_valid_simul.phpt27
-rw-r--r--src/tests/session_encryption/set_custom_session_handler.phpt72
-rw-r--r--src/tests/session_encryption/set_custom_session_handler2.phpt26
-rw-r--r--src/tests/session_encryption/set_custom_session_handler_ini.phpt17
11 files changed, 288 insertions, 0 deletions
diff --git a/src/tests/session_encryption/config/config_crypt_session.ini b/src/tests/session_encryption/config/config_crypt_session.ini
new file mode 100644
index 0000000..14b0c2c
--- /dev/null
+++ b/src/tests/session_encryption/config/config_crypt_session.ini
@@ -0,0 +1,2 @@
1sp.global.secret_key("abcdef").cookie_env_var("REMOTE_ADDR");
2sp.session.encrypt(); \ No newline at end of file
diff --git a/src/tests/session_encryption/config/config_crypt_session_simul.ini b/src/tests/session_encryption/config/config_crypt_session_simul.ini
new file mode 100644
index 0000000..fbd43eb
--- /dev/null
+++ b/src/tests/session_encryption/config/config_crypt_session_simul.ini
@@ -0,0 +1,3 @@
1sp.global.secret_key("abcdef").cookie_env_var("REMOTE_ADDR");
2sp.session.encrypt();
3sp.session.simulation(); \ No newline at end of file
diff --git a/src/tests/session_encryption/crypt_session_corrupted_session.phpt b/src/tests/session_encryption/crypt_session_corrupted_session.phpt
new file mode 100644
index 0000000..5853efd
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_corrupted_session.phpt
@@ -0,0 +1,30 @@
1--TEST--
2Set a custom session handler
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7session.save_path = "/tmp"
8--ENV--
9return <<<EOF
10REMOTE_ADDR=127.0.0.1
11EOF;
12--FILE--
13<?php
14
15session_start(); // Start new_session , it will read an empty session
16$_SESSION["tete"] = "titi"; // Encrypt and write the session
17$id = session_id(); // Get the session_id to use it later
18$filename = session_save_path() . '/sess_' . $id;
19session_write_close();
20
21$file_handle = fopen($filename, 'w');
22fwrite($file_handle, 'toto|s:4:"tata";');
23fclose($file_handle);
24
25session_id($id);
26session_start();
27var_dump($_SESSION);
28?>
29--EXPECTF--
30Fatal error: [snuffleupagus][cookie_encryption] Buffer underflow tentative detected in cookie encryption handling in %s/crypt_session_corrupted_session.php on line %s
diff --git a/src/tests/session_encryption/crypt_session_invalid.phpt b/src/tests/session_encryption/crypt_session_invalid.phpt
new file mode 100644
index 0000000..8a57149
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_invalid.phpt
@@ -0,0 +1,24 @@
1--TEST--
2SESSION crypt and bad decrypt
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7--ENV--
8return <<<EOF
9REMOTE_ADDR=127.0.0.1
10EOF;
11--FILE--
12<?php
13// Do it like that to write (encrypt) the session and then to read (decrypt) the session
14session_start(); // Start new_session , it will read an empty session
15$_SESSION["toto"] = "tata"; // Encrypt and write the session
16$id = session_id(); // Get the session_id to use it later
17session_write_close(); // Close the session
18putenv("REMOTE_ADDR=127.0.0.2");
19session_id($id); // Recover the session with the previous session_id
20session_start(); // Re start the session, It will read and decrypt the non empty session
21var_dump($_SESSION); // Dump the session
22?>
23--EXPECTF--
24Warning: [snuffleupagus][cookie_encryption] Something went wrong with the decryption of the session in %s/crypt_session_invalid.php on line %d
diff --git a/src/tests/session_encryption/crypt_session_invalid_simul.phpt b/src/tests/session_encryption/crypt_session_invalid_simul.phpt
new file mode 100644
index 0000000..7bfefcb
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_invalid_simul.phpt
@@ -0,0 +1,27 @@
1--TEST--
2SESSION crypt and bad decrypt
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session_simul.ini
7--ENV--
8return <<<EOF
9REMOTE_ADDR=127.0.0.1
10EOF;
11--FILE--
12<?php
13// Do it like that to write (encrypt) the session and then to read (decrypt) the session
14session_start(); // Start new_session , it will read an empty session
15$_SESSION["toto"] = "tata"; // Encrypt and write the session
16$id = session_id(); // Get the session_id to use it later
17session_write_close(); // Close the session
18putenv("REMOTE_ADDR=127.0.0.2");
19session_id($id); // Recover the session with the previous session_id
20session_start(); // Re start the session, It will read and decrypt the non empty session
21var_dump($_SESSION); // Dump the session
22?>
23--EXPECTF--
24array(1) {
25 ["toto"]=>
26 string(4) "tata"
27}
diff --git a/src/tests/session_encryption/crypt_session_read_uncrypt.phpt b/src/tests/session_encryption/crypt_session_read_uncrypt.phpt
new file mode 100644
index 0000000..f15d8b6
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_read_uncrypt.phpt
@@ -0,0 +1,33 @@
1--TEST--
2SESSION crypt/decrypt valid
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session_simul.ini
7--ENV--
8return <<<EOF
9REMOTE_ADDR=127.0.0.1
10EOF;
11--FILE--
12<?php
13$current_path = dirname(getcwd()) . "/src/tests/" ;
14ini_set("session.save_path", $current_path);
15
16session_start();
17$id = session_id(); // Get the session_id to use it later
18$filename_sess = $current_path . "sess_" . $id;
19file_put_contents($filename_sess, "toto|s:4:\"tata\";"); // Write a unencrypted session
20session_write_close(); // Close the session
21
22session_id($id);
23session_start(); // Try to read the unencrypted session, it will fail to decrypt but it must return the session
24var_dump($_SESSION);
25echo "OK";
26unlink($filename_sess);
27?>
28--EXPECTF--
29array(1) {
30 ["toto"]=>
31 string(4) "tata"
32}
33OK
diff --git a/src/tests/session_encryption/crypt_session_valid.phpt b/src/tests/session_encryption/crypt_session_valid.phpt
new file mode 100644
index 0000000..bf9fea0
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_valid.phpt
@@ -0,0 +1,27 @@
1--TEST--
2SESSION crypt/decrypt valid
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7--ENV--
8return <<<EOF
9REMOTE_ADDR=127.0.0.1
10EOF;
11--FILE--
12<?php
13// Do it like that to write (encrypt) the session and then to read (decrypt) the session
14session_start(); // Start new_session , it will read an empty session
15$_SESSION["toto"] = "tata"; // Encrypt and write the session
16$id = session_id(); // Get the session_id to use it later
17
18session_write_close(); // Close the session
19session_id($id); // Recover the session with the previous session_id
20session_start(); // Re start the session, It will read and decrypt the non empty session
21var_dump($_SESSION); // Dump the session
22?>
23--EXPECTF--
24array(1) {
25 ["toto"]=>
26 string(4) "tata"
27}
diff --git a/src/tests/session_encryption/crypt_session_valid_simul.phpt b/src/tests/session_encryption/crypt_session_valid_simul.phpt
new file mode 100644
index 0000000..28083cf
--- /dev/null
+++ b/src/tests/session_encryption/crypt_session_valid_simul.phpt
@@ -0,0 +1,27 @@
1--TEST--
2SESSION crypt/decrypt valid
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session_simul.ini
7--ENV--
8return <<<EOF
9REMOTE_ADDR=127.0.0.1
10EOF;
11--FILE--
12<?php
13// Do it like that to write (encrypt) the session and then to read (decrypt) the session
14session_start(); // Start new_session , it will read an empty session
15$_SESSION["toto"] = "tata"; // Encrypt and write the session
16$id = session_id(); // Get the session_id to use it later
17session_write_close(); // Close the session
18
19session_id($id); // Recover the session with the previous session_id
20session_start(); // Re start the session, It will read and decrypt the non empty session
21var_dump($_SESSION); // Dump the session
22?>
23--EXPECTF--
24array(1) {
25 ["toto"]=>
26 string(4) "tata"
27}
diff --git a/src/tests/session_encryption/set_custom_session_handler.phpt b/src/tests/session_encryption/set_custom_session_handler.phpt
new file mode 100644
index 0000000..5b46fbc
--- /dev/null
+++ b/src/tests/session_encryption/set_custom_session_handler.phpt
@@ -0,0 +1,72 @@
1--TEST--
2Set a custom session handler
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7session.save_path = "/tmp"
8--ENV--
9return <<<EOF
10REMOTE_ADDR=127.0.0.1
11EOF;
12--FILE--
13<?php
14class FileSessionHandler {
15 private $savePath;
16
17 function open($savePath, $sessionName) {
18 $this->savePath = $savePath;
19 if (!is_dir($this->savePath)) {
20 mkdir($this->savePath, 0777);
21 }
22
23 return true;
24 }
25
26 function close() {
27 return true;
28 }
29
30 function read($id) {
31 return (string)@file_get_contents("$this->savePath/sess_$id");
32 }
33
34 function write($id, $data) {
35 return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
36 }
37
38 function destroy($id) {
39 $file = "$this->savePath/sess_$id";
40 if (file_exists($file)) {
41 unlink($file);
42 }
43
44 return true;
45 }
46
47 function gc($maxlifetime) {
48 foreach (glob("$this->savePath/sess_*") as $file) {
49 if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
50 unlink($file);
51 }
52 }
53 return true;
54 }
55}
56
57$handler = new FileSessionHandler();
58session_set_save_handler(
59 array($handler, 'open'),
60 array($handler, 'close'),
61 array($handler, 'read'),
62 array($handler, 'write'),
63 array($handler, 'destroy'),
64 array($handler, 'gc')
65 );
66
67// the following prevents unexpected effects when using objects as save handlers
68register_shutdown_function('session_write_close');
69
70session_start();
71// proceed to set and retrieve values by key from $_SESSION
72--EXPECTF--
diff --git a/src/tests/session_encryption/set_custom_session_handler2.phpt b/src/tests/session_encryption/set_custom_session_handler2.phpt
new file mode 100644
index 0000000..18bc3f7
--- /dev/null
+++ b/src/tests/session_encryption/set_custom_session_handler2.phpt
@@ -0,0 +1,26 @@
1--TEST--
2Set a custom session handler, twice
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7session.save_path = "/tmp"
8--ENV--
9return <<<EOF
10REMOTE_ADDR=127.0.0.1
11EOF;
12--FILE--
13<?php
14session_set_save_handler(new SessionHandler(), true);
15session_start();
16$_SESSION['a'] = 'b';
17#var_dump($_SESSION);
18session_destroy();
19session_set_save_handler(new SessionHandler(), true);
20session_start();
21$_SESSION['a'] = 'b';
22var_dump($_SESSION);
23session_destroy();
24?>
25--EXPECTF--
26%a
diff --git a/src/tests/session_encryption/set_custom_session_handler_ini.phpt b/src/tests/session_encryption/set_custom_session_handler_ini.phpt
new file mode 100644
index 0000000..7ed56d6
--- /dev/null
+++ b/src/tests/session_encryption/set_custom_session_handler_ini.phpt
@@ -0,0 +1,17 @@
1--TEST--
2Set a custom session handler
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
5--INI--
6sp.configuration_file={PWD}/config/config_crypt_session.ini
7session.save_handler =
8--ENV--
9return <<<EOF
10REMOTE_ADDR=127.0.0.1
11EOF;
12--FILE--
13<?php
14echo "win";
15?>
16--EXPECT--
17win