|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Handler\SignEngine; |
| 10 | + |
| 11 | +use OCA\Libresign\Handler\SignEngine\TSA; |
| 12 | +use OCA\Libresign\Vendor\phpseclib3\File\ASN1; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class TSATest extends TestCase { |
| 16 | + private TSA $tsa; |
| 17 | + |
| 18 | + protected function setUp(): void { |
| 19 | + parent::setUp(); |
| 20 | + $this->tsa = new TSA(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testConstructorLoadsOIDs(): void { |
| 24 | + $tsa = new TSA(); |
| 25 | + |
| 26 | + $this->assertEquals('2.16.840.1.101.3.4.2.1', ASN1::getOID('id-sha256')); |
| 27 | + $this->assertEquals('1.3.14.3.2.26', ASN1::getOID('id-sha1')); |
| 28 | + } |
| 29 | + |
| 30 | + public function testExtractWithEmptyArray(): void { |
| 31 | + $result = $this->tsa->extract([]); |
| 32 | + |
| 33 | + $this->assertIsArray($result); |
| 34 | + $this->assertArrayHasKey('genTime', $result); |
| 35 | + $this->assertArrayHasKey('policy', $result); |
| 36 | + $this->assertArrayHasKey('serialNumber', $result); |
| 37 | + $this->assertArrayHasKey('cnHints', $result); |
| 38 | + $this->assertArrayHasKey('displayName', $result); |
| 39 | + |
| 40 | + $this->assertNull($result['policy']); |
| 41 | + $this->assertNull($result['serialNumber']); |
| 42 | + $this->assertEquals([], $result['cnHints']); |
| 43 | + } |
| 44 | + |
| 45 | + public function testGetSigninTimeWithNoSigningTime(): void { |
| 46 | + $result = $this->tsa->getSigninTime([]); |
| 47 | + $this->assertNull($result); |
| 48 | + } |
| 49 | + |
| 50 | + public function testGetSigninTimeWithInvalidStructure(): void { |
| 51 | + $invalidStructure = [ |
| 52 | + [ |
| 53 | + 'type' => ASN1::TYPE_SEQUENCE, |
| 54 | + 'content' => 'invalid', |
| 55 | + ] |
| 56 | + ]; |
| 57 | + |
| 58 | + $result = $this->tsa->getSigninTime($invalidStructure); |
| 59 | + $this->assertNull($result); |
| 60 | + } |
| 61 | + |
| 62 | + public function testExtractWithMinimalValidStructure(): void { |
| 63 | + $mockTstInfo = $this->createMockTstInfo(); |
| 64 | + |
| 65 | + $result = $this->tsa->extract($mockTstInfo); |
| 66 | + |
| 67 | + $this->assertIsArray($result); |
| 68 | + $this->assertArrayHasKey('genTime', $result); |
| 69 | + $this->assertArrayHasKey('policy', $result); |
| 70 | + $this->assertArrayHasKey('serialNumber', $result); |
| 71 | + $this->assertArrayHasKey('cnHints', $result); |
| 72 | + $this->assertArrayHasKey('displayName', $result); |
| 73 | + } |
| 74 | + |
| 75 | + public function testExtractWithMalformedData(): void { |
| 76 | + $malformedData = [ |
| 77 | + [ |
| 78 | + 'type' => 'invalid_type', |
| 79 | + 'content' => 'malformed_content', |
| 80 | + ] |
| 81 | + ]; |
| 82 | + |
| 83 | + $result = $this->tsa->extract($malformedData); |
| 84 | + |
| 85 | + $this->assertIsArray($result); |
| 86 | + $this->assertArrayHasKey('genTime', $result); |
| 87 | + } |
| 88 | + |
| 89 | + public function testDisplayNameGeneration(): void { |
| 90 | + $mockStructureWithCN = $this->createMockStructureWithCommonName(); |
| 91 | + |
| 92 | + $result = $this->tsa->extract($mockStructureWithCN); |
| 93 | + |
| 94 | + $this->assertIsArray($result); |
| 95 | + $this->assertArrayHasKey('displayName', $result); |
| 96 | + $this->assertIsString($result['displayName']); |
| 97 | + } |
| 98 | + |
| 99 | + private function createMockTstInfo(): array { |
| 100 | + return [ |
| 101 | + [ |
| 102 | + 'type' => ASN1::TYPE_SEQUENCE, |
| 103 | + 'content' => [ |
| 104 | + [ |
| 105 | + 'type' => ASN1::TYPE_OBJECT_IDENTIFIER, |
| 106 | + 'content' => '1.2.840.113549.1.9.16.2.14' // id-aa-timeStampToken |
| 107 | + ], |
| 108 | + [ |
| 109 | + 'type' => ASN1::TYPE_SET, |
| 110 | + 'content' => [ |
| 111 | + [ |
| 112 | + 'type' => ASN1::TYPE_SEQUENCE, |
| 113 | + 'content' => [] |
| 114 | + ] |
| 115 | + ] |
| 116 | + ] |
| 117 | + ] |
| 118 | + ] |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + private function createMockStructureWithCommonName(): array { |
| 123 | + return [ |
| 124 | + [ |
| 125 | + 'type' => ASN1::TYPE_SEQUENCE, |
| 126 | + 'content' => [ |
| 127 | + [ |
| 128 | + 'type' => ASN1::TYPE_OBJECT_IDENTIFIER, |
| 129 | + 'content' => '2.5.4.3' // commonName OID |
| 130 | + ], |
| 131 | + [ |
| 132 | + 'type' => ASN1::TYPE_UTF8_STRING, |
| 133 | + 'content' => 'Test CA', |
| 134 | + ] |
| 135 | + ] |
| 136 | + ] |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + public function testSerialNumberHandling(): void { |
| 141 | + $result = $this->tsa->extract([]); |
| 142 | + |
| 143 | + $this->assertNull($result['serialNumber']); |
| 144 | + } |
| 145 | + |
| 146 | + public function testFallbackParsingWithEmptyData(): void { |
| 147 | + $result = $this->tsa->extract([ |
| 148 | + [ |
| 149 | + 'type' => 'invalid', |
| 150 | + 'content' => null |
| 151 | + ] |
| 152 | + ]); |
| 153 | + |
| 154 | + $this->assertIsArray($result); |
| 155 | + $this->assertNull($result['genTime']); |
| 156 | + $this->assertIsArray($result['cnHints']); |
| 157 | + $this->assertIsString($result['displayName']); |
| 158 | + } |
| 159 | + |
| 160 | + public function testOIDMapping(): void { |
| 161 | + $structureWithMultipleOIDs = [ |
| 162 | + [ |
| 163 | + 'type' => ASN1::TYPE_SEQUENCE, |
| 164 | + 'content' => [ |
| 165 | + [ |
| 166 | + 'type' => ASN1::TYPE_OBJECT_IDENTIFIER, |
| 167 | + 'content' => '2.5.4.6', // countryName |
| 168 | + ], |
| 169 | + [ |
| 170 | + 'type' => ASN1::TYPE_PRINTABLE_STRING, |
| 171 | + 'content' => 'US', |
| 172 | + ], |
| 173 | + [ |
| 174 | + 'type' => ASN1::TYPE_OBJECT_IDENTIFIER, |
| 175 | + 'content' => '2.5.4.10', // organizationName |
| 176 | + ], |
| 177 | + [ |
| 178 | + 'type' => ASN1::TYPE_UTF8_STRING, |
| 179 | + 'content' => 'Test Organization', |
| 180 | + ] |
| 181 | + ] |
| 182 | + ] |
| 183 | + ]; |
| 184 | + |
| 185 | + $result = $this->tsa->extract($structureWithMultipleOIDs); |
| 186 | + |
| 187 | + $this->assertIsArray($result); |
| 188 | + $this->assertArrayHasKey('cnHints', $result); |
| 189 | + $this->assertIsArray($result['cnHints']); |
| 190 | + } |
| 191 | +} |
0 commit comments