CertificateTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Security;
  23. use OC\Security\Certificate;
  24. class CertificateTest extends \Test\TestCase {
  25. /** @var Certificate That contains a valid certificate */
  26. protected $goodCertificate;
  27. /** @var Certificate That contains an invalid certificate */
  28. protected $invalidCertificate;
  29. /** @var Certificate That contains an expired certificate */
  30. protected $expiredCertificate;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
  34. $this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
  35. $badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
  36. $this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
  37. $expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
  38. $this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
  39. }
  40. public function testBogusData() {
  41. $this->expectException(\Exception::class);
  42. $this->expectExceptionMessage('Certificate could not get parsed.');
  43. $certificate = new Certificate('foo', 'bar');
  44. $certificate->getIssueDate();
  45. }
  46. public function testOpenSslTrustedCertificateFormat(): void {
  47. $trustedCertificate = file_get_contents(__DIR__ . '/../../data/certificates/openSslTrustedCertificate.crt');
  48. $certificate = new Certificate($trustedCertificate, 'TrustedCertificate');
  49. $this->assertSame('thawte, Inc.', $certificate->getOrganization());
  50. }
  51. public function testCertificateStartingWithFileReference() {
  52. $this->expectException(\Exception::class);
  53. $this->expectExceptionMessage('Certificate could not get parsed.');
  54. new Certificate('file://'.__DIR__ . '/../../data/certificates/goodCertificate.crt', 'bar');
  55. }
  56. public function testGetName() {
  57. $this->assertSame('GoodCertificate', $this->goodCertificate->getName());
  58. $this->assertSame('BadCertificate', $this->invalidCertificate->getName());
  59. }
  60. public function testGetCommonName() {
  61. $this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
  62. $this->assertSame(null, $this->invalidCertificate->getCommonName());
  63. }
  64. public function testGetOrganization() {
  65. $this->assertSame('ownCloud Security', $this->goodCertificate->getOrganization());
  66. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
  67. }
  68. public function testGetIssueDate() {
  69. $expected = new \DateTime('2015-08-27 20:03:42 GMT');
  70. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
  71. $expected = new \DateTime('2015-08-27 20:19:13 GMT');
  72. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
  73. }
  74. public function testGetExpireDate() {
  75. $expected = new \DateTime('2025-08-24 20:03:42 GMT');
  76. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
  77. $expected = new \DateTime('2025-08-24 20:19:13 GMT');
  78. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
  79. $expected = new \DateTime('2014-08-28 09:12:43 GMT');
  80. $this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
  81. }
  82. public function testIsExpired() {
  83. $this->assertSame(false, $this->goodCertificate->isExpired());
  84. $this->assertSame(false, $this->invalidCertificate->isExpired());
  85. $this->assertSame(true, $this->expiredCertificate->isExpired());
  86. }
  87. public function testGetIssuerName() {
  88. $this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
  89. $this->assertSame(null, $this->invalidCertificate->getIssuerName());
  90. $this->assertSame(null, $this->expiredCertificate->getIssuerName());
  91. }
  92. public function testGetIssuerOrganization() {
  93. $this->assertSame('ownCloud Security', $this->goodCertificate->getIssuerOrganization());
  94. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
  95. $this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
  96. }
  97. }