diff options
Diffstat (limited to 'tests')
23 files changed, 1020 insertions, 246 deletions
diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index c98d88531..439c7fdab 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -40,6 +40,32 @@ isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Prep our test environment include_once $dir.'/mocks/core/common.php'; include_once SYSTEM_PATH.'core/Common.php'; + + +if (extension_loaded('mbstring')) +{ + defined('MB_ENABLED') OR define('MB_ENABLED', TRUE); + mb_internal_encoding('UTF-8'); + mb_substitute_character('none'); +} +else +{ + defined('MB_ENABLED') OR define('MB_ENABLED', FALSE); +} + +if (extension_loaded('iconv')) +{ + defined('ICONV_ENABLED') OR define('ICONV_ENABLED', TRUE); + iconv_set_encoding('internal_encoding', 'UTF-8'); +} +else +{ + defined('ICONV_ENABLED') OR define('ICONV_ENABLED', FALSE); +} + +include_once SYSTEM_PATH.'core/compat/mbstring.php'; +include_once SYSTEM_PATH.'core/compat/password.php'; + include_once $dir.'/mocks/autoloader.php'; spl_autoload_register('autoload'); diff --git a/tests/codeigniter/core/Benchmark_test.php b/tests/codeigniter/core/Benchmark_test.php index aff736a40..7fd0727e2 100644 --- a/tests/codeigniter/core/Benchmark_test.php +++ b/tests/codeigniter/core/Benchmark_test.php @@ -27,10 +27,34 @@ class Benchmark_test extends CI_TestCase { $this->assertEmpty($this->benchmark->elapsed_time('undefined_point')); $this->benchmark->mark('code_start'); - sleep(1); $this->benchmark->mark('code_end'); + // Override values, because time isn't testable, but make sure the markers were set + if (isset($this->benchmark->marker['code_start']) && is_float($this->benchmark->marker['code_start'])) + { + $this->benchmark->marker['code_start'] = 1389956144.1944; + } + + if (isset($this->benchmark->marker['code_end']) && is_float($this->benchmark->marker['code_end'])) + { + $this->benchmark->marker['code_end'] = 1389956145.1946; + } + $this->assertEquals('1', $this->benchmark->elapsed_time('code_start', 'code_end', 0)); + $this->assertEquals('1.0', $this->benchmark->elapsed_time('code_start', 'code_end', 1)); + $this->assertEquals('1.00', $this->benchmark->elapsed_time('code_start', 'code_end', 2)); + $this->assertEquals('1.000', $this->benchmark->elapsed_time('code_start', 'code_end', 3)); + $this->assertEquals('1.0002', $this->benchmark->elapsed_time('code_start', 'code_end', 4)); + $this->assertEquals('1.0002', $this->benchmark->elapsed_time('code_start', 'code_end')); + + // Test with non-existing 2nd marker, but again - we need to override the value + $this->benchmark->elapsed_time('code_start', 'code_end2'); + if (isset($this->benchmark->marker['code_end2']) && is_float($this->benchmark->marker['code_end2'])) + { + $this->benchmark->marker['code_end2'] = 1389956146.2046; + } + + $this->assertEquals('2.0102', $this->benchmark->elapsed_time('code_start', 'code_end2')); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index ba9a2c070..6a0a7a35f 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -180,7 +180,7 @@ class Config_test extends CI_TestCase { $cfg = array( 'one' => 'prime', 'two' => 2, - 'three' => true + 'three' => TRUE ); $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config'); $this->assertTrue($this->config->load($file, TRUE)); diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 0a98e556c..95833fc91 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -138,13 +138,24 @@ class Input_test extends CI_TestCase { public function test_valid_ip() { - $ip_v4 = '192.18.0.1'; - $this->assertTrue($this->input->valid_ip($ip_v4)); + $this->assertTrue($this->input->valid_ip('192.18.0.1')); + $this->assertTrue($this->input->valid_ip('192.18.0.1', 'ipv4')); + $this->assertFalse($this->input->valid_ip('555.0.0.0')); + $this->assertFalse($this->input->valid_ip('2001:db8:0:85a3::ac1f:8001', 'ipv4')); + + // v6 tests + $this->assertFalse($this->input->valid_ip('192.18.0.1', 'ipv6')); + + $ip_v6 = array( + '2001:0db8:0000:85a3:0000:0000:ac1f:8001', + '2001:db8:0:85a3:0:0:ac1f:8001', + '2001:db8:0:85a3::ac1f:8001' + ); - $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); foreach ($ip_v6 as $ip) { $this->assertTrue($this->input->valid_ip($ip)); + $this->assertTrue($this->input->valid_ip($ip, 'ipv6')); } } @@ -171,4 +182,34 @@ class Input_test extends CI_TestCase { $this->assertTrue($this->input->is_ajax_request()); } + // -------------------------------------------------------------------- + + public function test_input_stream() + { + $this->markTestSkipped('TODO: Find a way to test input://'); + } + + // -------------------------------------------------------------------- + + public function test_set_cookie() + { + $this->markTestSkipped('TODO: Find a way to test HTTP headers'); + } + + public function test_ip_address() + { + // 127.0.0.1 is set in our Bootstrap file + $this->assertEquals('127.0.0.1', $this->input->ip_address()); + + // Invalid + $_SERVER['REMOTE_ADDR'] = 'invalid_ip_address'; + $this->input->ip_address = FALSE; // reset cached value + $this->assertEquals('0.0.0.0', $this->input->ip_address()); + + // TODO: Add proxy_ips tests + + // Back to reality + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // back to reality + } + }
\ No newline at end of file diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index 9ad3ca6b9..93ca5b223 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -31,15 +31,12 @@ class Loader_test extends CI_TestCase { $this->assertFalse($this->load->is_loaded(ucfirst($lib))); // Test loading as an array. - $this->assertNull($this->load->library(array($lib))); + $this->assertInstanceOf('CI_Loader', $this->load->library(array($lib))); $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); - // Test no lib given - $this->assertNull($this->load->library()); - // Test a string given to params - $this->assertNull($this->load->library($lib, ' ')); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib, ' ')); // Create library w/o class $lib = 'bad_test_lib'; @@ -50,7 +47,7 @@ class Loader_test extends CI_TestCase { 'RuntimeException', 'CI Error: Unable to load the requested class: '.ucfirst($lib) ); - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); } // -------------------------------------------------------------------- @@ -66,7 +63,7 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create($ext, '<?php class '.$ext.' extends '.$class.' { }', $this->ci_app_root, 'libraries'); // Test loading with extension - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertTrue(class_exists($ext), $ext.' does not exist'); $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); @@ -74,13 +71,13 @@ class Loader_test extends CI_TestCase { // Test reloading with object name $obj = 'exttest'; - $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib, NULL, $obj)); $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); $this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj); // Test reloading unset($this->ci_obj->$name); - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); $this->assertObjectNotHasAttribute($name, $this->ci_obj); // Create baseless library @@ -94,7 +91,7 @@ class Loader_test extends CI_TestCase { 'RuntimeException', 'CI Error: Unable to load the requested class: '.$lib ); - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); } // -------------------------------------------------------------------- @@ -117,7 +114,7 @@ class Loader_test extends CI_TestCase { // Test object name and config $obj = 'testy'; - $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib, NULL, $obj)); $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); $this->assertEquals($cfg, $this->ci_obj->$obj->config); @@ -136,7 +133,7 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create(ucfirst($lib), '<?php class '.$class.' { }', $this->ci_app_root, 'libraries'); // Load library - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); // Was the model class instantiated. $this->assertTrue(class_exists($class), $class.' does not exist'); @@ -158,20 +155,17 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create(ucfirst($driver), $content, $this->ci_base_root, 'libraries/'.$dir); // Test loading as an array. - $this->assertNull($this->load->driver(array($driver))); + $this->assertInstanceOf('CI_Loader', $this->load->driver(array($driver))); $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertAttributeInstanceOf($class, $driver, $this->ci_obj); // Test loading as a library with a name $obj = 'testdrive'; - $this->assertNull($this->load->library($driver, NULL, $obj)); + $this->assertInstanceOf('CI_Loader', $this->load->library($driver, NULL, $obj)); $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); - // Test no driver given - $this->assertFalse($this->load->driver()); - // Test a string given to params - $this->assertNull($this->load->driver($driver, ' ')); + $this->assertInstanceOf('CI_Loader', $this->load->driver($driver, ' ')); } // -------------------------------------------------------------------- @@ -186,14 +180,14 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models'); // Load model - $this->assertNull($this->load->model($model)); + $this->assertInstanceOf('CI_Loader', $this->load->model($model)); // Was the model class instantiated. $this->assertTrue(class_exists($model)); $this->assertObjectHasAttribute($model, $this->ci_obj); // Test no model given - $this->assertNull($this->load->model('')); + $this->assertInstanceOf('CI_Loader', $this->load->model('')); } // -------------------------------------------------------------------- @@ -212,7 +206,7 @@ class Loader_test extends CI_TestCase { // Load model $name = 'testors'; - $this->assertNull($this->load->model($subdir.'/'.$model, $name)); + $this->assertInstanceOf('CI_Loader', $this->load->model($subdir.'/'.$model, $name)); // Was the model class instantiated? $this->assertTrue(class_exists($model)); @@ -246,8 +240,8 @@ class Loader_test extends CI_TestCase { // public function testDatabase() // { - // $this->assertNull($this->load->database()); - // $this->assertNull($this->load->dbutil()); + // $this->assertInstanceOf('CI_Loader', $this->load->database()); + // $this->assertInstanceOf('CI_Loader', $this->load->dbutil()); // } // -------------------------------------------------------------------- @@ -271,7 +265,7 @@ class Loader_test extends CI_TestCase { $this->ci_instance_var('output', $output); // Test view output - $this->assertNull($this->load->view($view, array($var => $value))); + $this->assertInstanceOf('CI_Loader', $this->load->view($view, array($var => $value))); } // -------------------------------------------------------------------- @@ -317,8 +311,8 @@ class Loader_test extends CI_TestCase { $val1 = 'bar'; $key2 = 'boo'; $val2 = 'hoo'; - $this->assertNull($this->load->vars(array($key1 => $val1))); - $this->assertNull($this->load->vars($key2, $val2)); + $this->assertInstanceOf('CI_Loader', $this->load->vars(array($key1 => $val1))); + $this->assertInstanceOf('CI_Loader', $this->load->vars($key2, $val2)); $this->assertEquals($val1, $this->load->get_var($key1)); $this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars()); } @@ -330,16 +324,16 @@ class Loader_test extends CI_TestCase { // Create helper in VFS $helper = 'test'; $func = '_my_helper_test_func'; - $content = '<?php function '.$func.'() { return true; } '; + $content = '<?php function '.$func.'() { return TRUE; } '; $this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers'); // Create helper extension $exfunc = '_my_extension_func'; - $content = '<?php function '.$exfunc.'() { return true; } '; + $content = '<?php function '.$exfunc.'() { return TRUE; } '; $this->ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Load helper - $this->assertNull($this->load->helper($helper)); + $this->assertInstanceOf('CI_Loader', $this->load->helper($helper)); $this->assertTrue(function_exists($func), $func.' does not exist'); $this->assertTrue(function_exists($exfunc), $exfunc.' does not exist'); @@ -379,12 +373,12 @@ class Loader_test extends CI_TestCase { $helpers[] = $helper; $func = '_my_helper_test_func'.$i; $funcs[] = $func; - $files[$helper.'_helper'] = '<?php function '.$func.'() { return true; } '; + $files[$helper.'_helper'] = '<?php function '.$func.'() { return TRUE; } '; } $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); // Load helpers - $this->assertNull($this->load->helpers($helpers)); + $this->assertInstanceOf('CI_Loader', $this->load->helpers($helpers)); // Verify helper existence foreach ($funcs as $func) { @@ -401,7 +395,7 @@ class Loader_test extends CI_TestCase { $lang = $this->getMock('CI_Lang', array('load')); $lang->expects($this->once())->method('load')->with($file); $this->ci_instance_var('lang', $lang); - $this->assertNull($this->load->language($file)); + $this->assertInstanceOf('CI_Loader', $this->load->language($file)); } // -------------------------------------------------------------------- @@ -419,24 +413,24 @@ class Loader_test extends CI_TestCase { // Add path and verify $path = APPPATH.$dir.'/'; - $this->assertNull($this->load->add_package_path($path)); + $this->assertInstanceOf('CI_Loader', $this->load->add_package_path($path)); $this->assertContains($path, $this->load->get_package_paths(TRUE)); // Test successful load - $this->assertNull($this->load->library($lib)); + $this->assertInstanceOf('CI_Loader', $this->load->library($lib)); $this->assertTrue(class_exists($class), $class.' does not exist'); // Add another path $path2 = APPPATH.'another/'; - $this->assertNull($this->load->add_package_path($path2)); + $this->assertInstanceOf('CI_Loader', $this->load->add_package_path($path2)); $this->assertContains($path2, $this->load->get_package_paths(TRUE)); // Remove last path - $this->assertNull($this->load->remove_package_path()); + $this->assertInstanceOf('CI_Loader', $this->load->remove_package_path()); $this->assertNotContains($path2, $this->load->get_package_paths(TRUE)); // Remove path and verify restored paths - $this->assertNull($this->load->remove_package_path($path)); + $this->assertInstanceOf('CI_Loader', $this->load->remove_package_path($path)); $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); // Test failed load without path @@ -463,7 +457,7 @@ class Loader_test extends CI_TestCase { // Create helper in VFS $helper = 'autohelp'; $hlp_func = '_autohelp_test_func'; - $content = '<?php function '.$hlp_func.'() { return true; }'; + $content = '<?php function '.$hlp_func.'() { return TRUE; }'; $this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Create library in VFS diff --git a/tests/codeigniter/core/Model_test.php b/tests/codeigniter/core/Model_test.php new file mode 100644 index 000000000..80dc97b3b --- /dev/null +++ b/tests/codeigniter/core/Model_test.php @@ -0,0 +1,37 @@ +<?php + +class Model_test extends CI_TestCase { + + private $ci_obj; + + public function set_up() + { + $loader = $this->ci_core_class('loader'); + $this->load = new $loader(); + $this->ci_obj = $this->ci_instance(); + $this->ci_set_core_class('model', 'CI_Model'); + + $model_code =<<<MODEL +<?php +class Test_model extends CI_Model { + + public \$property = 'foo'; + +} +MODEL; + + $this->ci_vfs_create('Test_model', $model_code, $this->ci_app_root, 'models'); + $this->load->model('test_model'); + } + + // -------------------------------------------------------------------- + + public function test__get() + { + $this->assertEquals('foo', $this->ci_obj->test_model->property); + + $this->ci_obj->controller_property = 'bar'; + $this->assertEquals('bar', $this->ci_obj->test_model->controller_property); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Security_test.php b/tests/codeigniter/core/Security_test.php index 3f6e3b07a..433ad313f 100644 --- a/tests/codeigniter/core/Security_test.php +++ b/tests/codeigniter/core/Security_test.php @@ -5,7 +5,7 @@ class Security_test extends CI_TestCase { public function set_up() { // Set cookie for security test - $_COOKIE['ci_csrf_cookie'] = md5(uniqid(rand(), TRUE)); + $_COOKIE['ci_csrf_cookie'] = md5(uniqid(mt_rand(), TRUE)); // Set config for Security class $this->ci_set_config('csrf_protection', TRUE); diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 7fa0e6265..6589c1f5a 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -26,6 +26,10 @@ class URI_test extends CI_TestCase { // -------------------------------------------------------------------- + /* + + This has been moved to the constructor + public function test_fetch_uri_string() { define('SELF', 'index.php'); @@ -86,9 +90,14 @@ class URI_test extends CI_TestCase { // uri_protocol: REQUEST_URI // uri_protocol: CLI } + */ // -------------------------------------------------------------------- + /* + + This has been moved into _set_uri_string() + public function test_explode_segments() { // Let's test the function's ability to clean up this mess @@ -107,16 +116,15 @@ class URI_test extends CI_TestCase { $this->assertEquals($a, $this->uri->segments); } } - + */ // -------------------------------------------------------------------- public function test_filter_uri() { - $this->uri->config->set_item('enable_query_strings', FALSE); - $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-'); + $this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-'); $str_in = 'abc01239~%.:_-'; - $str = $this->uri->_filter_uri($str_in); + $str = $this->uri->filter_uri($str_in); $this->assertEquals($str, $str_in); } @@ -126,11 +134,9 @@ class URI_test extends CI_TestCase { public function test_filter_uri_escaping() { // ensure escaping even if dodgey characters are permitted + $this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-()$'); - $this->uri->config->set_item('enable_query_strings', FALSE); - $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-()$'); - - $str = $this->uri->_filter_uri('$destroy_app(foo)'); + $str = $this->uri->filter_uri('$destroy_app(foo)'); $this->assertEquals($str, '$destroy_app(foo)'); } @@ -142,25 +148,8 @@ class URI_test extends CI_TestCase { $this->setExpectedException('RuntimeException'); $this->uri->config->set_item('enable_query_strings', FALSE); - $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-'); - $this->uri->_filter_uri('$this()'); - } - - // -------------------------------------------------------------------- - - public function test_remove_url_suffix() - { - $this->uri->config->set_item('url_suffix', '.html'); - - $this->uri->uri_string = 'controller/method/index.html'; - $this->uri->_remove_url_suffix(); - - $this->assertEquals($this->uri->uri_string, 'controller/method/index'); - - $this->uri->uri_string = 'controller/method/index.htmlify.html'; - $this->uri->_remove_url_suffix(); - - $this->assertEquals($this->uri->uri_string, 'controller/method/index.htmlify'); + $this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-'); + $this->uri->filter_uri('$this()'); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/core/Utf8_test.php b/tests/codeigniter/core/Utf8_test.php index caa7b6986..71299134e 100644 --- a/tests/codeigniter/core/Utf8_test.php +++ b/tests/codeigniter/core/Utf8_test.php @@ -11,10 +11,15 @@ class Utf8_test extends CI_TestCase { public function test_convert_to_utf8() { - $this->assertEquals( - $this->utf8->convert_to_utf8('', 'WINDOWS-1251'), - 'тест' - ); + $this->assertEquals('тест', $this->utf8->convert_to_utf8('', 'WINDOWS-1251')); + } + + // -------------------------------------------------------------------- + + public function test_is_ascii() + { + $this->assertTrue($this->utf8->is_ascii_test('foo bar')); + $this->assertFalse($this->utf8->is_ascii_test('тест')); } }
\ No newline at end of file diff --git a/tests/codeigniter/core/compat/mbstring_test.php b/tests/codeigniter/core/compat/mbstring_test.php new file mode 100644 index 000000000..415222446 --- /dev/null +++ b/tests/codeigniter/core/compat/mbstring_test.php @@ -0,0 +1,54 @@ +<?php + +class mbstring_test extends CI_TestCase { + + public function test_bootstrap() + { + if (MB_ENABLED) + { + return $this->markTestSkipped('ext/mbstring is loaded'); + } + + $this->assertTrue(function_exists('mb_strlen')); + $this->assertTrue(function_exists('mb_substr')); + } + + // ------------------------------------------------------------------------ + + /** + * @depends test_bootstrap + */ + public function test_mb_strlen() + { + $this->assertEquals(ICONV_ENABLED ? 4 : 8, mb_strlen('тест')); + $this->assertEquals(ICONV_ENABLED ? 4 : 8, mb_strlen('тест', 'UTF-8')); + } + + // ------------------------------------------------------------------------ + + /** + * @depends test_boostrap + */ + public function test_mb_strpos() + { + $this->assertEquals(ICONV_ENABLED ? 3 : 6, mb_strpos('тест', 'с')); + $this->assertFalse(mb_strpos('тест', 'с', 3)); + $this->assertEquals(ICONV_ENABLED ? 3 : 6, mb_strpos('тест', 'с', 1, 'UTF-8')); + } + + // ------------------------------------------------------------------------ + + /** + * @depends test_boostrap + */ + public function test_mb_substr() + { + $this->assertEquals(ICONV_ENABLED ? 'стинг' : 'естинг', mb_substr('тестинг', 2)); + $this->assertEquals(ICONV_ENABLED ? 'нг' : 'г', mb_substr('тестинг', -2)); + $this->assertEquals(ICONV_ENABLED ? 'ст' : 'е', mb_substr('тестинг', 2, 2)); + $this->assertEquals(ICONV_ENABLED ? 'стинг' : 'естинг', mb_substr('тестинг', 2, 'UTF-8')); + $this->assertEquals(ICONV_ENABLED ? 'нг' : 'г', mb_substr('тестинг', -2, 'UTF-8')); + $this->assertEquals(ICONV_ENABLED ? 'ст' : 'е', mb_substr('тестинг', 2, 2, 'UTF-8')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/compat/password_test.php b/tests/codeigniter/core/compat/password_test.php new file mode 100644 index 000000000..4014e7415 --- /dev/null +++ b/tests/codeigniter/core/compat/password_test.php @@ -0,0 +1,158 @@ +<?php + +class password_test extends CI_TestCase { + + public function test_bootstrap() + { + if (is_php('5.5')) + { + return $this->markTestSkipped('ext/standard/password is available on PHP 5.5'); + } + elseif ( ! is_php('5.3.7')) + { + $this->assertFalse(defined('PASSWORD_BCRYPT')); + return $this->markTestSkipped("PHP versions prior to 5.3.7 don't have the '2y' Blowfish version"); + } + elseif ( ! defined('CRYPT_BLOWFISH') OR CRYPT_BLOWFISH !== 1) + { + $this->assertFalse(defined('PASSWORD_BCRYPT')); + return $this->markTestSkipped('CRYPT_BLOWFISH is not available'); + } + + $this->assertTrue(defined('PASSWORD_BCRYPT')); + $this->assertTrue(defined('PASSWORD_DEFAULT')); + $this->assertEquals(1, PASSWORD_BCRYPT); + $this->assertEquals(PASSWORD_BCRYPT, PASSWORD_DEFAULT); + $this->assertTrue(function_exists('password_get_info')); + $this->assertTrue(function_exists('password_hash')); + $this->assertTrue(function_exists('password_needs_rehash')); + $this->assertTrue(function_exists('password_verify')); + } + + // ------------------------------------------------------------------------ + + /** + * password_get_info() test + * + * Borrowed from PHP's own tests + * + * @depends test_bootstrap + */ + public function test_password_get_info() + { + $expected = array( + 'algo' => 1, + 'algoName' => 'bcrypt', + 'options' => array('cost' => 10) + ); + + // default + $this->assertEquals($expected, password_get_info('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y')); + + $expected['options']['cost'] = 11; + + // cost + $this->assertEquals($expected, password_get_info('$2y$11$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y')); + + $expected = array( + 'algo' => 0, + 'algoName' => 'unknown', + 'options' => array() + ); + + // invalid length + $this->assertEquals($expected, password_get_info('$2y$11$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100')); + + // non-bcrypt + $this->assertEquals($expected, password_get_info('$1$rasmusle$rISCgZzpwk3UhDidwXvin0')); + } + + // ------------------------------------------------------------------------ + + /** + * password_hash() test + * + * Borrowed from PHP's own tests + * + * @depends test_bootstrap + */ + public function test_password_hash() + { + // FALSE is returned if no CSPRNG source is available + if ( ! defined('MCRYPT_DEV_URANDOM') && ! function_exists('openssl_random_pseudo_bytes') + && (DIRECTORY_SEPARATOR !== '/' OR ! is_readable('/dev/arandom') OR ! is_readable('/dev/urandom')) + ) + { + $this->assertFalse(password_hash('foo', PASSWORD_BCRYPT)); + } + else + { + $this->assertEquals(60, strlen(password_hash('foo', PASSWORD_BCRYPT))); + $this->assertTrue(($hash = password_hash('foo', PASSWORD_BCRYPT)) === crypt('foo', $hash)); + } + + $this->assertEquals( + '$2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi', + password_hash('rasmuslerdorf', PASSWORD_BCRYPT, array('cost' => 7, 'salt' => 'usesomesillystringforsalt')) + ); + + $this->assertEquals( + '$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', + password_hash('test', PASSWORD_BCRYPT, array('salt' => '123456789012345678901'.chr(0))) + ); + } + + // ------------------------------------------------------------------------ + + /** + * password_needs_rehash() test + * + * Borrowed from PHP's own tests + * + * @depends test_password_get_info + */ + public function test_password_needs_rehash() + { + // invalid hash: always rehash + $this->assertTrue(password_needs_rehash('', PASSWORD_BCRYPT)); + + // valid, because it's an unknown algorithm + $this->assertFalse(password_needs_rehash('', 0)); + + // valid with same cost + $this->assertFalse(password_needs_rehash('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT, array('cost' => 10))); + + // valid with same cost and additional parameters + $this->assertFalse(password_needs_rehash('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT, array('cost' => 10, 'foo' => 3))); + + // invalid: different (lower) cost + $this->assertTrue(password_needs_rehash('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT, array('cost' => 09))); + + // invalid: different (higher) cost + $this->assertTrue(password_needs_rehash('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT, array('cost' => 11))); + + // valid with default cost + $this->assertFalse(password_needs_rehash('$2y$'.str_pad(10, 2, '0', STR_PAD_LEFT).'$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT)); + + // invalid: 'foo' is cast to 0 + $this->assertTrue(password_needs_rehash('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', PASSWORD_BCRYPT, array('cost' => 'foo'))); + } + + // ------------------------------------------------------------------------ + + /** + * password_verify() test + * + * Borrowed from PHP's own tests + * + * @depends test_bootstrap + */ + public function test_password_verify() + { + $this->assertFalse(password_verify(123, 123)); + $this->assertFalse(password_verify('foo', '$2a$07$usesomesillystringforsalt$')); + $this->assertFalse(password_verify('rasmusler', '$2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi')); + $this->assertTrue(password_verify('rasmuslerdorf', '$2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi')); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php index d75d26208..7a7dc0a12 100644 --- a/tests/codeigniter/helpers/text_helper_test.php +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -106,17 +106,19 @@ class Text_helper_test extends CI_TestCase { public function test_highlight_phrase() { $strs = array( - 'this is a phrase' => '<strong>this is</strong> a phrase', - 'this is another' => '<strong>this is</strong> another', - 'Gimme a test, Sally' => 'Gimme a test, Sally', - 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>', - '' => '' + 'this is a phrase' => '<mark>this is</mark> a phrase', + 'this is another' => '<mark>this is</mark> another', + 'Gimme a test, Sally' => 'Gimme a test, Sally', + 'Or tell me what this is' => 'Or tell me what <mark>this is</mark>', + '' => '' ); foreach ($strs as $str => $expect) { $this->assertEquals($expect, highlight_phrase($str, 'this is')); } + + $this->assertEquals('<strong>this is</strong> a strong test', highlight_phrase('this is a strong test', 'this is', '<strong>', '</strong>')); } // ------------------------------------------------------------------------ diff --git a/tests/codeigniter/libraries/Calendar_test.php b/tests/codeigniter/libraries/Calendar_test.php index 952e8a8d2..768bc8573 100644 --- a/tests/codeigniter/libraries/Calendar_test.php +++ b/tests/codeigniter/libraries/Calendar_test.php @@ -169,30 +169,33 @@ class Calendar_test extends CI_TestCase { $this->assertEquals(31, $this->calendar->get_total_days(12, 2012)); } - function test_default_template() + public function test_default_template() { $array = array( - 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', - 'heading_row_start' => '<tr>', + 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', + 'heading_row_start' => '<tr>', 'heading_previous_cell' => '<th><a href="{previous_url}"><<</a></th>', 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>', - 'heading_next_cell' => '<th><a href="{next_url}">>></a></th>', - 'heading_row_end' => '</tr>', - 'week_row_start' => '<tr>', - 'week_day_cell' => '<td>{week_day}</td>', - 'week_row_end' => '</tr>', - 'cal_row_start' => '<tr>', - 'cal_cell_start' => '<td>', + 'heading_next_cell' => '<th><a href="{next_url}">>></a></th>', + 'heading_row_end' => '</tr>', + 'week_row_start' => '<tr>', + 'week_day_cell' => '<td>{week_day}</td>', + 'week_row_end' => '</tr>', + 'cal_row_start' => '<tr>', + 'cal_cell_start' => '<td>', 'cal_cell_start_today' => '<td>', - 'cal_cell_content' => '<a href="{content}">{day}</a>', + 'cal_cell_content' => '<a href="{content}">{day}</a>', 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>', 'cal_cell_no_content' => '{day}', 'cal_cell_no_content_today' => '<strong>{day}</strong>', - 'cal_cell_blank' => ' ', - 'cal_cell_end' => '</td>', + 'cal_cell_blank' => ' ', + 'cal_cell_end' => '</td>', 'cal_cell_end_today' => '</td>', - 'cal_row_end' => '</tr>', - 'table_close' => '</table>' + 'cal_row_end' => '</tr>', + 'table_close' => '</table>', + 'cal_cell_start_other' => '<td style="color: #666;">', + 'cal_cell_other' => '{day}', + 'cal_cell_end_other' => '</td>' ); $this->assertEquals($array, $this->calendar->default_template()); diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php new file mode 100644 index 000000000..a8b5bc81e --- /dev/null +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -0,0 +1,394 @@ +<?php + +class Encryption_test extends CI_TestCase { + + public function set_up() + { + $this->encryption = new Mock_Libraries_Encryption(); + } + + /** + * __construct test + * + * Covers behavior with $config['encryption_key'] set or not + */ + public function test___construct() + { + // Assume no configuration from set_up() + $this->assertNull($this->encryption->get_key()); + + // Try with an empty value + $this->ci_set_config('encryption_key'); + $this->encrypt = new Mock_Libraries_Encryption(); + $this->assertNull($this->encrypt->get_key()); + + $this->ci_set_config('encryption_key', str_repeat("\x0", 16)); + $this->encrypt = new Mock_Libraries_Encryption(); + $this->assertEquals(str_repeat("\x0", 16), $this->encrypt->get_key()); + } + + // -------------------------------------------------------------------- + + /** + * hkdf() test + * + * Applies test vectors described in Appendix A(1-3) RFC5869. + * Described vectors 4-7 SHA-1, which we don't support and are + * therefore excluded. + * + * Because our implementation is a single method instead of being + * split into hkdf_extract() and hkdf_expand(), we cannot test for + * the PRK value. As long as the OKM is correct though, it's fine. + * + * @link https://tools.ietf.org/rfc/rfc5869.txt + */ + public function test_hkdf() + { + $vectors = array( + // A.1: Basic test case with SHA-256 + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c", + 'length' => 42, + 'info' => "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9", + // 'prk' => "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84\x4a\xd7\xc2\xb3\xe5", + 'okm' => "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65" + ), + // A.2: Test with SHA-256 and longer inputs/outputs + array( + 'digest' => 'sha256', + 'ikm' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f", + 'salt' => "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf", + 'length' => 82, + 'info' => "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", + // 'prk' => "\x06\xa6\xb8\x8c\x58\x53\x36\x1a\x06\x10\x4c\x9c\xeb\x35\xb4\x5c\xef\x76\x00\x14\x90\x46\x71\x01\x4a\x19\x3f\x40\xc1\x5f\xc2\x44", + 'okm' => "\xb1\x1e\x39\x8d\xc8\x03\x27\xa1\xc8\xe7\xf7\x8c\x59\x6a\x49\x34\x4f\x01\x2e\xda\x2d\x4e\xfa\xd8\xa0\x50\xcc\x4c\x19\xaf\xa9\x7c\x59\x04\x5a\x99\xca\xc7\x82\x72\x71\xcb\x41\xc6\x5e\x59\x0e\x09\xda\x32\x75\x60\x0c\x2f\x09\xb8\x36\x77\x93\xa9\xac\xa3\xdb\x71\xcc\x30\xc5\x81\x79\xec\x3e\x87\xc1\x4c\x01\xd5\xc1\xf3\x43\x4f\x1d\x87", + ), + // A.3: Test with SHA-256 and zero-length salt/info + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => '', + 'length' => 42, + 'info' => '', + // 'prk' => "\x19\xef\x24\xa3\x2c\x71\x7b\x16\x7f\x33\xa9\x1d\x6f\x64\x8b\xdf\x96\x59\x67\x76\xaf\xdb\x63\x77\xac\x43\x4c\x1c\x29\x3c\xcb\x04", + 'okm' => "\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8", + ) + ); + + foreach ($vectors as $test) + { + $this->assertEquals( + $test['okm'], + $this->encryption->hkdf( + $test['ikm'], + $test['digest'], + $test['salt'], + $test['length'], + $test['info'] + ) + ); + } + + // Test default length, it must match the digest size + $this->assertEquals(64, strlen($this->encryption->hkdf('foobar', 'sha512'))); + + // Test maximum length (RFC5869 says that it must be up to 255 times the digest size) + $this->assertEquals(12240, strlen($this->encryption->hkdf('foobar', 'sha384', NULL, 48 * 255))); + $this->assertFalse($this->encryption->hkdf('foobar', 'sha224', NULL, 28 * 255 + 1)); + + // CI-specific test for an invalid digest + $this->assertFalse($this->encryption->hkdf('fobar', 'sha1')); + } + + // -------------------------------------------------------------------- + + /** + * _get_params() test + * + * @uses Mock_Libraries_Encryption::__get_params() + */ + public function test__get_params() + { + $key = str_repeat("\x0", 16); + + // Invalid custom parameters + $params = array( + // No cipher, mode or key + array('cipher' => 'aes-128', 'mode' => 'cbc'), + array('cipher' => 'aes-128', 'key' => $key), + array('mode' => 'cbc', 'key' => $key), + // No HMAC key or not a valid digest + array('cipher' => 'aes-128', 'mode' => 'cbc', 'key' => $key), + array('cipher' => 'aes-128', 'mode' => 'cbc', 'key' => $key, 'hmac_digest' => 'sha1', 'hmac_key' => $key), + // Invalid mode + array('cipher' => 'aes-128', 'mode' => 'foo', 'key' => $key, 'hmac_digest' => 'sha256', 'hmac_key' => $key) + ); + + for ($i = 0, $c = count($params); $i < $c; $i++) + { + $this->assertFalse($this->encryption->__get_params($params[$i])); + } + + // Valid parameters + $params = array( + 'cipher' => 'aes-128', + 'mode' => 'cbc', + 'key' => str_repeat("\x0", 16), + 'hmac_key' => str_repeat("\x0", 16) + ); + + $this->assertTrue(is_array($this->encryption->__get_params($params))); + + $params['iv'] = NULL; + $params['base64'] = TRUE; + $params['hmac_digest'] = 'sha512'; + + // Including all parameters + $params = array( + 'cipher' => 'aes-128', + 'mode' => 'cbc', + 'key' => str_repeat("\x0", 16), + 'iv' => str_repeat("\x0", 16), + 'raw_data' => TRUE, + 'hmac_key' => str_repeat("\x0", 16), + 'hmac_digest' => 'sha256' + ); + + $output = $this->encryption->__get_params($params); + unset($output['handle'], $params['raw_data']); + $params['base64'] = FALSE; + $this->assertEquals($params, $output); + + // HMAC disabled + unset($params['hmac_key'], $params['hmac_digest']); + $params['hmac'] = $params['raw_data'] = FALSE; + $output = $this->encryption->__get_params($params); + unset($output['handle'], $params['hmac'], $params['raw_data']); + $params['base64'] = TRUE; + $params['hmac_digest'] = $params['hmac_key'] = NULL; + $this->assertEquals($params, $output); + } + + // -------------------------------------------------------------------- + + /** + * initialize(), encrypt(), decrypt() test + * + * Testing the three methods separately is not realistic as they are + * designed to work together. A more thorough test for initialize() + * though is the OpenSSL/MCrypt compatibility test. + * + * @depends test_hkdf + * @depends test__get_params + */ + public function test_initialize_encrypt_decrypt() + { + $message = 'This is a plain-text message.'; + $key = "\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c"; + + // Default state (AES-128/Rijndael-128 in CBC mode) + $this->encryption->initialize(array('key' => $key)); + + // Was the key properly set? + $this->assertEquals($key, $this->encryption->get_key()); + + $this->assertEquals($message, $this->encryption->decrypt($this->encryption->encrypt($message))); + + // Try DES in ECB mode, just for the sake of changing stuff + $this->encryption->initialize(array('cipher' => 'des', 'mode' => 'ecb')); + $this->assertEquals($message, $this->encryption->decrypt($this->encryption->encrypt($message))); + } + + // -------------------------------------------------------------------- + + /** + * encrypt(), decrypt test with custom parameters + * + * @depends test___get_params + */ + public function test_encrypt_decrypt_custom() + { + $message = 'Another plain-text message.'; + + // A random invalid parameter + $this->assertFalse($this->encryption->encrypt($message, array('foo'))); + $this->assertFalse($this->encryption->decrypt($message, array('foo'))); + + // Custom IV (we'll check it), no HMAC, binary output + $params = array( + 'cipher' => 'tripledes', + 'mode' => 'cfb', + 'key' => str_repeat("\x1", 16), + 'iv' => str_repeat("\x2", 8), + 'base64' => FALSE, + 'hmac' => FALSE + ); + + $ciphertext = $this->encryption->encrypt($message, $params); + $this->assertEquals(0, strncmp($params['iv'], $ciphertext, 8)); + + // IV should be found in the cipher-text, no matter if it was supplied or not + $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + unset($params['iv']); + $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + } + + // -------------------------------------------------------------------- + + /** + * _mcrypt_get_handle() test + */ + public function test__mcrypt_get_handle() + { + if ($this->encryption->drivers['mcrypt'] === FALSE) + { + return $this->markTestSkipped('Cannot test MCrypt because it is not available.'); + } + + $this->assertTrue(is_resource($this->encryption->__driver_get_handle('mcrypt', 'rijndael-128', 'cbc'))); + } + + // -------------------------------------------------------------------- + + /** + * _openssl_get_handle() test + */ + public function test__openssl_mcrypt_get_handle() + { + if ($this->encryption->drivers['openssl'] === FALSE) + { + return $this->markTestSkipped('Cannot test OpenSSL because it is not available.'); + } + + $this->assertEquals('aes-128-cbc', $this->encryption->__driver_get_handle('openssl', 'aes-128', 'cbc')); + $this->assertEquals('rc4-40', $this->encryption->__driver_get_handle('openssl', 'rc4-40', 'stream')); + } + + // -------------------------------------------------------------------- + + /** + * OpenSSL/MCrypt portability test + * + * Amongst the obvious stuff, _cipher_alias() is also tested here. + */ + public function test_portability() + { + if ( ! $this->encryption->drivers['mcrypt'] OR ! $this->encryption->drivers['openssl']) + { + $this->markTestSkipped('Both MCrypt and OpenSSL support are required for portability tests.'); + return; + } + + $message = 'This is a message encrypted via MCrypt and decrypted via OpenSSL, or vice-versa.'; + + // Format is: <Cipher name>, <Cipher mode>, <Key size> + $portable = array( + array('aes-128', 'cbc', 16), + array('aes-128', 'cfb', 16), + array('aes-128', 'cfb8', 16), + array('aes-128', 'ofb', 16), + array('aes-128', 'ecb', 16), + array('aes-128', 'ctr', 16), + array('aes-192', 'cbc', 24), + array('aes-192', 'cfb', 24), + array('aes-192', 'cfb8', 24), + array('aes-192', 'ofb', 24), + array('aes-192', 'ecb', 24), + array('aes-192', 'ctr', 24), + array('aes-256', 'cbc', 32), + array('aes-256', 'cfb', 32), + array('aes-256', 'cfb8', 32), + array('aes-256', 'ofb', 32), + array('aes-256', 'ecb', 32), + array('aes-256', 'ctr', 32), + array('des', 'cbc', 7), + array('des', 'cfb', 7), + array('des', 'cfb8', 7), + array('des', 'ofb', 7), + array('des', 'ecb', 7), + array('tripledes', 'cbc', 7), + array('tripledes', 'cfb', 7), + array('tripledes', 'cfb8', 7), + array('tripledes', 'ofb', 7), + array('tripledes', 'cbc', 14), + array('tripledes', 'cfb', 14), + array('tripledes', 'cfb8', 14), + array('tripledes', 'ofb', 14), + array('tripledes', 'cbc', 21), + array('tripledes', 'cfb', 21), + array('tripledes', 'cfb8', 21), + array('tripledes', 'ofb', 21), + array('blowfish', 'cbc', 16), + array('blowfish', 'cfb', 16), + array('blowfish', 'ofb', 16), + array('blowfish', 'ecb', 16), + array('blowfish', 'cbc', 56), + array('blowfish', 'cfb', 56), + array('blowfish', 'ofb', 56), + array('blowfish', 'ecb', 56), + array('cast5', 'cbc', 5), + array('cast5', 'cfb', 5), + array('cast5', 'ofb', 5), + array('cast5', 'ecb', 5), + array('cast5', 'cbc', 8), + array('cast5', 'cfb', 8), + array('cast5', 'ofb', 8), + array('cast5', 'ecb', 8), + array('cast5', 'cbc', 10), + array('cast5', 'cfb', 10), + array('cast5', 'ofb', 10), + array('cast5', 'ecb', 10), + array('cast5', 'cbc', 16), + array('cast5', 'cfb', 16), + array('cast5', 'ofb', 16), + array('cast5', 'ecb', 16), + array('rc4', 'stream', 5), + array('rc4', 'stream', 8), + array('rc4', 'stream', 16), + array('rc4', 'stream', 32), + array('rc4', 'stream', 64), + array('rc4', 'stream', 128), + array('rc4', 'stream', 256) + ); + $driver_index = array('mcrypt', 'openssl'); + + foreach ($portable as &$test) + { + // Add some randomness to the selected driver + $driver = mt_rand(0,1); + $params = array( + 'driver' => $driver_index[$driver], + 'cipher' => $test[0], + 'mode' => $test[1], + 'key' => openssl_random_pseudo_bytes($test[2]) + ); + + $this->encryption->initialize($params); + $ciphertext = $this->encryption->encrypt($message); + + $driver = (int) ! $driver; + $params['driver'] = $driver_index[$driver]; + + $this->encryption->initialize($params); + $this->assertEquals($message, $this->encryption->decrypt($ciphertext)); + } + } + + // -------------------------------------------------------------------- + + /** + * __get() test + */ + public function test_magic_get() + { + $this->assertNull($this->encryption->foo); + $this->assertEquals(array('mcrypt', 'openssl'), array_keys($this->encryption->drivers)); + + // 'stream' mode is translated into an empty string for OpenSSL + $this->encryption->initialize(array('cipher' => 'rc4', 'mode' => 'stream')); + $this->assertEquals('stream', $this->encryption->mode); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php index 394c22692..3755cf1a0 100644 --- a/tests/codeigniter/libraries/Parser_test.php +++ b/tests/codeigniter/libraries/Parser_test.php @@ -33,7 +33,7 @@ class Parser_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_parse_simple_string() + public function test_parse_string() { $data = array( 'title' => 'Page Title', @@ -69,16 +69,12 @@ class Parser_test extends CI_TestCase { { $data = array( 'title' => 'Super Heroes', - 'powers' => array( - array( - 'invisibility' => 'yes', - 'flying' => 'no'), - ) + 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) ); - $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}"; + $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}"; - $this->assertEquals("Super Heroes\nyes\nno", $this->parser->parse_string($template, $data, TRUE)); + $this->assertEquals("Super Heroes\nyes\nno\nsecond: yes no", $this->parser->parse_string($template, $data, TRUE)); } // -------------------------------------------------------------------- @@ -87,11 +83,7 @@ class Parser_test extends CI_TestCase { { $data = array( 'title' => 'Super Heroes', - 'powers' => array( - array( - 'invisibility' => 'yes', - 'flying' => 'no'), - ) + 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) ); $template = "{title}\n{powers}{invisibility}\n{flying}"; diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index 6edda99d7..6f1332384 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -8,7 +8,7 @@ class Session_test extends CI_TestCase { protected $settings = array( 'use_cookies' => 0, 'use_only_cookies' => 0, - 'cache_limiter' => false + 'cache_limiter' => FALSE ); protected $setting_vals = array(); protected $cookie_vals; @@ -91,7 +91,7 @@ class Session_test extends CI_TestCase { $cmsg1 = 'Some test data'; $cmsg2 = 42; $nmsg1 = 'Other test data'; - $nmsg2 = true; + $nmsg2 = TRUE; $this->session->cookie->set_userdata($key1, $cmsg1); $this->session->set_userdata($ckey2, $cmsg2); $this->session->native->set_userdata($key1, $nmsg1); @@ -156,11 +156,11 @@ class Session_test extends CI_TestCase { $this->session->native->set_userdata($ndata); // Make sure all values are present - $call = $this->session->cookie->all_userdata(); + $call = $this->session->cookie->userdata(); foreach ($cdata as $key => $value) { $this->assertEquals($value, $call[$key]); } - $nall = $this->session->native->all_userdata(); + $nall = $this->session->native->userdata(); foreach ($ndata as $key => $value) { $this->assertEquals($value, $nall[$key]); } @@ -283,8 +283,8 @@ class Session_test extends CI_TestCase { // Simulate page reload and verify independent messages $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($cdata, $this->session->cookie->all_flashdata()); - $this->assertEquals($ndata, $this->session->native->all_flashdata()); + $this->assertEquals($cdata, $this->session->cookie->flashdata()); + $this->assertEquals($ndata, $this->session->native->flashdata()); // Keep messages $this->session->cookie->keep_flashdata($kdata); @@ -293,14 +293,14 @@ class Session_test extends CI_TestCase { // Simulate next page reload and verify message persistence $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($cdata, $this->session->cookie->all_flashdata()); - $this->assertEquals($ndata, $this->session->native->all_flashdata()); + $this->assertEquals($cdata, $this->session->cookie->flashdata()); + $this->assertEquals($ndata, $this->session->native->flashdata()); // Simulate next page reload and verify absence of messages $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEmpty($this->session->cookie->all_flashdata()); - $this->assertEmpty($this->session->native->all_flashdata()); + $this->assertEmpty($this->session->cookie->flashdata()); + $this->assertEmpty($this->session->native->flashdata()); } /** @@ -329,8 +329,8 @@ class Session_test extends CI_TestCase { // Simulate page reload and make sure all values are present $this->session->cookie->reload(); $this->session->native->reload(); - $this->assertEquals($cdata, $this->session->cookie->all_flashdata()); - $this->assertEquals($ndata, $this->session->native->all_flashdata()); + $this->assertEquals($cdata, $this->session->cookie->flashdata()); + $this->assertEquals($ndata, $this->session->native->flashdata()); } /** diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index ce04b6a6d..8e7452474 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -34,7 +34,7 @@ class Table_test extends CI_TestCase { } /* - * @depends testPrepArgs + * @depends test_prep_args */ public function test_set_heading() { @@ -55,7 +55,7 @@ class Table_test extends CI_TestCase { } /* - * @depends testPrepArgs + * @depends test_prep_args */ public function test_add_row() { @@ -200,16 +200,14 @@ class Table_test extends CI_TestCase { public function test_set_from_array() { - $this->assertFalse($this->table->set_from_array('bogus')); - $this->assertFalse($this->table->set_from_array(NULL)); - $data = array( array('name', 'color', 'number'), array('Laura', 'Red', '22'), array('Katie', 'Blue') ); - $this->table->set_from_array($data, FALSE); + $this->table->auto_heading = FALSE; + $this->table->set_from_array($data); $this->assertEmpty($this->table->heading); $this->table->clear(); @@ -235,22 +233,14 @@ class Table_test extends CI_TestCase { public function test_set_from_object() { - // Make a stub of query instance - $query = new CI_TestCase(); - $query->list_fields = function(){ - return array('name', 'email'); - }; - $query->result_array = function(){ - return array( - array('name' => 'John Doe', 'email' => 'john@doe.com'), - array('name' => 'Foo Bar', 'email' => 'foo@bar.com'), - ); - }; - $query->num_rows = function(){ - return 2; - }; - - $this->table->set_from_object($query); + // This needs to be passed by reference to CI_DB_result::__construct() + $dummy = new stdClass(); + $dummy->conn_id = NULL; + $dummy->result_id = NULL; + + $db_result = new DB_result_dummy($dummy); + + $this->table->set_from_db_result($db_result); $expected = array( array('data' => 'name'), @@ -290,4 +280,21 @@ class Table_test extends CI_TestCase { $this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE); } +} + +// We need this for the _set_from_db_result() test +class DB_result_dummy extends CI_DB_result +{ + public function list_fields() + { + return array('name', 'email'); + } + + public function result_array() + { + return array( + array('name' => 'John Doe', 'email' => 'john@doe.com'), + array('name' => 'Foo Bar', 'email' => 'foo@bar.com') + ); + } }
\ No newline at end of file diff --git a/tests/codeigniter/libraries/Useragent_test.php b/tests/codeigniter/libraries/Useragent_test.php index e3726554e..aed38b8c2 100644 --- a/tests/codeigniter/libraries/Useragent_test.php +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -11,9 +11,7 @@ class UserAgent_test extends CI_TestCase { $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; $this->ci_vfs_clone('application/config/user_agents.php'); - $this->agent = new Mock_Libraries_UserAgent(); - $this->ci_instance_var('agent', $this->agent); } @@ -40,12 +38,27 @@ class UserAgent_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_util_is_functions() + public function test_is_functions() { $this->assertTrue($this->agent->is_browser()); + $this->assertTrue($this->agent->is_browser('Safari')); + $this->assertFalse($this->agent->is_browser('Firefox')); $this->assertFalse($this->agent->is_robot()); $this->assertFalse($this->agent->is_mobile()); + } + + // -------------------------------------------------------------------- + + public function test_referrer() + { + $_SERVER['HTTP_REFERER'] = 'http://codeigniter.com/user_guide/'; + $this->assertTrue($this->agent->is_referral()); + $this->assertEquals('http://codeigniter.com/user_guide/', $this->agent->referrer()); + + $this->agent->referer = NULL; + unset($_SERVER['HTTP_REFERER']); $this->assertFalse($this->agent->is_referral()); + $this->assertEquals('', $this->agent->referrer()); } // -------------------------------------------------------------------- @@ -63,7 +76,6 @@ class UserAgent_test extends CI_TestCase { $this->assertEquals('Safari', $this->agent->browser()); $this->assertEquals('533.20.27', $this->agent->version()); $this->assertEquals('', $this->agent->robot()); - $this->assertEquals('', $this->agent->referrer()); } // -------------------------------------------------------------------- @@ -71,14 +83,43 @@ class UserAgent_test extends CI_TestCase { public function test_charsets() { $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; + $this->agent->charsets = array(); + $this->agent->charsets(); + $this->assertTrue($this->agent->accept_charset('utf8')); + $this->assertFalse($this->agent->accept_charset('foo')); + $this->assertEquals('utf8', $this->agent->charsets[0]); + + $_SERVER['HTTP_ACCEPT_CHARSET'] = ''; + $this->agent->charsets = array(); + $this->assertFalse($this->agent->accept_charset()); + $this->assertEquals('Undefined', $this->agent->charsets[0]); - $charsets = $this->agent->charsets(); - - $this->assertEquals('utf8', $charsets[0]); + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'iso-8859-5, unicode-1-1; q=0.8'; + $this->agent->charsets = array(); + $this->assertTrue($this->agent->accept_charset('iso-8859-5')); + $this->assertTrue($this->agent->accept_charset('unicode-1-1')); + $this->assertFalse($this->agent->accept_charset('foo')); + $this->assertEquals('iso-8859-5', $this->agent->charsets[0]); + $this->assertEquals('unicode-1-1', $this->agent->charsets[1]); unset($_SERVER['HTTP_ACCEPT_CHARSET']); + } - $this->assertFalse($this->agent->accept_charset()); + public function test_parse() + { + $new_agent = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0'; + $this->agent->parse($new_agent); + + $this->assertEquals('Android', $this->agent->platform()); + $this->assertEquals('Firefox', $this->agent->browser()); + $this->assertEquals('13.0', $this->agent->version()); + $this->assertEquals('', $this->agent->robot()); + $this->assertEquals('Android', $this->agent->mobile()); + $this->assertEquals($new_agent, $this->agent->agent_string()); + $this->assertTrue($this->agent->is_browser()); + $this->assertFalse($this->agent->is_robot()); + $this->assertTrue($this->agent->is_mobile()); + $this->assertTrue($this->agent->is_mobile('android')); } }
\ No newline at end of file diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 3d216da1f..33942768d 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -14,26 +14,49 @@ function autoload($class) $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; $ci_core = array( - 'Benchmark', 'Config', 'Controller', - 'Exceptions', 'Hooks', 'Input', - 'Lang', 'Loader', 'Log', 'Model', - 'Output', 'Router', 'Security', - 'URI', 'Utf8', + 'Benchmark', + 'Config', + 'Controller', + 'Exceptions', + 'Hooks', + 'Input', + 'Lang', + 'Loader', + 'Log', + 'Model', + 'Output', + 'Router', + 'Security', + 'URI', + 'Utf8' ); $ci_libraries = array( - 'Calendar', 'Cart', 'Driver_Library', - 'Email', 'Encrypt', 'Form_validation', - 'Ftp', 'Image_lib', 'Javascript', - 'Migration', 'Pagination', 'Parser', - 'Profiler', 'Table', 'Trackback', - 'Typography', 'Unit_test', 'Upload', - 'User_agent', 'Xmlrpc', 'Zip' + 'Calendar', + 'Cart', + 'Driver_Library', + 'Email', + 'Encrypt', + 'Encryption', + 'Form_validation', + 'Ftp', + 'Image_lib', + 'Javascript', + 'Migration', + 'Pagination', + 'Parser', + 'Profiler', + 'Table', + 'Trackback', + 'Typography', + 'Unit_test', + 'Upload', + 'User_agent', + 'Xmlrpc', + 'Zip' ); - $ci_drivers = array( - 'Session', - ); + $ci_drivers = array('Session', 'Cache'); if (strpos($class, 'Mock_') === 0) { @@ -89,21 +112,7 @@ function autoload($class) if ( ! file_exists($file)) { - $trace = debug_backtrace(); - - if ($trace[2]['function'] === 'class_exists' OR $trace[2]['function'] === 'file_exists') - { - // If the autoload call came from `class_exists` or `file_exists`, - // we skipped and return FALSE - return FALSE; - } - elseif (($autoloader = spl_autoload_functions()) && end($autoloader) !== __FUNCTION__) - { - // If there was other custom autoloader, passed away - return FALSE; - } - - throw new InvalidArgumentException("Unable to load {$class}."); + return FALSE; } include_once($file); diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index e5dc29c86..9eb6b0954 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -87,40 +87,6 @@ if ( ! function_exists('load_class')) } } -// This is sort of meh. Should probably be mocked up with -// controllable output, so that we can test some of our -// security code. The function itself will be tested in the -// bootstrap testsuite. -// -------------------------------------------------------------------- - -if ( ! function_exists('remove_invisible_characters')) -{ - function remove_invisible_characters($str, $url_encoded = TRUE) - { - $non_displayables = array(); - - // every control character except newline (dec 10) - // carriage return (dec 13), and horizontal tab (dec 09) - - if ($url_encoded) - { - $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 - $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 - } - - $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 - - do - { - $str = preg_replace($non_displayables, '', $str, -1, $count); - } - while ($count); - - return $str; - } -} - - // Clean up error messages // -------------------------------------------------------------------- @@ -150,23 +116,6 @@ if ( ! function_exists('_exception_handler')) // We assume a few things about our environment ... // -------------------------------------------------------------------- - -if ( ! function_exists('is_php')) -{ - function is_php($version = '5.0.0') - { - return ! (version_compare(PHP_VERSION, $version) < 0); - } -} - -if ( ! function_exists('is_really_writable')) -{ - function is_really_writable($file) - { - return is_writable($file); - } -} - if ( ! function_exists('is_loaded')) { function &is_loaded() @@ -190,4 +139,13 @@ if ( ! function_exists('set_status_header')) { return TRUE; } +} + +if ( ! function_exists('is_cli')) +{ + // In order to test HTTP functionality, we need to lie about this + function is_cli() + { + return FALSE; + } }
\ No newline at end of file diff --git a/tests/mocks/core/uri.php b/tests/mocks/core/uri.php index 94f75df64..96ec5afa1 100644 --- a/tests/mocks/core/uri.php +++ b/tests/mocks/core/uri.php @@ -10,17 +10,23 @@ class Mock_Core_URI extends CI_URI { // set predictable config values $test->ci_set_config(array( 'index_page' => 'index.php', - 'base_url' => 'http://example.com/', - 'subclass_prefix' => 'MY_' + 'base_url' => 'http://example.com/', + 'subclass_prefix' => 'MY_', + 'enable_query_strings' => FALSE, + 'permitted_uri_chars' => 'a-z 0-9~%.:_\-' )); $this->config = new $cls; + if ($this->config->item('enable_query_strings') !== TRUE OR is_cli()) + { + $this->_permitted_uri_chars = $this->config->item('permitted_uri_chars'); + } } - protected function _is_cli_request() + public function _set_permitted_uri_chars($value) { - return FALSE; + $this->_permitted_uri_chars = $value; } }
\ No newline at end of file diff --git a/tests/mocks/core/utf8.php b/tests/mocks/core/utf8.php index 068e74ac1..30b78adfe 100644 --- a/tests/mocks/core/utf8.php +++ b/tests/mocks/core/utf8.php @@ -3,24 +3,19 @@ class Mock_Core_Utf8 extends CI_Utf8 { /** - * We need to define several constants as - * the same process within CI_Utf8 class constructor. + * We need to define UTF8_ENABLED the same way that + * CI_Utf8 constructor does. * * @covers CI_Utf8::__construct() */ public function __construct() { defined('UTF8_ENABLED') OR define('UTF8_ENABLED', TRUE); + } - if (extension_loaded('mbstring')) - { - defined('MB_ENABLED') OR define('MB_ENABLED', TRUE); - mb_internal_encoding('UTF-8'); - } - else - { - defined('MB_ENABLED') OR define('MB_ENABLED', FALSE); - } + public function is_ascii_test($str) + { + return $this->_is_ascii($str); } }
\ No newline at end of file diff --git a/tests/mocks/libraries/encryption.php b/tests/mocks/libraries/encryption.php new file mode 100644 index 000000000..028eecc72 --- /dev/null +++ b/tests/mocks/libraries/encryption.php @@ -0,0 +1,39 @@ +<?php + +class Mock_Libraries_Encryption extends CI_Encryption { + + /** + * __get_params() + * + * Allows public calls to the otherwise protected _get_params(). + */ + public function __get_params($params) + { + return $this->_get_params($params); + } + + // -------------------------------------------------------------------- + + /** + * get_key() + * + * Allows checking for key changes. + */ + public function get_key() + { + return $this->_key; + } + + // -------------------------------------------------------------------- + + /** + * __driver_get_handle() + * + * Allows checking for _mcrypt_get_handle(), _openssl_get_handle() + */ + public function __driver_get_handle($driver, $cipher, $mode) + { + return $this->{'_'.$driver.'_get_handle'}($cipher, $mode); + } + +}
\ No newline at end of file |