certificatemanager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@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. use \OC\Security\CertificateManager;
  9. /**
  10. * Class CertificateManagerTest
  11. *
  12. * @group DB
  13. */
  14. class CertificateManagerTest extends \Test\TestCase {
  15. /** @var CertificateManager */
  16. private $certificateManager;
  17. /** @var String */
  18. private $username;
  19. protected function setUp() {
  20. parent::setUp();
  21. $this->username = $this->getUniqueID('', 20);
  22. OC_User::createUser($this->username, $this->getUniqueID('', 20));
  23. \OC_Util::tearDownFS();
  24. \OC_User::setUserId('');
  25. \OC\Files\Filesystem::tearDown();
  26. \OC_Util::setupFS($this->username);
  27. $config = $this->getMock('OCP\IConfig');
  28. $config->expects($this->any())->method('getSystemValue')
  29. ->with('installed', false)->willReturn(true);
  30. $this->certificateManager = new CertificateManager($this->username, new \OC\Files\View(), $config);
  31. }
  32. protected function tearDown() {
  33. \OC_User::deleteUser($this->username);
  34. parent::tearDown();
  35. }
  36. protected function assertEqualsArrays($expected, $actual) {
  37. sort($expected);
  38. sort($actual);
  39. $this->assertEquals($expected, $actual);
  40. }
  41. function testListCertificates() {
  42. // Test empty certificate bundle
  43. $this->assertSame(array(), $this->certificateManager->listCertificates());
  44. // Add some certificates
  45. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  46. $certificateStore = array();
  47. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  48. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  49. // Add another certificates
  50. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  51. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  52. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  53. }
  54. /**
  55. * @expectedException \Exception
  56. * @expectedExceptionMessage Certificate could not get parsed.
  57. */
  58. function testAddInvalidCertificate() {
  59. $this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function dangerousFileProvider() {
  65. return [
  66. ['.htaccess'],
  67. ['../../foo.txt'],
  68. ['..\..\foo.txt'],
  69. ];
  70. }
  71. /**
  72. * @expectedException \Exception
  73. * @expectedExceptionMessage Filename is not valid
  74. * @dataProvider dangerousFileProvider
  75. * @param string $filename
  76. */
  77. function testAddDangerousFile($filename) {
  78. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), $filename);
  79. }
  80. function testRemoveDangerousFile() {
  81. $this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
  82. }
  83. function testRemoveExistingFile() {
  84. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  85. $this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
  86. }
  87. function testGetCertificateBundle() {
  88. $this->assertSame('/' . $this->username . '/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
  89. }
  90. }