CertificateTest.php 4.6 KB

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