HasherTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Security;
  9. use OC\Security\Hasher;
  10. use OCP\IConfig;
  11. /**
  12. * Class HasherTest
  13. */
  14. class HasherTest extends \Test\TestCase {
  15. /**
  16. * @return array
  17. */
  18. public function versionHashProvider()
  19. {
  20. return array(
  21. array('asf32äà$$a.|3', null),
  22. array('asf32äà$$a.|3|5', null),
  23. array('1|2|3|4', array('version' => 1, 'hash' => '2|3|4')),
  24. array('1|我看|这本书。 我看這本書', array('version' => 1, 'hash' => '我看|这本书。 我看這本書'))
  25. );
  26. }
  27. /**
  28. * @return array
  29. */
  30. public function allHashProviders()
  31. {
  32. return array(
  33. // Bogus values
  34. array(null, 'asf32äà$$a.|3', false),
  35. array(null, false, false),
  36. // Valid SHA1 strings
  37. array('password', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', true),
  38. array('owncloud.com', '27a4643e43046c3569e33b68c1a4b15d31306d29', true),
  39. // Invalid SHA1 strings
  40. array('InvalidString', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', false),
  41. array('AnotherInvalidOne', '27a4643e43046c3569e33b68c1a4b15d31306d29', false),
  42. // Valid legacy password string with password salt "6Wow67q1wZQZpUUeI6G2LsWUu4XKx"
  43. array('password', '$2a$08$emCpDEl.V.QwPWt5gPrqrOhdpH6ailBmkj2Hd2vD5U8qIy20HBe7.', true),
  44. array('password', '$2a$08$yjaLO4ev70SaOsWZ9gRS3eRSEpHVsmSWTdTms1949mylxJ279hzo2', true),
  45. array('password', '$2a$08$.jNRG/oB4r7gHJhAyb.mDupNUAqTnBIW/tWBqFobaYflKXiFeG0A6', true),
  46. array('owncloud.com', '$2a$08$YbEsyASX/hXVNMv8hXQo7ezreN17T8Jl6PjecGZvpX.Ayz2aUyaZ2', true),
  47. array('owncloud.com', '$2a$11$cHdDA2IkUP28oNGBwlL7jO/U3dpr8/0LIjTZmE8dMPA7OCUQsSTqS', true),
  48. array('owncloud.com', '$2a$08$GH.UoIfJ1e.qeZ85KPqzQe6NR8XWRgJXWIUeE1o/j1xndvyTA1x96', true),
  49. // Invalid legacy passwords
  50. array('password', '$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false),
  51. // Valid passwords "6Wow67q1wZQZpUUeI6G2LsWUu4XKx"
  52. array('password', '1|$2a$05$ezAE0dkwk57jlfo6z5Pql.gcIK3ReXT15W7ITNxVS0ksfhO/4E4Kq', true),
  53. array('password', '1|$2a$05$4OQmloFW4yTVez2MEWGIleDO9Z5G9tWBXxn1vddogmKBQq/Mq93pe', true),
  54. array('password', '1|$2a$11$yj0hlp6qR32G9exGEXktB.yW2rgt2maRBbPgi3EyxcDwKrD14x/WO', true),
  55. array('owncloud.com', '1|$2a$10$Yiss2WVOqGakxuuqySv5UeOKpF8d8KmNjuAPcBMiRJGizJXjA2bKm', true),
  56. array('owncloud.com', '1|$2a$10$v9mh8/.mF/Ut9jZ7pRnpkuac3bdFCnc4W/gSumheQUi02Sr.xMjPi', true),
  57. array('owncloud.com', '1|$2a$05$ST5E.rplNRfDCzRpzq69leRzsTGtY7k88h9Vy2eWj0Ug/iA9w5kGK', true),
  58. // Invalid passwords
  59. array('password', '0|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false),
  60. array('password', '1|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false),
  61. array('password', '2|$2a$08$oKAQY5IhnZocP.61MwP7xu7TNeOb7Ostvk3j6UpacvaNMs.xRj7O2', false),
  62. );
  63. }
  64. /** @var Hasher */
  65. protected $hasher;
  66. /** @var IConfig */
  67. protected $config;
  68. protected function setUp() {
  69. parent::setUp();
  70. $this->config = $this->getMockBuilder(IConfig::class)
  71. ->disableOriginalConstructor()->getMock();
  72. $this->hasher = new Hasher($this->config);
  73. }
  74. function testHash() {
  75. $hash = $this->hasher->hash('String To Hash');
  76. $this->assertNotNull($hash);
  77. }
  78. /**
  79. * @dataProvider versionHashProvider
  80. */
  81. function testSplitHash($hash, $expected) {
  82. $relativePath = self::invokePrivate($this->hasher, 'splitHash', array($hash));
  83. $this->assertSame($expected, $relativePath);
  84. }
  85. /**
  86. * @dataProvider allHashProviders
  87. */
  88. function testVerify($password, $hash, $expected) {
  89. $this->config
  90. ->expects($this->any())
  91. ->method('getSystemValue')
  92. ->with('passwordsalt', null)
  93. ->will($this->returnValue('6Wow67q1wZQZpUUeI6G2LsWUu4XKx'));
  94. $result = $this->hasher->verify($password, $hash);
  95. $this->assertSame($expected, $result);
  96. }
  97. }