/src/applications/files/format/__tests__/PhabricatorFileStorageFormatTestCase.php

https://github.com/dannysu/phabricator · PHP · 77 lines · 53 code · 17 blank · 7 comment · 0 complexity · 752e609c7efde175a07aa825b6ff07e1 MD5 · raw file

  1. <?php
  2. final class PhabricatorFileStorageFormatTestCase extends PhabricatorTestCase {
  3. protected function getPhabricatorTestCaseConfiguration() {
  4. return array(
  5. self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
  6. );
  7. }
  8. public function testRot13Storage() {
  9. $engine = new PhabricatorTestStorageEngine();
  10. $rot13_format = PhabricatorFileROT13StorageFormat::FORMATKEY;
  11. $data = 'The cow jumped over the full moon.';
  12. $expect = 'Gur pbj whzcrq bire gur shyy zbba.';
  13. $params = array(
  14. 'name' => 'test.dat',
  15. 'storageEngines' => array(
  16. $engine,
  17. ),
  18. 'format' => $rot13_format,
  19. );
  20. $file = PhabricatorFile::newFromFileData($data, $params);
  21. // We should have a file stored as rot13, which reads back the input
  22. // data correctly.
  23. $this->assertEqual($rot13_format, $file->getStorageFormat());
  24. $this->assertEqual($data, $file->loadFileData());
  25. // The actual raw data in the storage engine should be encoded.
  26. $raw_data = $engine->readFile($file->getStorageHandle());
  27. $this->assertEqual($expect, $raw_data);
  28. }
  29. public function testAES256Storage() {
  30. $engine = new PhabricatorTestStorageEngine();
  31. $key_name = 'test.abcd';
  32. $key_text = 'abcdefghijklmnopABCDEFGHIJKLMNOP';
  33. PhabricatorKeyring::addKey(
  34. array(
  35. 'name' => $key_name,
  36. 'type' => 'aes-256-cbc',
  37. 'material.base64' => base64_encode($key_text),
  38. ));
  39. $format = id(new PhabricatorFileAES256StorageFormat())
  40. ->selectMasterKey($key_name);
  41. $data = 'The cow jumped over the full moon.';
  42. $params = array(
  43. 'name' => 'test.dat',
  44. 'storageEngines' => array(
  45. $engine,
  46. ),
  47. 'format' => $format,
  48. );
  49. $file = PhabricatorFile::newFromFileData($data, $params);
  50. // We should have a file stored as AES256.
  51. $format_key = $format->getStorageFormatKey();
  52. $this->assertEqual($format_key, $file->getStorageFormat());
  53. $this->assertEqual($data, $file->loadFileData());
  54. // The actual raw data in the storage engine should be encrypted. We
  55. // can't really test this, but we can make sure it's not the same as the
  56. // input data.
  57. $raw_data = $engine->readFile($file->getStorageHandle());
  58. $this->assertTrue($data !== $raw_data);
  59. }
  60. }