PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework-1.10.0/tests/Zend/Ldap/Ldif/SimpleEncoderTest.php

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