blob: adbca31b24f214090afc07dd4a2718e7c592b169 [file] [log] [blame]
Joffrey Jaffeux9d140752012-06-06 01:40:18 +02001<?php
Andrey Andreev43090cc2014-08-14 12:22:07 +03002/**
3 * @requires extension mcrypt
4 */
Joffrey Jaffeux9d140752012-06-06 01:40:18 +02005class Encrypt_test extends CI_TestCase {
6
Andrey Andreevc1862882012-06-09 23:16:58 +03007 public function set_up()
8 {
Andrey Andreev43090cc2014-08-14 12:22:07 +03009 if ( ! extension_loaded('mcrypt'))
10 {
11 return;
12 }
Andrey Andreevc95df512016-08-22 13:19:24 +030013 elseif (version_compare(PHP_VERSION, '7.1.0-alpha', '>='))
14 {
15 return $this->markTestSkipped('ext/mcrypt is deprecated since PHP 7.1 and will generate notices here.');
16 }
Andrey Andreev43090cc2014-08-14 12:22:07 +030017
dchill427ecc5cd2012-10-12 16:25:51 -040018 $this->encrypt = new Mock_Libraries_Encrypt();
19 $this->ci_instance_var('encrypt', $this->encrypt);
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020020
Andrey Andreevc1862882012-06-09 23:16:58 +030021 $this->ci_set_config('encryption_key', "Encryptin'glike@boss!");
22 $this->msg = 'My secret message';
23 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020024
Andrey Andreevc1862882012-06-09 23:16:58 +030025 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020026
Andrey Andreevc1862882012-06-09 23:16:58 +030027 public function test_encode()
28 {
29 $this->assertNotEquals($this->msg, $this->encrypt->encode($this->msg));
30 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020031
Andrey Andreevc1862882012-06-09 23:16:58 +030032 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020033
Andrey Andreevc1862882012-06-09 23:16:58 +030034 public function test_decode()
35 {
36 $encoded_msg = $this->encrypt->encode($this->msg);
37 $this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg));
38 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020039
Andrey Andreevc1862882012-06-09 23:16:58 +030040 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020041
Andrey Andreevc1862882012-06-09 23:16:58 +030042 public function test_optional_key()
43 {
44 $key = 'Ohai!ù0129°03182%HD1892P0';
45 $encoded_msg = $this->encrypt->encode($this->msg, $key);
46 $this->assertEquals($this->msg, $this->encrypt->decode($encoded_msg, $key));
47 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020048
Andrey Andreevc1862882012-06-09 23:16:58 +030049 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020050
Andrey Andreevc1862882012-06-09 23:16:58 +030051 public function test_default_cipher()
52 {
53 $this->assertEquals('rijndael-256', $this->encrypt->get_cipher());
54 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020055
Andrey Andreevc1862882012-06-09 23:16:58 +030056 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020057
Andrey Andreevc1862882012-06-09 23:16:58 +030058 public function test_set_cipher()
59 {
60 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
61 $this->assertEquals('blowfish', $this->encrypt->get_cipher());
62 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020063
Andrey Andreevc1862882012-06-09 23:16:58 +030064 // --------------------------------------------------------------------
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020065
Andrey Andreevc1862882012-06-09 23:16:58 +030066 public function test_default_mode()
67 {
68 $this->assertEquals('cbc', $this->encrypt->get_mode());
69 }
70
71 // --------------------------------------------------------------------
72
73 public function test_set_mode()
74 {
75 $this->encrypt->set_mode(MCRYPT_MODE_CFB);
76 $this->assertEquals('cfb', $this->encrypt->get_mode());
77 }
Joffrey Jaffeux9d140752012-06-06 01:40:18 +020078
Andrey Andreevc95df512016-08-22 13:19:24 +030079}