Skip to content

Commit

Permalink
Merge pull request #2089 from sudeepta-bhuyan/master
Browse files Browse the repository at this point in the history
Fixes #1827 : Added new golang test cases for ECA Registrar feature
  • Loading branch information
srderson committed Jul 1, 2016
2 parents 37b6688 + b2ebe8c commit 6dbeac0
Showing 1 changed file with 86 additions and 8 deletions.
94 changes: 86 additions & 8 deletions membersrvc/ca/eca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import (
)

type User struct {
enrollID string
enrollPwd []byte
enrollPrivKey *ecdsa.PrivateKey
role int
affiliation string
affiliationRole string
enrollID string
enrollPwd []byte
enrollPrivKey *ecdsa.PrivateKey
role int
affiliation string
affiliationRole string
registrarRoles []string
registrarDelegateRoles []string
}

var (
Expand All @@ -49,6 +51,14 @@ var (
testUser = User{enrollID: "testUser", role: 1, affiliation: "institution_a", affiliationRole: "00001"}
testUser2 = User{enrollID: "testUser2", role: 1, affiliation: "institution_a", affiliationRole: "00001"}
testAuditor = User{enrollID: "testAuditor", role: 8}
testClient1 = User{enrollID: "testClient1", role: 1, affiliation: "institution_a", affiliationRole: "00001",
registrarRoles: []string{"client"}, registrarDelegateRoles: []string{"client"}}
testClient2 = User{enrollID: "testClient2", role: 1, affiliation: "institution_a", affiliationRole: "00001",
registrarRoles: []string{"client"}}
testClient3 = User{enrollID: "testClient2", role: 1, affiliation: "institution_a", affiliationRole: "00001",
registrarRoles: []string{"client"}}
testPeer = User{enrollID: "testPeer", role: 2, affiliation: "institution_a", affiliationRole: "00001",
registrarRoles: []string{"peer"}}
)

//helper function for multiple tests
Expand Down Expand Up @@ -160,8 +170,12 @@ func registerUser(registrar User, user *User) error {
Role: pb.Role(user.role),
Account: user.affiliation,
Affiliation: user.affiliationRole,
Registrar: &pb.Registrar{Id: &pb.Identity{Id: registrar.enrollID}},
Sig: nil}
Registrar: &pb.Registrar{
Id: &pb.Identity{Id: registrar.enrollID},
Roles: user.registrarRoles,
DelegateRoles: user.registrarDelegateRoles,
},
Sig: nil}

//sign the req
hash := primitives.NewHash()
Expand Down Expand Up @@ -289,6 +303,70 @@ func TestRegisterUserNonRegistrar(t *testing.T) {
t.Logf("Expected an error and indeed received: [%s]", err.Error())
}

//testAdmin should NOT be able to register testPeer since testAdmin's
//delegateRoles field DOES NOT contain the value "peer"
func TestRegisterUserPeer(t *testing.T) {

err := registerUser(testAdmin, &testPeer)

if err == nil {
t.Fatal("User without appropriate delegateRoles should not be able to register a new user")
}
t.Logf("Expected an error and indeed received: [%s]", err.Error())
}

//testAdmin should be able to register testClient1 since testAdmin's
//delegateRoles field contains the value "client"
func TestRegisterUserClient(t *testing.T) {

err := registerUser(testAdmin, &testClient1)

if err != nil {
t.Error(err.Error())
}
}

//testClient1 registered in the previous test should be able to enroll
func TestCreateCertificatePairClient(t *testing.T) {

err := enrollUser(&testClient1)

if err != nil {
t.Fatalf("Failed to enroll testClient1: [%s]", err.Error())
}
}

//testClient1 should be able to register testClient2 since testClient1's
//delegateRoles field contains the value "client"
func TestRegisterUserClientAsRegistrar(t *testing.T) {

err := registerUser(testClient1, &testClient2)

if err != nil {
t.Error(err.Error())
}

}

//testClient2 should NOT be able to register testClient3 since testClient2's
//delegateRoles field is empty
func TestRegisterUserNoDelegateRoles(t *testing.T) {

err := enrollUser(&testClient2)

if err != nil {
t.Fatalf("Failed to enroll testClient2: [%s]", err.Error())
}

err = registerUser(testClient2, &testClient3)

if err == nil {
t.Fatal("User without delegateRoles should not be able to register a new user")
}

t.Logf("Expected an error and indeed received: [%s]", err.Error())
}

func TestReadCACertificate(t *testing.T) {
ecap := &ECAP{eca}
_, err := ecap.ReadCACertificate(context.Background(), &pb.Empty{})
Expand Down

0 comments on commit 6dbeac0

Please sign in to comment.