1
0

utiltest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Test\Encryption;
  3. use OC\Encryption\Util;
  4. use Test\TestCase;
  5. class UtilTest extends TestCase {
  6. /**
  7. * block size will always be 8192 for a PHP stream
  8. * @see https://bugs.php.net/bug.php?id=21641
  9. * @var integer
  10. */
  11. protected $headerSize = 8192;
  12. /** @var \PHPUnit_Framework_MockObject_MockObject */
  13. protected $view;
  14. /** @var \PHPUnit_Framework_MockObject_MockObject */
  15. protected $userManager;
  16. /** @var \PHPUnit_Framework_MockObject_MockObject */
  17. private $config;
  18. public function setUp() {
  19. parent::setUp();
  20. $this->view = $this->getMockBuilder('OC\Files\View')
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->userManager = $this->getMockBuilder('OC\User\Manager')
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->config = $this->getMockBuilder('OCP\IConfig')
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. }
  30. /**
  31. * @dataProvider providesHeadersForEncryptionModule
  32. */
  33. public function testGetEncryptionModuleId($expected, $header) {
  34. $u = new Util($this->view, $this->userManager, $this->config);
  35. $id = $u->getEncryptionModuleId($header);
  36. $this->assertEquals($expected, $id);
  37. }
  38. public function providesHeadersForEncryptionModule() {
  39. return [
  40. ['', []],
  41. ['', ['1']],
  42. [2, ['oc_encryption_module' => 2]],
  43. ];
  44. }
  45. /**
  46. * @dataProvider providesHeaders
  47. */
  48. public function testReadHeader($header, $expected, $moduleId) {
  49. $expected['oc_encryption_module'] = $moduleId;
  50. $u = new Util($this->view, $this->userManager, $this->config);
  51. $result = $u->readHeader($header);
  52. $this->assertSameSize($expected, $result);
  53. foreach ($expected as $key => $value) {
  54. $this->assertArrayHasKey($key, $result);
  55. $this->assertSame($value, $result[$key]);
  56. }
  57. }
  58. /**
  59. * @dataProvider providesHeaders
  60. */
  61. public function testCreateHeader($expected, $header, $moduleId) {
  62. $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
  63. $em->expects($this->any())->method('getId')->willReturn($moduleId);
  64. $u = new Util($this->view, $this->userManager, $this->config);
  65. $result = $u->createHeader($header, $em);
  66. $this->assertEquals($expected, $result);
  67. }
  68. public function providesHeaders() {
  69. return [
  70. [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  71. , [], '0'],
  72. [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
  73. , ['custom_header' => 'foo'], '0'],
  74. ];
  75. }
  76. /**
  77. * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
  78. */
  79. public function testCreateHeaderFailed() {
  80. $header = array('header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo');
  81. $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
  82. $em->expects($this->any())->method('getId')->willReturn('moduleId');
  83. $u = new Util($this->view, $this->userManager, $this->config);
  84. $u->createHeader($header, $em);
  85. }
  86. /**
  87. * @dataProvider providePathsForTestIsExcluded
  88. */
  89. public function testIsEcluded($path, $expected) {
  90. $this->userManager
  91. ->expects($this->any())
  92. ->method('userExists')
  93. ->will($this->returnCallback(array($this, 'isExcludedCallback')));
  94. $u = new Util($this->view, $this->userManager, $this->config);
  95. $this->assertSame($expected,
  96. $u->isExcluded($path)
  97. );
  98. }
  99. public function providePathsForTestIsExcluded() {
  100. return array(
  101. array('files_encryption/foo.txt', true),
  102. array('test/foo.txt', false),
  103. array('/user1/files_encryption/foo.txt', true),
  104. array('/user1/files/foo.txt', false),
  105. );
  106. }
  107. public function isExcludedCallback() {
  108. $args = func_get_args();
  109. if ($args[0] === 'user1') {
  110. return true;
  111. }
  112. return false;
  113. }
  114. }