diff --git a/network/instance.go b/network/instance.go index 95859bcda503a7481b88662c4b58345c86e90709..092ba6020ab65fee291e36df853f1180a01d5654 100644 --- a/network/instance.go +++ b/network/instance.go @@ -631,6 +631,25 @@ func (i *Instance) GetPermissioningCert() string { } +// GetEllipticPublicKey gets the permissioning's elliptic public key +// from one of the NDFs +// It first checks the full ndf and returns if that has the key +// If not it checks the partial ndf and returns if it has it +// Otherwise it returns an empty string +func (i *Instance) GetEllipticPublicKey() string { + // Check if the full ndf has the information + if i.GetFullNdf() != nil { + return i.GetFullNdf().Get().Registration.EllipticPubKey + } else if i.GetPartialNdf() != nil { + // Else check if the partial ndf has the information + return i.GetPartialNdf().Get().Registration.EllipticPubKey + } + + // If neither do, return an empty string + return "" + +} + // GetPermissioningId gets the permissioning ID from primitives func (i *Instance) GetPermissioningId() *id.ID { return &id.Permissioning diff --git a/network/instance_test.go b/network/instance_test.go index b9611d3537a79b65a9f54adf9c8efb5b3196bcf3..43357dfced275af2dd0da66d3cdec5960e47192c 100644 --- a/network/instance_test.go +++ b/network/instance_test.go @@ -772,6 +772,78 @@ func TestInstance_GetPermissioningCert_NilCase(t *testing.T) { nilNdfInstance.GetPermissioningCert() } +// Happy path: Tests GetEllipticPublicKey with the full ndf set, the partial ndf set +// and no ndf set +func TestInstance_GetEllipticPublicKey(t *testing.T) { + + // Create populated ndf (secured) and empty ndf + secured, _ := NewSecuredNdf(testutils.NDF) + // Create an instance object, setting full to be populated + // and partial to be empty + fullNdfInstance := Instance{ + full: secured, + } + + // Expected cert gotten from testutils.NDF + expectedKey := "MqaJJ3GjFisNRM6LRedRnooi14gepMaQxyWctXVU/w4=" + + // GetEllipticPublicKey from the instance and compare with the expected value + receivedKey := fullNdfInstance.GetEllipticPublicKey() + if expectedKey != receivedKey { + t.Errorf("GetEllipticPublicKey did not get expected value!"+ + "\n\tExpected: %+v"+ + "\n\tReceived: %+v", expectedKey, receivedKey) + } + + // Create an instance object, setting partial to be populated + // and full to be empty + partialNdfInstance := Instance{ + partial: secured, + } + + // GetEllipticPublicKey from the instance and compare with the expected value + receivedKey = partialNdfInstance.GetEllipticPublicKey() + if expectedKey != receivedKey { + t.Errorf("GetEllipticPublicKey did not get expected value!"+ + "\n\tExpected: %+v"+ + "\n\tReceived: %+v", expectedKey, receivedKey) + } + + // Create an instance object, setting no ndf + noNdfInstance := Instance{} + + // GetEllipticPublicKey, should be an empty string as no ndf's are set + receivedKey = noNdfInstance.GetEllipticPublicKey() + if receivedKey != "" { + t.Errorf("GetEllipticPublicKey did not get expected value!"+ + "No ndf set, cert should be an empty string. "+ + "\n\tReceived: %+v", receivedKey) + } + +} + +// Error path: nil ndf is in the instance should cause a seg fault +func TestInstance_GetEllipticPublicKey_NilCase(t *testing.T) { + // Handle expected seg fault here + defer func() { + if r := recover(); r == nil { + t.Errorf("Expected error case, should seg fault when a nil ndf is passed through") + } + }() + + // Create a nil ndf + nilNdf, _ := NewSecuredNdf(nil) + + // Create an instance object with this nil ndf + nilNdfInstance := Instance{ + full: nilNdf, + partial: nilNdf, + } + + // Attempt to call getter, should seg fault + nilNdfInstance.GetEllipticPublicKey() +} + // GetPermissioningId should fetch the value of id.PERMISSIONING in primitives func TestInstance_GetPermissioningId(t *testing.T) { // Create an instance object,