CertificateTest.php 4.5 KB

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