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