From 7cbb6e48352263444feed9855f6c7c621fbb044b Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sat, 26 Jan 2019 12:17:15 -0800 Subject: [PATCH 1/3] Add unit tests for bls utils --- crypto/bls/bls_test.go | 36 ++++++++++++++++++++++++++++++++++++ internal/utils/utils.go | 1 + internal/utils/utils_test.go | 8 ++++++++ 3 files changed, 45 insertions(+) create mode 100644 crypto/bls/bls_test.go diff --git a/crypto/bls/bls_test.go b/crypto/bls/bls_test.go new file mode 100644 index 000000000..427756fe2 --- /dev/null +++ b/crypto/bls/bls_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") + } +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 6337715e6..da0629b55 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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() diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 7911ed9ba..4bd52b087 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -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) { From 456002406c9e723acbb3ab2ed832d7efe3427ae9 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sun, 27 Jan 2019 21:24:05 -0800 Subject: [PATCH 2/3] Fix goimport lint --- crypto/bls/bls_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crypto/bls/bls_test.go b/crypto/bls/bls_test.go index 427756fe2..b7a4924ff 100644 --- a/crypto/bls/bls_test.go +++ b/crypto/bls/bls_test.go @@ -1,9 +1,10 @@ package bls import ( + "testing" + "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/internal/utils" - "testing" ) func TestNewMask(test *testing.T) { From 97a04231df2484fca3b79a8a9f150f43c33b8a82 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 28 Jan 2019 11:16:55 -0800 Subject: [PATCH 3/3] Add comments for test and improve bls test --- crypto/bls/bls_test.go | 1 + internal/utils/utils_test.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/crypto/bls/bls_test.go b/crypto/bls/bls_test.go index b7a4924ff..76db0e705 100644 --- a/crypto/bls/bls_test.go +++ b/crypto/bls/bls_test.go @@ -7,6 +7,7 @@ import ( "github.com/harmony-one/harmony/internal/utils" ) +// Test the basic functionality of a BLS multi-sig mask. func TestNewMask(test *testing.T) { _, pubKey1 := utils.GenKeyBLS("127.0.0.1", "5555") _, pubKey2 := utils.GenKeyBLS("127.0.0.1", "6666") diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 4bd52b087..17125be00 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -1,6 +1,8 @@ package utils import ( + "bytes" + "encoding/hex" "testing" "github.com/harmony-one/harmony/p2p" @@ -54,6 +56,11 @@ func TestGenKeyBLS(t *testing.T) { if priKey == nil || pubKey == nil { t.Error("Failed to create keys for BLS sig") } + pubKeyBytes, _ := hex.DecodeString("ca6247713431a59cbadfe282b36cb13746b6e5c5db6e5972a10a83adffdf23f8aab246229cb3050e061e1024aa9b6518e200dd9663a8c855e596f1007150aa0672e6f40d073947aa027e8ffe8e89d894ca3916f80fdb350f4b8643f6ff99510c") + + if bytes.Compare(pubKey.Serialize(), pubKeyBytes) != 0 { + t.Errorf("Unexpected public key: %s", hex.EncodeToString(pubKey.Serialize())) + } } // Test for GenKeyP2P, noted the length of private key can be random