@ -3,6 +3,8 @@ package blsloader
import (
import (
"errors"
"errors"
"fmt"
"fmt"
"os"
"path/filepath"
"strings"
"strings"
"sync"
"sync"
"testing"
"testing"
@ -11,8 +13,28 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/multibls"
"github.com/harmony-one/harmony/multibls"
dircopy "github.com/otiai10/copy"
)
)
func init ( ) {
// Move the test data to temp directory
os . RemoveAll ( testDir )
if err := os . MkdirAll ( testDir , 0777 ) ; err != nil {
fmt . Println ( "makedir error" , err )
}
info , exist := os . Stat ( testDir )
fmt . Println ( "testDir" , info , exist )
testDataFolder := "testdata"
fmt . Println ( testDir )
if err := dircopy . Copy ( testDataFolder , testDir ) ; err != nil {
panic ( fmt . Sprintf ( "failed to copy dir: %v" , err ) )
}
fmt . Println ( testDir )
}
var testDir = filepath . Join ( os . TempDir ( ) , "harmony/BlsLoader" )
type testKey struct {
type testKey struct {
publicKey string
publicKey string
privateKey string
privateKey string
@ -28,16 +50,16 @@ var validTestKeys = []testKey{
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
passphrase : "" ,
passphrase : "" ,
passFile : "testData/ blskey_passphrase/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.pass" ,
passFile : "blskey_passphrase/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.pass" ,
path : "testData/ blskey_passphrase/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
path : "blskey_passphrase/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
} ,
} ,
{
{
// key with non empty passphrase
// key with non empty passphrase
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
passphrase : "harmony" ,
passphrase : "harmony" ,
passFile : "testData/ blskey_passphrase/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass" ,
passFile : "blskey_passphrase/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass" ,
path : "testData/ blskey_passphrase/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
path : "blskey_passphrase/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
} ,
} ,
}
}
@ -48,14 +70,14 @@ var emptyPassTestKeys = []testKey{
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
passphrase : "" ,
passphrase : "" ,
path : "testData/ blskey_emptypass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
path : "blskey_emptypass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
} ,
} ,
{
{
// key with non empty passphrase
// key with non empty passphrase
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
passphrase : "harmony" ,
passphrase : "harmony" ,
path : "testData/ blskey_emptypass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
path : "blskey_emptypass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
} ,
} ,
}
}
@ -66,16 +88,16 @@ var wrongPassTestKeys = []testKey{
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
publicKey : "0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
privateKey : "78c88c331195591b396e3205830071901a7a79e14fd0ede7f06bfb4c5e9f3473" ,
passphrase : "evil harmony" ,
passphrase : "evil harmony" ,
passFile : "testData/ blskey_wrongpass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.pass" ,
passFile : "blskey_wrongpass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.pass" ,
path : "testData/ blskey_wrongpass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
path : "blskey_wrongpass/0e969f8b302cf7648bc39652ca7a279a8562b72933a3f7cddac2252583280c7c3495c9ae854f00f6dd19c32fc5a17500.key" ,
} ,
} ,
{
{
// key with non empty passphrase
// key with non empty passphrase
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
publicKey : "152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
privateKey : "c20fa8de733d08e27e3101436d41f6a3207b8bedad7525c6e91a77ae2a49cf56" ,
passphrase : "harmony" ,
passphrase : "harmony" ,
passFile : "testData/ blskey_wrongpass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass" ,
passFile : "blskey_wrongpass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass" ,
path : "testData/ blskey_wrongpass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
path : "blskey_wrongpass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.key" ,
} ,
} ,
}
}
@ -84,7 +106,7 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
cfg Config
cfg Config
inputs [ ] string
inputs [ ] string
expOutput s [ ] string
expOutSub s [ ] string
expPubKeys [ ] string
expPubKeys [ ] string
expErr error
expErr error
} {
} {
@ -96,9 +118,7 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
} ,
} ,
inputs : [ ] string { } ,
inputs : [ ] string { } ,
expOutputs : [ ] string {
expOutSubs : [ ] string { } ,
fmt . Sprintf ( "loaded bls key %s\n" , validTestKeys [ 0 ] . publicKey ) ,
} ,
expPubKeys : [ ] string { validTestKeys [ 0 ] . publicKey } ,
expPubKeys : [ ] string { validTestKeys [ 0 ] . publicKey } ,
} ,
} ,
{
{
@ -109,9 +129,7 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
} ,
} ,
inputs : [ ] string { } ,
inputs : [ ] string { } ,
expOutputs : [ ] string {
expOutSubs : [ ] string { } ,
fmt . Sprintf ( "loaded bls key %s\n" , validTestKeys [ 1 ] . publicKey ) ,
} ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
} ,
} ,
{
{
@ -122,9 +140,8 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
} ,
} ,
inputs : [ ] string { validTestKeys [ 1 ] . passphrase } ,
inputs : [ ] string { validTestKeys [ 1 ] . passphrase } ,
expOutput s : [ ] string {
expOutSub s : [ ] string {
fmt . Sprintf ( "Enter passphrase for the BLS key file %s:" , validTestKeys [ 1 ] . path ) ,
fmt . Sprintf ( "Enter passphrase for the BLS key file %s:" , validTestKeys [ 1 ] . path ) ,
fmt . Sprintf ( "loaded bls key %s\n" , validTestKeys [ 1 ] . publicKey ) ,
} ,
} ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
} ,
} ,
@ -136,9 +153,7 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
} ,
} ,
inputs : [ ] string { } ,
inputs : [ ] string { } ,
expOutputs : [ ] string {
expOutSubs : [ ] string { } ,
fmt . Sprintf ( "loaded bls key %s\n" , validTestKeys [ 1 ] . publicKey ) ,
} ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
expPubKeys : [ ] string { validTestKeys [ 1 ] . publicKey } ,
} ,
} ,
{
{
@ -149,10 +164,9 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
} ,
} ,
inputs : [ ] string { emptyPassTestKeys [ 1 ] . passphrase } ,
inputs : [ ] string { emptyPassTestKeys [ 1 ] . passphrase } ,
expOutput s : [ ] string {
expOutSub s : [ ] string {
"unable to get passphrase from passphrase file testData/blskey_emptypass/152beed46d7a0002ef0f960946008887eedd4775bdf2ed238809aa74e20d31fdca267443615cc6f4ede49d58911ee083.pass: cannot open passphrase file\n " ,
"unable to get passphrase" ,
fmt . Sprintf ( "Enter passphrase for the BLS key file %s:" , emptyPassTestKeys [ 1 ] . path ) ,
fmt . Sprintf ( "Enter passphrase for the BLS key file %s:" , emptyPassTestKeys [ 1 ] . path ) ,
fmt . Sprintf ( "loaded bls key %s\n" , emptyPassTestKeys [ 1 ] . publicKey ) ,
} ,
} ,
expPubKeys : [ ] string { emptyPassTestKeys [ 1 ] . publicKey } ,
expPubKeys : [ ] string { emptyPassTestKeys [ 1 ] . publicKey } ,
} ,
} ,
@ -161,7 +175,7 @@ func TestLoadKeys_SingleBls_File(t *testing.T) {
ts := & testSuite {
ts := & testSuite {
cfg : test . cfg ,
cfg : test . cfg ,
inputs : test . inputs ,
inputs : test . inputs ,
expOutputs : test . expOutput s ,
expOutSub : test . expOutSub s ,
expErr : test . expErr ,
expErr : test . expErr ,
expPubKeys : test . expPubKeys ,
expPubKeys : test . expPubKeys ,
}
}
@ -191,7 +205,7 @@ type testSuite struct {
cfg Config
cfg Config
inputs [ ] string
inputs [ ] string
expOutputs [ ] string
expOutSub [ ] string
expPubKeys [ ] string
expPubKeys [ ] string
expErr error
expErr error
@ -206,7 +220,7 @@ type testSuite struct {
}
}
func ( ts * testSuite ) init ( ) {
func ( ts * testSuite ) init ( ) {
ts . gotOutputs = make ( [ ] string , 0 , len ( ts . expOutputs ) )
ts . gotOutputs = make ( [ ] string , 0 , len ( ts . expOutSub ) )
ts . console = newTestConsole ( )
ts . console = newTestConsole ( )
setTestConsole ( ts . console )
setTestConsole ( ts . console )
ts . timeout = 1 * time . Second
ts . timeout = 1 * time . Second
@ -232,7 +246,7 @@ func (ts *testSuite) checkResult() error {
default :
default :
}
}
fmt . Println ( "got outputs:" , ts . gotOutputs )
fmt . Println ( "got outputs:" , ts . gotOutputs )
fmt . Println ( "expect outputs:" , ts . expOutputs )
fmt . Println ( "expect outputs:" , ts . expOutSub )
if isClean , msg := ts . console . checkClean ( ) ; ! isClean {
if isClean , msg := ts . console . checkClean ( ) ; ! isClean {
return fmt . Errorf ( "console not clean: %v" , msg )
return fmt . Errorf ( "console not clean: %v" , msg )
}
}
@ -249,12 +263,12 @@ func (ts *testSuite) checkResult() error {
}
}
func ( ts * testSuite ) checkOutputs ( ) error {
func ( ts * testSuite ) checkOutputs ( ) error {
if len ( ts . gotOutputs ) != len ( ts . expOutputs ) {
if len ( ts . gotOutputs ) != len ( ts . expOutSub ) {
return fmt . Errorf ( "output size not expected: %v / %v" , len ( ts . gotOutputs ) , len ( ts . expOutputs ) )
return fmt . Errorf ( "output size not expected: %v / %v" , len ( ts . gotOutputs ) , len ( ts . expOutSub ) )
}
}
for i , gotOutput := range ts . gotOutputs {
for i , gotOutput := range ts . gotOutputs {
expOutput := ts . expOutputs [ i ]
expOutput := ts . expOutSub [ i ]
if gotOutput != expOutput {
if ! strings . Contains ( gotOutput , expOutput ) {
return fmt . Errorf ( "%vth output unexpected: [%v] / [%v]" , i , gotOutput , expOutput )
return fmt . Errorf ( "%vth output unexpected: [%v] / [%v]" , i , gotOutput , expOutput )
}
}
}
}
@ -313,7 +327,7 @@ func (ts *testSuite) threadedLoadOutputs() {
var (
var (
i = 0
i = 0
)
)
for i < len ( ts . expOutputs ) {
for i < len ( ts . expOutSub ) {
select {
select {
case got := <- ts . console . Out :
case got := <- ts . console . Out :
ts . gotOutputs = append ( ts . gotOutputs , got )
ts . gotOutputs = append ( ts . gotOutputs , got )