Skip to content

Commit f10f462

Browse files
authored
sa: streamline use of dates in test (#7924)
Add mustTime and mustTimestamp, each of which parses a time in a simple format and panics if it cannot be parsed. Also, make the intent of each check in the GetRevokedCerts tests a little clearer by starting with a basicRequest, and then defining variations in terms of that request. Fix the "different issuer" case in `TestGetRevokedCerts`, which was not actually setting a different issuer.
1 parent 673b93c commit f10f462

File tree

1 file changed

+63
-79
lines changed

1 file changed

+63
-79
lines changed

sa/sa_test.go

Lines changed: 63 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ var (
6464
}`
6565
)
6666

67+
func mustTime(s string) time.Time {
68+
t, err := time.Parse("2006-01-02 15:04", s)
69+
if err != nil {
70+
panic(fmt.Sprintf("parsing %q: %s", s, err))
71+
}
72+
return t.UTC()
73+
}
74+
75+
func mustTimestamp(s string) *timestamppb.Timestamp {
76+
return timestamppb.New(mustTime(s))
77+
}
78+
6779
type fakeServerStream[T any] struct {
6880
grpc.ServerStream
6981
output chan<- *T
@@ -95,7 +107,7 @@ func initSA(t testing.TB) (*SQLStorageAuthority, clock.FakeClock, func()) {
95107
}
96108

97109
fc := clock.NewFake()
98-
fc.Set(time.Date(2015, 3, 4, 5, 0, 0, 0, time.UTC))
110+
fc.Set(mustTime("2015-03-04 05:00"))
99111

100112
saro, err := NewSQLStorageAuthorityRO(dbMap, dbIncidentsMap, metrics.NoopRegisterer, 1, 0, fc, log)
101113
if err != nil {
@@ -116,7 +128,7 @@ func createWorkingRegistration(t testing.TB, sa *SQLStorageAuthority) *corepb.Re
116128
reg, err := sa.NewRegistration(context.Background(), &corepb.Registration{
117129
Key: []byte(theKey),
118130
Contact: []string{"mailto:[email protected]"},
119-
CreatedAt: timestamppb.New(time.Date(2003, 5, 10, 0, 0, 0, 0, time.UTC)),
131+
CreatedAt: mustTimestamp("2003-05-10 00:00"),
120132
Status: string(core.StatusValid),
121133
})
122134
if err != nil {
@@ -408,11 +420,11 @@ func TestAddPrecertificate(t *testing.T) {
408420

409421
// Add the cert as a precertificate
410422
regID := reg.Id
411-
issuedTime := time.Date(2018, 4, 1, 7, 0, 0, 0, time.UTC)
423+
issuedTime := mustTimestamp("2018-04-01 07:00")
412424
_, err := sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
413425
Der: testCert.Raw,
414426
RegID: regID,
415-
Issued: timestamppb.New(issuedTime),
427+
Issued: issuedTime,
416428
IssuerNameID: 1,
417429
})
418430
test.AssertNotError(t, err, "Couldn't add test cert")
@@ -435,7 +447,7 @@ func TestAddPrecertificate(t *testing.T) {
435447
_, err = sa.AddCertificate(ctx, &sapb.AddCertificateRequest{
436448
Der: testCert.Raw,
437449
RegID: regID,
438-
Issued: timestamppb.New(issuedTime),
450+
Issued: issuedTime,
439451
})
440452
test.AssertNotError(t, err, "unexpected err adding final cert after precert")
441453
}
@@ -448,11 +460,11 @@ func TestAddPrecertificateNoOCSP(t *testing.T) {
448460
_, testCert := test.ThrowAwayCert(t, clk)
449461

450462
regID := reg.Id
451-
issuedTime := time.Date(2018, 4, 1, 7, 0, 0, 0, time.UTC)
463+
issuedTime := mustTimestamp("2018-04-01 07:00")
452464
_, err := sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
453465
Der: testCert.Raw,
454466
RegID: regID,
455-
Issued: timestamppb.New(issuedTime),
467+
Issued: issuedTime,
456468
IssuerNameID: 1,
457469
})
458470
test.AssertNotError(t, err, "Couldn't add test cert")
@@ -496,11 +508,10 @@ func TestAddPrecertificateIncomplete(t *testing.T) {
496508

497509
// Add the cert as a precertificate
498510
regID := reg.Id
499-
issuedTime := time.Date(2018, 4, 1, 7, 0, 0, 0, time.UTC)
500511
_, err := sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
501512
Der: testCert.Raw,
502513
RegID: regID,
503-
Issued: timestamppb.New(issuedTime),
514+
Issued: mustTimestamp("2018-04-01 07:00"),
504515
// Leaving out IssuerNameID
505516
})
506517

@@ -858,7 +869,7 @@ func (e *queryRecorder) QueryContext(ctx context.Context, query string, args ...
858869
func TestAddIssuedNames(t *testing.T) {
859870
serial := big.NewInt(1)
860871
expectedSerial := "000000000000000000000000000000000001"
861-
notBefore := time.Date(2018, 2, 14, 12, 0, 0, 0, time.UTC)
872+
notBefore := mustTime("2018-02-14 12:00")
862873
expectedNotBefore := notBefore.Truncate(24 * time.Hour)
863874
placeholdersPerName := "(?,?,?,?)"
864875
baseQuery := "INSERT INTO issuedNames (reversedName,serial,notBefore,renewal) VALUES"
@@ -2528,7 +2539,7 @@ func TestFinalizeAuthorization2(t *testing.T) {
25282539
sa, fc, cleanUp := initSA(t)
25292540
defer cleanUp()
25302541

2531-
fc.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
2542+
fc.Set(mustTime("2021-01-01 00:00"))
25322543

25332544
authzID := createPendingAuthorization(t, sa, "aaa", fc.Now().Add(time.Hour))
25342545
expires := fc.Now().Add(time.Hour * 2).UTC()
@@ -2600,7 +2611,7 @@ func TestRehydrateHostPort(t *testing.T) {
26002611
sa, fc, cleanUp := initSA(t)
26012612
defer cleanUp()
26022613

2603-
fc.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
2614+
fc.Set(mustTime("2021-01-01 00:00"))
26042615

26052616
expires := fc.Now().Add(time.Hour * 2).UTC()
26062617
attemptedAt := fc.Now()
@@ -3284,73 +3295,59 @@ func TestGetRevokedCerts(t *testing.T) {
32843295
return entriesReceived, err
32853296
}
32863297

3287-
// Asking for revoked certs now should return no results.
3288-
expiresAfter := time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3289-
expiresBefore := time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
3290-
revokedBefore := time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
3291-
count, err := countRevokedCerts(&sapb.GetRevokedCertsRequest{
3298+
// The basic request covers a time range that should include this certificate.
3299+
basicRequest := &sapb.GetRevokedCertsRequest{
32923300
IssuerNameID: 1,
3293-
ExpiresAfter: timestamppb.New(expiresAfter),
3294-
ExpiresBefore: timestamppb.New(expiresBefore),
3295-
RevokedBefore: timestamppb.New(revokedBefore),
3296-
})
3301+
ExpiresAfter: mustTimestamp("2023-03-01 00:00"),
3302+
ExpiresBefore: mustTimestamp("2023-04-01 00:00"),
3303+
RevokedBefore: mustTimestamp("2023-04-01 00:00"),
3304+
}
3305+
count, err := countRevokedCerts(basicRequest)
32973306
test.AssertNotError(t, err, "zero rows shouldn't result in error")
32983307
test.AssertEquals(t, count, 0)
32993308

33003309
// Revoke the certificate.
3301-
date := time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC)
33023310
_, err = sa.RevokeCertificate(context.Background(), &sapb.RevokeCertificateRequest{
33033311
IssuerID: 1,
33043312
Serial: core.SerialToString(eeCert.SerialNumber),
3305-
Date: timestamppb.New(date),
3313+
Date: mustTimestamp("2023-01-01 00:00"),
33063314
Reason: 1,
33073315
Response: []byte{1, 2, 3},
33083316
})
33093317
test.AssertNotError(t, err, "failed to revoke test cert")
33103318

33113319
// Asking for revoked certs now should return one result.
3312-
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3313-
IssuerNameID: 1,
3314-
ExpiresAfter: timestamppb.New(expiresAfter),
3315-
ExpiresBefore: timestamppb.New(expiresBefore),
3316-
RevokedBefore: timestamppb.New(revokedBefore),
3317-
})
3320+
count, err = countRevokedCerts(basicRequest)
33183321
test.AssertNotError(t, err, "normal usage shouldn't result in error")
33193322
test.AssertEquals(t, count, 1)
33203323

33213324
// Asking for revoked certs with an old RevokedBefore should return no results.
3322-
expiresAfter = time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3323-
expiresBefore = time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
3324-
revokedBefore = time.Date(2020, time.March, 1, 0, 0, 0, 0, time.UTC)
33253325
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
33263326
IssuerNameID: 1,
3327-
ExpiresAfter: timestamppb.New(expiresAfter),
3328-
ExpiresBefore: timestamppb.New(expiresBefore),
3329-
RevokedBefore: timestamppb.New(revokedBefore),
3327+
ExpiresAfter: basicRequest.ExpiresAfter,
3328+
ExpiresBefore: basicRequest.ExpiresBefore,
3329+
RevokedBefore: mustTimestamp("2020-03-01 00:00"),
33303330
})
33313331
test.AssertNotError(t, err, "zero rows shouldn't result in error")
33323332
test.AssertEquals(t, count, 0)
33333333

33343334
// Asking for revoked certs in a time period that does not cover this cert's
33353335
// notAfter timestamp should return zero results.
3336-
expiresAfter = time.Date(2022, time.March, 1, 0, 0, 0, 0, time.UTC)
3337-
expiresBefore = time.Date(2022, time.April, 1, 0, 0, 0, 0, time.UTC)
3338-
revokedBefore = time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
33393336
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
33403337
IssuerNameID: 1,
3341-
ExpiresAfter: timestamppb.New(expiresAfter),
3342-
ExpiresBefore: timestamppb.New(expiresBefore),
3343-
RevokedBefore: timestamppb.New(revokedBefore),
3338+
ExpiresAfter: mustTimestamp("2022-03-01 00:00"),
3339+
ExpiresBefore: mustTimestamp("2022-04-01 00:00"),
3340+
RevokedBefore: mustTimestamp("2023-04-01 00:00"),
33443341
})
33453342
test.AssertNotError(t, err, "zero rows shouldn't result in error")
33463343
test.AssertEquals(t, count, 0)
33473344

33483345
// Asking for revoked certs from a different issuer should return zero results.
33493346
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3350-
IssuerNameID: 1,
3351-
ExpiresAfter: timestamppb.New(time.Date(2022, time.March, 1, 0, 0, 0, 0, time.UTC)),
3352-
ExpiresBefore: timestamppb.New(time.Date(2022, time.April, 1, 0, 0, 0, 0, time.UTC)),
3353-
RevokedBefore: timestamppb.New(time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)),
3347+
IssuerNameID: 5678,
3348+
ExpiresAfter: basicRequest.ExpiresAfter,
3349+
ExpiresBefore: basicRequest.ExpiresBefore,
3350+
RevokedBefore: basicRequest.RevokedBefore,
33543351
})
33553352
test.AssertNotError(t, err, "zero rows shouldn't result in error")
33563353
test.AssertEquals(t, count, 0)
@@ -3404,25 +3401,25 @@ func TestGetRevokedCertsByShard(t *testing.T) {
34043401
return entriesReceived, err
34053402
}
34063403

3407-
// Asking for revoked certs now should return no results.
3408-
expiresAfter := time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3409-
revokedBefore := time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
3410-
count, err := countRevokedCerts(&sapb.GetRevokedCertsRequest{
3404+
// The basic request covers a time range and shard that should include this certificate.
3405+
basicRequest := &sapb.GetRevokedCertsRequest{
34113406
IssuerNameID: 1,
34123407
ShardIdx: 9,
3413-
ExpiresAfter: timestamppb.New(expiresAfter),
3414-
RevokedBefore: timestamppb.New(revokedBefore),
3415-
})
3408+
ExpiresAfter: mustTimestamp("2023-03-01 00:00"),
3409+
RevokedBefore: mustTimestamp("2023-04-01 00:00"),
3410+
}
3411+
3412+
// Nothing's been revoked yet. Count should be zero.
3413+
count, err := countRevokedCerts(basicRequest)
34163414
test.AssertNotError(t, err, "zero rows shouldn't result in error")
34173415
test.AssertEquals(t, count, 0)
34183416

34193417
// Revoke the certificate, providing the ShardIdx so it gets written into
34203418
// both the certificateStatus and revokedCertificates tables.
3421-
date := time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC)
34223419
_, err = sa.RevokeCertificate(context.Background(), &sapb.RevokeCertificateRequest{
34233420
IssuerID: 1,
34243421
Serial: core.SerialToString(eeCert.SerialNumber),
3425-
Date: timestamppb.New(date),
3422+
Date: mustTimestamp("2023-01-01 00:00"),
34263423
Reason: 1,
34273424
Response: []byte{1, 2, 3},
34283425
ShardIdx: 9,
@@ -3437,49 +3434,36 @@ func TestGetRevokedCertsByShard(t *testing.T) {
34373434
test.AssertEquals(t, c.Int64, int64(1))
34383435

34393436
// Asking for revoked certs now should return one result.
3440-
expiresAfter = time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3441-
revokedBefore = time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
3442-
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3443-
IssuerNameID: 1,
3444-
ShardIdx: 9,
3445-
ExpiresAfter: timestamppb.New(expiresAfter),
3446-
RevokedBefore: timestamppb.New(revokedBefore),
3447-
})
3437+
count, err = countRevokedCerts(basicRequest)
34483438
test.AssertNotError(t, err, "normal usage shouldn't result in error")
34493439
test.AssertEquals(t, count, 1)
34503440

34513441
// Asking for revoked certs from a different issuer should return zero results.
3452-
expiresAfter = time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3453-
revokedBefore = time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
34543442
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3455-
IssuerNameID: 2,
3456-
ShardIdx: 9,
3457-
ExpiresAfter: timestamppb.New(expiresAfter),
3458-
RevokedBefore: timestamppb.New(revokedBefore),
3443+
IssuerNameID: 5678,
3444+
ShardIdx: basicRequest.ShardIdx,
3445+
ExpiresAfter: basicRequest.ExpiresAfter,
3446+
RevokedBefore: basicRequest.RevokedBefore,
34593447
})
34603448
test.AssertNotError(t, err, "zero rows shouldn't result in error")
34613449
test.AssertEquals(t, count, 0)
34623450

34633451
// Asking for revoked certs from a different shard should return zero results.
3464-
expiresAfter = time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3465-
revokedBefore = time.Date(2023, time.April, 1, 0, 0, 0, 0, time.UTC)
34663452
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3467-
IssuerNameID: 1,
3453+
IssuerNameID: basicRequest.IssuerNameID,
34683454
ShardIdx: 8,
3469-
ExpiresAfter: timestamppb.New(expiresAfter),
3470-
RevokedBefore: timestamppb.New(revokedBefore),
3455+
ExpiresAfter: basicRequest.ExpiresAfter,
3456+
RevokedBefore: basicRequest.RevokedBefore,
34713457
})
34723458
test.AssertNotError(t, err, "zero rows shouldn't result in error")
34733459
test.AssertEquals(t, count, 0)
34743460

34753461
// Asking for revoked certs with an old RevokedBefore should return no results.
3476-
expiresAfter = time.Date(2023, time.March, 1, 0, 0, 0, 0, time.UTC)
3477-
revokedBefore = time.Date(2020, time.March, 1, 0, 0, 0, 0, time.UTC)
34783462
count, err = countRevokedCerts(&sapb.GetRevokedCertsRequest{
3479-
IssuerNameID: 1,
3480-
ShardIdx: 9,
3481-
ExpiresAfter: timestamppb.New(expiresAfter),
3482-
RevokedBefore: timestamppb.New(revokedBefore),
3463+
IssuerNameID: basicRequest.IssuerNameID,
3464+
ShardIdx: basicRequest.ShardIdx,
3465+
ExpiresAfter: basicRequest.ExpiresAfter,
3466+
RevokedBefore: mustTimestamp("2020-03-01 00:00"),
34833467
})
34843468
test.AssertNotError(t, err, "zero rows shouldn't result in error")
34853469
test.AssertEquals(t, count, 0)

0 commit comments

Comments
 (0)