github.com/lbryio/lbcd@v0.22.119/chaincfg/chainhash/hash_test.go (about)

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