You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

certificatemanager.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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->user = \OC::$server->getUserManager()->get($this->username);
  25. $this->certificateManager = new CertificateManager($this->user);
  26. }
  27. protected function tearDown() {
  28. \OC_User::deleteUser($this->username);
  29. parent::tearDown();
  30. }
  31. protected function assertEqualsArrays($expected, $actual) {
  32. sort($expected);
  33. sort($actual);
  34. $this->assertEquals($expected, $actual);
  35. }
  36. function testListCertificates() {
  37. // Test empty certificate bundle
  38. $this->assertSame(array(), $this->certificateManager->listCertificates());
  39. // Add some certificates
  40. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  41. $certificateStore = array();
  42. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  43. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  44. // Add another certificates
  45. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  46. $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
  47. $this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
  48. }
  49. /**
  50. * @expectedException \Exception
  51. * @expectedExceptionMessage Certificate could not get parsed.
  52. */
  53. function testAddInvalidCertificate() {
  54. $this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
  55. }
  56. function testAddDangerousFile() {
  57. $this->assertFalse($this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), '.htaccess'));
  58. $this->assertFalse($this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/expiredCertificate.crt'), '../../foo.txt'));
  59. }
  60. function testRemoveDangerousFile() {
  61. $this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
  62. }
  63. function testRemoveExistingFile() {
  64. $this->certificateManager->addCertificate(file_get_contents(__DIR__.'/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
  65. $this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
  66. }
  67. function testGetCertificateBundle() {
  68. $this->assertSame($this->user->getHome().'/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
  69. }
  70. }