diff options
| author | jvoisin | 2019-01-14 19:29:25 +0000 |
|---|---|---|
| committer | GitHub | 2019-01-14 19:29:25 +0000 |
| commit | e79f7e3bd992c7f0915ef9afe7afb6d79740527a (patch) | |
| tree | f881c25694eb00da2331a9ab280ec1c24a5662ab /src/tests/session_encryption | |
| parent | c943db586ac46b686b49bdf61d8473e39dd93000 (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')
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 @@ | |||
| 1 | sp.global.secret_key("abcdef").cookie_env_var("REMOTE_ADDR"); | ||
| 2 | sp.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 @@ | |||
| 1 | sp.global.secret_key("abcdef").cookie_env_var("REMOTE_ADDR"); | ||
| 2 | sp.session.encrypt(); | ||
| 3 | sp.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-- | ||
| 2 | Set a custom session handler | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | session.save_path = "/tmp" | ||
| 8 | --ENV-- | ||
| 9 | return <<<EOF | ||
| 10 | REMOTE_ADDR=127.0.0.1 | ||
| 11 | EOF; | ||
| 12 | --FILE-- | ||
| 13 | <?php | ||
| 14 | |||
| 15 | session_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; | ||
| 19 | session_write_close(); | ||
| 20 | |||
| 21 | $file_handle = fopen($filename, 'w'); | ||
| 22 | fwrite($file_handle, 'toto|s:4:"tata";'); | ||
| 23 | fclose($file_handle); | ||
| 24 | |||
| 25 | session_id($id); | ||
| 26 | session_start(); | ||
| 27 | var_dump($_SESSION); | ||
| 28 | ?> | ||
| 29 | --EXPECTF-- | ||
| 30 | Fatal 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-- | ||
| 2 | SESSION crypt and bad decrypt | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | --ENV-- | ||
| 8 | return <<<EOF | ||
| 9 | REMOTE_ADDR=127.0.0.1 | ||
| 10 | EOF; | ||
| 11 | --FILE-- | ||
| 12 | <?php | ||
| 13 | // Do it like that to write (encrypt) the session and then to read (decrypt) the session | ||
| 14 | session_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 | session_write_close(); // Close the session | ||
| 18 | putenv("REMOTE_ADDR=127.0.0.2"); | ||
| 19 | session_id($id); // Recover the session with the previous session_id | ||
| 20 | session_start(); // Re start the session, It will read and decrypt the non empty session | ||
| 21 | var_dump($_SESSION); // Dump the session | ||
| 22 | ?> | ||
| 23 | --EXPECTF-- | ||
| 24 | Warning: [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-- | ||
| 2 | SESSION crypt and bad decrypt | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session_simul.ini | ||
| 7 | --ENV-- | ||
| 8 | return <<<EOF | ||
| 9 | REMOTE_ADDR=127.0.0.1 | ||
| 10 | EOF; | ||
| 11 | --FILE-- | ||
| 12 | <?php | ||
| 13 | // Do it like that to write (encrypt) the session and then to read (decrypt) the session | ||
| 14 | session_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 | session_write_close(); // Close the session | ||
| 18 | putenv("REMOTE_ADDR=127.0.0.2"); | ||
| 19 | session_id($id); // Recover the session with the previous session_id | ||
| 20 | session_start(); // Re start the session, It will read and decrypt the non empty session | ||
| 21 | var_dump($_SESSION); // Dump the session | ||
| 22 | ?> | ||
| 23 | --EXPECTF-- | ||
| 24 | array(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-- | ||
| 2 | SESSION crypt/decrypt valid | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session_simul.ini | ||
| 7 | --ENV-- | ||
| 8 | return <<<EOF | ||
| 9 | REMOTE_ADDR=127.0.0.1 | ||
| 10 | EOF; | ||
| 11 | --FILE-- | ||
| 12 | <?php | ||
| 13 | $current_path = dirname(getcwd()) . "/src/tests/" ; | ||
| 14 | ini_set("session.save_path", $current_path); | ||
| 15 | |||
| 16 | session_start(); | ||
| 17 | $id = session_id(); // Get the session_id to use it later | ||
| 18 | $filename_sess = $current_path . "sess_" . $id; | ||
| 19 | file_put_contents($filename_sess, "toto|s:4:\"tata\";"); // Write a unencrypted session | ||
| 20 | session_write_close(); // Close the session | ||
| 21 | |||
| 22 | session_id($id); | ||
| 23 | session_start(); // Try to read the unencrypted session, it will fail to decrypt but it must return the session | ||
| 24 | var_dump($_SESSION); | ||
| 25 | echo "OK"; | ||
| 26 | unlink($filename_sess); | ||
| 27 | ?> | ||
| 28 | --EXPECTF-- | ||
| 29 | array(1) { | ||
| 30 | ["toto"]=> | ||
| 31 | string(4) "tata" | ||
| 32 | } | ||
| 33 | OK | ||
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-- | ||
| 2 | SESSION crypt/decrypt valid | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | --ENV-- | ||
| 8 | return <<<EOF | ||
| 9 | REMOTE_ADDR=127.0.0.1 | ||
| 10 | EOF; | ||
| 11 | --FILE-- | ||
| 12 | <?php | ||
| 13 | // Do it like that to write (encrypt) the session and then to read (decrypt) the session | ||
| 14 | session_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 | |||
| 18 | session_write_close(); // Close the session | ||
| 19 | session_id($id); // Recover the session with the previous session_id | ||
| 20 | session_start(); // Re start the session, It will read and decrypt the non empty session | ||
| 21 | var_dump($_SESSION); // Dump the session | ||
| 22 | ?> | ||
| 23 | --EXPECTF-- | ||
| 24 | array(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-- | ||
| 2 | SESSION crypt/decrypt valid | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session_simul.ini | ||
| 7 | --ENV-- | ||
| 8 | return <<<EOF | ||
| 9 | REMOTE_ADDR=127.0.0.1 | ||
| 10 | EOF; | ||
| 11 | --FILE-- | ||
| 12 | <?php | ||
| 13 | // Do it like that to write (encrypt) the session and then to read (decrypt) the session | ||
| 14 | session_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 | session_write_close(); // Close the session | ||
| 18 | |||
| 19 | session_id($id); // Recover the session with the previous session_id | ||
| 20 | session_start(); // Re start the session, It will read and decrypt the non empty session | ||
| 21 | var_dump($_SESSION); // Dump the session | ||
| 22 | ?> | ||
| 23 | --EXPECTF-- | ||
| 24 | array(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-- | ||
| 2 | Set a custom session handler | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | session.save_path = "/tmp" | ||
| 8 | --ENV-- | ||
| 9 | return <<<EOF | ||
| 10 | REMOTE_ADDR=127.0.0.1 | ||
| 11 | EOF; | ||
| 12 | --FILE-- | ||
| 13 | <?php | ||
| 14 | class 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(); | ||
| 58 | session_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 | ||
| 68 | register_shutdown_function('session_write_close'); | ||
| 69 | |||
| 70 | session_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-- | ||
| 2 | Set a custom session handler, twice | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | session.save_path = "/tmp" | ||
| 8 | --ENV-- | ||
| 9 | return <<<EOF | ||
| 10 | REMOTE_ADDR=127.0.0.1 | ||
| 11 | EOF; | ||
| 12 | --FILE-- | ||
| 13 | <?php | ||
| 14 | session_set_save_handler(new SessionHandler(), true); | ||
| 15 | session_start(); | ||
| 16 | $_SESSION['a'] = 'b'; | ||
| 17 | #var_dump($_SESSION); | ||
| 18 | session_destroy(); | ||
| 19 | session_set_save_handler(new SessionHandler(), true); | ||
| 20 | session_start(); | ||
| 21 | $_SESSION['a'] = 'b'; | ||
| 22 | var_dump($_SESSION); | ||
| 23 | session_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-- | ||
| 2 | Set a custom session handler | ||
| 3 | --SKIPIF-- | ||
| 4 | <?php if (!extension_loaded("snuffleupagus")) die "skip"; ?> | ||
| 5 | --INI-- | ||
| 6 | sp.configuration_file={PWD}/config/config_crypt_session.ini | ||
| 7 | session.save_handler = | ||
| 8 | --ENV-- | ||
| 9 | return <<<EOF | ||
| 10 | REMOTE_ADDR=127.0.0.1 | ||
| 11 | EOF; | ||
| 12 | --FILE-- | ||
| 13 | <?php | ||
| 14 | echo "win"; | ||
| 15 | ?> | ||
| 16 | --EXPECT-- | ||
| 17 | win | ||
