@@ -11,13 +11,11 @@ import (
1111 "encoding/asn1"
1212 "encoding/hex"
1313 "fmt"
14- "os"
1514 "testing"
1615 "time"
1716
1817 cfsslConfig "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cloudflare/cfssl/config"
1918 ocspConfig "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/cloudflare/cfssl/ocsp/config"
20- _ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
2119 "github.com/letsencrypt/boulder/mocks"
2220
2321 "github.com/letsencrypt/boulder/core"
@@ -339,22 +337,37 @@ const profileName = "ee"
339337const caKeyFile = "../test/test-ca.key"
340338const caCertFile = "../test/test-ca.pem"
341339
342- func TestMain (m * testing.M ) {
340+ // TODO(jmhodges): change this to boulder_ca_test database
341+ var dbConnStr = "mysql+tcp://boulder@localhost:3306/boulder_test"
343342
344- os .Exit (m .Run ())
345- }
346-
347- func setup (t * testing.T ) (cadb core.CertificateAuthorityDatabase , storageAuthority core.StorageAuthority , caConfig Config ) {
343+ func setup (t * testing.T ) (core.CertificateAuthorityDatabase , core.StorageAuthority , Config , func ()) {
348344 // Create an SA
349- ssa , err := sa .NewSQLStorageAuthority ("sqlite3" , ":memory:" )
350- test .AssertNotError (t , err , "Failed to create SA" )
351- ssa .CreateTablesIfNotExists ()
352- storageAuthority = ssa
345+ dbMap , err := sa .NewDbMap (dbConnStr )
346+ if err != nil {
347+ t .Fatalf ("Failed to create dbMap: %s" , err )
348+ }
349+ ssa , err := sa .NewSQLStorageAuthority (dbMap )
350+ if err != nil {
351+ t .Fatalf ("Failed to create SA: %s" , err )
352+ }
353+ if err = ssa .CreateTablesIfNotExists (); err != nil {
354+ t .Fatalf ("Failed to create tables: %s" , err )
355+ }
356+ if err = dbMap .TruncateTables (); err != nil {
357+ t .Fatalf ("Failed to truncate tables: %s" , err )
358+ }
353359
354- cadb , _ = mocks .NewMockCertificateAuthorityDatabase ()
360+ cadb , caDBCleanUp := caDBImpl (t )
361+ cleanUp := func () {
362+ if err = dbMap .TruncateTables (); err != nil {
363+ t .Fatalf ("Failed to truncate tables after the test: %s" , err )
364+ }
365+ dbMap .Db .Close ()
366+ caDBCleanUp ()
367+ }
355368
356369 // Create a CA
357- caConfig = Config {
370+ caConfig : = Config {
358371 Profile : profileName ,
359372 SerialPrefix : 17 ,
360373 Key : KeyConfig {
@@ -399,18 +412,21 @@ func setup(t *testing.T) (cadb core.CertificateAuthorityDatabase, storageAuthori
399412 },
400413 },
401414 }
402- return cadb , storageAuthority , caConfig
415+ return cadb , ssa , caConfig , cleanUp
403416}
404417
405418func TestFailNoSerial (t * testing.T ) {
406- cadb , _ , caConfig := setup (t )
419+ cadb , _ , caConfig , cleanUp := setup (t )
420+ defer cleanUp ()
421+
407422 caConfig .SerialPrefix = 0
408423 _ , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
409424 test .AssertError (t , err , "CA should have failed with no SerialPrefix" )
410425}
411426
412427func TestRevoke (t * testing.T ) {
413- cadb , storageAuthority , caConfig := setup (t )
428+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
429+ defer cleanUp ()
414430 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
415431 test .AssertNotError (t , err , "Failed to create CA" )
416432 if err != nil {
@@ -442,7 +458,8 @@ func TestRevoke(t *testing.T) {
442458}
443459
444460func TestIssueCertificate (t * testing.T ) {
445- cadb , storageAuthority , caConfig := setup (t )
461+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
462+ defer cleanUp ()
446463 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
447464 test .AssertNotError (t , err , "Failed to create CA" )
448465 ca .SA = storageAuthority
@@ -518,7 +535,8 @@ func TestIssueCertificate(t *testing.T) {
518535}
519536
520537func TestRejectNoName (t * testing.T ) {
521- cadb , storageAuthority , caConfig := setup (t )
538+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
539+ defer cleanUp ()
522540 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
523541 test .AssertNotError (t , err , "Failed to create CA" )
524542 ca .SA = storageAuthority
@@ -534,7 +552,8 @@ func TestRejectNoName(t *testing.T) {
534552}
535553
536554func TestRejectTooManyNames (t * testing.T ) {
537- cadb , storageAuthority , caConfig := setup (t )
555+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
556+ defer cleanUp ()
538557 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
539558 test .AssertNotError (t , err , "Failed to create CA" )
540559 ca .SA = storageAuthority
@@ -547,7 +566,8 @@ func TestRejectTooManyNames(t *testing.T) {
547566}
548567
549568func TestDeduplication (t * testing.T ) {
550- cadb , storageAuthority , caConfig := setup (t )
569+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
570+ defer cleanUp ()
551571 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
552572 test .AssertNotError (t , err , "Failed to create CA" )
553573 ca .SA = storageAuthority
@@ -576,7 +596,8 @@ func TestDeduplication(t *testing.T) {
576596}
577597
578598func TestRejectValidityTooLong (t * testing.T ) {
579- cadb , storageAuthority , caConfig := setup (t )
599+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
600+ defer cleanUp ()
580601 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
581602 test .AssertNotError (t , err , "Failed to create CA" )
582603 ca .SA = storageAuthority
@@ -597,7 +618,8 @@ func TestRejectValidityTooLong(t *testing.T) {
597618}
598619
599620func TestShortKey (t * testing.T ) {
600- cadb , storageAuthority , caConfig := setup (t )
621+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
622+ defer cleanUp ()
601623 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
602624 ca .SA = storageAuthority
603625 ca .MaxKeySize = 4096
@@ -610,7 +632,8 @@ func TestShortKey(t *testing.T) {
610632}
611633
612634func TestRejectBadAlgorithm (t * testing.T ) {
613- cadb , storageAuthority , caConfig := setup (t )
635+ cadb , storageAuthority , caConfig , cleanUp := setup (t )
636+ defer cleanUp ()
614637 ca , err := NewCertificateAuthorityImpl (cadb , caConfig , caCertFile )
615638 ca .SA = storageAuthority
616639 ca .MaxKeySize = 4096
0 commit comments