1
0

CertificateTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(): void {
  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 testOpenSslTrustedCertificateFormat(): void {
  33. $trustedCertificate = file_get_contents(__DIR__ . '/../../data/certificates/openSslTrustedCertificate.crt');
  34. $certificate = new Certificate($trustedCertificate, 'TrustedCertificate');
  35. $this->assertSame('thawte, Inc.', $certificate->getOrganization());
  36. }
  37. public function testCertificateStartingWithFileReference(): void {
  38. $this->expectException(\Exception::class);
  39. $this->expectExceptionMessage('Certificate could not get parsed.');
  40. new Certificate('file://' . __DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
  41. }
  42. public function testGetName(): void {
  43. $this->assertSame('GoodCertificate', $this->goodCertificate->getName());
  44. $this->assertSame('BadCertificate', $this->invalidCertificate->getName());
  45. }
  46. public function testGetCommonName(): void {
  47. $this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
  48. $this->assertSame(null, $this->invalidCertificate->getCommonName());
  49. }
  50. public function testGetOrganization(): void {
  51. $this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
  52. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
  53. }
  54. public function testGetIssueDate(): void {
  55. $expected = new \DateTime('2015-08-27 20:03:42 GMT');
  56. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
  57. $expected = new \DateTime('2015-08-27 20:19:13 GMT');
  58. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
  59. }
  60. public function testGetExpireDate(): void {
  61. $expected = new \DateTime('2025-08-24 20:03:42 GMT');
  62. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
  63. $expected = new \DateTime('2025-08-24 20:19:13 GMT');
  64. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
  65. $expected = new \DateTime('2014-08-28 09:12:43 GMT');
  66. $this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
  67. }
  68. public function testIsExpired(): void {
  69. $this->assertSame(false, $this->goodCertificate->isExpired());
  70. $this->assertSame(false, $this->invalidCertificate->isExpired());
  71. $this->assertSame(true, $this->expiredCertificate->isExpired());
  72. }
  73. public function testGetIssuerName(): void {
  74. $this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
  75. $this->assertSame(null, $this->invalidCertificate->getIssuerName());
  76. $this->assertSame(null, $this->expiredCertificate->getIssuerName());
  77. }
  78. public function testGetIssuerOrganization(): void {
  79. $this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
  80. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
  81. $this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
  82. }
  83. }