certificate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. use \OC\Security\Certificate;
  9. class CertificateTest extends \Test\TestCase {
  10. /** @var Certificate That contains a valid certificate */
  11. protected $goodCertificate;
  12. /** @var Certificate That contains an invalid certificate */
  13. protected $invalidCertificate;
  14. /** @var Certificate That contains an expired certificate */
  15. protected $expiredCertificate;
  16. protected function setUp() {
  17. parent::setUp();
  18. $goodCertificate = file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt');
  19. $this->goodCertificate = new Certificate($goodCertificate, 'GoodCertificate');
  20. $badCertificate = file_get_contents(__DIR__ . '/../../data/certificates/badCertificate.crt');
  21. $this->invalidCertificate = new Certificate($badCertificate, 'BadCertificate');
  22. $expiredCertificate = file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt');
  23. $this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
  24. }
  25. /**
  26. * @expectedException \Exception
  27. * @expectedExceptionMessage Certificate could not get parsed.
  28. */
  29. function testBogusData() {
  30. new Certificate('foo', 'bar');
  31. }
  32. function testGetName() {
  33. $this->assertSame('GoodCertificate', $this->goodCertificate->getName());
  34. $this->assertSame('BadCertificate', $this->invalidCertificate->getName());
  35. }
  36. function testGetCommonName() {
  37. $this->assertSame('security.owncloud.com', $this->goodCertificate->getCommonName());
  38. $this->assertSame(null, $this->invalidCertificate->getCommonName());
  39. }
  40. function testGetOrganization() {
  41. $this->assertSame('ownCloud Inc.', $this->goodCertificate->getOrganization());
  42. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getOrganization());
  43. }
  44. function testGetIssueDate() {
  45. $expected = new DateTime('2014-08-27 08:45:52 GMT');
  46. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getIssueDate()->getTimestamp());
  47. $expected = new DateTime('2014-08-27 08:48:51 GMT');
  48. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getIssueDate()->getTimestamp());
  49. }
  50. function testGetExpireDate() {
  51. $expected = new DateTime('2015-08-27 08:45:52 GMT');
  52. $this->assertEquals($expected->getTimestamp(), $this->goodCertificate->getExpireDate()->getTimestamp());
  53. $expected = new DateTime('2015-08-27 08:48:51 GMT');
  54. $this->assertEquals($expected->getTimestamp(), $this->invalidCertificate->getExpireDate()->getTimestamp());
  55. $expected = new DateTime('2014-08-28 09:12:43 GMT');
  56. $this->assertEquals($expected->getTimestamp(), $this->expiredCertificate->getExpireDate()->getTimestamp());
  57. }
  58. /**
  59. * Obviously the following test case might fail after 2015-08-27, just create a new certificate with longer validity then
  60. */
  61. function testIsExpired() {
  62. $this->assertSame(false, $this->goodCertificate->isExpired());
  63. $this->assertSame(false, $this->invalidCertificate->isExpired());
  64. $this->assertSame(true, $this->expiredCertificate->isExpired());
  65. }
  66. function testGetIssuerName() {
  67. $this->assertSame('security.owncloud.com', $this->goodCertificate->getIssuerName());
  68. $this->assertSame(null, $this->invalidCertificate->getIssuerName());
  69. $this->assertSame(null, $this->expiredCertificate->getIssuerName());
  70. }
  71. function testGetIssuerOrganization() {
  72. $this->assertSame('ownCloud Inc.', $this->goodCertificate->getIssuerOrganization());
  73. $this->assertSame('Internet Widgits Pty Ltd', $this->invalidCertificate->getIssuerOrganization());
  74. $this->assertSame('Internet Widgits Pty Ltd', $this->expiredCertificate->getIssuerOrganization());
  75. }
  76. }