| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- App::uses('MasterPasswordBehavior', 'Tools.Model/Behavior');
- App::uses('ModelBehavior', 'Model');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- Configure::write('MasterPassword.password', '7c4a8d09ca3762af61e59520943dc26494f8941b');
- class MasterPasswordBehaviorTest extends MyCakeTestCase {
- public $MasterPasswordBehavior;
- public function setUp() {
- parent::setUp();
- $this->MasterPasswordBehavior = new MasterPasswordBehavior();
- $this->Model = ClassRegistry::init('MasterPasswordTestModel');
- }
- public function testObject() {
- $this->assertTrue(is_object($this->MasterPasswordBehavior));
- $this->assertInstanceOf('MasterPasswordBehavior', $this->MasterPasswordBehavior);
- }
- /**
- * test 123456
- */
- public function testSinglePwd() {
- $this->Model->Behaviors->load('Tools.MasterPassword');
- $data = array(
- 'some_comment' => 'xyz',
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => ''
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => '123'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => '123456'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- }
- /**
- * test xxzzyy
- * with specific settings
- */
- public function testComplex() {
- Configure::write('MasterPassword.password', '373e28e7cdb42d7aefc49c5f34fa589a7ff1eefd0ac01f573d90299f79a01a05');
- $this->Model->Behaviors->load('Tools.MasterPassword', array('field'=>'master_password', 'hash'=>'sha256', 'message'=>'No way'));
- $data = array(
- 'some_comment' => 'xyz',
- 'master_password' => '123'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $this->assertEquals(__('No way'), $this->Model->validationErrors['master_password'][0]);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_password' => 'xxyyzz'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- $this->assertEmpty($this->Model->invalidFields());
- }
- /**
- * test xxzzyy with salt
- * with specific settings
- */
- public function testWithSalt() {
- $hash = hash('sha256', Configure::read('Security.salt').'xxyyzz');
- Configure::write('MasterPassword.password', $hash);
- $this->Model->Behaviors->load('Tools.MasterPassword', array('hash'=>'sha256', 'salt'=>true));
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => 'xxyyzz'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- $hash = hash('sha256', '123'.'xxyyzz');
- Configure::write('MasterPassword.password', $hash);
- $this->Model->Behaviors->load('Tools.MasterPassword', array('hash'=>'sha256', 'salt'=>'123'));
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => 'xxyyzz'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- }
- /**
- * test 123456 and 654321
- */
- public function testMultiplePwd() {
- Configure::write('MasterPassword.password', array('dd5fef9c1c1da1394d6d34b248c51be2ad740840', '7c4a8d09ca3762af61e59520943dc26494f8941b'));
- $this->Model->Behaviors->load('Tools.MasterPassword');
- $data = array(
- 'some_comment' => 'xyz',
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => ''
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => '123'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertFalse($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => '123456'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- $data = array(
- 'some_comment' => 'xyz',
- 'master_pwd' => '654321'
- );
- $this->Model->set($data);
- $res = $this->Model->validates();
- $this->assertTrue($res);
- }
- }
- class MasterPasswordTestModel extends CakeTestModel {
- public $useTable = false;
- }
|