decred.org/dcrdex@v1.0.5/server/account/account_test.go (about)

     1  package account
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  )
     8  
     9  func TestNewAccountFromPubKey(t *testing.T) {
    10  	tests := []struct {
    11  		pk      []byte
    12  		wantErr bool
    13  	}{
    14  		{
    15  			pk: []byte{0x03, 0xbb, 0x43, 0xcf, 0xfd, 0x94,
    16  				0xac, 0x0e, 0xb7, 0xe2, 0x21, 0x93, 0x08, 0xdb, 0x24, 0x14,
    17  				0x90, 0xce, 0x69, 0x09, 0x6c, 0x0f, 0xe7, 0x5c, 0x3c, 0xcf,
    18  				0xfc, 0x13, 0x30, 0x0d, 0xa9, 0xc5, 0x35},
    19  			wantErr: false,
    20  		},
    21  		{
    22  			pk: []byte{0x0bb, 0xbb, 0x43, 0xcf, 0xfd, 0x94,
    23  				0xac, 0x0e, 0xb7, 0xe2, 0x21, 0x93, 0x08, 0xdb, 0x24, 0x14,
    24  				0x90, 0xce, 0x69, 0x09, 0x6c, 0x0f, 0xe7, 0x5c, 0x3c, 0xcf,
    25  				0xfc, 0x13, 0x30, 0x0d, 0xa9, 0xc5, 0x35},
    26  			wantErr: true,
    27  		},
    28  		{
    29  			pk:      nil,
    30  			wantErr: true,
    31  		},
    32  	}
    33  
    34  	for idx, tc := range tests {
    35  		_, err := NewAccountFromPubKey(tc.pk)
    36  		if (err != nil) != tc.wantErr {
    37  			t.Errorf("[NewAccountFromPubkey #%d] error: %v, wantErr: %v",
    38  				idx+1, err, tc.wantErr)
    39  		}
    40  	}
    41  }
    42  
    43  func TestNewID(t *testing.T) {
    44  	tests := []struct {
    45  		pk         []byte
    46  		id         []byte
    47  		idMismatch bool
    48  	}{
    49  		{
    50  			pk: []byte{0x03, 0xbb, 0x43, 0xcf, 0xfd, 0x94,
    51  				0xac, 0x0e, 0xb7, 0xe2, 0x21, 0x93, 0x08, 0xdb, 0x24, 0x14,
    52  				0x90, 0xce, 0x69, 0x09, 0x6c, 0x0f, 0xe7, 0x5c, 0x3c, 0xcf,
    53  				0xfc, 0x13, 0x30, 0x0d, 0xa9, 0xc5, 0x35},
    54  			id: []byte{0x61, 0x24, 0xaa, 0x30, 0x17, 0xa3, 0xd3, 0x23,
    55  				0x0d, 0xf9, 0xb6, 0x76, 0x58, 0xb5, 0xd3, 0xca, 0x14, 0x79,
    56  				0x1a, 0x8e, 0xd4, 0x66, 0x66, 0x1c, 0x69, 0x25, 0x6c, 0x03,
    57  				0xb9, 0xed, 0xc3, 0xd1},
    58  			idMismatch: false,
    59  		},
    60  		{
    61  			pk: []byte{},
    62  			id: []byte{0x61, 0x24, 0xaa, 0x30, 0x17, 0xa3, 0xd3, 0x23,
    63  				0x0d, 0xf9, 0xb6, 0x76, 0x58, 0xb5, 0xd3, 0xca, 0x14, 0x79,
    64  				0x1a, 0x8e, 0xd4, 0x66, 0x66, 0x1c, 0x69, 0x25, 0x6c, 0x03,
    65  				0xb9, 0xed, 0xc3, 0xd1},
    66  			idMismatch: true,
    67  		},
    68  		{
    69  			pk: nil,
    70  			id: []byte{0x61, 0x24, 0xaa, 0x30, 0x17, 0xa3, 0xd3, 0x23,
    71  				0x0d, 0xf9, 0xb6, 0x76, 0x58, 0xb5, 0xd3, 0xca, 0x14, 0x79,
    72  				0x1a, 0x8e, 0xd4, 0x66, 0x66, 0x1c, 0x69, 0x25, 0x6c, 0x03,
    73  				0xb9, 0xed, 0xc3, 0xd1},
    74  			idMismatch: true,
    75  		},
    76  	}
    77  
    78  	for idx, tc := range tests {
    79  		id := NewID(tc.pk)
    80  		if !bytes.Equal(id[:], tc.id) != tc.idMismatch {
    81  			t.Errorf("[NewID #%d] expected id %v, got %v",
    82  				idx+1, hex.EncodeToString(tc.id), hex.EncodeToString(id[:]))
    83  		}
    84  	}
    85  }