Add unit tests for bls utils

pull/339/head
Rongjian Lan 6 years ago
parent 711bcbf2f3
commit 7cbb6e4835
  1. 36
      crypto/bls/bls_test.go
  2. 1
      internal/utils/utils.go
  3. 8
      internal/utils/utils_test.go

@ -0,0 +1,36 @@
package bls
import (
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/internal/utils"
"testing"
)
func TestNewMask(test *testing.T) {
_, pubKey1 := utils.GenKeyBLS("127.0.0.1", "5555")
_, pubKey2 := utils.GenKeyBLS("127.0.0.1", "6666")
_, pubKey3 := utils.GenKeyBLS("127.0.0.1", "7777")
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey1)
if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
}
if mask.Len() != 1 {
test.Errorf("Mask created with wrong size: %d", mask.Len())
}
enabled, err := mask.KeyEnabled(pubKey1)
if !enabled || err != nil {
test.Errorf("My key pubKey1 should have been enabled: %s", err)
}
if mask.CountEnabled() != 1 {
test.Error("Only one key should have been enabled")
}
if mask.CountTotal() != 3 {
test.Error("Should have a total of 3 keys")
}
}

@ -85,6 +85,7 @@ func GenKeyBLS(ip, port string) (*bls.SecretKey, *bls.PublicKey) {
err := privateKey.SetLittleEndian(nodeIDBytes)
if err != nil {
log.Print("failed to set private key", err)
return nil, nil
}
priKey := &privateKey
pubKey := privateKey.GetPublicKey()

@ -48,6 +48,14 @@ func TestGenKey(t *testing.T) {
GenKey("3.3.3.3", "3456")
}
// Test for GenKeyBLS
func TestGenKeyBLS(t *testing.T) {
priKey, pubKey := GenKeyBLS("3.3.3.3", "3456")
if priKey == nil || pubKey == nil {
t.Error("Failed to create keys for BLS sig")
}
}
// Test for GenKeyP2P, noted the length of private key can be random
// thus we don't test it here.
func TestGenKeyP2P(t *testing.T) {

Loading…
Cancel
Save