You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\User_LDAP\Tests;
  26. use OCA\User_LDAP\Connection;
  27. use OCA\User_LDAP\ILDAPWrapper;
  28. /**
  29. * Class Test_Connection
  30. *
  31. * @group DB
  32. *
  33. * @package OCA\User_LDAP\Tests
  34. */
  35. class ConnectionTest extends \Test\TestCase {
  36. /** @var \OCA\User_LDAP\ILDAPWrapper */
  37. protected $ldap;
  38. /** @var Connection */
  39. protected $connection;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->ldap = $this->createMock(ILDAPWrapper::class);
  43. // we use a mock here to replace the cache mechanism, due to missing DI in LDAP backend.
  44. $this->connection = $this->getMockBuilder('OCA\User_LDAP\Connection')
  45. ->setMethods(['getFromCache', 'writeToCache'])
  46. ->setConstructorArgs([$this->ldap, '', null])
  47. ->getMock();
  48. $this->ldap->expects($this->any())
  49. ->method('areLDAPFunctionsAvailable')
  50. ->will($this->returnValue(true));
  51. }
  52. public function testOriginalAgentUnchangedOnClone() {
  53. //background: upon login a bind is done with the user credentials
  54. //which is valid for the whole LDAP resource. It needs to be reset
  55. //to the agent's credentials
  56. $lw = $this->createMock(ILDAPWrapper::class);
  57. $connection = new Connection($lw, '', null);
  58. $agent = array(
  59. 'ldapAgentName' => 'agent',
  60. 'ldapAgentPassword' => '123456',
  61. );
  62. $connection->setConfiguration($agent);
  63. $testConnection = clone $connection;
  64. $user = array(
  65. 'ldapAgentName' => 'user',
  66. 'ldapAgentPassword' => 'password',
  67. );
  68. $testConnection->setConfiguration($user);
  69. $agentName = $connection->ldapAgentName;
  70. $agentPawd = $connection->ldapAgentPassword;
  71. $this->assertSame($agentName, $agent['ldapAgentName']);
  72. $this->assertSame($agentPawd, $agent['ldapAgentPassword']);
  73. }
  74. public function testUseBackupServer() {
  75. $mainHost = 'ldap://nixda.ldap';
  76. $backupHost = 'ldap://fallback.ldap';
  77. $config = [
  78. 'ldapConfigurationActive' => true,
  79. 'ldapHost' => $mainHost,
  80. 'ldapPort' => 389,
  81. 'ldapBackupHost' => $backupHost,
  82. 'ldapBackupPort' => 389,
  83. 'ldapAgentName' => 'uid=agent',
  84. 'ldapAgentPassword' => 'SuchASecret'
  85. ];
  86. $this->connection->setIgnoreValidation(true);
  87. $this->connection->setConfiguration($config);
  88. $this->ldap->expects($this->any())
  89. ->method('isResource')
  90. ->will($this->returnValue(true));
  91. $this->ldap->expects($this->any())
  92. ->method('setOption')
  93. ->will($this->returnValue(true));
  94. $this->ldap->expects($this->exactly(3))
  95. ->method('connect')
  96. ->will($this->returnValue('ldapResource'));
  97. $this->ldap->expects($this->any())
  98. ->method('errno')
  99. ->will($this->returnValue(0));
  100. // Not called often enough? Then, the fallback to the backup server is broken.
  101. $this->connection->expects($this->exactly(4))
  102. ->method('getFromCache')
  103. ->with('overrideMainServer')
  104. ->will($this->onConsecutiveCalls(false, false, true, true));
  105. $this->connection->expects($this->once())
  106. ->method('writeToCache')
  107. ->with('overrideMainServer', true);
  108. $isThrown = false;
  109. $this->ldap->expects($this->exactly(3))
  110. ->method('bind')
  111. ->will($this->returnCallback(function () use (&$isThrown) {
  112. if(!$isThrown) {
  113. $isThrown = true;
  114. throw new \OC\ServerNotAvailableException();
  115. }
  116. return true;
  117. }));
  118. $this->connection->init();
  119. $this->connection->resetConnectionResource();
  120. // with the second init() we test whether caching works
  121. $this->connection->init();
  122. }
  123. public function testBindWithInvalidCredentials() {
  124. // background: Bind with invalid credentials should return false
  125. // and not throw a ServerNotAvailableException.
  126. $host = 'ldap://nixda.ldap';
  127. $config = [
  128. 'ldapConfigurationActive' => true,
  129. 'ldapHost' => $host,
  130. 'ldapPort' => 389,
  131. 'ldapBackupHost' => '',
  132. 'ldapAgentName' => 'user',
  133. 'ldapAgentPassword' => 'password'
  134. ];
  135. $this->connection->setIgnoreValidation(true);
  136. $this->connection->setConfiguration($config);
  137. $this->ldap->expects($this->any())
  138. ->method('isResource')
  139. ->will($this->returnValue(true));
  140. $this->ldap->expects($this->any())
  141. ->method('setOption')
  142. ->will($this->returnValue(true));
  143. $this->ldap->expects($this->any())
  144. ->method('connect')
  145. ->will($this->returnValue('ldapResource'));
  146. $this->ldap->expects($this->exactly(2))
  147. ->method('bind')
  148. ->will($this->returnValue(false));
  149. // LDAP_INVALID_CREDENTIALS
  150. $this->ldap->expects($this->any())
  151. ->method('errno')
  152. ->will($this->returnValue(0x31));
  153. try {
  154. $this->assertFalse($this->connection->bind(), 'Connection::bind() should not return true with invalid credentials.');
  155. } catch (\OC\ServerNotAvailableException $e) {
  156. $this->fail('Failed asserting that exception of type "OC\ServerNotAvailableException" is not thrown.');
  157. }
  158. }
  159. public function testStartTlsNegotiationFailure() {
  160. // background: If Start TLS negotiation fails,
  161. // a ServerNotAvailableException should be thrown.
  162. $host = 'ldap://nixda.ldap';
  163. $port = 389;
  164. $config = [
  165. 'ldapConfigurationActive' => true,
  166. 'ldapHost' => $host,
  167. 'ldapPort' => $port,
  168. 'ldapTLS' => true,
  169. 'ldapBackupHost' => '',
  170. 'ldapAgentName' => 'user',
  171. 'ldapAgentPassword' => 'password'
  172. ];
  173. $this->connection->setIgnoreValidation(true);
  174. $this->connection->setConfiguration($config);
  175. $this->ldap->expects($this->any())
  176. ->method('isResource')
  177. ->will($this->returnValue(true));
  178. $this->ldap->expects($this->any())
  179. ->method('connect')
  180. ->will($this->returnValue('ldapResource'));
  181. $this->ldap->expects($this->any())
  182. ->method('setOption')
  183. ->will($this->returnValue(true));
  184. $this->ldap->expects($this->any())
  185. ->method('bind')
  186. ->will($this->returnValue(true));
  187. $this->ldap->expects($this->any())
  188. ->method('errno')
  189. ->will($this->returnValue(0));
  190. $this->ldap->expects($this->any())
  191. ->method('startTls')
  192. ->will($this->returnValue(false));
  193. $this->expectException(\OC\ServerNotAvailableException::class);
  194. $this->expectExceptionMessage('Start TLS failed, when connecting to LDAP host ' . $host . '.');
  195. $this->connection->init();
  196. }
  197. }