Changeset 1625
- Timestamp:
- 06/16/08 17:05:37 (2 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPConnection.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPEntry.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPSearchResult.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPURL.php (modified) (7 diffs)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPConnectionTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPEntryTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPSearchResultTestCase.php (modified) (5 diffs)
- trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPURLTestCase.php (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPConnection.php
r1570 r1625 6 6 * @package stubbles 7 7 * @subpackage peer_ldap 8 * @see RFC 4510 LDAP: Technical Specification Road Map http://tools.ietf.org/html/rfc4510 9 * @see RFC 4515 LDAP: String Representation of Search Filters http://tools.ietf.org/html/rfc4515 8 10 */ 9 11 stubClassLoader::load('net::stubbles::peer::ldap::stubLDAPURL', … … 16 18 * @package stubbles 17 19 * @subpackage peer_ldap 20 * @see RFC 4510 LDAP: Technical Specification Road Map http://tools.ietf.org/html/rfc4510 21 * @see RFC 4515 LDAP: String Representation of Search Filters http://tools.ietf.org/html/rfc4515 18 22 */ 19 23 class stubLDAPConnection extends stubBaseObject … … 31 35 */ 32 36 protected $handle; 37 /** 38 * decision provider for protocol choice (default is LDAPv3). 39 * 40 * @var bool 41 */ 42 protected $usesProtocolVersionDefault; 43 /** 44 * ldap options 45 * Keys are used for equality check, the values are actually superfluous. 46 * 47 * @var array(int, int) 48 */ 49 protected static $options = array(LDAP_OPT_DEREF => LDAP_OPT_DEREF, 50 LDAP_OPT_SIZELIMIT => LDAP_OPT_SIZELIMIT, 51 LDAP_OPT_NETWORK_TIMEOUT => LDAP_OPT_NETWORK_TIMEOUT, 52 LDAP_OPT_TIMELIMIT => LDAP_OPT_TIMELIMIT, 53 LDAP_OPT_PROTOCOL_VERSION => LDAP_OPT_PROTOCOL_VERSION, 54 LDAP_OPT_ERROR_NUMBER => LDAP_OPT_ERROR_NUMBER, 55 LDAP_OPT_REFERRALS => LDAP_OPT_REFERRALS, 56 LDAP_OPT_RESTART => LDAP_OPT_RESTART, 57 LDAP_OPT_HOST_NAME => LDAP_OPT_HOST_NAME, 58 LDAP_OPT_ERROR_STRING => LDAP_OPT_ERROR_STRING, 59 LDAP_OPT_MATCHED_DN => LDAP_OPT_MATCHED_DN, 60 LDAP_OPT_SERVER_CONTROLS => LDAP_OPT_SERVER_CONTROLS, 61 LDAP_OPT_CLIENT_CONTROLS => LDAP_OPT_CLIENT_CONTROLS); 33 62 34 63 /** … … 37 66 * @param stubLDAPURL $ldapUrl 38 67 */ 39 public function __construct( $ldapUrl)68 public function __construct(stubLDAPURL $ldapUrl) 40 69 { 41 70 $this->ldap = $ldapUrl; 42 71 $this->handle = ldap_connect($this->ldap->getHost(), $this->ldap->getPort()); 72 $this->usesProtocolVersionDefault = true; 73 } 74 75 /** 76 * Checks a LDAP option. 77 * 78 * @param string $name 79 * @param string $value 80 * @return bool 81 * @link http://de.php.net/manual/de/function.ldap-set-option.php 82 */ 83 public function isOptionValid($name) 84 { 85 return isset(self::$options[$name]); 43 86 } 44 87 45 88 /** 46 89 * Sets a LDAP option. 47 * TODO 48 * 49 * @param string $name 50 * @param string $value 90 * 91 * Possible options and their type: 92 * (depends also on which types the server returns) 93 * 94 * LDAP_OPT_DEREF integer 95 * LDAP_OPT_SIZELIMIT integer 96 * LDAP_OPT_TIMELIMIT integer 97 * LDAP_OPT_NETWORK_TIMEOUT integer 98 * LDAP_OPT_PROTOCOL_VERSION integer 99 * LDAP_OPT_ERROR_NUMBER integer 100 * LDAP_OPT_REFERRALS bool 101 * LDAP_OPT_RESTART bool 102 * LDAP_OPT_HOST_NAME string 103 * LDAP_OPT_ERROR_STRING string 104 * LDAP_OPT_MATCHED_DN string 105 * LDAP_OPT_SERVER_CONTROLS array 106 * LDAP_OPT_CLIENT_CONTROLS array 107 * 108 * @param string $name 109 * @param string $value 110 * @throws stubConnectionException 111 * @throws stubIllegalArgumentException 51 112 * @return stubLDAPConnection 52 113 */ 53 114 public function option($name, $value) 54 115 { 116 if($this->isOptionValid($name) === false) { 117 throw new stubIllegalArgumentException($name . ' is no valid LDAP option.'); 118 } 119 120 if($name === LDAP_OPT_PROTOCOL_VERSION && $value != 3) { 121 $this->usesProtocolVersionDefault = false; 122 } 123 124 $success = @ldap_set_option($this->handle, $name, $value); 125 if($success === false) { 126 throw new stubConnectionException(ldap_error($this->handle)); 127 } 128 55 129 return $this; 56 130 } … … 58 132 /** 59 133 * Gets a LDAP option. 60 * TODO 61 * 62 * @param string $name 134 * 135 * Possible options and their type: 136 * (depends also on which types the server returns) 137 * 138 * LDAP_OPT_DEREF integer 139 * LDAP_OPT_SIZELIMIT integer 140 * LDAP_OPT_TIMELIMIT integer 141 * LDAP_OPT_NETWORK_TIMEOUT integer 142 * LDAP_OPT_PROTOCOL_VERSION integer 143 * LDAP_OPT_ERROR_NUMBER integer 144 * LDAP_OPT_REFERRALS bool 145 * LDAP_OPT_RESTART bool 146 * LDAP_OPT_HOST_NAME string 147 * LDAP_OPT_ERROR_STRING string 148 * LDAP_OPT_MATCHED_DN string 149 * LDAP_OPT_SERVER_CONTROLS array 150 * LDAP_OPT_CLIENT_CONTROLS array 151 * 152 * @param string $name 153 * @return mixed $retValue 63 154 */ 64 155 public function getOption($name) 65 156 { 66 // ldap_get_option(); 67 } 68 69 /** 70 * Binds the LDAP connection (authenticate & specifiy LDAP protocol version) 71 * 72 * @throws stubException 157 if($this->isOptionValid($name) === false) { 158 throw new stubIllegalArgumentException($name . ' is no valid LDAP Option.'); 159 } 160 161 $success = @ldap_get_option($this->handle, $name, $retValue); 162 if($succes === false) { 163 throw new stubConnectionException(ldap_error($this->handle)); 164 } 165 166 return $retValue; 167 } 168 169 /** 170 * Binds the LDAP connection (authentication). 171 * Uses per default LDAPv3. 172 * 173 * @throws stubConnectionException 73 174 * @return stubLDAPConnection 74 175 */ 75 176 public function bind() 76 177 { 178 if($this->usesProtocolVersionDefault === true) { 179 $this->option(LDAP_OPT_PROTOCOL_VERSION, 3); 180 } 181 77 182 if($this->ldap->getUser() === null || $this->ldap->getPassword() === null) { 78 183 // anonymous bind 79 $success = ldap_bind($this->handle);184 $success = @ldap_bind($this->handle); 80 185 } else { 81 $success = ldap_bind($this->handle, $this->ldap->getUser(), $this->ldap->getPassword());186 $success = @ldap_bind($this->handle, $this->ldap->getUser(), $this->ldap->getPassword()); 82 187 } 83 188 84 189 if($success === false) { 85 throw new stub Exception(ldap_error($this->handle));190 throw new stubConnectionException(ldap_error($this->handle)); 86 191 } 87 192 … … 108 213 $this->ldap->setBaseDn($newBaseDn); 109 214 } 110 215 111 216 /** 112 217 * Retrieves the LDAP entries. 113 * If no parameters set, the paramteres from the LDAP url are used. 114 * 115 * @param string $attributes ldap attributes 116 * @param string $scope ldap scope (base|one|sub) 117 * @param string $filter ldap filter 118 * @throws stubException 218 * If no parameters set, the paramteres from the LDAP url are used (or the defaults). 219 * 220 * @param string $attributes ldap attributes narrow result set 221 * @param string $scope ldap scope (base|one|sub) 222 * @param string $filter ldap filter must be surrounded with parentheses 223 * @throws stubConnectionException 224 * @throws stubIllegalArgumentException 119 225 * @return stubLDAPSearchResult 120 226 */ … … 141 247 } 142 248 143 // TODO isParamValid() nötig?144 249 switch($scope) { 145 case 'base':146 $result = ldap_read($this->handle, $this->ldap->getBaseDn(), $filter, $attributes);250 case stubLDAPURL::SCOPE_BASE: 251 $result = @ldap_read($this->handle, $this->ldap->getBaseDn(), $filter, $attributes); 147 252 break; 148 253 149 case 'sub':150 $result = ldap_search($this->handle, $this->ldap->getBaseDn(), $filter, $attributes);254 case stubLDAPURL::SCOPE_ONE: 255 $result = @ldap_list($this->handle, $this->ldap->getBaseDn(), $filter, $attributes); 151 256 break; 152 257 153 case 'one':154 $result = ldap_list($this->handle, $this->ldap->getBaseDn(), $filter, $attributes);258 case stubLDAPURL::SCOPE_SUB: 259 $result = @ldap_search($this->handle, $this->ldap->getBaseDn(), $filter, $attributes); 155 260 break; 156 261 157 262 default: 158 // intentionally empty263 throw new stubIllegalArgumentException('Inavlid scope: ' . $scope . ', must be one of "base", "sub" or "one".'); 159 264 } 160 265 161 266 if($result === false) { 162 throw new stub Exception(ldap_error($this->handle));267 throw new stubConnectionException(ldap_error($this->handle)); 163 268 } 164 269 trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPEntry.php
r1570 r1625 28 28 protected $objectClass; 29 29 /** 30 * amount of attributes 30 * amount of attributes (inclusive objectClass) 31 31 * 32 32 * @var int … … 97 97 public function getAttributeValuesByName($name) 98 98 { 99 // new ArrayAccessor100 99 return (isset($this->attributes[$name]) !== false) ? $this->attributes[$name] : null; 101 100 } trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPSearchResult.php
r1570 r1625 87 87 $dn = ldap_get_dn($this->handle, $entryId); 88 88 if(empty($objectClass)) { 89 $objectClass = ldap_read($this->handle, $dn, ' objectClass=*', array('objectclass'));89 $objectClass = ldap_read($this->handle, $dn, '(objectClass=*)', array('objectclass')); 90 90 } 91 91 trunk/src/main/php/net/stubbles/peer/ldap/stubLDAPURL.php
r1570 r1625 6 6 * @package stubbles 7 7 * @subpackage peer_ldap 8 * @see RFC 4510 LDAP: Technical Specification Road Map http://tools.ietf.org/html/rfc4510 9 * @see RFC 4514 LDAP: String Representation of Distinguished Names http://tools.ietf.org/html/rfc4514 8 10 * @see RFC 4516 LDAP: Uniform Resource Locator http://tools.ietf.org/html/rfc4516 9 11 * @see RFC 4515 LDAP: String Representation of Search Filters http://tools.ietf.org/html/rfc4515 10 * @see RFC 4514 LDAP: String Representation of Distinguished Names http://tools.ietf.org/html/rfc451411 12 */ 12 13 stubClassLoader::load('net::stubbles::peer::stubURL', … … 19 20 * @package stubbles 20 21 * @subpackage peer_ldap 22 * @see RFC 4510 LDAP: Technical Specification Road Map http://tools.ietf.org/html/rfc4510 23 * @see RFC 4514 LDAP: String Representation of Distinguished Names http://tools.ietf.org/html/rfc4514 21 24 * @see RFC 4516 LDAP: Uniform Resource Locator http://tools.ietf.org/html/rfc4516 22 25 * @see RFC 4515 LDAP: String Representation of Search Filters http://tools.ietf.org/html/rfc4515 23 * @see RFC 4514 LDAP: String Representation of Distinguished Names http://tools.ietf.org/html/rfc451424 26 */ 25 27 class stubLDAPURL extends stubURL 26 28 { 27 29 /** 30 * LDAP scope constant for one 31 */ 32 const SCOPE_ONE = 'one'; 33 /** 34 * LDAP scope constant for base 35 */ 36 const SCOPE_BASE = 'base'; 37 /** 38 * LDAP scope constant for sub 39 */ 40 const SCOPE_SUB = 'sub'; 41 42 /** 28 43 * constructor 29 44 * … … 98 113 99 114 // check dn 100 // TODO regex 101 if(preg_match('/(([A-Za-z]+=[A-Za-z ]+),?)/', $this->url['base_dn']) === 0) { 102 return false; 103 } 104 105 // check (optional) attributes 106 // TODO regex 107 if($this->getParam('attributes') !== null && 108 preg_match('/([a-zA-Z],?|)+/', $this->getParam('attributes')) === 0) { 109 return false; 110 } 111 112 // check (optional) scope 113 // TODO regex 114 if($this->getParam('scope') !== null && 115 preg_match('/(base|one|sub|)/', $this->getParam('scope')) === 0) { 116 return false; 117 } 118 119 // check (optional) filter 120 // TODO: regex 121 if($this->getParam('filter') !== null && 122 preg_match('/(.*|)/', $this->getParam('filter')) === 0) { 115 if(preg_match('/^([A-Za-z]+=[A-Za-z ]+,?)*$/', $this->url['base_dn']) === 0) { 116 return false; 117 } 118 119 // (optional) attributes & (optional) filters are passed through only scope is checked 120 if($this->getParam('scope') !== null 121 && (self::SCOPE_ONE !== $this->getParam('scope') 122 && self::SCOPE_BASE !== $this->getParam('scope') 123 && self::SCOPE_SUB !== $this->getParam('scope'))) { 123 124 return false; 124 125 } … … 184 185 * attributes = '' 185 186 * scope = 'base' 186 * filters = ' objectClass=*'187 * filters = '(objectClass=*)' 187 188 * 188 189 * possibilities for query string: … … 224 225 $result = ($queryValues !== null 225 226 && count($queryValues) >= 3 226 && $queryValues[2] !== '') ? $queryValues[2] : ' objectClass=*';227 && $queryValues[2] !== '') ? $queryValues[2] : '(objectClass=*)'; 227 228 break; 228 229 229 230 default: 230 231 // intentionally empty … … 240 241 * @return string 241 242 */ 242 public function get($ port = false)243 public function get($withPort = false) 243 244 { 244 245 $url = ''; … … 253 254 } 254 255 255 if ($ port === true && isset($this->url['port']) === true) {256 if ($withPort === true && isset($this->url['port']) === true) { 256 257 $port = ':' . $this->url['port']; 257 258 } else { trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPConnectionTestCase.php
r1570 r1625 10 10 /** 11 11 * Test for net::stubbles::peer::http::stubLDAPConnectionTestCase. 12 * 13 * Preconditions: 14 * - db.debian.org (LDAP Server) 15 * - ou=hosts,dc=debian,dc=org (dn exists) 12 16 * 13 17 * @package stubbles … … 31 35 $this->markTestSkipped('The LDAP extension is not available.'); 32 36 } 33 34 $url = 'ldap:// x500.bund.de/ou=BPjM,o=BUND,c=DE';37 38 $url = 'ldap://db.debian.org/ou=hosts,dc=debian,dc=org'; 35 39 $this->ldap = stubLDAPURL::fromString($url)->connect(); 40 } 41 42 /** 43 * invalid bind 44 * 45 * @test 46 * @expectedException stubConnectionException 47 */ 48 public function invalidBind() 49 { 50 stubLDAPURL::fromString('ldap://badUser:pw@db.debian.org/ou=hosts,dc=debian,dc=org')->connect()->bind(); 36 51 } 37 52 … … 44 59 { 45 60 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search()); 46 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, 'sub', null));47 61 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, 'base', null)); 48 62 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, 'one', null)); 63 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, 'sub', null)); 49 64 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search('objectClass', null, null)); 50 65 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search('objectclass', null, null)); 51 66 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search('iDontExist', null, null)); 52 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, null, 'sn=iDontExist')); 53 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, null, 'sn=A*')); 67 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, null, '(sn=A*)')); 68 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, null, '(sn=iDontExist)')); 69 $this->assertType('stubLDAPSearchResult', $this->ldap->bind()->search(null, null, '(&(abc=def)(def=ghi))')); 54 70 } 55 71 56 72 /** 57 * ldap search es with badinput73 * ldap search with bad scope input 58 74 * 59 75 * @test 60 * @expectedException stub Exception76 * @expectedException stubIllegalArgumentException 61 77 */ 62 public function searchFailure ()78 public function searchFailureBadScopeInput() 63 79 { 64 // TODO more tests65 80 $this->ldap->bind()->search(null, 'notBaseOneOrSub', null); 66 81 } 67 82 68 83 /** 69 * assure option settings 84 * ldap search with bad filter input 85 * 86 * @test 87 * @expectedException stubConnectionException 88 */ 89 public function searchFailureBadFilterInput() 90 { 91 $this->ldap->bind()->search(null, null, '(noEqualSign)'); 92 } 93 94 /** 95 * ldap search with bad base dn 96 * 97 * @test 98 * @expectedException stubConnectionException 99 */ 100 public function searchWithBadBaseDn() 101 { 102 $this->ldap->setBaseDn('badDN'); 103 $this->ldap->bind()->search(); 104 } 105 106 /** 107 * assure setting of default protocol during bind() 70 108 * 71 109 * @test 72 110 */ 73 public function option()111 public function defaultProtocolVersion() 74 112 { 75 // TODO 113 $this->assertEquals(3, $this->ldap->bind()->getOption(LDAP_OPT_PROTOCOL_VERSION)); 114 } 115 116 /** 117 * assure option() and getOption() 118 * 119 * @test 120 */ 121 public function protocolVersionAndOverriding() 122 { 123 $this->ldap->option(LDAP_OPT_PROTOCOL_VERSION, 3); 124 $this->assertEquals(3, $this->ldap->getOption(LDAP_OPT_PROTOCOL_VERSION)); 125 $this->ldap->option(LDAP_OPT_PROTOCOL_VERSION, 2); 126 $this->assertEquals(2, $this->ldap->getOption(LDAP_OPT_PROTOCOL_VERSION)); 127 } 128 129 /** 130 * assure option getting 131 * 132 * @test 133 */ 134 public function validOptionGetting() 135 { 136 $this->assertType('string', $this->ldap->getOption(LDAP_OPT_HOST_NAME)); 137 $this->assertType('int', $this->ldap->getOption(LDAP_OPT_PROTOCOL_VERSION)); 138 } 139 140 /** 141 * invalid option setting 142 * 143 * @test 144 * @expectedException stubIllegalArgumentException 145 */ 146 public function invalidOptionSetting() 147 { 148 $this->ldap->option('badInput', 123); 149 } 150 151 /** 152 * invalid option getting 153 * 154 * @test 155 * @expectedException stubIllegalArgumentException 156 */ 157 public function invalidOptionGetting() 158 { 159 $this->ldap->getOption('badInput'); 160 } 161 162 /** 163 * assure option validity 164 * 165 * @test 166 */ 167 public function optionValidity() 168 { 169 $this->assertTrue($this->ldap->isOptionValid(LDAP_OPT_PROTOCOL_VERSION)); 170 $this->assertFalse($this->ldap->isOptionValid('badInput')); 76 171 } 77 172 trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPEntryTestCase.php
r1570 r1625 12 12 /** 13 13 * Test for net::stubbles::peer::http::stubLDAPEntryTestCase. 14 * 15 * Preconditions: 16 * - db.debian.org (LDAP Server) 17 * - uid=aaron,ou=users,dc=debian,dc=org (dn exists & attributes of entry exist) 14 18 * 15 19 * @package stubbles … … 39 43 $this->markTestSkipped('The LDAP extension is not available.'); 40 44 } 41 42 $url = 'ldap:// x500.bund.de/ou=BPjM,o=BUND,c=DE??sub?sn=Aufmkolk';45 46 $url = 'ldap://db.debian.org/ou=users,dc=debian,dc=org??sub?(sn=Howell)'; 43 47 $this->entry = stubLDAPURL::fromString($url)->connect()->bind()->search()->getEntry(); 44 48 } … … 46 50 /** 47 51 * assure getter functionality with good input 48 * 52 * 49 53 * @test 50 54 */ 51 55 public function getterSuccessful() 52 56 { 53 $this->assertEquals(array('ivbbPerson', 'top', 'person', 'organizationalPerson', 'inetOrgPerson'), $this->entry->getObjectClassValues()); 54 $this->assertEquals(6, count($this->entry->getAttributes())); 55 $this->assertEquals(array('Aufmkolk'), $this->entry->getAttributeValuesByName('sn')); 56 $this->assertEquals(array('cn','sn','givenName', 'mail', 'collectiveOrganizationalUnitName', 'ivbbLastDeliveryCollective'), $this->entry->getAttributeNames()); 57 $this->assertEquals(7, $this->entry->getAttributeCount()); 58 $this->assertEquals('cn=Aufmkolk Ingrid,l=Bonn,ou=BPjM,o=Bund,c=DE', $this->entry->getDn()); 57 $this->assertEquals(array('inetOrgPerson', 58 'debianAccount', 59 'shadowAccount', 60 'debianDeveloper'), $this->entry->getObjectClassValues()); 61 62 $this->assertEquals(13, count($this->entry->getAttributes())); 63 64 $this->assertEquals(array('Howell'), $this->entry->getAttributeValuesByName('sn')); 65 66 $this->assertEquals(array('cn', 67 'gidNumber', 68 'uid', 69 'sn', 70 'shadowWarning', 71 'shadowMin', 72 'shadowMax', 73 'gecos', 74 'homeDirectory', 75 'shadowLastChange', 76 'uidNumber', 77 'comment', 78 'shadowExpire' 79 ), $this->entry->getAttributeNames()); 80 81 $this->assertEquals(14, $this->entry->getAttributeCount()); 82 83 $this->assertEquals('uid=aaron,ou=users,dc=debian,dc=org', $this->entry->getDn()); 59 84 } 60 85 61 86 /** 62 87 * assure getter functionality with bad input 63 * 88 * 64 89 * @test 65 90 */ 66 91 public function getterFailures() 67 92 { 68 // TODO69 93 $this->assertNull($this->entry->getAttributeValuesByName('iDontExist')); 70 94 } trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPSearchResultTestCase.php
r1570 r1625 13 13 * Test for net::stubbles::peer::http::stubLDAPSearchResultTestCase.php. 14 14 * 15 * Preconditions: 16 * - db.debian.org (LDAP Server) 17 * - ou=users,dc=debian,dc=org (dn exists) 18 * 15 19 * @package stubbles 16 20 * @subpackage peer_ldap_test … … 26 30 27 31 /** 28 * set up test envi oronment32 * set up test environment 29 33 */ 30 34 public function setUp() … … 33 37 $this->markTestSkipped('The LDAP extension is not available.'); 34 38 } 35 36 $url = 'ldap://x500.bund.de/ou=BPjM,o=BUND,c=DE??sub?sn=A*'; // exactly 2 39 40 41 $url = 'ldap://db.debian.org/ou=users,dc=debian,dc=org??sub?(sn=A*)'; 37 42 $this->searchResult = stubLDAPURL::fromString($url)->connect()->bind()->search(); 38 43 } 39 44 40 45 /** 41 * assure getting an entry 42 * 46 * assure getting an entry (valid(), next(), getEntry(), rewind()) 47 * 43 48 * @test 44 49 */ 45 public function getEntry ()50 public function getEntryNextValidRewind() 46 51 { 47 52 $this->assertType('stubLDAPEntry', $this->searchResult->getEntry()); 48 $this->searchResult->next(); 49 $this->searchResult->next(); 53 for(;$this->searchResult->valid(); $this->searchResult->next()) { 54 $this->assertType('stubLDAPEntry', $this->searchResult->getEntry()); 55 } 50 56 $this->assertNull($this->searchResult->getEntry()); 57 58 $this->searchResult->rewind(); 59 $this->assertType('stubLDAPEntry', $this->searchResult->getEntry()); 51 60 } 52 61 53 62 /** 54 63 * assure getting the current item 55 * 64 * 56 65 * @test 57 66 */ … … 63 72 /** 64 73 * assure getting the key of an item 65 * 74 * 66 75 * @test 67 76 */ … … 70 79 $this->assertEquals('resource', gettype($this->searchResult->key())); 71 80 } 72 73 /**74 * assure setting the cursor to the next item75 *76 * @test77 */78 public function next()79 {80 $this->searchResult->next();81 $this->assertEquals('resource', gettype($this->searchResult->current()));82 $this->searchResult->next();83 $this->assertNull($this->searchResult->current());84 }85 86 /**87 * assure rewinding the cursor88 *89 * @test90 */91 public function rewind()92 {93 $this->searchResult->next();94 $next = $this->searchResult->current();95 $this->searchResult->rewind();96 $first = $this->searchResult->current();97 $this->assertNotEquals($next, $first);98 }99 100 /**101 * assure validity of the cursor position102 *103 * @test104 */105 public function valid()106 {107 $this->searchResult->next();108 $this->assertTrue($this->searchResult->valid());109 $this->searchResult->next();110 $this->assertFalse($this->searchResult->valid());111 }112 81 } 113 82 ?> trunk/src/test/php/net/stubbles/peer/ldap/stubLDAPURLTestCase.php
r1570 r1625 17 17 { 18 18 /** 19 * set up test envi oronment19 * set up test environment 20 20 */ 21 21 public function setUp() … … 25 25 } 26 26 } 27 27 28 28 /** 29 29 * assure url roundtrip without port … … 43 43 /** 44 44 * assure url roundtrip with standard port 45 * 45 * 46 46 * @test 47 47 */ … … 55 55 /** 56 56 * assure url roundtrip with arbitrary port 57 * 57 * 58 58 * @test 59 59 */ … … 67 67 /** 68 68 * assure url roundtrip with host ip 69 * 69 * 70 70 * @test 71 71 */ … … 79 79 /** 80 80 * assure url roundtrip with localhost 81 * 81 * 82 82 * @test 83 83 */ … … 91 91 /** 92 92 * assure url roundtrip without host and port 93 * 93 * 94 94 * @test 95 95 */ … … 104 104 /** 105 105 * assure url roundtrip without authentication data 106 * 106 * 107 107 * @test 108 108 */ … … 116 116 /** 117 117 * assure url roundtrip with params (all possibilitiess) 118 * 118 * 119 119 * @test 120 120 */ … … 122 122 { 123 123 $url = 'ldap://localhost:389/dc=example,dc=com?cn'; 124 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base? objectClass=*';124 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base?(objectClass=*)'; 125 125 $ldap = stubLDAPURL::fromString($url); 126 126 $this->assertEquals($expected, $ldap->get(true)); 127 127 128 128 $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub'; 129 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?sub? objectClass=*';130 $ldap = stubLDAPURL::fromString($url); 131 $this->assertEquals($expected, $ldap->get(true)); 132 133 $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub?(cn=John Doe)';134 $ldap = stubLDAPURL::fromString($url); 135 $this->assertEquals($url, $ldap->get(true)); 136 137 $url = 'ldap://localhost:389/dc=example,dc=com??sub?(cn=John Doe)';129 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?sub?(objectClass=*)'; 130 $ldap = stubLDAPURL::fromString($url); 131 $this->assertEquals($expected, $ldap->get(true)); 132 133 $url = 'ldap://localhost:389/dc=example,dc=com?cn?sub?(cn=John Doe)'; 134 $ldap = stubLDAPURL::fromString($url); 135 $this->assertEquals($url, $ldap->get(true)); 136 137 $url = 'ldap://localhost:389/dc=example,dc=com??sub?(cn=John Doe)'; 138 138 $ldap = stubLDAPURL::fromString($url); 139 139 $this->assertEquals($url, $ldap->get(true)); … … 152 152 /** 153 153 * assure url roundtrip with params (attributes) 154 * 154 * 155 155 * @test 156 156 */ … … 158 158 { 159 159 $url = 'ldap://localhost:389/dc=example,dc=com?cn'; 160 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base? objectClass=*';160 $expected = 'ldap://localhost:389/dc=example,dc=com?cn?base?(objectClass=*)'; 161 161 $ldap = stubLDAPURL::fromString($url); 162 162 $this->assertEquals($expected, $ldap->get(true)); 163 163 164 164 $url = 'ldap://localhost:389/dc=example,dc=com?cn,uid'; 165 $expected = 'ldap://localhost:389/dc=example,dc=com?cn,uid?base? objectClass=*';165 $expected = 'ldap://localhost:389/dc=example,dc=com?cn,uid?base?(objectClass=*)'; 166 166 $ldap = stubLDAPURL::fromString($url); 167 167 $this->assertEquals($expected, $ldap->get(true)); … … 170 170 /** 171 171 * assure url roundtrip with params (filter) 172 * 173 * @test 174 * TODO more complex filter 172 * 173 * @test 175 174 */ 176 175 public function urlWithParamsFilterValues() … … 184 183 /** 185 184 * assure url roundtrip with params (scope) 186 * 185 * 187 186 * @test 188 187 */ … … 190 189 { 191 190 $url = 'ldap://ldap.example.com:389/dc=example,dc=com??base'; 192 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??base? objectClass=*';191 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??base?(objectClass=*)'; 193 192 $ldap = stubLDAPURL::fromString($url); 194 193 $this->assertEquals($expected, $ldap->get(true)); 195 194 196 195 $url = 'ldap://ldap.example.com:389/dc=example,dc=com??one'; 197 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??one? objectClass=*';196 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??one?(objectClass=*)'; 198 197 $ldap = stubLDAPURL::fromString($url); 199 198 $this->assertEquals($expected, $ldap->get(true)); 200 199 201 200 $url = 'ldap://ldap.example.com:389/dc=example,dc=com??sub'; 202 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??sub? objectClass=*';201 $expected = 'ldap://ldap.example.com:389/dc=example,dc=com??sub?(objectClass=*)'; 203 202 $ldap = stubLDAPURL::fromString($url); 204 203 $this->assertEquals($expected, $ldap->get(true)); … … 207 206 /** 208 207 * assure url roundtrip with ssl port 209 * 208 * 210 209 * @test 211 210 */ 212 211 public function urlSSL() 213 212 { 214 $url = 'ldaps://ldap.example.com:636/dc=example,dc=com';213 $url = 'ldaps://ldap.example.com:636/dc=example,dc=com'; 215 214 $ldap = stubLDAPURL::fromString($url); 216 215 $this->assertEquals($url, $ldap->get(true)); … … 223 222 224 223 /** 224 * assure url with empty string returns 225 * 226 * @test 227 */ 228 public function urlBadFormatEmptyString() 229 { 230 $this->assertNull(stubLDAPURL::fromString('')); 231 } 232 233 /** 234 * data provider for assurance of throwing stubMalformedURLException 235 * 236 * @return array<array<string>> 237 */ 238 public static function malformedUrlProvider() 239 { 240 return array( 241 // no ldap:// 242 array('ldap.example.com/dc=example,dc=com'), 243 // bad scheme 244 array('badScheme://ldap.example.com/dc=example,dc=com'), 245 // bad url 246 array('ldap://ldapexamplecom/dc=example,dc=com'), 247 // bad base dn 248 array('ldap://ldap.example.com/dc=example|dc=com'), <
