1
0

ChecksumsContext.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. *
  4. * @author Joas Schilling <coding@schilljs.com>
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. require __DIR__ . '/../../vendor/autoload.php';
  24. use GuzzleHttp\Client;
  25. use GuzzleHttp\Message\ResponseInterface;
  26. class ChecksumsContext implements \Behat\Behat\Context\Context {
  27. /** @var string */
  28. private $baseUrl;
  29. /** @var Client */
  30. private $client;
  31. /** @var ResponseInterface */
  32. private $response;
  33. /**
  34. * @param string $baseUrl
  35. */
  36. public function __construct($baseUrl) {
  37. $this->baseUrl = $baseUrl;
  38. // in case of ci deployment we take the server url from the environment
  39. $testServerUrl = getenv('TEST_SERVER_URL');
  40. if ($testServerUrl !== false) {
  41. $this->baseUrl = substr($testServerUrl, 0, -5);
  42. }
  43. }
  44. /** @BeforeScenario */
  45. public function setUpScenario() {
  46. $this->client = new Client();
  47. }
  48. /** @AfterScenario */
  49. public function tearDownScenario() {
  50. }
  51. /**
  52. * @param string $userName
  53. * @return string
  54. */
  55. private function getPasswordForUser($userName) {
  56. if($userName === 'admin') {
  57. return 'admin';
  58. }
  59. return '123456';
  60. }
  61. /**
  62. * @When user :user uploads file :source to :destination with checksum :checksum
  63. * @param string $user
  64. * @param string $source
  65. * @param string $destination
  66. * @param string $checksum
  67. */
  68. public function userUploadsFileToWithChecksum($user, $source, $destination, $checksum)
  69. {
  70. $file = \GuzzleHttp\Stream\Stream::factory(fopen($source, 'r'));
  71. try {
  72. $this->response = $this->client->put(
  73. $this->baseUrl . '/remote.php/webdav' . $destination,
  74. [
  75. 'auth' => [
  76. $user,
  77. $this->getPasswordForUser($user)
  78. ],
  79. 'body' => $file,
  80. 'headers' => [
  81. 'OC-Checksum' => $checksum
  82. ]
  83. ]
  84. );
  85. } catch (\GuzzleHttp\Exception\ServerException $e) {
  86. // 4xx and 5xx responses cause an exception
  87. $this->response = $e->getResponse();
  88. }
  89. }
  90. /**
  91. * @Then The webdav response should have a status code :statusCode
  92. * @param int $statusCode
  93. * @throws \Exception
  94. */
  95. public function theWebdavResponseShouldHaveAStatusCode($statusCode) {
  96. if((int)$statusCode !== $this->response->getStatusCode()) {
  97. throw new \Exception("Expected $statusCode, got ".$this->response->getStatusCode());
  98. }
  99. }
  100. /**
  101. * @When user :user request the checksum of :path via propfind
  102. * @param string $user
  103. * @param string $path
  104. */
  105. public function userRequestTheChecksumOfViaPropfind($user, $path)
  106. {
  107. $request = $this->client->createRequest(
  108. 'PROPFIND',
  109. $this->baseUrl . '/remote.php/webdav' . $path,
  110. [
  111. 'body' => '<?xml version="1.0"?>
  112. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  113. <d:prop>
  114. <oc:checksums />
  115. </d:prop>
  116. </d:propfind>',
  117. 'auth' => [
  118. $user,
  119. $this->getPasswordForUser($user),
  120. ]
  121. ]
  122. );
  123. $this->response = $this->client->send($request);
  124. }
  125. /**
  126. * @Then The webdav checksum should match :checksum
  127. * @param string $checksum
  128. * @throws \Exception
  129. */
  130. public function theWebdavChecksumShouldMatch($checksum)
  131. {
  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. {
  150. $this->response = $this->client->get(
  151. $this->baseUrl . '/remote.php/webdav' . $path,
  152. [
  153. 'auth' => [
  154. $user,
  155. $this->getPasswordForUser($user),
  156. ]
  157. ]
  158. );
  159. }
  160. /**
  161. * @Then The header checksum should match :checksum
  162. * @param string $checksum
  163. * @throws \Exception
  164. */
  165. public function theHeaderChecksumShouldMatch($checksum)
  166. {
  167. if ($this->response->getHeader('OC-Checksum') !== $checksum) {
  168. throw new \Exception("Expected $checksum, got ".$this->response->getHeader('OC-Checksum'));
  169. }
  170. }
  171. /**
  172. * @Given User :user copied file :source to :destination
  173. * @param string $user
  174. * @param string $source
  175. * @param string $destination
  176. */
  177. public function userCopiedFileTo($user, $source, $destination)
  178. {
  179. $request = $this->client->createRequest(
  180. 'MOVE',
  181. $this->baseUrl . '/remote.php/webdav' . $source,
  182. [
  183. 'auth' => [
  184. $user,
  185. $this->getPasswordForUser($user),
  186. ],
  187. 'headers' => [
  188. 'Destination' => $this->baseUrl . '/remote.php/webdav' . $destination,
  189. ],
  190. ]
  191. );
  192. $this->response = $this->client->send($request);
  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'));
  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. }