HashWrapperTest.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Stream;
  8. use OC\Files\Stream\HashWrapper;
  9. use Test\TestCase;
  10. class HashWrapperTest extends TestCase {
  11. /**
  12. * @dataProvider hashProvider
  13. */
  14. public function testHashStream($data, string $algo, string $hash) {
  15. if (!is_resource($data)) {
  16. $tmpData = fopen('php://temp', 'r+');
  17. if ($data !== null) {
  18. fwrite($tmpData, $data);
  19. rewind($tmpData);
  20. }
  21. $data = $tmpData;
  22. }
  23. $wrapper = HashWrapper::wrap($data, $algo, function ($result) use ($hash) {
  24. $this->assertEquals($hash, $result);
  25. });
  26. stream_get_contents($wrapper);
  27. }
  28. public function hashProvider() {
  29. return [
  30. ['foo', 'md5', 'acbd18db4cc2f85cedef654fccc4a4d8'],
  31. ['foo', 'sha1', '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'],
  32. ['foo', 'sha256', '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'],
  33. [str_repeat('foo', 8192), 'md5', '96684d2b796a2c54a026b5d60f9de819'],
  34. ];
  35. }
  36. }