CertificateTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace Test\Security;
  9. use OC\Security\Certificate;
  10. class CertificateTest extends \Test\TestCase {
  11. /** @var Certificate That contains a valid certificate */
  12. protected $goodCertificate;
  13. /** @var Certificate That contains an invalid certificate */
  14. protected $invalidCertificate;
  15. /** @var Certificate That contains an expired certificate */
  16. protected $expiredCertificate;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
  20. $this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
  21. $badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
  22. $this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
  23. $expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
  24. $this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
  25. }
  26. public function testBogusData() {
  27. $this->expectException(\Exception::class);
  28. $this->expectExceptionMessage('Certificate could not get parsed.');
  29. $certificate = new Certificate('foo', 'bar');
  30. $certificate->getIssueDate();
  31. }
  32. public function testCertificateStartingWithFileReference() {
  33. $this->expectException(\Exception::class);
  34. $this->expectExceptionMessage('Certificate could not get parsed.');
  35. new Certificate('file://'.__DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
  36. }
  37. public function testGetName() {
  38. $this->assertSame('GoodCertificate', $this->goodCertificate->getName());
  39. $this->assertSame('BadCertificate', $this->invalidCertificate->getName());
  40. }
  41. public function testGetCommonName() {
  42. $this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
  43. $this->assertSame(null, $this->invalidCertificate->getCommonName());
  44. }
  45. public function testGetOrganization() {
  46. $this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
  47. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
  48. }
  49. public function testGetIssueDate() {
  50. $expected = new \DateTime('2015-08-27 20:03:42 GMT');
  51. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
  52. $expected = new \DateTime('2015-08-27 20:19:13 GMT');
  53. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
  54. }
  55. public function testGetExpireDate() {
  56. $expected = new \DateTime('2025-08-24 20:03:42 GMT');
  57. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
  58. $expected = new \DateTime('2025-08-24 20:19:13 GMT');
  59. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
  60. $expected = new \DateTime('2014-08-28 09:12:43 GMT');
  61. $this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
  62. }
  63. public function testIsExpired() {
  64. $this->assertSame(false, $this->goodCertificate->isExpired());
  65. $this->assertSame(false, $this->invalidCertificate->isExpired());
  66. $this->assertSame(true, $this->expiredCertificate->isExpired());
  67. }
  68. public function testGetIssuerName() {
  69. $this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
  70. $this->assertSame(null, $this->invalidCertificate->getIssuerName());
  71. $this->assertSame(null, $this->expiredCertificate->getIssuerName());
  72. }
  73. public function testGetIssuerOrganization() {
  74. $this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
  75. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
  76. $this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
  77. }
  78. }