prysm-pulse/shared/bls/spectest/aggregate_test.go
Preston Van Loon fb8be4d555
Refactor BLS (#6395)
* refactor BLS to expose an interface rather than a single implementation
* Split up tests, gofmt, goimports, docs
* godoc for signature.Copy()
* more godocs
* revert gomod / deps changes
* rm newline
* revert proto changes
* rename bls12 to herumi for clarity
2020-06-25 00:47:51 +00:00

64 lines
1.7 KiB
Go

package spectest
import (
"bytes"
"encoding/hex"
"path"
"strings"
"testing"
"github.com/ghodss/yaml"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bls/iface"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestAggregateYaml(t *testing.T) {
testFolders, testFolderPath := testutil.TestFolders(t, "general", "bls/aggregate/small")
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
file, err := testutil.BazelFileBytes(path.Join(testFolderPath, folder.Name(), "data.yaml"))
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}
test := &AggregateTest{}
if err := yaml.Unmarshal(file, test); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
var sigs []iface.Signature
for _, s := range test.Input {
sigBytes, err := hex.DecodeString(s[2:])
if err != nil {
t.Fatalf("Cannot decode string to bytes: %v", err)
}
sig, err := bls.SignatureFromBytes(sigBytes)
if err != nil {
t.Fatalf("Unable to unmarshal signature from bytes: %v", err)
}
sigs = append(sigs, sig)
}
if len(test.Input) == 0 {
if test.Output != "" {
t.Fatalf("Output Aggregate is not of zero length:Output %s", test.Output)
}
return
}
sig := bls.AggregateSignatures(sigs)
if strings.Contains(folder.Name(), "aggregate_na_pubkeys") {
if sig != nil {
t.Errorf("Expected nil signature, received: %v", sig)
}
return
}
outputBytes, err := hex.DecodeString(test.Output[2:])
if err != nil {
t.Fatalf("Cannot decode string to bytes: %v", err)
}
if !bytes.Equal(outputBytes, sig.Marshal()) {
t.Fatal("Output does not equal marshaled aggregated sig bytes")
}
})
}
}