CanonTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Ldap
  28. */
  29. require_once 'Zend/Ldap.php';
  30. /* Note: The ldap_connect function does not actually try to connect. This
  31. * is why many tests attempt to bind with invalid credentials. If the
  32. * bind returns 'Invalid credentials' we know the transport related work
  33. * was successful.
  34. */
  35. /**
  36. * @category Zend
  37. * @package Zend_Ldap
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Ldap
  42. */
  43. class Zend_Ldap_CanonTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $_options = null;
  46. public function setUp()
  47. {
  48. $this->_options = array(
  49. 'host' => TESTS_ZEND_LDAP_HOST,
  50. 'username' => TESTS_ZEND_LDAP_USERNAME,
  51. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  52. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  53. );
  54. if (defined('TESTS_ZEND_LDAP_PORT'))
  55. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  56. if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
  57. $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
  58. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  59. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  60. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  61. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  62. if (defined('TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT'))
  63. $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
  64. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
  65. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  66. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  67. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  68. }
  69. public function testPlainCanon()
  70. {
  71. $ldap = new Zend_Ldap($this->_options);
  72. /* This test tries to canonicalize each name (uname, uname@example.com,
  73. * EXAMPLE\uname) to each of the 3 forms (username, principal and backslash)
  74. * for a total of canonicalizations.
  75. */
  76. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME')) {
  77. $names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
  78. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
  79. $names[Zend_Ldap::ACCTNAME_FORM_PRINCIPAL] =
  80. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  81. }
  82. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT')) {
  83. $names[Zend_Ldap::ACCTNAME_FORM_BACKSLASH] =
  84. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME;
  85. }
  86. }
  87. foreach ($names as $_form => $name) {
  88. foreach ($names as $form => $_name) {
  89. $ret = $ldap->getCanonicalAccountName($name, $form);
  90. $this->assertEquals($names[$form], $ret);
  91. }
  92. }
  93. }
  94. public function testInvalidAccountCanon()
  95. {
  96. $ldap = new Zend_Ldap($this->_options);
  97. try {
  98. $ldap->bind('invalid', 'invalid');
  99. $this->fail('Expected exception not thrown');
  100. } catch (Zend_Ldap_Exception $zle) {
  101. $msg = $zle->getMessage();
  102. $this->assertTrue(strstr($msg, 'Invalid credentials') ||
  103. strstr($msg, 'No such object') ||
  104. strstr($msg, 'No object found'));
  105. }
  106. }
  107. public function testDnCanon()
  108. {
  109. $ldap = new Zend_Ldap($this->_options);
  110. $name = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME, Zend_Ldap::ACCTNAME_FORM_DN);
  111. $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $name);
  112. }
  113. public function testMismatchDomainBind()
  114. {
  115. $ldap = new Zend_Ldap($this->_options);
  116. try {
  117. $ldap->bind('BOGUS\\doesntmatter', 'doesntmatter');
  118. $this->fail('Expected exception not thrown');
  119. } catch (Zend_Ldap_Exception $zle) {
  120. $this->assertTrue($zle->getCode() == Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH);
  121. }
  122. }
  123. public function testAccountCanonization()
  124. {
  125. $options = $this->_options;
  126. $ldap = new Zend_Ldap($options);
  127. $canonDn = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  128. Zend_Ldap::ACCTNAME_FORM_DN);
  129. $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $canonDn);
  130. $canonUsername = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  131. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  132. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canonUsername);
  133. $canonBackslash = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  134. Zend_Ldap::ACCTNAME_FORM_BACKSLASH);
  135. $this->assertEquals(
  136. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME,
  137. $canonBackslash);
  138. $canonPrincipal = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  139. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL);
  140. $this->assertEquals(
  141. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  142. $canonPrincipal);
  143. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_USERNAME;
  144. $ldap->setOptions($options);
  145. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  146. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  147. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_BACKSLASH;
  148. $ldap->setOptions($options);
  149. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  150. $this->assertEquals(
  151. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  152. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_PRINCIPAL;
  153. $ldap->setOptions($options);
  154. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  155. $this->assertEquals(
  156. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME, $canon);
  157. unset($options['accountCanonicalForm']);
  158. unset($options['accountDomainName']);
  159. $ldap->setOptions($options);
  160. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  161. $this->assertEquals(
  162. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  163. unset($options['accountDomainNameShort']);
  164. $ldap->setOptions($options);
  165. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  166. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  167. $options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  168. $ldap->setOptions($options);
  169. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  170. $this->assertEquals(
  171. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME, $canon);
  172. }
  173. public function testDefaultAccountFilterFormat()
  174. {
  175. $options = $this->_options;
  176. unset($options['accountFilterFormat']);
  177. $options['bindRequiresDn'] = true;
  178. $ldap = new Zend_Ldap($options);
  179. try {
  180. $canon = $ldap->getCanonicalAccountName('invalid', Zend_Ldap::ACCTNAME_FORM_DN);
  181. $this->fail('Expected exception not thrown');
  182. } catch (Zend_Ldap_Exception $zle) {
  183. $this->assertContains('(&(objectClass=posixAccount)(uid=invalid))', $zle->getMessage());
  184. }
  185. $options['bindRequiresDn'] = false;
  186. $ldap = new Zend_Ldap($options);
  187. try {
  188. $canon = $ldap->getCanonicalAccountName('invalid', Zend_Ldap::ACCTNAME_FORM_DN);
  189. $this->fail('Expected exception not thrown');
  190. } catch (Zend_Ldap_Exception $zle) {
  191. $this->assertContains('(&(objectClass=user)(sAMAccountName=invalid))', $zle->getMessage());
  192. }
  193. }
  194. public function testPossibleAuthority()
  195. {
  196. $options = $this->_options;
  197. $ldap = new Zend_Ldap($options);
  198. try {
  199. $canon = $ldap->getCanonicalAccountName('invalid\invalid',
  200. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  201. $this->fail('Expected exception not thrown');
  202. } catch (Zend_Ldap_Exception $zle) {
  203. $this->assertContains('Binding domain is not an authority for user: invalid\invalid',
  204. $zle->getMessage());
  205. }
  206. try {
  207. $canon = $ldap->getCanonicalAccountName('invalid@invalid.tld',
  208. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  209. $this->fail('Expected exception not thrown');
  210. } catch (Zend_Ldap_Exception $zle) {
  211. $this->assertContains('Binding domain is not an authority for user: invalid@invalid.tld',
  212. $zle->getMessage());
  213. }
  214. unset($options['accountDomainName']);
  215. $ldap = new Zend_Ldap($options);
  216. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  217. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  218. $this->assertEquals('invalid', $canon);
  219. try {
  220. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  221. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  222. $this->fail('Expected exception not thrown');
  223. } catch (Zend_Ldap_Exception $zle) {
  224. $this->assertContains('Binding domain is not an authority for user: invalid@' .
  225. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  226. $zle->getMessage());
  227. }
  228. unset($options['accountDomainNameShort']);
  229. $options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  230. $ldap = new Zend_Ldap($options);
  231. try {
  232. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  233. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  234. $this->fail('Expected exception not thrown');
  235. } catch (Zend_Ldap_Exception $zle) {
  236. $this->assertContains('Binding domain is not an authority for user: ' .
  237. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  238. $zle->getMessage());
  239. }
  240. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  241. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  242. $this->assertEquals('invalid', $canon);
  243. unset($options['accountDomainName']);
  244. $ldap = new Zend_Ldap($options);
  245. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  246. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  247. $this->assertEquals('invalid', $canon);
  248. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  249. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  250. $this->assertEquals('invalid', $canon);
  251. }
  252. public function testInvalidAccountName()
  253. {
  254. $options = $this->_options;
  255. $ldap = new Zend_Ldap($options);
  256. try {
  257. $canon = $ldap->getCanonicalAccountName('0@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  258. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  259. $this->fail('Expected exception not thrown');
  260. } catch (Zend_Ldap_Exception $zle) {
  261. $this->assertContains('Invalid account name syntax: 0@' .
  262. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  263. $zle->getMessage());
  264. }
  265. try {
  266. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\0',
  267. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  268. $this->fail('Expected exception not thrown');
  269. } catch (Zend_Ldap_Exception $zle) {
  270. $this->assertContains('Invalid account name syntax: ' .
  271. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\0',
  272. $zle->getMessage());
  273. }
  274. }
  275. public function testGetUnknownCanonicalForm()
  276. {
  277. $options = $this->_options;
  278. $ldap = new Zend_Ldap($options);
  279. try {
  280. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME, 99);
  281. $this->fail('Expected exception not thrown');
  282. } catch (Zend_Ldap_Exception $zle) {
  283. $this->assertContains('Unknown canonical name form: 99',
  284. $zle->getMessage());
  285. }
  286. }
  287. public function testGetUnavailableCanoncialForm()
  288. {
  289. $options = $this->_options;
  290. unset($options['accountDomainName']);
  291. $ldap = new Zend_Ldap($options);
  292. try {
  293. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  294. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL);
  295. $this->fail('Expected exception not thrown');
  296. } catch (Zend_Ldap_Exception $zle) {
  297. $this->assertContains('Option required: accountDomainName',
  298. $zle->getMessage());
  299. }
  300. unset($options['accountDomainNameShort']);
  301. $ldap = new Zend_Ldap($options);
  302. try {
  303. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  304. Zend_Ldap::ACCTNAME_FORM_BACKSLASH);
  305. $this->fail('Expected exception not thrown');
  306. } catch (Zend_Ldap_Exception $zle) {
  307. $this->assertContains('Option required: accountDomainNameShort',
  308. $zle->getMessage());
  309. }
  310. }
  311. public function testSplittingOption()
  312. {
  313. $options = $this->_options;
  314. unset($options['accountDomainName']);
  315. unset($options['accountDomainNameShort']);
  316. $options['tryUsernameSplit'] = true;
  317. $ldap = new Zend_Ldap($options);
  318. $this->assertEquals('username', $ldap->getCanonicalAccountName('username@example.com',
  319. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  320. $this->assertEquals('username', $ldap->getCanonicalAccountName('EXAMPLE\username',
  321. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  322. $this->assertEquals('username', $ldap->getCanonicalAccountName('username',
  323. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  324. $options['tryUsernameSplit'] = false;
  325. $ldap = new Zend_Ldap($options);
  326. $this->assertEquals('username@example.com',
  327. $ldap->getCanonicalAccountName('username@example.com', Zend_Ldap::ACCTNAME_FORM_USERNAME));
  328. $this->assertEquals('example\username', $ldap->getCanonicalAccountName('EXAMPLE\username',
  329. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  330. $this->assertEquals('username', $ldap->getCanonicalAccountName('username',
  331. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  332. }
  333. /**
  334. * ZF-4495
  335. */
  336. public function testSpecialCharacterInUsername()
  337. {
  338. $options = $this->_options;
  339. $options['accountDomainName'] = 'example.com';
  340. $options['accountDomainNameShort'] = 'EXAMPLE';
  341. $ldap = new Zend_Ldap($options);
  342. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  343. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  344. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  345. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  346. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('SCHÄFER',
  347. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  348. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  349. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  350. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  351. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  352. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('SCHÄFER',
  353. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  354. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  355. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  356. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  357. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  358. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('SCHÄFER',
  359. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  360. }
  361. }