From 254be2ab2e13257af1c05276c063e9bed635e44b Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 16 Nov 2022 14:09:58 +1300 Subject: [PATCH] webtransport: tame log output of tests --- .../webtransport/cert_manager_test.go | 47 +++++----- p2p/transport/webtransport/crypto_test.go | 90 +++++++++---------- 2 files changed, 65 insertions(+), 72 deletions(-) diff --git a/p2p/transport/webtransport/cert_manager_test.go b/p2p/transport/webtransport/cert_manager_test.go index 4b4550bb9d..26ba0c032b 100644 --- a/p2p/transport/webtransport/cert_manager_test.go +++ b/p2p/transport/webtransport/cert_manager_test.go @@ -3,7 +3,6 @@ package libp2pwebtransport import ( "crypto/sha256" "crypto/tls" - "fmt" "testing" "testing/quick" "time" @@ -115,30 +114,28 @@ func TestDeterministicCertsAcrossReboots(t *testing.T) { // Run this test 100 times to make sure it's deterministic runs := 100 for i := 0; i < runs; i++ { - t.Run(fmt.Sprintf("Run=%d", i), func(t *testing.T) { - cl := clock.NewMock() - priv, _, err := test.SeededTestKeyPair(crypto.Ed25519, 256, 0) - require.NoError(t, err) - m, err := newCertManager(priv, cl) - require.NoError(t, err) - defer m.Close() - - conf := m.GetConfig() - require.Len(t, conf.Certificates, 1) - oldCerts := m.serializedCertHashes - - m.Close() - - cl.Add(time.Hour) - // reboot - m, err = newCertManager(priv, cl) - require.NoError(t, err) - defer m.Close() - - newCerts := m.serializedCertHashes - - require.Equal(t, oldCerts, newCerts) - }) + cl := clock.NewMock() + priv, _, err := test.SeededTestKeyPair(crypto.Ed25519, 256, 0) + require.NoError(t, err) + m, err := newCertManager(priv, cl) + require.NoError(t, err) + defer m.Close() + + conf := m.GetConfig() + require.Len(t, conf.Certificates, 1) + oldCerts := m.serializedCertHashes + + m.Close() + + cl.Add(time.Hour) + // reboot + m, err = newCertManager(priv, cl) + require.NoError(t, err) + defer m.Close() + + newCerts := m.serializedCertHashes + + require.Equal(t, oldCerts, newCerts) } } diff --git a/p2p/transport/webtransport/crypto_test.go b/p2p/transport/webtransport/crypto_test.go index 143cd6de45..ba439c28af 100644 --- a/p2p/transport/webtransport/crypto_test.go +++ b/p2p/transport/webtransport/crypto_test.go @@ -138,25 +138,23 @@ func TestDeterministicCertHashes(t *testing.T) { // Run this test 1000 times since we want to make sure the signatures are deterministic runs := 1000 for i := 0; i < runs; i++ { - t.Run(fmt.Sprintf("Run=%d", i), func(t *testing.T) { - zeroSeed := [32]byte{} - priv, _, err := ic.GenerateEd25519Key(bytes.NewReader(zeroSeed[:])) - require.NoError(t, err) - cert, certPriv, err := generateCert(priv, time.Time{}, time.Time{}.Add(time.Hour*24*14)) - require.NoError(t, err) - - keyBytes, err := x509.MarshalECPrivateKey(certPriv) - require.NoError(t, err) - - cert2, certPriv2, err := generateCert(priv, time.Time{}, time.Time{}.Add(time.Hour*24*14)) - require.NoError(t, err) - - require.Equal(t, cert2.Signature, cert.Signature) - require.Equal(t, cert2.Raw, cert.Raw) - keyBytes2, err := x509.MarshalECPrivateKey(certPriv2) - require.NoError(t, err) - require.Equal(t, keyBytes, keyBytes2) - }) + zeroSeed := [32]byte{} + priv, _, err := ic.GenerateEd25519Key(bytes.NewReader(zeroSeed[:])) + require.NoError(t, err) + cert, certPriv, err := generateCert(priv, time.Time{}, time.Time{}.Add(time.Hour*24*14)) + require.NoError(t, err) + + keyBytes, err := x509.MarshalECPrivateKey(certPriv) + require.NoError(t, err) + + cert2, certPriv2, err := generateCert(priv, time.Time{}, time.Time{}.Add(time.Hour*24*14)) + require.NoError(t, err) + + require.Equal(t, cert2.Signature, cert.Signature) + require.Equal(t, cert2.Raw, cert.Raw) + keyBytes2, err := x509.MarshalECPrivateKey(certPriv2) + require.NoError(t, err) + require.Equal(t, keyBytes, keyBytes2) } } @@ -168,33 +166,31 @@ func TestDeterministicSig(t *testing.T) { // Run this test 1000 times since we want to make sure the signatures are deterministic runs := 1000 for i := 0; i < runs; i++ { - t.Run(fmt.Sprintf("Run=%d", i), func(t *testing.T) { - zeroSeed := [32]byte{} - deterministicHKDFReader := newDeterministicReader(zeroSeed[:], nil, deterministicCertInfo) - b := [1024]byte{} - io.ReadFull(deterministicHKDFReader, b[:]) - caPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), deterministicHKDFReader) - require.NoError(t, err) - - sig, err := caPrivateKey.Sign(deterministicHKDFReader, b[:], crypto.SHA256) - require.NoError(t, err) - - deterministicHKDFReader = newDeterministicReader(zeroSeed[:], nil, deterministicCertInfo) - b2 := [1024]byte{} - io.ReadFull(deterministicHKDFReader, b2[:]) - caPrivateKey2, err := ecdsa.GenerateKey(elliptic.P256(), deterministicHKDFReader) - require.NoError(t, err) - - sig2, err := caPrivateKey2.Sign(deterministicHKDFReader, b2[:], crypto.SHA256) - require.NoError(t, err) - - keyBytes, err := x509.MarshalECPrivateKey(caPrivateKey) - require.NoError(t, err) - keyBytes2, err := x509.MarshalECPrivateKey(caPrivateKey2) - require.NoError(t, err) - - require.Equal(t, sig, sig2) - require.Equal(t, keyBytes, keyBytes2) - }) + zeroSeed := [32]byte{} + deterministicHKDFReader := newDeterministicReader(zeroSeed[:], nil, deterministicCertInfo) + b := [1024]byte{} + io.ReadFull(deterministicHKDFReader, b[:]) + caPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), deterministicHKDFReader) + require.NoError(t, err) + + sig, err := caPrivateKey.Sign(deterministicHKDFReader, b[:], crypto.SHA256) + require.NoError(t, err) + + deterministicHKDFReader = newDeterministicReader(zeroSeed[:], nil, deterministicCertInfo) + b2 := [1024]byte{} + io.ReadFull(deterministicHKDFReader, b2[:]) + caPrivateKey2, err := ecdsa.GenerateKey(elliptic.P256(), deterministicHKDFReader) + require.NoError(t, err) + + sig2, err := caPrivateKey2.Sign(deterministicHKDFReader, b2[:], crypto.SHA256) + require.NoError(t, err) + + keyBytes, err := x509.MarshalECPrivateKey(caPrivateKey) + require.NoError(t, err) + keyBytes2, err := x509.MarshalECPrivateKey(caPrivateKey2) + require.NoError(t, err) + + require.Equal(t, sig, sig2) + require.Equal(t, keyBytes, keyBytes2) } }