EncryptionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace Test\Files\Stream;
  3. use OC\Files\View;
  4. use OC\Memcache\ArrayCache;
  5. class EncryptionTest extends \Test\TestCase {
  6. /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit_Framework_MockObject_MockObject */
  7. private $encryptionModule;
  8. /**
  9. * @param string $fileName
  10. * @param string $mode
  11. * @param integer $unencryptedSize
  12. * @return resource
  13. */
  14. protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC\Files\Stream\Encryption') {
  15. clearstatcache();
  16. $size = filesize($fileName);
  17. $source = fopen($fileName, $mode);
  18. $internalPath = $fileName;
  19. $fullPath = $fileName;
  20. $header = [];
  21. $uid = '';
  22. $this->encryptionModule = $this->buildMockModule();
  23. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  24. ->disableOriginalConstructor()->getMock();
  25. $encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
  26. ->disableOriginalConstructor()->getMock();
  27. $config = $this->getMockBuilder('\OCP\IConfig')
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $arrayCache = $this->createMock(ArrayCache::class);
  31. $groupManager = $this->getMockBuilder('\OC\Group\Manager')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $file = $this->getMockBuilder('\OC\Encryption\File')
  35. ->disableOriginalConstructor()
  36. ->setMethods(['getAccessList'])
  37. ->getMock();
  38. $file->expects($this->any())->method('getAccessList')->willReturn([]);
  39. $util = $this->getMockBuilder('\OC\Encryption\Util')
  40. ->setMethods(['getUidAndFilename'])
  41. ->setConstructorArgs([new View(), new \OC\User\Manager($config), $groupManager, $config, $arrayCache])
  42. ->getMock();
  43. $util->expects($this->any())
  44. ->method('getUidAndFilename')
  45. ->willReturn(['user1', $internalPath]);
  46. return $wrapper::wrap($source, $internalPath,
  47. $fullPath, $header, $uid, $this->encryptionModule, $storage, $encStorage,
  48. $util, $file, $mode, $size, $unencryptedSize, 8192, $wrapper);
  49. }
  50. /**
  51. * @dataProvider dataProviderStreamOpen()
  52. */
  53. public function testStreamOpen($mode,
  54. $fullPath,
  55. $fileExists,
  56. $expectedSharePath,
  57. $expectedSize,
  58. $expectedUnencryptedSize,
  59. $expectedReadOnly) {
  60. // build mocks
  61. $encryptionModuleMock = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
  62. ->disableOriginalConstructor()->getMock();
  63. $encryptionModuleMock->expects($this->once())
  64. ->method('getUnencryptedBlockSize')->willReturn(99);
  65. $encryptionModuleMock->expects($this->once())
  66. ->method('begin')->willReturn(true);
  67. $storageMock = $this->getMockBuilder('\OC\Files\Storage\Storage')
  68. ->disableOriginalConstructor()->getMock();
  69. $storageMock->expects($this->once())->method('file_exists')->willReturn($fileExists);
  70. $fileMock = $this->getMockBuilder('\OC\Encryption\File')
  71. ->disableOriginalConstructor()->getMock();
  72. $fileMock->expects($this->once())->method('getAccessList')
  73. ->will($this->returnCallback(function($sharePath) use ($expectedSharePath) {
  74. $this->assertSame($expectedSharePath, $sharePath);
  75. return array();
  76. }));
  77. $utilMock = $this->getMockBuilder('\OC\Encryption\Util')
  78. ->disableOriginalConstructor()->getMock();
  79. $utilMock->expects($this->any())
  80. ->method('getHeaderSize')
  81. ->willReturn(8192);
  82. // get a instance of the stream wrapper
  83. $streamWrapper = $this->getMockBuilder('\OC\Files\Stream\Encryption')
  84. ->setMethods(['loadContext', 'writeHeader', 'skipHeader'])->disableOriginalConstructor()->getMock();
  85. // set internal properties of the stream wrapper
  86. $stream = new \ReflectionClass('\OC\Files\Stream\Encryption');
  87. $encryptionModule = $stream->getProperty('encryptionModule');
  88. $encryptionModule->setAccessible(true);
  89. $encryptionModule->setValue($streamWrapper, $encryptionModuleMock);
  90. $encryptionModule->setAccessible(false);
  91. $storage = $stream->getProperty('storage');
  92. $storage->setAccessible(true);
  93. $storage->setValue($streamWrapper, $storageMock);
  94. $storage->setAccessible(false);
  95. $file = $stream->getProperty('file');
  96. $file->setAccessible(true);
  97. $file->setValue($streamWrapper, $fileMock);
  98. $file->setAccessible(false);
  99. $util = $stream->getProperty('util');
  100. $util->setAccessible(true);
  101. $util->setValue($streamWrapper, $utilMock);
  102. $util->setAccessible(false);
  103. $fullPathP = $stream->getProperty('fullPath');
  104. $fullPathP->setAccessible(true);
  105. $fullPathP->setValue($streamWrapper, $fullPath);
  106. $fullPathP->setAccessible(false);
  107. $header = $stream->getProperty('header');
  108. $header->setAccessible(true);
  109. $header->setValue($streamWrapper, array());
  110. $header->setAccessible(false);
  111. $this->invokePrivate($streamWrapper, 'signed', [true]);
  112. // call stream_open, that's the method we want to test
  113. $dummyVar = 'foo';
  114. $streamWrapper->stream_open('', $mode, '', $dummyVar);
  115. // check internal properties
  116. $size = $stream->getProperty('size');
  117. $size->setAccessible(true);
  118. $this->assertSame($expectedSize,
  119. $size->getValue($streamWrapper)
  120. );
  121. $size->setAccessible(false);
  122. $unencryptedSize = $stream->getProperty('unencryptedSize');
  123. $unencryptedSize->setAccessible(true);
  124. $this->assertSame($expectedUnencryptedSize,
  125. $unencryptedSize->getValue($streamWrapper)
  126. );
  127. $unencryptedSize->setAccessible(false);
  128. $readOnly = $stream->getProperty('readOnly');
  129. $readOnly->setAccessible(true);
  130. $this->assertSame($expectedReadOnly,
  131. $readOnly->getValue($streamWrapper)
  132. );
  133. $readOnly->setAccessible(false);
  134. }
  135. public function dataProviderStreamOpen() {
  136. return array(
  137. array('r', '/foo/bar/test.txt', true, '/foo/bar/test.txt', null, null, true),
  138. array('r', '/foo/bar/test.txt', false, '/foo/bar', null, null, true),
  139. array('w', '/foo/bar/test.txt', true, '/foo/bar/test.txt', 8192, 0, false),
  140. );
  141. }
  142. public function testWriteRead() {
  143. $fileName = tempnam("/tmp", "FOO");
  144. $stream = $this->getStream($fileName, 'w+', 0);
  145. $this->assertEquals(6, fwrite($stream, 'foobar'));
  146. fclose($stream);
  147. $stream = $this->getStream($fileName, 'r', 6);
  148. $this->assertEquals('foobar', fread($stream, 100));
  149. fclose($stream);
  150. $stream = $this->getStream($fileName, 'r+', 6);
  151. $this->assertEquals(3, fwrite($stream, 'bar'));
  152. fclose($stream);
  153. $stream = $this->getStream($fileName, 'r', 6);
  154. $this->assertEquals('barbar', fread($stream, 100));
  155. fclose($stream);
  156. unlink($fileName);
  157. }
  158. public function testRewind() {
  159. $fileName = tempnam("/tmp", "FOO");
  160. $stream = $this->getStream($fileName, 'w+', 0);
  161. $this->assertEquals(6, fwrite($stream, 'foobar'));
  162. $this->assertEquals(TRUE, rewind($stream));
  163. $this->assertEquals('foobar', fread($stream, 100));
  164. $this->assertEquals(TRUE, rewind($stream));
  165. $this->assertEquals(3, fwrite($stream, 'bar'));
  166. fclose($stream);
  167. $stream = $this->getStream($fileName, 'r', 6);
  168. $this->assertEquals('barbar', fread($stream, 100));
  169. fclose($stream);
  170. unlink($fileName);
  171. }
  172. public function testSeek() {
  173. $fileName = tempnam("/tmp", "FOO");
  174. $stream = $this->getStream($fileName, 'w+', 0);
  175. $this->assertEquals(6, fwrite($stream, 'foobar'));
  176. $this->assertEquals(0, fseek($stream, 3));
  177. $this->assertEquals(6, fwrite($stream, 'foobar'));
  178. fclose($stream);
  179. $stream = $this->getStream($fileName, 'r', 9);
  180. $this->assertEquals('foofoobar', fread($stream, 100));
  181. $this->assertEquals(-1, fseek($stream, 10));
  182. $this->assertEquals(0, fseek($stream, 9));
  183. $this->assertEquals(-1, fseek($stream, -10, SEEK_CUR));
  184. $this->assertEquals(0, fseek($stream, -9, SEEK_CUR));
  185. $this->assertEquals(-1, fseek($stream, -10, SEEK_END));
  186. $this->assertEquals(0, fseek($stream, -9, SEEK_END));
  187. fclose($stream);
  188. unlink($fileName);
  189. }
  190. function dataFilesProvider() {
  191. return [
  192. ['lorem-big.txt'],
  193. ['block-aligned.txt'],
  194. ['block-aligned-plus-one.txt'],
  195. ];
  196. }
  197. /**
  198. * @dataProvider dataFilesProvider
  199. */
  200. public function testWriteReadBigFile($testFile) {
  201. $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
  202. // write it
  203. $fileName = tempnam("/tmp", "FOO");
  204. $stream = $this->getStream($fileName, 'w+', 0);
  205. // while writing the file from the beginning to the end we should never try
  206. // to read parts of the file. This should only happen for write operations
  207. // in the middle of a file
  208. $this->encryptionModule->expects($this->never())->method('decrypt');
  209. fwrite($stream, $expectedData);
  210. fclose($stream);
  211. // read it all
  212. $stream = $this->getStream($fileName, 'r', strlen($expectedData));
  213. $data = stream_get_contents($stream);
  214. fclose($stream);
  215. $this->assertEquals($expectedData, $data);
  216. // another read test with a loop like we do in several places:
  217. $stream = $this->getStream($fileName, 'r', strlen($expectedData));
  218. $data = '';
  219. while (!feof($stream)) {
  220. $data .= fread($stream, 8192);
  221. }
  222. fclose($stream);
  223. $this->assertEquals($expectedData, $data);
  224. unlink($fileName);
  225. }
  226. /**
  227. * simulate a non-seekable storage
  228. *
  229. * @dataProvider dataFilesProvider
  230. */
  231. public function testWriteToNonSeekableStorage($testFile) {
  232. $wrapper = $this->getMockBuilder('\OC\Files\Stream\Encryption')
  233. ->setMethods(['parentSeekStream'])->getMock();
  234. $wrapper->expects($this->any())->method('parentSeekStream')->willReturn(false);
  235. $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
  236. // write it
  237. $fileName = tempnam("/tmp", "FOO");
  238. $stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper');
  239. // while writing the file from the beginning to the end we should never try
  240. // to read parts of the file. This should only happen for write operations
  241. // in the middle of a file
  242. $this->encryptionModule->expects($this->never())->method('decrypt');
  243. fwrite($stream, $expectedData);
  244. fclose($stream);
  245. // read it all
  246. $stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper');
  247. $data = stream_get_contents($stream);
  248. fclose($stream);
  249. $this->assertEquals($expectedData, $data);
  250. // another read test with a loop like we do in several places:
  251. $stream = $this->getStream($fileName, 'r', strlen($expectedData));
  252. $data = '';
  253. while (!feof($stream)) {
  254. $data .= fread($stream, 8192);
  255. }
  256. fclose($stream);
  257. $this->assertEquals($expectedData, $data);
  258. unlink($fileName);
  259. }
  260. /**
  261. * @return \PHPUnit_Framework_MockObject_MockObject
  262. */
  263. protected function buildMockModule() {
  264. $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
  265. ->disableOriginalConstructor()
  266. ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll', 'isReadyForUser'])
  267. ->getMock();
  268. $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
  269. $encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
  270. $encryptionModule->expects($this->any())->method('begin')->willReturn([]);
  271. $encryptionModule->expects($this->any())->method('end')->willReturn('');
  272. $encryptionModule->expects($this->any())->method('isReadable')->willReturn(true);
  273. $encryptionModule->expects($this->any())->method('encrypt')->willReturnCallback(function($data) {
  274. // simulate different block size by adding some padding to the data
  275. if (isset($data[6125])) {
  276. return str_pad($data, 8192, 'X');
  277. }
  278. // last block
  279. return $data;
  280. });
  281. $encryptionModule->expects($this->any())->method('decrypt')->willReturnCallback(function($data) {
  282. if (isset($data[8191])) {
  283. return substr($data, 0, 6126);
  284. }
  285. // last block
  286. return $data;
  287. });
  288. $encryptionModule->expects($this->any())->method('update')->willReturn(true);
  289. $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
  290. $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(6126);
  291. return $encryptionModule;
  292. }
  293. }