ExceptionOnLostConnection.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\Tests\Integration;
  27. use OC\ServerNotAvailableException;
  28. use OCA\User_LDAP\LDAP;
  29. /**
  30. * Class ExceptionOnLostConnection
  31. *
  32. * integration test, ensures that an exception is thrown, when the connection is lost.
  33. *
  34. * LDAP must be available via toxiproxy.
  35. *
  36. * This test must be run manually.
  37. *
  38. */
  39. class ExceptionOnLostConnection {
  40. /** @var string */
  41. private $toxiProxyHost;
  42. /** @var string */
  43. private $toxiProxyName;
  44. /** @var string */
  45. private $ldapBase;
  46. /** @var string|null */
  47. private $ldapBindDN;
  48. /** @var string|null */
  49. private $ldapBindPwd;
  50. /** @var string */
  51. private $ldapHost;
  52. /** @var \OCA\User_LDAP\LDAP */
  53. private $ldap;
  54. /** @var bool */
  55. private $originalProxyState;
  56. /**
  57. * @param string $proxyHost host of toxiproxy as url, like http://localhost:8474
  58. * @param string $proxyName name of the LDAP proxy service as configured in toxiProxy
  59. * @param string $ldapBase any valid LDAP base DN
  60. * @param null $bindDN optional, bind DN if anonymous bind is not possible
  61. * @param null $bindPwd optional
  62. */
  63. public function __construct($proxyHost, $proxyName, $ldapBase, $bindDN = null, $bindPwd = null) {
  64. $this->toxiProxyHost = $proxyHost;
  65. $this->toxiProxyName = $proxyName;
  66. $this->ldapBase = $ldapBase;
  67. $this->ldapBindDN = $bindDN;
  68. $this->ldapBindPwd = $bindPwd;
  69. $this->setUp();
  70. }
  71. /**
  72. * destructor
  73. */
  74. public function __destruct() {
  75. $this->cleanUp();
  76. }
  77. /**
  78. * prepares everything for the test run. Includes loading Nextcloud and
  79. * the LDAP backend, as well as getting information about toxiproxy.
  80. * Also creates an instance of the LDAP class, the testee
  81. *
  82. * @throws \Exception
  83. */
  84. public function setUp(): void {
  85. require_once __DIR__ . '/../../../../lib/base.php';
  86. \OC_App::loadApps(['user_ldap']);
  87. $ch = $this->getCurl();
  88. $proxyInfoJson = curl_exec($ch);
  89. $this->checkCurlResult($ch, $proxyInfoJson);
  90. $proxyInfo = json_decode($proxyInfoJson, true);
  91. $this->originalProxyState = $proxyInfo['enabled'];
  92. $this->ldapHost = 'ldap://' . $proxyInfo['listen']; // contains port as well
  93. $this->ldap = new LDAP();
  94. }
  95. /**
  96. * restores original state of the LDAP proxy, if necessary
  97. */
  98. public function cleanUp() {
  99. if($this->originalProxyState === true) {
  100. $this->setProxyState(true);
  101. }
  102. }
  103. /**
  104. * runs the test and prints the result. Exit code is 0 if successful, 1 on
  105. * fail
  106. */
  107. public function run() {
  108. if($this->originalProxyState === false) {
  109. $this->setProxyState(true);
  110. }
  111. //host contains port, 2nd parameter will be ignored
  112. $cr = $this->ldap->connect($this->ldapHost, 0);
  113. $this->ldap->bind($cr, $this->ldapBindDN, $this->ldapBindPwd);
  114. $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5);
  115. // disable LDAP, will cause lost connection
  116. $this->setProxyState(false);
  117. try {
  118. $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5);
  119. } catch (ServerNotAvailableException $e) {
  120. print("Test PASSED" . PHP_EOL);
  121. exit(0);
  122. }
  123. print("Test FAILED" . PHP_EOL);
  124. exit(1);
  125. }
  126. /**
  127. * tests whether a curl operation ran successfully. If not, an exception
  128. * is thrown
  129. *
  130. * @param resource $ch
  131. * @param mixed $result
  132. * @throws \Exception
  133. */
  134. private function checkCurlResult($ch, $result) {
  135. if($result === false) {
  136. $error = curl_error($ch);
  137. curl_close($ch);
  138. throw new \Exception($error);
  139. }
  140. }
  141. /**
  142. * enables or disabled the LDAP proxy service in toxiproxy
  143. *
  144. * @param bool $isEnabled whether is should be enabled or disables
  145. * @throws \Exception
  146. */
  147. private function setProxyState($isEnabled) {
  148. if(!is_bool($isEnabled)) {
  149. throw new \InvalidArgumentException('Bool expected');
  150. }
  151. $postData = json_encode(['enabled' => $isEnabled]);
  152. $ch = $this->getCurl();
  153. curl_setopt($ch, CURLOPT_POST, true);
  154. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  155. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  156. 'Content-Type: application/json',
  157. 'Content-Length: ' . strlen($postData)]
  158. );
  159. $recvd = curl_exec($ch);
  160. $this->checkCurlResult($ch, $recvd);
  161. }
  162. /**
  163. * initializes a curl handler towards the toxiproxy LDAP proxy service
  164. * @return resource
  165. */
  166. private function getCurl() {
  167. $ch = curl_init();
  168. curl_setopt($ch, CURLOPT_URL, $this->toxiProxyHost . '/proxies/' . $this->toxiProxyName);
  169. curl_setopt($ch, CURLOPT_HEADER, false);
  170. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  171. return $ch;
  172. }
  173. }
  174. $test = new ExceptionOnLostConnection('http://localhost:8474', 'ldap', 'dc=owncloud,dc=bzoc');
  175. $test->run();