proxy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_CryptProxy extends UnitTestCase {
  9. private $oldConfig;
  10. private $oldKey;
  11. public function setUp() {
  12. $user=OC_User::getUser();
  13. $this->oldConfig=OCP\Config::getAppValue('files_encryption','enable_encryption', 'true');
  14. OCP\Config::setAppValue('files_encryption', 'enable_encryption', 'true');
  15. $this->oldKey=isset($_SESSION['enckey'])?$_SESSION['enckey']:null;
  16. //set testing key
  17. $_SESSION['enckey']=md5(time());
  18. //clear all proxies and hooks so we can do clean testing
  19. OC_FileProxy::clearProxies();
  20. OC_Hook::clear('OC_Filesystem');
  21. //enable only the encryption hook
  22. OC_FileProxy::register(new OC_FileProxy_Encryption());
  23. //set up temporary storage
  24. OC_Filesystem::clearMounts();
  25. OC_Filesystem::mount('OC_Filestorage_Temporary', array(), '/');
  26. OC_Filesystem::init('/'.$user.'/files');
  27. //set up the users home folder in the temp storage
  28. $rootView=new OC_FilesystemView('');
  29. $rootView->mkdir('/'.$user);
  30. $rootView->mkdir('/'.$user.'/files');
  31. }
  32. public function tearDown() {
  33. OCP\Config::setAppValue('files_encryption', 'enable_encryption', $this->oldConfig);
  34. if(!is_null($this->oldKey)) {
  35. $_SESSION['enckey']=$this->oldKey;
  36. }
  37. }
  38. public function testSimple() {
  39. $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
  40. $original=file_get_contents($file);
  41. OC_Filesystem::file_put_contents('/file', $original);
  42. OC_FileProxy::$enabled=false;
  43. $stored=OC_Filesystem::file_get_contents('/file');
  44. OC_FileProxy::$enabled=true;
  45. $fromFile=OC_Filesystem::file_get_contents('/file');
  46. $this->assertNotEqual($original, $stored);
  47. $this->assertEqual(strlen($original), strlen($fromFile));
  48. $this->assertEqual($original, $fromFile);
  49. }
  50. public function testView() {
  51. $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
  52. $original=file_get_contents($file);
  53. $rootView=new OC_FilesystemView('');
  54. $view=new OC_FilesystemView('/'.OC_User::getUser());
  55. $userDir='/'.OC_User::getUser().'/files';
  56. $rootView->file_put_contents($userDir.'/file', $original);
  57. OC_FileProxy::$enabled=false;
  58. $stored=$rootView->file_get_contents($userDir.'/file');
  59. OC_FileProxy::$enabled=true;
  60. $this->assertNotEqual($original, $stored);
  61. $fromFile=$rootView->file_get_contents($userDir.'/file');
  62. $this->assertEqual($original, $fromFile);
  63. $fromFile=$view->file_get_contents('files/file');
  64. $this->assertEqual($original, $fromFile);
  65. }
  66. public function testBinary() {
  67. $file=__DIR__.'/binary';
  68. $original=file_get_contents($file);
  69. OC_Filesystem::file_put_contents('/file', $original);
  70. OC_FileProxy::$enabled=false;
  71. $stored=OC_Filesystem::file_get_contents('/file');
  72. OC_FileProxy::$enabled=true;
  73. $fromFile=OC_Filesystem::file_get_contents('/file');
  74. $this->assertNotEqual($original, $stored);
  75. $this->assertEqual(strlen($original), strlen($fromFile));
  76. $this->assertEqual($original, $fromFile);
  77. $file=__DIR__.'/zeros';
  78. $original=file_get_contents($file);
  79. OC_Filesystem::file_put_contents('/file', $original);
  80. OC_FileProxy::$enabled=false;
  81. $stored=OC_Filesystem::file_get_contents('/file');
  82. OC_FileProxy::$enabled=true;
  83. $fromFile=OC_Filesystem::file_get_contents('/file');
  84. $this->assertNotEqual($original, $stored);
  85. $this->assertEqual(strlen($original), strlen($fromFile));
  86. }
  87. }