SimpleEncoderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Ldap_TestCase
  23. */
  24. require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestCase.php';
  25. /**
  26. * @see Zend_Ldap_Ldif_Encoder
  27. */
  28. require_once 'Zend/Ldap/Ldif/Encoder.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Ldap
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Ldap_Ldif_SimpleEncoderTest extends Zend_Ldap_TestCase
  37. {
  38. public static function stringEncodingProvider()
  39. {
  40. $testData = array(
  41. array('cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com',
  42. 'cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com'),
  43. array('Babs is a big sailing fan, and travels extensively in search of perfect sailing conditions.',
  44. 'Babs is a big sailing fan, and travels extensively in search of perfect sailing conditions.'),
  45. array("\x00 NULL CHAR first", base64_encode("\x00 NULL CHAR first")),
  46. array("\n LF CHAR first", base64_encode("\n LF CHAR first")),
  47. array("\r CR CHAR first", base64_encode("\r CR CHAR first")),
  48. array(' SPACE CHAR first', base64_encode(' SPACE CHAR first')),
  49. array(': colon CHAR first', base64_encode(': colon CHAR first')),
  50. array('< less-than CHAR first', base64_encode('< less-than CHAR first')),
  51. array("\x7f CHR(127) first", base64_encode("\x7f CHR(127) first")),
  52. array("NULL CHAR \x00 in string", base64_encode("NULL CHAR \x00 in string")),
  53. array("LF CHAR \n in string", base64_encode("LF CHAR \n in string")),
  54. array("CR CHAR \r in string", base64_encode("CR CHAR \r in string")),
  55. array("CHR(127) \x7f in string", base64_encode("CHR(127) \x7f in string")),
  56. array('Ä first', base64_encode('Ä first')),
  57. array('in Ä string', base64_encode('in Ä string')),
  58. array('last char is a string ', base64_encode('last char is a string '))
  59. );
  60. return $testData;
  61. }
  62. /**
  63. * @dataProvider stringEncodingProvider
  64. */
  65. public function testStringEncoding($string, $expected)
  66. {
  67. $this->assertEquals($expected, Zend_Ldap_Ldif_Encoder::encode($string));
  68. }
  69. public static function attributeEncodingProvider()
  70. {
  71. $testData = array(
  72. array(array('dn' => 'cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com'),
  73. 'dn: cn=Barbara Jensen, ou=Product Development, dc=airius, dc=com'),
  74. array(array('dn' => 'cn=Jürgen Österreicher, ou=Äpfel, dc=airius, dc=com'),
  75. 'dn:: ' . base64_encode('cn=Jürgen Österreicher, ou=Äpfel, dc=airius, dc=com')),
  76. array(array('description' => 'Babs is a big sailing fan, and travels extensively in search of perfect sailing conditions.'),
  77. 'description: Babs is a big sailing fan, and travels extensively in search of p' . PHP_EOL . ' erfect sailing conditions.'),
  78. array(array('description' => "CHR(127) \x7f in string"),
  79. 'description:: ' . base64_encode("CHR(127) \x7f in string")),
  80. array(array('description' => '1234567890123456789012345678901234567890123456789012345678901234 567890'),
  81. 'description: 1234567890123456789012345678901234567890123456789012345678901234 ' . PHP_EOL . ' 567890'),
  82. );
  83. return $testData;
  84. }
  85. /**
  86. * @dataProvider attributeEncodingProvider
  87. */
  88. public function testAttributeEncoding($array, $expected)
  89. {
  90. $actual = Zend_Ldap_Ldif_Encoder::encode($array);
  91. $this->assertEquals($expected, $actual);
  92. }
  93. public function testChangedWrapCount()
  94. {
  95. $input = '56789012345678901234567890';
  96. $expected = 'dn: 567890' . PHP_EOL . ' 1234567890' . PHP_EOL . ' 1234567890';
  97. $output = Zend_Ldap_Ldif_Encoder::encode(array('dn' => $input), array('wrap' => 10));
  98. $this->assertEquals($expected, $output);
  99. }
  100. public function testEncodeMultipleAttributes()
  101. {
  102. $data = array(
  103. 'a' => array('a', 'b'),
  104. 'b' => 'c',
  105. 'c' => '',
  106. 'd' => array(),
  107. 'e' => array(''));
  108. $expected = 'a: a' . PHP_EOL .
  109. 'a: b' . PHP_EOL .
  110. 'b: c' . PHP_EOL .
  111. 'c: ' . PHP_EOL .
  112. 'd: ' . PHP_EOL .
  113. 'e: ';
  114. $actual = Zend_Ldap_Ldif_Encoder::encode($data);
  115. $this->assertEquals($expected, $actual);
  116. }
  117. public function testEncodeUnsupportedType()
  118. {
  119. $this->assertNull(Zend_Ldap_Ldif_Encoder::encode(new stdClass()));
  120. }
  121. public function testSorting()
  122. {
  123. $data=array(
  124. 'cn' => array('name'),
  125. 'dn' => 'cn=name,dc=example,dc=org',
  126. 'host' => array('a', 'b', 'c'),
  127. 'empty' => array(),
  128. 'boolean' => array('TRUE', 'FALSE'),
  129. 'objectclass' => array('account', 'top'),
  130. );
  131. $expected = 'version: 1' . PHP_EOL .
  132. 'dn: cn=name,dc=example,dc=org' . PHP_EOL .
  133. 'objectclass: account' . PHP_EOL .
  134. 'objectclass: top' . PHP_EOL .
  135. 'boolean: TRUE' . PHP_EOL .
  136. 'boolean: FALSE' . PHP_EOL .
  137. 'cn: name' . PHP_EOL .
  138. 'empty: ' . PHP_EOL .
  139. 'host: a' . PHP_EOL .
  140. 'host: b' . PHP_EOL .
  141. 'host: c';
  142. $actual = Zend_Ldap_Ldif_Encoder::encode($data);
  143. $this->assertEquals($expected, $actual);
  144. $expected = 'version: 1' . PHP_EOL .
  145. 'cn: name' . PHP_EOL .
  146. 'dn: cn=name,dc=example,dc=org' . PHP_EOL .
  147. 'host: a' . PHP_EOL .
  148. 'host: b' . PHP_EOL .
  149. 'host: c' . PHP_EOL .
  150. 'empty: ' . PHP_EOL .
  151. 'boolean: TRUE' . PHP_EOL .
  152. 'boolean: FALSE' . PHP_EOL .
  153. 'objectclass: account' . PHP_EOL .
  154. 'objectclass: top';
  155. $actual = Zend_Ldap_Ldif_Encoder::encode($data, array('sort' => false));
  156. $this->assertEquals($expected, $actual);
  157. }
  158. public function testNodeEncoding()
  159. {
  160. $node = $this->_createTestNode();
  161. $expected = 'version: 1' . PHP_EOL .
  162. 'dn: cn=name,dc=example,dc=org' . PHP_EOL .
  163. 'objectclass: account' . PHP_EOL .
  164. 'objectclass: top' . PHP_EOL .
  165. 'boolean: TRUE' . PHP_EOL .
  166. 'boolean: FALSE' . PHP_EOL .
  167. 'cn: name' . PHP_EOL .
  168. 'empty: ' . PHP_EOL .
  169. 'host: a' . PHP_EOL .
  170. 'host: b' . PHP_EOL .
  171. 'host: c';
  172. $actual = $node->toLdif();
  173. $this->assertEquals($expected, $actual);
  174. $actual = Zend_Ldap_Ldif_Encoder::encode($node);
  175. $this->assertEquals($expected, $actual);
  176. }
  177. public function testSupressVersionHeader()
  178. {
  179. $data=array(
  180. 'cn' => array('name'),
  181. 'dn' => 'cn=name,dc=example,dc=org',
  182. 'host' => array('a', 'b', 'c'),
  183. 'empty' => array(),
  184. 'boolean' => array('TRUE', 'FALSE'),
  185. 'objectclass' => array('account', 'top'),
  186. );
  187. $expected = 'dn: cn=name,dc=example,dc=org' . PHP_EOL .
  188. 'objectclass: account' . PHP_EOL .
  189. 'objectclass: top' . PHP_EOL .
  190. 'boolean: TRUE' . PHP_EOL .
  191. 'boolean: FALSE' . PHP_EOL .
  192. 'cn: name' . PHP_EOL .
  193. 'empty: ' . PHP_EOL .
  194. 'host: a' . PHP_EOL .
  195. 'host: b' . PHP_EOL .
  196. 'host: c';
  197. $actual = Zend_Ldap_Ldif_Encoder::encode($data, array('version' => null));
  198. $this->assertEquals($expected, $actual);
  199. }
  200. public function testEncodingWithJapaneseCharacters()
  201. {
  202. $data=array(
  203. 'dn' => 'uid=rogasawara,ou=営業部,o=Airius',
  204. 'objectclass' => array('top', 'person', 'organizationalPerson', 'inetOrgPerson'),
  205. 'uid' => array('rogasawara'),
  206. 'mail' => array('rogasawara@airius.co.jp'),
  207. 'givenname;lang-ja' => array('ロドニー'),
  208. 'sn;lang-ja' => array('小笠原'),
  209. 'cn;lang-ja' => array('小笠原 ロドニー'),
  210. 'title;lang-ja' => array('営業部 部長'),
  211. 'preferredlanguage' => array('ja'),
  212. 'givenname' => array('ロドニー'),
  213. 'sn' => array('小笠原'),
  214. 'cn' => array('小笠原 ロドニー'),
  215. 'title' => array('営業部 部長'),
  216. 'givenname;lang-ja;phonetic' => array('ろどにー'),
  217. 'sn;lang-ja;phonetic' => array('おがさわら'),
  218. 'cn;lang-ja;phonetic' => array('おがさわら ろどにー'),
  219. 'title;lang-ja;phonetic' => array('えいぎょうぶ ぶちょう'),
  220. 'givenname;lang-en' => array('Rodney'),
  221. 'sn;lang-en' => array('Ogasawara'),
  222. 'cn;lang-en' => array('Rodney Ogasawara'),
  223. 'title;lang-en' => array('Sales, Director'),
  224. );
  225. $expected = 'dn:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz' . PHP_EOL .
  226. 'objectclass: top' . PHP_EOL .
  227. 'objectclass: person' . PHP_EOL .
  228. 'objectclass: organizationalPerson' . PHP_EOL .
  229. 'objectclass: inetOrgPerson' . PHP_EOL .
  230. 'uid: rogasawara' . PHP_EOL .
  231. 'mail: rogasawara@airius.co.jp' . PHP_EOL .
  232. 'givenname;lang-ja:: 44Ot44OJ44OL44O8' . PHP_EOL .
  233. 'sn;lang-ja:: 5bCP56yg5Y6f' . PHP_EOL .
  234. 'cn;lang-ja:: 5bCP56yg5Y6fIOODreODieODi+ODvA==' . PHP_EOL .
  235. 'title;lang-ja:: 5Za25qWt6YOoIOmDqOmVtw==' . PHP_EOL .
  236. 'preferredlanguage: ja' . PHP_EOL .
  237. 'givenname:: 44Ot44OJ44OL44O8' . PHP_EOL .
  238. 'sn:: 5bCP56yg5Y6f' . PHP_EOL .
  239. 'cn:: 5bCP56yg5Y6fIOODreODieODi+ODvA==' . PHP_EOL .
  240. 'title:: 5Za25qWt6YOoIOmDqOmVtw==' . PHP_EOL .
  241. 'givenname;lang-ja;phonetic:: 44KN44Gp44Gr44O8' . PHP_EOL .
  242. 'sn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJ' . PHP_EOL .
  243. 'cn;lang-ja;phonetic:: 44GK44GM44GV44KP44KJIOOCjeOBqeOBq+ODvA==' . PHP_EOL .
  244. 'title;lang-ja;phonetic:: 44GI44GE44GO44KH44GG44G2IOOBtuOBoeOCh+OBhg==' . PHP_EOL .
  245. 'givenname;lang-en: Rodney' . PHP_EOL .
  246. 'sn;lang-en: Ogasawara' . PHP_EOL .
  247. 'cn;lang-en: Rodney Ogasawara' . PHP_EOL .
  248. 'title;lang-en: Sales, Director';
  249. $actual = Zend_Ldap_Ldif_Encoder::encode($data, array('sort' => false, 'version' => null));
  250. $this->assertEquals($expected, $actual);
  251. }
  252. }