From aa8f861400bf985a419d6a06c4fc2e01f7f4f540 Mon Sep 17 00:00:00 2001 From: moncho Date: Sat, 2 Dec 2017 17:32:09 +0100 Subject: [PATCH] Add tests --- bitcoin/bitcoin_test.go | 37 ++++++++++++++ bitcoin/pubkey.go | 1 + bitcoin/pubkey_test.go | 110 ++++++++++++++++++++++++++++++++++++++++ warp/warp_test.go | 102 +++++++++++++++++++++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 bitcoin/pubkey_test.go create mode 100644 warp/warp_test.go diff --git a/bitcoin/bitcoin_test.go b/bitcoin/bitcoin_test.go index 5a6339e..606532e 100644 --- a/bitcoin/bitcoin_test.go +++ b/bitcoin/bitcoin_test.go @@ -2,6 +2,7 @@ package bitcoin import ( "encoding/hex" + "reflect" "testing" "github.com/moncho/warpwallet/warp" @@ -272,3 +273,39 @@ func Test_BitcoinPrivKeyGeneration(t *testing.T) { } } + +func Test_paddedPrepend(t *testing.T) { + type args struct { + size uint + src []byte + } + tests := []struct { + name string + args args + want []byte + }{ + { + "paddedPrepend does not do anything when slice has the wanted length", + args{ + 16, + []byte("YELLOW SUBMARINE"), + }, + []byte("YELLOW SUBMARINE"), + }, + { + "paddedPrepend prepends 0 to reach the expected length", + args{ + 18, + []byte("YELLOW SUBMARINE"), + }, + []byte("\x00\x00YELLOW SUBMARINE"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := paddedPrepend(tt.args.size, tt.args.src); !reflect.DeepEqual(got, tt.want) { + t.Errorf("paddedPrepend() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/bitcoin/pubkey.go b/bitcoin/pubkey.go index a934961..1731c35 100644 --- a/bitcoin/pubkey.go +++ b/bitcoin/pubkey.go @@ -59,6 +59,7 @@ func serializeUncompressed(pub ecdsa.PublicKey) []byte { } // serializeCompressed serializes a public key to a 33-byte compressed format. +// See https://bitcoin.org/en/developer-guide#mini-private-key-format func serializeCompressed(pub ecdsa.PublicKey) []byte { var prefix byte if isOdd(pub.Y) { diff --git a/bitcoin/pubkey_test.go b/bitcoin/pubkey_test.go new file mode 100644 index 0000000..b600343 --- /dev/null +++ b/bitcoin/pubkey_test.go @@ -0,0 +1,110 @@ +package bitcoin + +import ( + "bytes" + "crypto/ecdsa" + "math/big" + "reflect" + "testing" +) + +func Test_isOdd(t *testing.T) { + type args struct { + a *big.Int + } + tests := []struct { + name string + args args + want bool + }{ + { + "test odd", + args{ + big.NewInt(3), + }, + true, + }, + { + "test even", + args{ + big.NewInt(2), + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isOdd(tt.args.a); got != tt.want { + t.Errorf("isOdd() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_serializeUncompressed(t *testing.T) { + type args struct { + pub ecdsa.PublicKey + } + tests := []struct { + name string + args args + want []byte + }{ + { + "ECDSA public key uncompressed serialization", + args{ + ecdsa.PublicKey{ + X: big.NewInt(0), + Y: big.NewInt(1), + }, + }, + append(append([]byte{'\x04'}, bytes.Repeat([]byte{'\x00'}, 63)...), '\x01'), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := serializeUncompressed(tt.args.pub); !reflect.DeepEqual(got, tt.want) { + t.Errorf("serializeUncompressed() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_serializeCompressed(t *testing.T) { + type args struct { + pub ecdsa.PublicKey + } + tests := []struct { + name string + args args + want []byte + }{ + { + "ECDSA public key compressed serialization", + args{ + ecdsa.PublicKey{ + X: big.NewInt(1), + Y: big.NewInt(2), + }, + }, + append(append([]byte{PubKeyCompressedEvenPrefix}, bytes.Repeat([]byte{'\x00'}, 31)...), '\x01'), + }, + { + "ECDSA public key compressed serialization", + args{ + ecdsa.PublicKey{ + X: big.NewInt(1), + Y: big.NewInt(1), + }, + }, + append(append([]byte{PubKeyCompressedOddPrefix}, bytes.Repeat([]byte{'\x00'}, 31)...), '\x01'), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := serializeCompressed(tt.args.pub); !reflect.DeepEqual(got, tt.want) { + t.Errorf("serializeCompressed() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/warp/warp_test.go b/warp/warp_test.go new file mode 100644 index 0000000..6e2190a --- /dev/null +++ b/warp/warp_test.go @@ -0,0 +1,102 @@ +package warp + +import ( + "encoding/hex" + "testing" +) + +type args struct { + passphrase string + salt string +} + +var tests = []struct { + name string + args args + want string + wantErr bool +}{ + { + "test 1", + args{ + "ER8FT+HFjk0", + "7DpniYifN6c", + }, + "6f2552e159f2a1e1e26c2262da459818fd56c81c363fcc70b94c423def42e59f", + false, + }, + { + "test 2", + args{ + "YqIDBApDYME", + "G34HqIgjrIc", + }, + "da009602a5781a8795d55c6e68a4b4d52969a75955ea70255869dd17c3398592", + false, + }, + { + "test 3", + args{ + "FPdAxCygMJg", + "X+qaSwhUYXw", + }, + "2f6af9ad997b831963f4de48278c044e687ff3cecc25739d1564985b929cb3dd", + false, + }, + { + "test 4", + args{ + "uyVkW5vKXX3RpvnUcj7U3Q", + "zXrlmk3p5Lxr0vjJKdcJWQ", + }, + "00c1eecb8f56387ba4ecef1574abc1d0078cf8366a301ef15e94fb313a853453", + false, + }, +} + +func Test_parallelSeedGeneration(t *testing.T) { + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parallelSeedGeneration(tt.args.passphrase, tt.args.salt) + if (err != nil) != tt.wantErr { + t.Errorf("parallelSeedGeneration() error = %v, wantErr %v", err, tt.wantErr) + return + } + if hex.EncodeToString(got) != tt.want { + t.Errorf("%q. parallelSeedGeneration() = %v, want %v", tt.name, hex.EncodeToString(got), tt.want) + } + }) + } +} + +func Test_serialSeedGeneration(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := serialSeedGeneration(tt.args.passphrase, tt.args.salt) + if (err != nil) != tt.wantErr { + t.Errorf("serialSeedGeneration() error = %v, wantErr %v", err, tt.wantErr) + return + } + if hex.EncodeToString(got) != tt.want { + t.Errorf("%q. serialSeedGeneration() = %v, want %v", tt.name, hex.EncodeToString(got), tt.want) + } + }) + } +} + +func TestNewSeed(t *testing.T) { + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NewSeed(tt.args.passphrase, tt.args.salt) + if (err != nil) != tt.wantErr { + t.Errorf("NewSeed() error = %v, wantErr %v", err, tt.wantErr) + return + } + if hex.EncodeToString(got) != tt.want { + t.Errorf("%q. NewSeed() = %v, want %v", tt.name, hex.EncodeToString(got), tt.want) + } + }) + } +}