summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Encrypt_test.php
blob: adbca31b24f214090afc07dd4a2718e7c592b169 (plain)
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
<?php
/**
 * @requires extension mcrypt
 */
class Encrypt_test extends CI_TestCase {

	public function set_up()
	{
		if ( ! extension_loaded('mcrypt'))
		{
			return;
		}
		elseif (version_compare(PHP_VERSION, '7.1.0-alpha', '>='))
		{
			return $this->markTestSkipped('ext/mcrypt is deprecated since PHP 7.1 and will generate notices here.');
		}

		$this->encrypt = new Mock_Libraries_Encrypt();
		$this->ci_instance_var('encrypt', $this->encrypt);

		$this->ci_set_config('encryption_key', "Encryptin'glike@boss!");
		$this->msg = 'My secret message';
	}

	// --------------------------------------------------------------------

	public function test_encode()
	{
		$this->assertNotEquals($this->msg, $this->encrypt->encode($this->msg));
	}

	// --------------------------------------------------------------------

	public function test_decode()
	{
		$encoded_msg = $this->encrypt->encode($this->msg);
		$this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg));
	}

	// --------------------------------------------------------------------

	public function test_optional_key()
	{
		$key = 'Ohai!ù0129°03182%HD1892P0';
		$encoded_msg = $this->encrypt->encode($this->msg, $key);
		$this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg, $key));
	}

	// --------------------------------------------------------------------

	public function test_default_cipher()
	{
		$this->assertEquals('rijndael-256', $this->encrypt->get_cipher());
	}

	// --------------------------------------------------------------------

	public function test_set_cipher()
	{
		$this->encrypt->set_cipher(MCRYPT_BLOWFISH);
		$this->assertEquals('blowfish', $this->encrypt->get_cipher());
	}

	// --------------------------------------------------------------------

	public function test_default_mode()
	{
		$this->assertEquals('cbc', $this->encrypt->get_mode());
	}

	// --------------------------------------------------------------------

	public function test_set_mode()
	{
		$this->encrypt->set_mode(MCRYPT_MODE_CFB);
		$this->assertEquals('cfb', $this->encrypt->get_mode());
	}

}