keymanager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Sam Tuke <samtuke@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. //require_once "PHPUnit/Framework/TestCase.php";
  9. require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
  10. require_once realpath( dirname(__FILE__).'/../lib/crypt.php' );
  11. require_once realpath( dirname(__FILE__).'/../lib/keymanager.php' );
  12. require_once realpath( dirname(__FILE__).'/../lib/proxy.php' );
  13. require_once realpath( dirname(__FILE__).'/../lib/stream.php' );
  14. require_once realpath( dirname(__FILE__).'/../lib/util.php' );
  15. require_once realpath( dirname(__FILE__).'/../appinfo/app.php' );
  16. use OCA\Encryption;
  17. // This has to go here because otherwise session errors arise, and the private
  18. // encryption key needs to be saved in the session
  19. \OC_User::login( 'admin', 'admin' );
  20. class Test_Keymanager extends \PHPUnit_Framework_TestCase {
  21. function setUp() {
  22. \OC_FileProxy::$enabled = false;
  23. // set content for encrypting / decrypting in tests
  24. $this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
  25. $this->dataShort = 'hats';
  26. $this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' );
  27. $this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
  28. $this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
  29. $this->randomKey = Encryption\Crypt::generateKey();
  30. $keypair = Encryption\Crypt::createKeypair();
  31. $this->genPublicKey = $keypair['publicKey'];
  32. $this->genPrivateKey = $keypair['privateKey'];
  33. $this->view = new \OC_FilesystemView( '/' );
  34. \OC_User::setUserId( 'admin' );
  35. $this->userId = 'admin';
  36. $this->pass = 'admin';
  37. \OC_Filesystem::init( '/' );
  38. \OC_Filesystem::mount( 'OC_Filestorage_Local', array('datadir' => \OC_User::getHome($this->userId)), '/' );
  39. }
  40. function tearDown(){
  41. \OC_FileProxy::$enabled = true;
  42. }
  43. function testGetPrivateKey() {
  44. $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
  45. // Will this length vary? Perhaps we should use a range instead
  46. $this->assertEquals( 2296, strlen( $key ) );
  47. }
  48. function testGetPublicKey() {
  49. $key = Encryption\Keymanager::getPublicKey( $this->view, $this->userId );
  50. $this->assertEquals( 451, strlen( $key ) );
  51. $this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $key, 0, 26 ) );
  52. }
  53. function testSetFileKey() {
  54. # NOTE: This cannot be tested until we are able to break out
  55. # of the FileSystemView data directory root
  56. $key = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->randomKey, 'hat' );
  57. $path = 'unittest-'.time().'txt';
  58. //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
  59. Encryption\Keymanager::setFileKey( $this->view, $path, $this->userId, $key['key'] );
  60. }
  61. // /**
  62. // * @depends testGetPrivateKey
  63. // */
  64. // function testGetPrivateKey_decrypt() {
  65. //
  66. // $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
  67. //
  68. // # TODO: replace call to Crypt with a mock object?
  69. // $decrypted = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->passphrase );
  70. //
  71. // $this->assertEquals( 1704, strlen( $decrypted ) );
  72. //
  73. // $this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $decrypted, 0, 27 ) );
  74. //
  75. // }
  76. function testGetUserKeys() {
  77. $keys = Encryption\Keymanager::getUserKeys( $this->view, $this->userId );
  78. $this->assertEquals( 451, strlen( $keys['publicKey'] ) );
  79. $this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $keys['publicKey'], 0, 26 ) );
  80. $this->assertEquals( 2296, strlen( $keys['privateKey'] ) );
  81. }
  82. function testGetPublicKeys() {
  83. # TODO: write me
  84. }
  85. function testGetFileKey() {
  86. // Encryption\Keymanager::getFileKey( $this->view, $this->userId, $this->filePath );
  87. }
  88. }