github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnutil/btclib_test.go (about)

     1  package lnutil
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
     8  	"github.com/mit-dci/lit/wire"
     9  )
    10  
    11  // OutPointsEqual
    12  // test 2 patterns x 2 patterns, a.Hash compared with b.Hash and a.Index compared with b.Index
    13  func TestOutPointsEqual(t *testing.T) {
    14  	var hash1 chainhash.Hash = [32]byte{0x00, 0x00, 0x00, 0x00,
    15  		0x00, 0x00, 0x00, 0x00,
    16  		0x00, 0x00, 0x00, 0x00,
    17  		0x00, 0x00, 0x00, 0x00,
    18  		0x00, 0x00, 0x00, 0x00,
    19  		0x00, 0x00, 0x00, 0x00,
    20  		0x00, 0x00, 0x00, 0x00,
    21  		0x00, 0x00, 0x00, 0x01}
    22  	var hash2 chainhash.Hash = [32]byte{0x00, 0x00, 0x00, 0x00,
    23  		0x00, 0x00, 0x00, 0x00,
    24  		0x00, 0x00, 0x00, 0x00,
    25  		0x00, 0x00, 0x00, 0x00,
    26  		0x00, 0x00, 0x00, 0x00,
    27  		0x00, 0x00, 0x00, 0x00,
    28  		0x00, 0x00, 0x00, 0x00,
    29  		0x00, 0x00, 0x00, 0x02}
    30  
    31  	var u1 uint32 = 1
    32  	var u2 uint32 = 2
    33  
    34  	tests := []struct {
    35  		inA  wire.OutPoint
    36  		inB  wire.OutPoint
    37  		want bool
    38  	}{
    39  		//
    40  		// normal situation
    41  		//
    42  		// hash of inA is not the same as hash of inB
    43  		// index of inA is not the same as index of inB
    44  		{
    45  			wire.OutPoint{Hash: hash1, Index: u2}, // inA
    46  			wire.OutPoint{Hash: hash2, Index: u1}, // inB
    47  			false,
    48  		},
    49  
    50  		// hash of inA is not the same as hash of inB
    51  		// index of inA is the same as index of inB
    52  		{
    53  			wire.OutPoint{Hash: hash1, Index: u1}, // inA
    54  			wire.OutPoint{Hash: hash2, Index: u1}, // inB
    55  			false,
    56  		},
    57  
    58  		// hash of inA is the same as hash of inB
    59  		// index of inA is not the same as index of inB
    60  		{
    61  			wire.OutPoint{Hash: hash1, Index: u1}, // inA
    62  			wire.OutPoint{Hash: hash1, Index: u2}, // inB
    63  			false,
    64  		},
    65  
    66  		// hash of inA is the same as hash of inB
    67  		// index of inA is the same as index of inB
    68  		{
    69  			wire.OutPoint{Hash: hash1, Index: u1}, // inA
    70  			wire.OutPoint{Hash: hash1, Index: u1}, // inB
    71  			true,
    72  		},
    73  
    74  		//
    75  		// anomaly situation
    76  		//
    77  		// index of OutPoint is uint32 and the index can not be nil
    78  		// so no test for nil index
    79  		// inA and inB can not be nil so test just initialized one
    80  
    81  		// hash of inA is just initialized
    82  		{
    83  			wire.OutPoint{Hash: [32]byte{}, Index: u1}, // inA
    84  			wire.OutPoint{Hash: hash1, Index: u1},      // inB
    85  			false,
    86  		},
    87  
    88  		// hash of inB is just initialized
    89  		{
    90  			wire.OutPoint{Hash: hash1, Index: u1},      // inA
    91  			wire.OutPoint{Hash: [32]byte{}, Index: u1}, // inB
    92  			false,
    93  		},
    94  
    95  		// hash of inA is just initialized
    96  		// hash of inB is just initialized
    97  		{
    98  			wire.OutPoint{Hash: [32]byte{}, Index: u1}, // inA
    99  			wire.OutPoint{Hash: [32]byte{}, Index: u1}, // inB
   100  			true,
   101  		},
   102  	}
   103  
   104  	// compare with want
   105  	for i, test := range tests {
   106  		result := OutPointsEqual(test.inA, test.inB)
   107  		if test.want != result {
   108  			t.Fatalf("test failed at %d th test", i+1)
   109  			continue
   110  		}
   111  	}
   112  }
   113  
   114  // OutPointToBytes
   115  // test some byte arrays
   116  func TestOutPointToBytes(t *testing.T) {
   117  	// test for a normal situation
   118  	// input: [32]bytes array, uint32
   119  	// output: [36]bytes array
   120  	//
   121  	// no problem if input equals to output
   122  
   123  	// OutPoint to be an input for OutPointToBytes
   124  	var hash32 chainhash.Hash = [32]byte{0x00, 0x00, 0x00, 0x00,
   125  		0x00, 0x00, 0x00, 0x00,
   126  		0x00, 0x00, 0x00, 0x00,
   127  		0x00, 0x00, 0x00, 0x00,
   128  		0x00, 0x00, 0x00, 0x00,
   129  		0x00, 0x00, 0x00, 0x00,
   130  		0x00, 0x00, 0x00, 0x00,
   131  		0x00, 0x00, 0x00, 0x01}
   132  	var u uint32 = 1
   133  	op := wire.OutPoint{Hash: hash32, Index: u}
   134  
   135  	// a bytes array to be compared
   136  	b36 := [36]byte{0x00, 0x00, 0x00, 0x00,
   137  		0x00, 0x00, 0x00, 0x00,
   138  		0x00, 0x00, 0x00, 0x00,
   139  		0x00, 0x00, 0x00, 0x00,
   140  		0x00, 0x00, 0x00, 0x00,
   141  		0x00, 0x00, 0x00, 0x00,
   142  		0x00, 0x00, 0x00, 0x00,
   143  		0x00, 0x00, 0x00, 0x01,
   144  		0x00, 0x00, 0x00, 0x01}
   145  
   146  	if OutPointToBytes(op) != b36 {
   147  		t.Fatalf("it needs to be equal")
   148  	}
   149  
   150  	// TODO: one more test case
   151  }
   152  
   153  // OutPointFromBytes
   154  // test some bytes arrays of 36 byte long
   155  // this depends on a test of NewOutPoint() in adiabat/btcd/wire/msgtx.go
   156  // and OutPointsEqual() in this file
   157  func TestOutPointFromBytes(t *testing.T) {
   158  	// test for a normal situation
   159  	// input: [36]bytes array
   160  	// output: Outpoint created by wire.NewOutPoint()
   161  	//
   162  	// no problem if input equals to output
   163  
   164  	// a bytes array to be an input for OutPointFromBytes
   165  	b36 := [36]byte{0x00, 0x00, 0x00, 0x00,
   166  		0x00, 0x00, 0x00, 0x00,
   167  		0x00, 0x00, 0x00, 0x00,
   168  		0x00, 0x00, 0x00, 0x00,
   169  		0x00, 0x00, 0x00, 0x00,
   170  		0x00, 0x00, 0x00, 0x00,
   171  		0x00, 0x00, 0x00, 0x00,
   172  		0x00, 0x00, 0x00, 0x01,
   173  		0x00, 0x00, 0x00, 0x01}
   174  
   175  	// OutPoint to be compared
   176  	var hash32 chainhash.Hash = [32]byte{0x00, 0x00, 0x00, 0x00,
   177  		0x00, 0x00, 0x00, 0x00,
   178  		0x00, 0x00, 0x00, 0x00,
   179  		0x00, 0x00, 0x00, 0x00,
   180  		0x00, 0x00, 0x00, 0x00,
   181  		0x00, 0x00, 0x00, 0x00,
   182  		0x00, 0x00, 0x00, 0x00,
   183  		0x00, 0x00, 0x00, 0x01}
   184  	var u uint32 = 1
   185  	op := wire.NewOutPoint(&hash32, u)
   186  
   187  	// compare an output of OutPointFromBytes() and op
   188  	if !OutPointsEqual(*OutPointFromBytes(b36), *op) {
   189  		t.Fatalf("it needs to be equal")
   190  	}
   191  
   192  	// TODO: one more test case
   193  }
   194  
   195  // P2WSHify
   196  // test some simple script bytes
   197  func TestP2WSHify(t *testing.T) {
   198  	// test for a normal situation(blackbox test)
   199  	// input: inB2, []bytes slice, []byte{0x00, 0x01}
   200  	// want: wantScriptB, []bytes slice, script bytes
   201  
   202  	// a bytes slice to be an input for P2WSHify()
   203  	var inB2 = []byte{0x00, 0x01}
   204  
   205  	// script to be compared
   206  	wantScriptB := []byte{0x00, 0x20, 0xb4, 0x13, 0xf4, 0x7d, 0x13, 0xee,
   207  		0x2f, 0xe6, 0xc8, 0x45, 0xb2, 0xee, 0x14, 0x1a,
   208  		0xf8, 0x1d, 0xe8, 0x58, 0xdf, 0x4e, 0xc5, 0x49,
   209  		0xa5, 0x8b, 0x79, 0x70, 0xbb, 0x96, 0x64, 0x5b,
   210  		0xc8, 0xd2}
   211  
   212  	if !bytes.Equal(P2WSHify(inB2), wantScriptB) {
   213  		t.Fatalf("it needs to be equal")
   214  	}
   215  
   216  	// TODO: one more test case
   217  }
   218  
   219  // DirectWPKHScript
   220  // test some public keys
   221  // this depends on a test of Hash160() in adiabat/btcutil
   222  func TestDirectWPKHScript(t *testing.T) {
   223  	// test for a normal situation(blackbox test)
   224  	// input: inB33, [33]bytes array, public key
   225  	// want: wantScriptB, []bytes slice, script bytes
   226  
   227  	// a bytes array to be an input for P2WSHify()
   228  	var inB33 = [33]byte{0x00, 0x00, 0x00, 0x00,
   229  		0x00, 0x00, 0x00, 0x00,
   230  		0x00, 0x00, 0x00, 0x00,
   231  		0x00, 0x00, 0x00, 0x00,
   232  		0x00, 0x00, 0x00, 0x00,
   233  		0x00, 0x00, 0x00, 0x00,
   234  		0x00, 0x00, 0x00, 0x00,
   235  		0x00, 0x00, 0x00, 0x00,
   236  		0x01}
   237  
   238  	// script to be compared
   239  	wantScriptB := []byte{0x00, 0x14, 0x4c, 0x20, 0xbb, 0x22, 0xf2, 0x88,
   240  		0xee, 0x1f, 0xae, 0xc2, 0x66, 0xdf, 0x21, 0x52,
   241  		0x0e, 0x4c, 0x13, 0xc7, 0x25, 0x6c}
   242  
   243  	if !bytes.Equal(DirectWPKHScript(inB33), wantScriptB) {
   244  		t.Fatalf("it needs to be equal")
   245  	}
   246  
   247  	// TODO: one more test case
   248  }
   249  
   250  // DirectWPKHScriptFromPKH
   251  // test some public key hashes
   252  func TestDirectWPKHScriptFromPKH(t *testing.T) {
   253  	// test for a normal situation(blackbox test)
   254  	// input: inB20, [20]bytes array, public key hash
   255  	// want: wantScriptB, []bytes slice, script bytes
   256  
   257  	// a bytes array to be an input for DirectWPKHScriptFromPKH()
   258  	var inB20 = [20]byte{0x00, 0x00, 0x00, 0x00,
   259  		0x00, 0x00, 0x00, 0x00,
   260  		0x00, 0x00, 0x00, 0x00,
   261  		0x00, 0x00, 0x00, 0x01}
   262  
   263  	// Script to be compared
   264  	wantScriptB := []byte{0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   265  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   266  		0x00, 0x01, 0x00, 0x00, 0x00, 0x00}
   267  
   268  	if !bytes.Equal(DirectWPKHScriptFromPKH(inB20), wantScriptB) {
   269  		t.Fatalf("it needs to be equal")
   270  	}
   271  
   272  	// TODO: one more test case
   273  }