Skip to content

Commit

Permalink
Add QuickCheck tests for cert behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 20, 2022
1 parent 2ebdbaa commit c387a22
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions p2p/transport/webtransport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,9 @@ func TestSNIIsSent(t *testing.T) {

func TestServerSendsBackValidCert(t *testing.T) {
const clockSkewAllowance = time.Hour
var maxTimeoutErrors = 10

require.NoError(t, quick.Check(func(timeSinceUnixEpoch time.Duration, keySeed int64, clientSkew time.Duration) bool {
require.NoError(t, quick.Check(func(timeSinceUnixEpoch time.Duration, keySeed int64, randomClientSkew time.Duration) bool {
if timeSinceUnixEpoch < 0 {
timeSinceUnixEpoch = -timeSinceUnixEpoch
}
Expand All @@ -605,7 +606,7 @@ func TestServerSendsBackValidCert(t *testing.T) {
timeSinceUnixEpoch += time.Hour * 24 * 365
start := time.UnixMilli(timeSinceUnixEpoch.Milliseconds())

clientSkew = clientSkew % clockSkewAllowance
randomClientSkew = randomClientSkew % clockSkewAllowance

cl := clock.NewMock()
cl.Set(start)
Expand All @@ -629,8 +630,6 @@ func TestServerSendsBackValidCert(t *testing.T) {
panic(err)
}

fmt.Println("Addr:", l.Addr().String())

conn, err := quic.DialAddr(l.Addr().String(), &tls.Config{
NextProtos: []string{"h3"},
InsecureSkipVerify: true,
Expand All @@ -640,46 +639,33 @@ func TestServerSendsBackValidCert(t *testing.T) {
if err != nil {
return err
}
clientTime := cl.Now().Add(clientSkew)
if clientTime.After(cert.NotAfter) || clientTime.Before(cert.NotBefore) {
return fmt.Errorf("Times are not valid: server_now=%v client_now=%v certstart=%v certend=%v", cl.Now().UTC(), clientTime.UTC(), cert.NotBefore.UTC(), cert.NotAfter.UTC())

for _, clientSkew := range []time.Duration{randomClientSkew, -clockSkewAllowance, clockSkewAllowance} {
clientTime := cl.Now().Add(clientSkew)
if clientTime.After(cert.NotAfter) || clientTime.Before(cert.NotBefore) {
return fmt.Errorf("Times are not valid: server_now=%v client_now=%v certstart=%v certend=%v", cl.Now().UTC(), clientTime.UTC(), cert.NotBefore.UTC(), cert.NotAfter.UTC())
}
}

}
return nil
},
}, &quic.Config{
// Tracer: qlog.NewTracer(getLogWriter),
})
}, &quic.Config{MaxIdleTimeout: time.Second})
_ = getLogWriter

if err != nil {
if _, ok := err.(*quic.IdleTimeoutError); ok {
// Sporadic timeout errors
// return true
maxTimeoutErrors -= 1
fmt.Println("Timeout")
if maxTimeoutErrors <= 0 {
fmt.Println("Too many timeout errors")
}
// Sporadic timeout errors on macOS
return true
}
fmt.Println("err:", err)
_, err2 := quic.DialAddr(l.Addr().String(), &tls.Config{
NextProtos: []string{"h3"},
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
for _, c := range rawCerts {
cert, err := x509.ParseCertificate(c)
if err != nil {
return err
}
clientTime := cl.Now().Add(clientSkew)
if clientTime.After(cert.NotAfter) || clientTime.Before(cert.NotBefore) {
return fmt.Errorf("Times are not valid: server_now=%v client_now=%v certstart=%v certend=%v", cl.Now().UTC(), clientTime.UTC(), cert.NotBefore.UTC(), cert.NotAfter.UTC())
}

// TODO: Check that the cert is not in the first hour or last hour
}
return nil
},
}, &quic.Config{
// Tracer: qlog.NewTracer(getLogWriter),
})
fmt.Println("err2:", err2)
// Print the error so we see what happened, since we only return
// true/false to quickcheck
fmt.Println("Error:", err)
return false
}
defer conn.CloseWithError(0, "")
Expand Down

0 comments on commit c387a22

Please sign in to comment.