ChecksumsContext.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Phil Davis <phil.davis@inf.org>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. require __DIR__ . '/../../vendor/autoload.php';
  26. use GuzzleHttp\Client;
  27. use GuzzleHttp\Message\ResponseInterface;
  28. class ChecksumsContext implements \Behat\Behat\Context\Context {
  29. /** @var string */
  30. private $baseUrl;
  31. /** @var Client */
  32. private $client;
  33. /** @var ResponseInterface */
  34. private $response;
  35. /**
  36. * @param string $baseUrl
  37. */
  38. public function __construct($baseUrl) {
  39. $this->baseUrl = $baseUrl;
  40. // in case of ci deployment we take the server url from the environment
  41. $testServerUrl = getenv('TEST_SERVER_URL');
  42. if ($testServerUrl !== false) {
  43. $this->baseUrl = substr($testServerUrl, 0, -5);
  44. }
  45. }
  46. /** @BeforeScenario */
  47. public function setUpScenario() {
  48. $this->client = new Client();
  49. }
  50. /** @AfterScenario */
  51. public function tearDownScenario() {
  52. }
  53. /**
  54. * @param string $userName
  55. * @return string
  56. */
  57. private function getPasswordForUser($userName) {
  58. if($userName === 'admin') {
  59. return 'admin';
  60. }
  61. return '123456';
  62. }
  63. /**
  64. * @When user :user uploads file :source to :destination with checksum :checksum
  65. * @param string $user
  66. * @param string $source
  67. * @param string $destination
  68. * @param string $checksum
  69. */
  70. public function userUploadsFileToWithChecksum($user, $source, $destination, $checksum)
  71. {
  72. $file = \GuzzleHttp\Psr7\stream_for(fopen($source, 'r'));
  73. try {
  74. $this->response = $this->client->put(
  75. $this->baseUrl . '/remote.php/webdav' . $destination,
  76. [
  77. 'auth' => [
  78. $user,
  79. $this->getPasswordForUser($user)
  80. ],
  81. 'body' => $file,
  82. 'headers' => [
  83. 'OC-Checksum' => $checksum
  84. ]
  85. ]
  86. );
  87. } catch (\GuzzleHttp\Exception\ServerException $e) {
  88. // 4xx and 5xx responses cause an exception
  89. $this->response = $e->getResponse();
  90. }
  91. }
  92. /**
  93. * @Then The webdav response should have a status code :statusCode
  94. * @param int $statusCode
  95. * @throws \Exception
  96. */
  97. public function theWebdavResponseShouldHaveAStatusCode($statusCode) {
  98. if((int)$statusCode !== $this->response->getStatusCode()) {
  99. throw new \Exception("Expected $statusCode, got ".$this->response->getStatusCode());
  100. }
  101. }
  102. /**
  103. * @When user :user request the checksum of :path via propfind
  104. * @param string $user
  105. * @param string $path
  106. */
  107. public function userRequestTheChecksumOfViaPropfind($user, $path)
  108. {
  109. $this->response = $this->client->request(
  110. 'PROPFIND',
  111. $this->baseUrl . '/remote.php/webdav' . $path,
  112. [
  113. 'body' => '<?xml version="1.0"?>
  114. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  115. <d:prop>
  116. <oc:checksums />
  117. </d:prop>
  118. </d:propfind>',
  119. 'auth' => [
  120. $user,
  121. $this->getPasswordForUser($user),
  122. ]
  123. ]
  124. );
  125. }
  126. /**
  127. * @Then The webdav checksum should match :checksum
  128. * @param string $checksum
  129. * @throws \Exception
  130. */
  131. public function theWebdavChecksumShouldMatch($checksum)
  132. {
  133. $service = new Sabre\Xml\Service();
  134. $parsed = $service->parse($this->response->getBody()->getContents());
  135. /*
  136. * Fetch the checksum array
  137. * Maybe we want to do this a bit cleaner ;)
  138. */
  139. $checksums = $parsed[0]['value'][1]['value'][0]['value'][0];
  140. if ($checksums['value'][0]['value'] !== $checksum) {
  141. throw new \Exception("Expected $checksum, got ".$checksums['value'][0]['value']);
  142. }
  143. }
  144. /**
  145. * @When user :user downloads the file :path
  146. * @param string $user
  147. * @param string $path
  148. */
  149. public function userDownloadsTheFile($user, $path)
  150. {
  151. $this->response = $this->client->get(
  152. $this->baseUrl . '/remote.php/webdav' . $path,
  153. [
  154. 'auth' => [
  155. $user,
  156. $this->getPasswordForUser($user),
  157. ]
  158. ]
  159. );
  160. }
  161. /**
  162. * @Then The header checksum should match :checksum
  163. * @param string $checksum
  164. * @throws \Exception
  165. */
  166. public function theHeaderChecksumShouldMatch($checksum)
  167. {
  168. if ($this->response->getHeader('OC-Checksum')[0] !== $checksum) {
  169. throw new \Exception("Expected $checksum, got ".$this->response->getHeader('OC-Checksum')[0]);
  170. }
  171. }
  172. /**
  173. * @Given User :user copied file :source to :destination
  174. * @param string $user
  175. * @param string $source
  176. * @param string $destination
  177. */
  178. public function userCopiedFileTo($user, $source, $destination)
  179. {
  180. $this->response = $this->client->request(
  181. 'MOVE',
  182. $this->baseUrl . '/remote.php/webdav' . $source,
  183. [
  184. 'auth' => [
  185. $user,
  186. $this->getPasswordForUser($user),
  187. ],
  188. 'headers' => [
  189. 'Destination' => $this->baseUrl . '/remote.php/webdav' . $destination,
  190. ],
  191. ]
  192. );
  193. }
  194. /**
  195. * @Then The webdav checksum should be empty
  196. */
  197. public function theWebdavChecksumShouldBeEmpty()
  198. {
  199. $service = new Sabre\Xml\Service();
  200. $parsed = $service->parse($this->response->getBody()->getContents());
  201. /*
  202. * Fetch the checksum array
  203. * Maybe we want to do this a bit cleaner ;)
  204. */
  205. $status = $parsed[0]['value'][1]['value'][1]['value'];
  206. if ($status !== 'HTTP/1.1 404 Not Found') {
  207. throw new \Exception("Expected 'HTTP/1.1 404 Not Found', got ".$status);
  208. }
  209. }
  210. /**
  211. * @Then The OC-Checksum header should not be there
  212. */
  213. public function theOcChecksumHeaderShouldNotBeThere()
  214. {
  215. if ($this->response->hasHeader('OC-Checksum')) {
  216. throw new \Exception("Expected no checksum header but got ".$this->response->getHeader('OC-Checksum')[0]);
  217. }
  218. }
  219. /**
  220. * @Given user :user uploads chunk file :num of :total with :data to :destination with checksum :checksum
  221. * @param string $user
  222. * @param int $num
  223. * @param int $total
  224. * @param string $data
  225. * @param string $destination
  226. * @param string $checksum
  227. */
  228. public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination, $checksum)
  229. {
  230. $num -= 1;
  231. $this->response = $this->client->put(
  232. $this->baseUrl . '/remote.php/webdav' . $destination . '-chunking-42-'.$total.'-'.$num,
  233. [
  234. 'auth' => [
  235. $user,
  236. $this->getPasswordForUser($user)
  237. ],
  238. 'body' => $data,
  239. 'headers' => [
  240. 'OC-Checksum' => $checksum,
  241. 'OC-Chunked' => '1',
  242. ]
  243. ]
  244. );
  245. }
  246. }