@@ -245,4 +245,57 @@ public static function dataReadCertificate(): array {
245245 ],
246246 ];
247247 }
248+
249+ public function testSerialNumberGeneration (): void {
250+ $ rootInstance = $ this ->getInstance ();
251+ $ rootInstance ->generateRootCert ('' , []);
252+
253+ $ signerInstance = $ this ->getInstance ();
254+ $ signerInstance ->setCommonName ('Test User ' );
255+ $ signerInstance ->setPassword ('123456 ' );
256+
257+ $ certificate = $ signerInstance ->generateCertificate ();
258+ $ parsed = $ signerInstance ->readCertificate ($ certificate , '123456 ' );
259+
260+ $ this ->assertArrayHasKey ('serialNumber ' , $ parsed , 'Certificate should have serialNumber field ' );
261+ $ this ->assertArrayHasKey ('serialNumberHex ' , $ parsed , 'Certificate should have serialNumberHex field ' );
262+ $ this ->assertNotNull ($ parsed ['serialNumber ' ], 'Serial number should not be null ' );
263+ $ this ->assertNotNull ($ parsed ['serialNumberHex ' ], 'Serial number hex should not be null ' );
264+
265+ $ this ->assertNotEquals ('0 ' , $ parsed ['serialNumber ' ], 'Serial number should not be zero ' );
266+ $ this ->assertNotEquals ('00 ' , $ parsed ['serialNumberHex ' ], 'Serial number hex should not be zero ' );
267+
268+ $ serialInt = (int )$ parsed ['serialNumber ' ];
269+ $ this ->assertGreaterThanOrEqual (1000000 , $ serialInt , 'Serial number should be >= 1000000 ' );
270+ $ this ->assertLessThanOrEqual (2147483647 , $ serialInt , 'Serial number should be <= 2147483647 ' );
271+
272+ $ this ->assertIsNumeric ($ parsed ['serialNumber ' ], 'Serial number should be numeric ' );
273+ $ this ->assertMatchesRegularExpression ('/^[0-9A-Fa-f]+$/ ' , $ parsed ['serialNumberHex ' ], 'Serial number hex should contain only hex characters ' );
274+ }
275+
276+ public function testUniqueSerialNumbers (): void {
277+ $ rootInstance = $ this ->getInstance ();
278+ $ rootInstance ->generateRootCert ('' , []);
279+
280+ $ serialNumbers = [];
281+ $ numCertificates = 3 ;
282+
283+ for ($ i = 0 ; $ i < $ numCertificates ; $ i ++) {
284+ $ signerInstance = $ this ->getInstance ();
285+ $ signerInstance ->setCommonName ("Test Certificate $ i " );
286+ $ signerInstance ->setPassword ('123456 ' );
287+ $ certificateContent = $ signerInstance ->generateCertificate ();
288+ $ parsed = $ signerInstance ->readCertificate ($ certificateContent , '123456 ' );
289+
290+ $ serialNumber = $ parsed ['serialNumber ' ];
291+
292+ $ this ->assertNotEquals ('0 ' , $ serialNumber , "Certificate $ i should not have serial number 0 " );
293+
294+ $ this ->assertNotContains ($ serialNumber , $ serialNumbers , "Certificate $ i should have unique serial number " );
295+
296+ $ serialNumbers [] = $ serialNumber ;
297+ }
298+
299+ $ this ->assertCount ($ numCertificates , array_unique ($ serialNumbers ), 'All serial numbers should be unique ' );
300+ }
248301}
0 commit comments