AttributeTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Ldap_Attribute
  24. */
  25. require_once 'Zend/Ldap/Attribute.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Ldap
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Ldap
  33. */
  34. class Zend_Ldap_AttributeTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected function _assertLocalDateTimeString($timestamp, $value)
  37. {
  38. $tsValue = date('YmdHisO', $timestamp);
  39. if(date('O', strtotime('20120101'))) {
  40. // Local timezone is +0000 when DST is off. Zend_Ldap converts
  41. // +0000 to "Z" (see Zend_Ldap_Converter:toLdapDateTime()), so
  42. // take account of that here
  43. $tsValue = str_replace('+0000', 'Z', $tsValue);
  44. }
  45. $this->assertEquals($tsValue, $value);
  46. }
  47. protected function _assertUtcDateTimeString($localTimestamp, $value)
  48. {
  49. $localOffset = date('Z', $localTimestamp);
  50. $utcTimestamp = $localTimestamp - $localOffset;
  51. $this->assertEquals(date('YmdHis', $utcTimestamp) . 'Z', $value);
  52. }
  53. public function testGetAttributeValue()
  54. {
  55. $data=array('uid' => array('value'));
  56. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid', 0);
  57. $this->assertEquals('value', $value);
  58. }
  59. public function testGetNonExistentAttributeValue()
  60. {
  61. $data=array('uid' => array('value'));
  62. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid', 1);
  63. $this->assertNull($value);
  64. }
  65. public function testGetNonExistentAttribute()
  66. {
  67. $data=array('uid' => array('value'));
  68. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid2', 0);
  69. $this->assertNull($value);
  70. $array=Zend_Ldap_Attribute::getAttribute($data, 'uid2');
  71. $this->assertTrue(is_array($array));
  72. $this->assertEquals(0, count($array));
  73. }
  74. public function testGetAttributeWithWrongIndexType()
  75. {
  76. $data=array('uid' => array('value'));
  77. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid', 'index');
  78. $this->assertNull($value);
  79. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid', 3.1415);
  80. $this->assertNull($value);
  81. }
  82. public function testGetAttributeArray()
  83. {
  84. $data=array('uid' => array('value'));
  85. $value=Zend_Ldap_Attribute::getAttribute($data, 'uid');
  86. $this->assertTrue(is_array($value));
  87. $this->assertEquals(1, count($value));
  88. $this->assertContains('value', $value);
  89. }
  90. public function testSimpleSetAttribute()
  91. {
  92. $data=array();
  93. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'new', false);
  94. $this->assertArrayHasKey('uid', $data);
  95. $this->assertTrue(is_array($data['uid']));
  96. $this->assertEquals(1, count($data['uid']));
  97. $this->assertContains('new', $data['uid']);
  98. }
  99. public function testSimpleOverwriteAttribute()
  100. {
  101. $data=array('uid' => array('old'));
  102. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'new', false);
  103. $this->assertArrayHasKey('uid', $data);
  104. $this->assertTrue(is_array($data['uid']));
  105. $this->assertEquals(1, count($data['uid']));
  106. $this->assertContains('new', $data['uid']);
  107. }
  108. public function testSimpleAppendAttribute()
  109. {
  110. $data=array('uid' => array('old'));
  111. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'new', true);
  112. $this->assertArrayHasKey('uid', $data);
  113. $this->assertTrue(is_array($data['uid']));
  114. $this->assertEquals(2, count($data['uid']));
  115. $this->assertContains('old', $data['uid']);
  116. $this->assertContains('new', $data['uid']);
  117. $this->assertEquals('old', $data['uid'][0]);
  118. $this->assertEquals('new', $data['uid'][1]);
  119. }
  120. public function testBooleanAttributeHandling()
  121. {
  122. $data=array(
  123. 'p1_true' => array('TRUE'),
  124. 'p1_false' => array('FALSE')
  125. );
  126. Zend_Ldap_Attribute::setAttribute($data, 'p2_true', true);
  127. Zend_Ldap_Attribute::setAttribute($data, 'p2_false', false);
  128. $this->assertEquals('TRUE', $data['p2_true'][0]);
  129. $this->assertEquals('FALSE', $data['p2_false'][0]);
  130. $this->assertEquals(true, Zend_Ldap_Attribute::getAttribute($data, 'p1_true', 0));
  131. $this->assertEquals(false, Zend_Ldap_Attribute::getAttribute($data, 'p1_false', 0));
  132. }
  133. public function testArraySetAttribute()
  134. {
  135. $data=array();
  136. Zend_Ldap_Attribute::setAttribute($data, 'uid', array('new1', 'new2'), false);
  137. $this->assertArrayHasKey('uid', $data);
  138. $this->assertTrue(is_array($data['uid']));
  139. $this->assertEquals(2, count($data['uid']));
  140. $this->assertContains('new1', $data['uid']);
  141. $this->assertContains('new2', $data['uid']);
  142. $this->assertEquals('new1', $data['uid'][0]);
  143. $this->assertEquals('new2', $data['uid'][1]);
  144. }
  145. public function testArrayOverwriteAttribute()
  146. {
  147. $data=array('uid' => array('old'));
  148. Zend_Ldap_Attribute::setAttribute($data, 'uid', array('new1', 'new2'), false);
  149. $this->assertArrayHasKey('uid', $data);
  150. $this->assertTrue(is_array($data['uid']));
  151. $this->assertEquals(2, count($data['uid']));
  152. $this->assertContains('new1', $data['uid']);
  153. $this->assertContains('new2', $data['uid']);
  154. $this->assertEquals('new1', $data['uid'][0]);
  155. $this->assertEquals('new2', $data['uid'][1]);
  156. }
  157. public function testArrayAppendAttribute()
  158. {
  159. $data=array('uid' => array('old'));
  160. Zend_Ldap_Attribute::setAttribute($data, 'uid', array('new1', 'new2'), true);
  161. $this->assertArrayHasKey('uid', $data);
  162. $this->assertTrue(is_array($data['uid']));
  163. $this->assertEquals(3, count($data['uid']));
  164. $this->assertContains('old', $data['uid']);
  165. $this->assertContains('new1', $data['uid']);
  166. $this->assertContains('new2', $data['uid']);
  167. $this->assertEquals('old', $data['uid'][0]);
  168. $this->assertEquals('new1', $data['uid'][1]);
  169. $this->assertEquals('new2', $data['uid'][2]);
  170. }
  171. public function testPasswordSettingSHA()
  172. {
  173. $data=array();
  174. Zend_Ldap_Attribute::setPassword($data, 'pa$$w0rd', Zend_Ldap_Attribute::PASSWORD_HASH_SHA);
  175. $password=Zend_Ldap_Attribute::getAttribute($data, 'userPassword', 0);
  176. $this->assertEquals('{SHA}vi3X+3ptD4ulrdErXo+3W72mRyE=', $password);
  177. }
  178. public function testPasswordSettingMD5()
  179. {
  180. $data=array();
  181. Zend_Ldap_Attribute::setPassword($data, 'pa$$w0rd', Zend_Ldap_Attribute::PASSWORD_HASH_MD5);
  182. $password=Zend_Ldap_Attribute::getAttribute($data, 'userPassword', 0);
  183. $this->assertEquals('{MD5}bJuLJ96h3bhF+WqiVnxnVA==', $password);
  184. }
  185. public function testPasswordSettingUnicodePwd()
  186. {
  187. $data=array();
  188. Zend_Ldap_Attribute::setPassword($data, 'new', Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
  189. $password=Zend_Ldap_Attribute::getAttribute($data, 'unicodePwd', 0);
  190. $this->assertEquals("\x22\x00\x6E\x00\x65\x00\x77\x00\x22\x00", $password);
  191. }
  192. public function testPasswordSettingCustomAttribute()
  193. {
  194. $data=array();
  195. Zend_Ldap_Attribute::setPassword($data, 'pa$$w0rd',
  196. Zend_Ldap_Attribute::PASSWORD_HASH_SHA, 'myAttribute');
  197. $password=Zend_Ldap_Attribute::getAttribute($data, 'myAttribute', 0);
  198. $this->assertNotNull($password);
  199. }
  200. public function testSetAttributeWithObject()
  201. {
  202. $data=array();
  203. $object=new stdClass();
  204. $object->a=1;
  205. $object->b=1.23;
  206. $object->c='string';
  207. Zend_Ldap_Attribute::setAttribute($data, 'object', $object);
  208. $this->assertEquals(serialize($object), $data['object'][0]);
  209. }
  210. public function testSetAttributeWithFilestream()
  211. {
  212. $data=array();
  213. $stream=fopen(dirname(__FILE__) . '/_files/AttributeTest.input.txt', 'r');
  214. Zend_Ldap_Attribute::setAttribute($data, 'file', $stream);
  215. fclose($stream);
  216. $this->assertEquals('String from file', $data['file'][0]);
  217. }
  218. public function testSetDateTimeValueLocal()
  219. {
  220. $ts=mktime(12, 30, 30, 6, 25, 2008);
  221. $data=array();
  222. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, false);
  223. $this->_assertLocalDateTimeString($ts, $data['ts'][0]);
  224. }
  225. public function testSetDateTimeValueUtc()
  226. {
  227. $ts=mktime(12, 30, 30, 6, 25, 2008);
  228. $data=array();
  229. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, true);
  230. $this->_assertUtcDateTimeString($ts, $data['ts'][0]);
  231. }
  232. public function testSetDateTimeValueLocalArray()
  233. {
  234. $ts=array();
  235. $ts[]=mktime(12, 30, 30, 6, 25, 2008);
  236. $ts[]=mktime(1, 25, 30, 1, 2, 2008);
  237. $data=array();
  238. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, false);
  239. $this->_assertLocalDateTimeString($ts[0], $data['ts'][0]);
  240. $this->_assertLocalDateTimeString($ts[1], $data['ts'][1]);
  241. }
  242. public function testSetDateTimeValueIllegal()
  243. {
  244. $ts='dummy';
  245. $data=array();
  246. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, false);
  247. $this->assertEquals(0, count($data['ts']));
  248. }
  249. public function testGetDateTimeValueFromLocal()
  250. {
  251. $ts=mktime(12, 30, 30, 6, 25, 2008);
  252. $data=array();
  253. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, false);
  254. $this->_assertLocalDateTimeString($ts, $data['ts'][0]);
  255. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts', 0);
  256. $this->assertEquals($ts, $retTs);
  257. }
  258. public function testGetDateTimeValueFromUtc()
  259. {
  260. $ts=mktime(12, 30, 30, 6, 25, 2008);
  261. $data=array();
  262. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, true);
  263. $this->_assertUtcDateTimeString($ts, $data['ts'][0]);
  264. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts', 0);
  265. $this->assertEquals($ts, $retTs);
  266. }
  267. public function testGetDateTimeValueFromArray()
  268. {
  269. $ts=array();
  270. $ts[]=mktime(12, 30, 30, 6, 25, 2008);
  271. $ts[]=mktime(1, 25, 30, 1, 2, 2008);
  272. $data=array();
  273. Zend_Ldap_Attribute::setDateTimeAttribute($data, 'ts', $ts, false);
  274. $this->_assertLocalDateTimeString($ts[0], $data['ts'][0]);
  275. $this->_assertLocalDateTimeString($ts[1], $data['ts'][1]);
  276. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts');
  277. $this->assertEquals($ts[0], $retTs[0]);
  278. $this->assertEquals($ts[1], $retTs[1]);
  279. }
  280. public function testGetDateTimeValueIllegal()
  281. {
  282. $data=array('ts' => array('dummy'));
  283. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts', 0);
  284. $this->assertEquals('dummy', $retTs);
  285. }
  286. public function testGetDateTimeValueNegativeOffet()
  287. {
  288. $data=array('ts' => array('20080612143045-0700'));
  289. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts', 0);
  290. $tsCompare=gmmktime(21, 30, 45, 6, 12, 2008);
  291. $this->assertEquals($tsCompare, $retTs);
  292. }
  293. public function testGetDateTimeValueNegativeOffet2()
  294. {
  295. $data=array('ts' => array('20080612143045-0715'));
  296. $retTs=Zend_Ldap_Attribute::getDateTimeAttribute($data, 'ts', 0);
  297. $tsCompare=gmmktime(21, 45, 45, 6, 12, 2008);
  298. $this->assertEquals($tsCompare, $retTs);
  299. }
  300. public function testRemoveAttributeValueSimple()
  301. {
  302. $data=array('test' => array('value1', 'value2', 'value3', 'value3'));
  303. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', 'value2');
  304. $this->assertArrayHasKey('test', $data);
  305. $this->assertTrue(is_array($data['test']));
  306. $this->assertEquals(3, count($data['test']));
  307. $this->assertContains('value1', $data['test']);
  308. $this->assertContains('value3', $data['test']);
  309. $this->assertNotContains('value2', $data['test']);
  310. }
  311. public function testRemoveAttributeValueArray()
  312. {
  313. $data=array('test' => array('value1', 'value2', 'value3', 'value3'));
  314. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', array('value1', 'value2'));
  315. $this->assertArrayHasKey('test', $data);
  316. $this->assertTrue(is_array($data['test']));
  317. $this->assertEquals(2, count($data['test']));
  318. $this->assertContains('value3', $data['test']);
  319. $this->assertNotContains('value1', $data['test']);
  320. $this->assertNotContains('value2', $data['test']);
  321. }
  322. public function testRemoveAttributeMultipleValueSimple()
  323. {
  324. $data=array('test' => array('value1', 'value2', 'value3', 'value3'));
  325. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', 'value3');
  326. $this->assertArrayHasKey('test', $data);
  327. $this->assertTrue(is_array($data['test']));
  328. $this->assertEquals(2, count($data['test']));
  329. $this->assertContains('value1', $data['test']);
  330. $this->assertContains('value2', $data['test']);
  331. $this->assertNotContains('value3', $data['test']);
  332. }
  333. public function testRemoveAttributeMultipleValueArray()
  334. {
  335. $data=array('test' => array('value1', 'value2', 'value3', 'value3'));
  336. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', array('value1', 'value3'));
  337. $this->assertArrayHasKey('test', $data);
  338. $this->assertTrue(is_array($data['test']));
  339. $this->assertEquals(1, count($data['test']));
  340. $this->assertContains('value2', $data['test']);
  341. $this->assertNotContains('value1', $data['test']);
  342. $this->assertNotContains('value3', $data['test']);
  343. }
  344. public function testRemoveAttributeValueBoolean()
  345. {
  346. $data=array('test' => array('TRUE', 'FALSE', 'TRUE', 'FALSE'));
  347. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', false);
  348. $this->assertArrayHasKey('test', $data);
  349. $this->assertTrue(is_array($data['test']));
  350. $this->assertEquals(2, count($data['test']));
  351. $this->assertContains('TRUE', $data['test']);
  352. $this->assertNotContains('FALSE', $data['test']);
  353. }
  354. public function testRemoveAttributeValueInteger()
  355. {
  356. $data=array('test' => array('1', '2', '3', '4'));
  357. Zend_Ldap_Attribute::removeFromAttribute($data, 'test', array(2, 4));
  358. $this->assertArrayHasKey('test', $data);
  359. $this->assertTrue(is_array($data['test']));
  360. $this->assertEquals(2, count($data['test']));
  361. $this->assertContains('1', $data['test']);
  362. $this->assertContains('3', $data['test']);
  363. $this->assertNotContains('2', $data['test']);
  364. $this->assertNotContains('4', $data['test']);
  365. }
  366. public function testConvertFromLdapValue()
  367. {
  368. $this->assertEquals(true, Zend_Ldap_Attribute::convertFromLdapValue('TRUE'));
  369. $this->assertEquals(false, Zend_Ldap_Attribute::convertFromLdapValue('FALSE'));
  370. }
  371. public function testConvertToLdapValue()
  372. {
  373. $this->assertEquals('string', Zend_Ldap_Attribute::convertToLdapValue('string'));
  374. $this->assertEquals('1', Zend_Ldap_Attribute::convertToLdapValue(1));
  375. $this->assertEquals('TRUE', Zend_Ldap_Attribute::convertToLdapValue(true));
  376. }
  377. public function testConvertFromLdapDateTimeValue()
  378. {
  379. $ldap='20080625123030+0200';
  380. $this->assertEquals(gmmktime(10, 30, 30, 6, 25, 2008),
  381. Zend_Ldap_Attribute::convertFromLdapDateTimeValue($ldap));
  382. }
  383. public function testConvertFromLdapDateTimeValueActiveDirectory()
  384. {
  385. $ldap='20080625123030.0+0200';
  386. $this->assertEquals(gmmktime(10, 30, 30, 6, 25, 2008),
  387. Zend_Ldap_Attribute::convertFromLdapDateTimeValue($ldap));
  388. }
  389. public function testConvertToLdapDateTimeValue()
  390. {
  391. $ts=mktime(12, 30, 30, 6, 25, 2008);
  392. $this->_assertLocalDateTimeString($ts, Zend_Ldap_Attribute::convertToLdapDateTimeValue($ts));
  393. }
  394. public function testRemoveDuplicates()
  395. {
  396. $data=array(
  397. 'strings1' => array('value1', 'value2', 'value2', 'value3'),
  398. 'strings2' => array('value1', 'value2', 'value3', 'value4'),
  399. 'boolean1' => array('TRUE', 'TRUE', 'TRUE', 'TRUE'),
  400. 'boolean2' => array('TRUE', 'FALSE', 'TRUE', 'FALSE'),
  401. );
  402. $expected=array(
  403. 'strings1' => array('value1', 'value2', 'value3'),
  404. 'strings2' => array('value1', 'value2', 'value3', 'value4'),
  405. 'boolean1' => array('TRUE'),
  406. 'boolean2' => array('TRUE', 'FALSE'),
  407. );
  408. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($data, 'strings1');
  409. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($data, 'strings2');
  410. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($data, 'boolean1');
  411. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($data, 'boolean2');
  412. $this->assertEquals($expected, $data);
  413. }
  414. public function testHasValue()
  415. {
  416. $data=array(
  417. 'strings1' => array('value1', 'value2', 'value2', 'value3'),
  418. 'strings2' => array('value1', 'value2', 'value3', 'value4'),
  419. 'boolean1' => array('TRUE', 'TRUE', 'TRUE', 'TRUE'),
  420. 'boolean2' => array('TRUE', 'FALSE', 'TRUE', 'FALSE'),
  421. );
  422. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'strings1', 'value1'));
  423. $this->assertFalse(Zend_Ldap_Attribute::attributeHasValue($data, 'strings1', 'value4'));
  424. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'boolean1', true));
  425. $this->assertFalse(Zend_Ldap_Attribute::attributeHasValue($data, 'boolean1', false));
  426. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'strings1',
  427. array('value1', 'value2')));
  428. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'strings1',
  429. array('value1', 'value2', 'value3')));
  430. $this->assertFalse(Zend_Ldap_Attribute::attributeHasValue($data, 'strings1',
  431. array('value1', 'value2', 'value3', 'value4')));
  432. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'strings2',
  433. array('value1', 'value2', 'value3', 'value4')));
  434. $this->assertTrue(Zend_Ldap_Attribute::attributeHasValue($data, 'boolean2',
  435. array(true, false)));
  436. $this->assertFalse(Zend_Ldap_Attribute::attributeHasValue($data, 'boolean1',
  437. array(true, false)));
  438. }
  439. public function testPasswordGenerationSSHA()
  440. {
  441. $password = 'pa$$w0rd';
  442. $ssha = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_HASH_SSHA);
  443. $encoded = substr($ssha, strpos($ssha, '}'));
  444. $binary = base64_decode($encoded);
  445. $this->assertEquals(24, strlen($binary));
  446. $hash = substr($binary, 0, 20);
  447. $salt = substr($binary, 20);
  448. $this->assertEquals(4, strlen($salt));
  449. $this->assertEquals(sha1($password . $salt, true), $hash);
  450. }
  451. public function testPasswordGenerationSHA()
  452. {
  453. $password = 'pa$$w0rd';
  454. $sha = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_HASH_SHA);
  455. $encoded = substr($sha, strpos($sha, '}'));
  456. $binary = base64_decode($encoded);
  457. $this->assertEquals(20, strlen($binary));
  458. $this->assertEquals(sha1($password, true), $binary);
  459. }
  460. public function testPasswordGenerationSMD5()
  461. {
  462. $password = 'pa$$w0rd';
  463. $smd5 = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_HASH_SMD5);
  464. $encoded = substr($smd5, strpos($smd5, '}'));
  465. $binary = base64_decode($encoded);
  466. $this->assertEquals(20, strlen($binary));
  467. $hash = substr($binary, 0, 16);
  468. $salt = substr($binary, 16);
  469. $this->assertEquals(4, strlen($salt));
  470. $this->assertEquals(md5($password . $salt, true), $hash);
  471. }
  472. public function testPasswordGenerationMD5()
  473. {
  474. $password = 'pa$$w0rd';
  475. $md5 = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_HASH_MD5);
  476. $encoded = substr($md5, strpos($md5, '}'));
  477. $binary = base64_decode($encoded);
  478. $this->assertEquals(16, strlen($binary));
  479. $this->assertEquals(md5($password, true), $binary);
  480. }
  481. public function testPasswordGenerationUnicodePwd()
  482. {
  483. $password = 'new';
  484. $unicodePwd = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
  485. $this->assertEquals(10, strlen($unicodePwd));
  486. $this->assertEquals("\x22\x00\x6E\x00\x65\x00\x77\x00\x22\x00", $unicodePwd);
  487. }
  488. }