certificatemanager.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. class CertificateManagerTest extends \Test\TestCase {
  10. /** @var CertificateManager */
  11. private $certificateManager;
  12. /** @var String */
  13. private $username;
  14. /** @var \OC\User\User */
  15. private $user;
  16. protected function setUp() {
  17. parent::setUp();
  18. $this->username = $this->getUniqueID('', 20);
  19. OC_User::createUser($this->username, $this->getUniqueID('', 20));
  20. \OC_Util::tearDownFS();
  21. \OC_User::setUserId('');
  22. \OC\Files\Filesystem::tearDown();
  23. \OC_Util::setupFS($this->username);
  24. $this->certificateManager = new CertificateManager($this->username, new \OC\Files\View());
  25. }
  26. protected function tearDown() {
  27. \OC_User::deleteUser($this->username);
  28. parent::tearDown();
  29. }
  30. protected function assertEqualsArrays($expected, $actual) {
  31. sort($expected);
  32. sort($actual);
  33. $this->assertEquals($expected, $actual);
  34. }
  35. function testListCertificates() {
  36. // Test empty certificate bundle
  37. $this->assertSame(array(), $this->certificateManager->listCertificates());
  38. // Add some certificates
  39. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  40. $certificateStore = array();
  41. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  42. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  43. // Add another certificates
  44. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  45. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  46. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  47. }
  48. /**
  49. * @expectedException \Exception
  50. * @expectedExceptionMessage Certificate could not get parsed.
  51. */
  52. function testAddInvalidCertificate() {
  53. $this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
  54. }
  55. function testAddDangerousFile() {
  56. $this->assertFalse($this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), '.htaccess'));
  57. $this->assertFalse($this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), '../../foo.txt'));
  58. }
  59. function testRemoveDangerousFile() {
  60. $this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
  61. }
  62. function testRemoveExistingFile() {
  63. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  64. $this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
  65. }
  66. function testGetCertificateBundle() {
  67. $this->assertSame('/' . $this->username . '/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
  68. }
  69. }