github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/shahash_test.go (about)

     1  // Copyright (c) 2013-2015 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package wire_test
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/hex"
    11  	"testing"
    12  
    13  	"github.com/BlockABC/godash/wire"
    14  )
    15  
    16  // TestShaHash tests the ShaHash API.
    17  func TestShaHash(t *testing.T) {
    18  
    19  	// Hash of block 234439.
    20  	blockHashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef"
    21  	blockHash, err := wire.NewShaHashFromStr(blockHashStr)
    22  	if err != nil {
    23  		t.Errorf("NewShaHashFromStr: %v", err)
    24  	}
    25  
    26  	// Hash of block 234440 as byte slice.
    27  	buf := []byte{
    28  		0x79, 0xa6, 0x1a, 0xdb, 0xc6, 0xe5, 0xa2, 0xe1,
    29  		0x39, 0xd2, 0x71, 0x3a, 0x54, 0x6e, 0xc7, 0xc8,
    30  		0x75, 0x63, 0x2e, 0x75, 0xf1, 0xdf, 0x9c, 0x3f,
    31  		0xa6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    32  	}
    33  
    34  	hash, err := wire.NewShaHash(buf)
    35  	if err != nil {
    36  		t.Errorf("NewShaHash: unexpected error %v", err)
    37  	}
    38  
    39  	// Ensure proper size.
    40  	if len(hash) != wire.HashSize {
    41  		t.Errorf("NewShaHash: hash length mismatch - got: %v, want: %v",
    42  			len(hash), wire.HashSize)
    43  	}
    44  
    45  	// Ensure contents match.
    46  	if !bytes.Equal(hash[:], buf) {
    47  		t.Errorf("NewShaHash: hash contents mismatch - got: %v, want: %v",
    48  			hash[:], buf)
    49  	}
    50  
    51  	// Ensure contents of hash of block 234440 don't match 234439.
    52  	if hash.IsEqual(blockHash) {
    53  		t.Errorf("IsEqual: hash contents should not match - got: %v, want: %v",
    54  			hash, blockHash)
    55  	}
    56  
    57  	// Set hash from byte slice and ensure contents match.
    58  	err = hash.SetBytes(blockHash.Bytes())
    59  	if err != nil {
    60  		t.Errorf("SetBytes: %v", err)
    61  	}
    62  	if !hash.IsEqual(blockHash) {
    63  		t.Errorf("IsEqual: hash contents mismatch - got: %v, want: %v",
    64  			hash, blockHash)
    65  	}
    66  
    67  	// Invalid size for SetBytes.
    68  	err = hash.SetBytes([]byte{0x00})
    69  	if err == nil {
    70  		t.Errorf("SetBytes: failed to received expected err - got: nil")
    71  	}
    72  
    73  	// Invalid size for NewShaHash.
    74  	invalidHash := make([]byte, wire.HashSize+1)
    75  	_, err = wire.NewShaHash(invalidHash)
    76  	if err == nil {
    77  		t.Errorf("NewShaHash: failed to received expected err - got: nil")
    78  	}
    79  }
    80  
    81  // TestShaHashString  tests the stringized output for sha hashes.
    82  func TestShaHashString(t *testing.T) {
    83  	// Block 100000 hash.
    84  	wantStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
    85  	hash := wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
    86  		0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39,
    87  		0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2,
    88  		0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa,
    89  		0x27, 0xba, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
    90  	})
    91  
    92  	hashStr := hash.String()
    93  	if hashStr != wantStr {
    94  		t.Errorf("String: wrong hash string - got %v, want %v",
    95  			hashStr, wantStr)
    96  	}
    97  }
    98  
    99  // TestNewShaHashFromStr executes tests against the NewShaHashFromStr function.
   100  func TestNewShaHashFromStr(t *testing.T) {
   101  	tests := []struct {
   102  		in   string
   103  		want wire.ShaHash
   104  		err  error
   105  	}{
   106  		// Genesis hash.
   107  		{
   108  			"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
   109  			mainNetGenesisHash,
   110  			nil,
   111  		},
   112  
   113  		// Genesis hash with stripped leading zeros.
   114  		{
   115  			"19d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
   116  			mainNetGenesisHash,
   117  			nil,
   118  		},
   119  
   120  		// Empty string.
   121  		{
   122  			"",
   123  			wire.ShaHash{},
   124  			nil,
   125  		},
   126  
   127  		// Single digit hash.
   128  		{
   129  			"1",
   130  			wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
   131  				0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   132  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   133  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   134  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   135  			}),
   136  			nil,
   137  		},
   138  
   139  		// Block 203707 with stripped leading zeros.
   140  		{
   141  			"3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc",
   142  			wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
   143  				0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
   144  				0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
   145  				0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
   146  				0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   147  			}),
   148  			nil,
   149  		},
   150  
   151  		// Hash string that is too long.
   152  		{
   153  			"01234567890123456789012345678901234567890123456789012345678912345",
   154  			wire.ShaHash{},
   155  			wire.ErrHashStrSize,
   156  		},
   157  
   158  		// Hash string that is contains non-hex chars.
   159  		{
   160  			"abcdefg",
   161  			wire.ShaHash{},
   162  			hex.InvalidByteError('g'),
   163  		},
   164  	}
   165  
   166  	unexpectedErrStr := "NewShaHashFromStr #%d failed to detect expected error - got: %v want: %v"
   167  	unexpectedResultStr := "NewShaHashFromStr #%d got: %v want: %v"
   168  	t.Logf("Running %d tests", len(tests))
   169  	for i, test := range tests {
   170  		result, err := wire.NewShaHashFromStr(test.in)
   171  		if err != test.err {
   172  			t.Errorf(unexpectedErrStr, i, err, test.err)
   173  			continue
   174  		} else if err != nil {
   175  			// Got expected error. Move on to the next test.
   176  			continue
   177  		}
   178  		if !test.want.IsEqual(result) {
   179  			t.Errorf(unexpectedResultStr, i, result, &test.want)
   180  			continue
   181  		}
   182  	}
   183  }