Skip to content

Commit

Permalink
[FAB-6529] Fix new CA unit-tests
Browse files Browse the repository at this point in the history
This CR adds to the unit-tests calls to ca.closeDB
so that the DB file can effectively be removed, and
error checking to catch any problems with the removal
of test files.

With these changes the unit-tests pass:

$ go test -v
=== RUN   TestCABadCACertificates
2017/10/10 14:00:54 [INFO] The CA key and certificate already exist
2017/10/10 14:00:54 [INFO] The key is stored by BCCSP provider 'SW'
...
2017/10/10 14:03:14 [DEBUG] Closing server DBs
2017/10/10 14:03:15 [DEBUG] Stop: successful stop on port 7075
--- PASS: TestRegisterationAffiliation (1.81s)
=== RUN   TestEnd
--- PASS: TestEnd (0.14s)
=== RUN   TestSRVServerClean
--- PASS: TestSRVServerClean (0.03s)
PASS
ok  	github.com/hyperledger/fabric-ca/lib	141.073s

Patch-set #2: rebase

Change-Id: Iea4b8afce63cc1bf7a8c100322d2736ffd056156
Signed-off-by: Arnaud J Le Hors <[email protected]>
  • Loading branch information
lehors committed Oct 10, 2017
1 parent 2528217 commit 3e15d7e
Showing 1 changed file with 77 additions and 36 deletions.
113 changes: 77 additions & 36 deletions lib/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var srv Server

func TestCABadCACertificates(t *testing.T) {
ca, err := newCA(configFile, &CAConfig{}, &srv, false)
if err != nil {
t.Fatal("newCA failed ", err)
}
err = ca.validateCert(noCACert, noCAkey)
t.Log("validateCert Error: ", err)
if err == nil {
Expand Down Expand Up @@ -92,7 +95,7 @@ func TestCABadCACertificates(t *testing.T) {
testValidKeySize(cert, t)
testValidMatchingKeys(cert, t)
testValidUsages(cert, t)
CAclean()
CAclean(ca, t)
}

func testValidDates(cert *x509.Certificate, t *testing.T) {
Expand Down Expand Up @@ -426,46 +429,46 @@ func TestCAloadCNFromEnrollmentInfo(t *testing.T) {
if err == nil {
t.Error("Should have failed: ")
}
CAclean()
CAclean(ca, t)
}

func TestCAgetUserAffiliation(t *testing.T) {
ca, err := newCA(configFile, &CAConfig{}, &srv, false)
if err != nil {
t.Fatal("NewCa failed ", err)
t.Fatal("newCA failed ", err)
}
_, err = ca.getUserAffiliation(string(0))
t.Log("getUserAffiliation err: ", err)
if err == nil {
t.Error("getUserAffiliation should have failed: bad parameter")
}
CAclean()
CAclean(ca, t)
}

func TestCAuserHasAttribute(t *testing.T) {
ca, err := newCA(configFile, &CAConfig{}, &srv, false)
if err != nil {
t.Fatal("NewCa failed ", err)
t.Fatal("newCA failed ", err)
}
_, err = ca.userHasAttribute(string(0), string(0))
t.Log("userHasAttribute err: ", err)
if err == nil {
t.Error("userHasAttribute should have failed: bad parameter")
}
CAclean()
CAclean(ca, t)
}

func TestCAgetUserAttrValue(t *testing.T) {
ca, err := newCA(configFile, &CAConfig{}, &srv, false)
if err != nil {
t.Fatal("NewCa failed: ", err)
t.Fatal("newCA failed: ", err)
}
_, err = ca.getUserAttrValue("maryjokopechne", "delmont")
t.Log("getUserAttrValue err: ", err)
if err == nil {
t.Error("getUserAttrValue sould have failed: no such user")
}
CAclean()
CAclean(ca, t)
}

func TestCAaddIdentity(t *testing.T) {
Expand All @@ -478,7 +481,7 @@ func TestCAaddIdentity(t *testing.T) {
cfg.Registry = CAConfigRegistry{MaxEnrollments: 10}
ca, err := newCA(configFile, &cfg, &srv, false)
if err != nil {
t.Fatal("NewCa failed: ", err)
t.Fatal("newCA failed: ", err)
}
err = ca.addIdentity(id, true)
t.Log("ca.addIdentity err: ", err)
Expand All @@ -490,24 +493,33 @@ func TestCAaddIdentity(t *testing.T) {
if err == nil {
t.Error("getUserAttrValue sould have failed: duplicate id")
}
CAclean()
CAclean(ca, t)
}

func TestCAinitUserRegistry(t *testing.T) {
os.Remove(testdir + dbname)
os.Remove(configFile)
err := os.RemoveAll(testdir + dbname)
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
err = os.RemoveAll(configFile)
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
cfg = CAConfig{}
cfg.LDAP.Enabled = true
cfg.LDAP.URL = "ldap://CN=admin,dc=example,dc=com:adminpw@localhost:389/dc=example,dc=com"
_, err := newCA(configFile, &cfg, &srv, false)
_, err = newCA(configFile, &cfg, &srv, false)
if err != nil {
t.Fatal("newCA FAILED", err)
}
os.Remove(testdir + dbname)
err = os.RemoveAll(testdir + dbname)
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
}

func TestCAgetCaCert(t *testing.T) {
CAclean()
CAclean(nil, t)
os.Remove(configFile)
cfg = CAConfig{}

Expand Down Expand Up @@ -538,7 +550,7 @@ func TestCAgetCaCert(t *testing.T) {
t.Fatal("newCA should have failed")
}

CAclean()
CAclean(ca, t)
os.Remove(configFile)
}

Expand Down Expand Up @@ -587,7 +599,7 @@ func TestCADBinit(t *testing.T) {
cfg.DB = CAConfigDB{Datasource: "root:mysql@" + util.RandomString(237)}
ca, err := newCA(confDir, &cfg, &srv, false)
if ca.db != nil {
t.Error("Create DB shold have failed")
t.Error("Create DB should have failed")
}
}

Expand Down Expand Up @@ -628,28 +640,34 @@ func TestCAloadAffiliationsTableR(t *testing.T) {
}

func TestCAloadUsersTable(t *testing.T) {
CAclean()
os.Remove(configFile)
CAclean(nil, t)
err := os.RemoveAll(configFile)
if err != nil {
t.Fatalf("RemoveAll failed: %s", err)
}
cfg = CAConfig{}
u := &CAConfigIdentity{Name: "a", MaxEnrollments: -10}
cfg.Registry = CAConfigRegistry{Identities: []CAConfigIdentity{*u}, MaxEnrollments: 10}
_, err := newCA(configFile, &cfg, &srv, false)
ca, err := newCA(configFile, &cfg, &srv, false)
t.Log("ca.newCA error: ", err)
if err == nil {
t.Error("ca.newCA should have failed")
}

//Chase down all error paths using duplicate entries
// Chase down all error paths using duplicate entries
i := make([]interface{}, 3)
i[1] = []string{"", "root", "root"}
cfg.Affiliations = make(map[string]interface{}, 3)
cfg.Affiliations["a"] = i

//Valid registration
os.Remove(testdir + dbname)
// Valid registration
err = os.Remove(testdir + dbname)
if err != nil {
t.Fatalf("Remove failed: %s", err)
}
u = &CAConfigIdentity{Name: "a", MaxEnrollments: 10}
cfg.Registry = CAConfigRegistry{Identities: []CAConfigIdentity{*u}, MaxEnrollments: 10}
ca, err := newCA(configFile, &cfg, &srv, false)
ca, err = newCA(configFile, &cfg, &srv, false)
if err != nil {
t.Fatal("newCA FAILED", err)
}
Expand All @@ -661,28 +679,34 @@ func TestCAloadUsersTable(t *testing.T) {
t.Error("ca.loadUsersTable failed ", err)
}

//Duplicate resgistration, non-error
// Duplicate resgistration, non-error
u = &CAConfigIdentity{Name: "a", MaxEnrollments: 10}
ca.Config.Registry = CAConfigRegistry{Identities: []CAConfigIdentity{*u}, MaxEnrollments: 10}
err = ca.loadUsersTable()
if err != nil {
t.Error("ca.loadUsersTable error path should have succeeded: ", err)
}

//Database error
// Database error (db is closed)
u = &CAConfigIdentity{Name: "b", MaxEnrollments: 10}
ca.Config.Registry = CAConfigRegistry{Identities: []CAConfigIdentity{*u}, MaxEnrollments: 10}
os.Remove(testdir + dbname)
err = ca.closeDB()
if err != nil {
t.Fatalf("CloseDB failed: %s", err)
}
err = os.Remove(testdir + dbname)
if err != nil {
t.Fatalf("Remove failed: %s", err)
}
err = ca.loadUsersTable()
t.Log("ca.loadUsersTable error: ", err)
if err == nil {
t.Error("ca.loadUsersTable should have failed due to DB error ", err)
}
os.Remove(testdir + dbname)
}

func TestCAVerifyCertificate(t *testing.T) {
CAclean()
CAclean(nil, t)
os.Remove(configFile)
cfg = CAConfig{}
ca, err := newCA(configFile, &cfg, &srv, false)
Expand Down Expand Up @@ -736,8 +760,7 @@ func TestCAVerifyCertificate(t *testing.T) {
if err == nil {
t.Error("VerifyCertificate should have failed")
}

CAclean()
CAclean(ca, t)
}

func getCertFromFile(f string) (*x509.Certificate, error) {
Expand All @@ -761,9 +784,27 @@ func cleanupTmpfiles(t *testing.T, d string) {
}
}

func CAclean() {
os.RemoveAll(testdir + "msp")
os.Remove(testdir + "ca-cert.pem")
os.Remove(testdir + "ca-key.pem")
os.Remove(testdir + dbname)
func CAclean(ca *CA, t *testing.T) {
if ca != nil {
err := ca.closeDB()
if err != nil {
t.Error("CloseDB failed: ", err)
}
}
err := os.RemoveAll(testdir + "msp")
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
err = os.RemoveAll(testdir + "ca-cert.pem")
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
err = os.RemoveAll(testdir + "ca-key.pem")
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
err = os.RemoveAll(testdir + dbname)
if err != nil {
t.Fatal("RemoveAll failed: ", err)
}
}

0 comments on commit 3e15d7e

Please sign in to comment.