MasterPasswordBehaviorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. App::uses('MasterPasswordBehavior', 'Tools.Model/Behavior');
  3. App::uses('ModelBehavior', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. Configure::write('MasterPassword.password', '7c4a8d09ca3762af61e59520943dc26494f8941b');
  6. class MasterPasswordBehaviorTest extends MyCakeTestCase {
  7. public $MasterPasswordBehavior;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->MasterPasswordBehavior = new MasterPasswordBehavior();
  11. $this->Model = ClassRegistry::init('MasterPasswordTestModel');
  12. }
  13. public function testObject() {
  14. $this->assertTrue(is_object($this->MasterPasswordBehavior));
  15. $this->assertInstanceOf('MasterPasswordBehavior', $this->MasterPasswordBehavior);
  16. }
  17. /**
  18. * test 123456
  19. */
  20. public function testSinglePwd() {
  21. $this->Model->Behaviors->load('Tools.MasterPassword');
  22. $data = array(
  23. 'some_comment' => 'xyz',
  24. );
  25. $this->Model->set($data);
  26. $res = $this->Model->validates();
  27. $this->assertFalse($res);
  28. $data = array(
  29. 'some_comment' => 'xyz',
  30. 'master_pwd' => ''
  31. );
  32. $this->Model->set($data);
  33. $res = $this->Model->validates();
  34. $this->assertFalse($res);
  35. $data = array(
  36. 'some_comment' => 'xyz',
  37. 'master_pwd' => '123'
  38. );
  39. $this->Model->set($data);
  40. $res = $this->Model->validates();
  41. $this->assertFalse($res);
  42. $data = array(
  43. 'some_comment' => 'xyz',
  44. 'master_pwd' => '123456'
  45. );
  46. $this->Model->set($data);
  47. $res = $this->Model->validates();
  48. $this->assertTrue($res);
  49. }
  50. /**
  51. * test xxzzyy
  52. * with specific settings
  53. */
  54. public function testComplex() {
  55. Configure::write('MasterPassword.password', '373e28e7cdb42d7aefc49c5f34fa589a7ff1eefd0ac01f573d90299f79a01a05');
  56. $this->Model->Behaviors->load('Tools.MasterPassword', array('field'=>'master_password', 'hash'=>'sha256', 'message'=>'No way'));
  57. $data = array(
  58. 'some_comment' => 'xyz',
  59. 'master_password' => '123'
  60. );
  61. $this->Model->set($data);
  62. $res = $this->Model->validates();
  63. $this->assertFalse($res);
  64. $this->assertEquals(__('No way'), $this->Model->validationErrors['master_password'][0]);
  65. $data = array(
  66. 'some_comment' => 'xyz',
  67. 'master_password' => 'xxyyzz'
  68. );
  69. $this->Model->set($data);
  70. $res = $this->Model->validates();
  71. $this->assertTrue($res);
  72. $this->assertEmpty($this->Model->invalidFields());
  73. }
  74. /**
  75. * test xxzzyy with salt
  76. * with specific settings
  77. */
  78. public function testWithSalt() {
  79. $hash = hash('sha256', Configure::read('Security.salt').'xxyyzz');
  80. Configure::write('MasterPassword.password', $hash);
  81. $this->Model->Behaviors->load('Tools.MasterPassword', array('hash'=>'sha256', 'salt'=>true));
  82. $data = array(
  83. 'some_comment' => 'xyz',
  84. 'master_pwd' => 'xxyyzz'
  85. );
  86. $this->Model->set($data);
  87. $res = $this->Model->validates();
  88. $this->assertTrue($res);
  89. $hash = hash('sha256', '123'.'xxyyzz');
  90. Configure::write('MasterPassword.password', $hash);
  91. $this->Model->Behaviors->load('Tools.MasterPassword', array('hash'=>'sha256', 'salt'=>'123'));
  92. $data = array(
  93. 'some_comment' => 'xyz',
  94. 'master_pwd' => 'xxyyzz'
  95. );
  96. $this->Model->set($data);
  97. $res = $this->Model->validates();
  98. $this->assertTrue($res);
  99. }
  100. /**
  101. * test 123456 and 654321
  102. */
  103. public function testMultiplePwd() {
  104. Configure::write('MasterPassword.password', array('dd5fef9c1c1da1394d6d34b248c51be2ad740840', '7c4a8d09ca3762af61e59520943dc26494f8941b'));
  105. $this->Model->Behaviors->load('Tools.MasterPassword');
  106. $data = array(
  107. 'some_comment' => 'xyz',
  108. );
  109. $this->Model->set($data);
  110. $res = $this->Model->validates();
  111. $this->assertFalse($res);
  112. $data = array(
  113. 'some_comment' => 'xyz',
  114. 'master_pwd' => ''
  115. );
  116. $this->Model->set($data);
  117. $res = $this->Model->validates();
  118. $this->assertFalse($res);
  119. $data = array(
  120. 'some_comment' => 'xyz',
  121. 'master_pwd' => '123'
  122. );
  123. $this->Model->set($data);
  124. $res = $this->Model->validates();
  125. $this->assertFalse($res);
  126. $data = array(
  127. 'some_comment' => 'xyz',
  128. 'master_pwd' => '123456'
  129. );
  130. $this->Model->set($data);
  131. $res = $this->Model->validates();
  132. $this->assertTrue($res);
  133. $data = array(
  134. 'some_comment' => 'xyz',
  135. 'master_pwd' => '654321'
  136. );
  137. $this->Model->set($data);
  138. $res = $this->Model->validates();
  139. $this->assertTrue($res);
  140. }
  141. }
  142. class MasterPasswordTestModel extends CakeTestModel {
  143. public $useTable = false;
  144. }