ConnectionTest.php 6.9 KB

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