a tiny mvc framework for php using php-activerecord
1<?php
2
3require(__DIR__ . "/../lib/halfmoon.php");
4
5HalfMoon\Config::set_session_store("encrypted_cookie",
6 array("encryption_key" => str_repeat("0", 32)));
7
8class EncryptedCookieTest extends PHPUnit_Framework_TestCase {
9 static $str = "australia's darrell lea soft eating liquorice";
10 static $key = "3d737148b5d7c1a08e0e92d26f8d020b";
11 static $cookie = "test";
12
13 public function setupSS($key, $cookie) {
14 $this->ss = new HalfMoon\EncryptedCookieSessionStore($key);
15 $this->ss->open("", $cookie);
16 }
17
18 public function testCookieEncryptionAndDecryption() {
19 for ($z = 0; $z < 5000; $z++) {
20 $key = bin2hex(openssl_random_pseudo_bytes(16));
21 $this->setupSS($key, "test_" . $z);
22
23 $ki = rand(20, 40);
24 for ($k = "", $x = 0; $x++ < $ki; $k .= bin2hex(chr(mt_rand(0,255))))
25 ;
26
27 $vi = rand(20, 500);
28 for ($v = "", $x = 0; $x++ < $vi; $v .= bin2hex(chr(mt_rand(0,255))))
29 ;
30
31 $data = var_export(array($k, $v), true);
32 $this->ss->write("", $data);
33
34 $this->setupSS($key, "test_" . $z);
35 $dec_data = $this->ss->read("");
36 $this->assertEquals($data, $dec_data);
37 }
38 }
39
40 public function testExistingDecryption() {
41 $this->setupSS(static::$key, static::$cookie);
42 $this->ss->write("", static::$str);
43 $enc = $_COOKIE[static::$cookie];
44 $this->assertEquals(0, preg_match("/liquorice/", $enc));
45
46 $this->setupSS(static::$key, static::$cookie);
47 $_COOKIE[static::$cookie] = $enc;
48 $this->assertEquals(static::$str, $this->ss->read(""));
49 }
50
51 public function testBadKey() {
52 $this->setupSS(static::$key, static::$cookie);
53 $this->ss->write("", static::$str);
54 $enc = $_COOKIE[static::$cookie];
55
56 $this->setupSS(str_replace("3", "4", static::$key), static::$cookie);
57 $_COOKIE[static::$cookie] = $enc;
58 $this->assertEquals("", $this->ss->read(""));
59 }
60
61 /**
62 * @expectedException HalfMoon\InvalidCookieData
63 */
64 public function testBadData() {
65 $this->setupSS(static::$key, static::$cookie);
66 $this->ss->write("", static::$str);
67 $enc = $_COOKIE[static::$cookie];
68
69 $this->setupSS(static::$key, static::$cookie);
70 $_COOKIE[static::$cookie] = substr($enc, 0, strlen($enc) - 5);
71 $this->assertEquals("", $this->ss->read(""));
72 }
73}
74
75?>