MasterPasswordBehaviorTest.php 4.2 KB

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