ChecksumsContext.php 6.9 KB

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