@@ -28,38 +28,52 @@ import com.yubico.internal.util.BinaryUtil
2828import com .yubico .internal .util .CertificateParser
2929import com .yubico .webauthn .TestAuthenticator
3030import com .yubico .webauthn .data .ByteArray
31+ import com .yubico .webauthn .data .Generators .arbitraryByteArray
32+ import com .yubico .webauthn .data .Generators .shrinkByteArray
3133import org .bouncycastle .asn1 .DEROctetString
3234import org .junit .runner .RunWith
3335import org .scalatest .funspec .AnyFunSpec
3436import org .scalatest .matchers .should .Matchers
3537import org .scalatestplus .junit .JUnitRunner
38+ import org .scalatestplus .scalacheck .ScalaCheckDrivenPropertyChecks
3639
3740import java .security .cert .X509Certificate
3841import scala .jdk .OptionConverters .RichOptional
3942
4043@ RunWith (classOf [JUnitRunner ])
41- class CertificateUtilSpec extends AnyFunSpec with Matchers {
44+ class CertificateUtilSpec
45+ extends AnyFunSpec
46+ with Matchers
47+ with ScalaCheckDrivenPropertyChecks {
4248 describe(" parseFidoSerNumExtension" ) {
4349 val idFidoGenCeSernum = " 1.3.6.1.4.1.45724.1.1.2"
4450
4551 it(" correctly parses the id-fido-gen-ce-sernum extension." ) {
46- val (cert, _): (X509Certificate , _) = TestAuthenticator
47- .generateAttestationCertificate(
48- extensions = List (
49- (
50- idFidoGenCeSernum,
51- false ,
52- new DEROctetString (Array [Byte ](0 , 1 , 2 , 3 )),
52+ forAll(
53+ // 500-byte long serial numbers are not realistic, but would be valid DER data.
54+ sizeRange(500 )
55+ ) {
56+ // Using Array[Byte] here causes an (almost) infinite loop in the shrinker in case of failure.
57+ // See: https://github.com/typelevel/scalacheck/issues/968#issuecomment-2594018791
58+ sernum : ByteArray =>
59+ val (cert, _): (X509Certificate , _) = TestAuthenticator
60+ .generateAttestationCertificate(
61+ extensions = List (
62+ (
63+ idFidoGenCeSernum,
64+ false ,
65+ new DEROctetString (sernum.getBytes),
66+ )
67+ )
5368 )
54- )
55- )
5669
57- val result =
58- CertificateUtil
59- .parseFidoSerNumExtension(cert)
60- .toScala
61- .map(new ByteArray (_))
62- result should equal(Some (ByteArray .fromHex(" 00010203" )))
70+ val result =
71+ CertificateUtil
72+ .parseFidoSerNumExtension(cert)
73+ .toScala
74+ .map(new ByteArray (_))
75+ result should equal(Some (sernum))
76+ }
6377 }
6478
6579 it(" returns empty when cert has no id-fido-gen-ce-sernum extension." ) {
0 commit comments