Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moncho committed Dec 2, 2017
1 parent 381bcc3 commit aa8f861
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bitcoin/bitcoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitcoin

import (
"encoding/hex"
"reflect"
"testing"

"github.com/moncho/warpwallet/warp"
Expand Down Expand Up @@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions bitcoin/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
110 changes: 110 additions & 0 deletions bitcoin/pubkey_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
102 changes: 102 additions & 0 deletions warp/warp_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit aa8f861

Please sign in to comment.