github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/chaincfg/register_test.go (about)

     1  package chaincfg_test
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  
     8  	. "github.com/dashpay/godash/chaincfg"
     9  )
    10  
    11  // Define some of the required parameters for a user-registered
    12  // network.  This is necessary to test the registration of and
    13  // lookup of encoding magics from the network.
    14  var mockNetParams = Params{
    15  	Name:             "mocknet",
    16  	Net:              1<<32 - 1,
    17  	PubKeyHashAddrID: 0x9f,
    18  	ScriptHashAddrID: 0xf9,
    19  	HDPrivateKeyID:   [4]byte{0x01, 0x02, 0x03, 0x04},
    20  	HDPublicKeyID:    [4]byte{0x05, 0x06, 0x07, 0x08},
    21  }
    22  
    23  func TestRegister(t *testing.T) {
    24  	type registerTest struct {
    25  		name   string
    26  		params *Params
    27  		err    error
    28  	}
    29  	type magicTest struct {
    30  		magic byte
    31  		valid bool
    32  	}
    33  	type hdTest struct {
    34  		priv []byte
    35  		want []byte
    36  		err  error
    37  	}
    38  
    39  	tests := []struct {
    40  		name        string
    41  		register    []registerTest
    42  		p2pkhMagics []magicTest
    43  		p2shMagics  []magicTest
    44  		hdMagics    []hdTest
    45  	}{
    46  		{
    47  			name: "default networks",
    48  			register: []registerTest{
    49  				{
    50  					name:   "duplicate mainnet",
    51  					params: &MainNetParams,
    52  					err:    ErrDuplicateNet,
    53  				},
    54  				{
    55  					name:   "duplicate regtest",
    56  					params: &RegressionNetParams,
    57  					err:    ErrDuplicateNet,
    58  				},
    59  				{
    60  					name:   "duplicate testnet3",
    61  					params: &TestNet3Params,
    62  					err:    ErrDuplicateNet,
    63  				},
    64  				{
    65  					name:   "duplicate simnet",
    66  					params: &SimNetParams,
    67  					err:    ErrDuplicateNet,
    68  				},
    69  			},
    70  			p2pkhMagics: []magicTest{
    71  				{
    72  					magic: MainNetParams.PubKeyHashAddrID,
    73  					valid: true,
    74  				},
    75  				{
    76  					magic: TestNet3Params.PubKeyHashAddrID,
    77  					valid: true,
    78  				},
    79  				{
    80  					magic: RegressionNetParams.PubKeyHashAddrID,
    81  					valid: true,
    82  				},
    83  				{
    84  					magic: SimNetParams.PubKeyHashAddrID,
    85  					valid: true,
    86  				},
    87  				{
    88  					magic: mockNetParams.PubKeyHashAddrID,
    89  					valid: false,
    90  				},
    91  				{
    92  					magic: 0xFF,
    93  					valid: false,
    94  				},
    95  			},
    96  			p2shMagics: []magicTest{
    97  				{
    98  					magic: MainNetParams.ScriptHashAddrID,
    99  					valid: true,
   100  				},
   101  				{
   102  					magic: TestNet3Params.ScriptHashAddrID,
   103  					valid: true,
   104  				},
   105  				{
   106  					magic: RegressionNetParams.ScriptHashAddrID,
   107  					valid: true,
   108  				},
   109  				{
   110  					magic: SimNetParams.ScriptHashAddrID,
   111  					valid: true,
   112  				},
   113  				{
   114  					magic: mockNetParams.ScriptHashAddrID,
   115  					valid: false,
   116  				},
   117  				{
   118  					magic: 0xFF,
   119  					valid: false,
   120  				},
   121  			},
   122  			hdMagics: []hdTest{
   123  				{
   124  					priv: MainNetParams.HDPrivateKeyID[:],
   125  					want: MainNetParams.HDPublicKeyID[:],
   126  					err:  nil,
   127  				},
   128  				{
   129  					priv: TestNet3Params.HDPrivateKeyID[:],
   130  					want: TestNet3Params.HDPublicKeyID[:],
   131  					err:  nil,
   132  				},
   133  				{
   134  					priv: RegressionNetParams.HDPrivateKeyID[:],
   135  					want: RegressionNetParams.HDPublicKeyID[:],
   136  					err:  nil,
   137  				},
   138  				{
   139  					priv: SimNetParams.HDPrivateKeyID[:],
   140  					want: SimNetParams.HDPublicKeyID[:],
   141  					err:  nil,
   142  				},
   143  				{
   144  					priv: mockNetParams.HDPrivateKeyID[:],
   145  					err:  ErrUnknownHDKeyID,
   146  				},
   147  				{
   148  					priv: []byte{0xff, 0xff, 0xff, 0xff},
   149  					err:  ErrUnknownHDKeyID,
   150  				},
   151  				{
   152  					priv: []byte{0xff},
   153  					err:  ErrUnknownHDKeyID,
   154  				},
   155  			},
   156  		},
   157  		{
   158  			name: "register mocknet",
   159  			register: []registerTest{
   160  				{
   161  					name:   "mocknet",
   162  					params: &mockNetParams,
   163  					err:    nil,
   164  				},
   165  			},
   166  			p2pkhMagics: []magicTest{
   167  				{
   168  					magic: MainNetParams.PubKeyHashAddrID,
   169  					valid: true,
   170  				},
   171  				{
   172  					magic: TestNet3Params.PubKeyHashAddrID,
   173  					valid: true,
   174  				},
   175  				{
   176  					magic: RegressionNetParams.PubKeyHashAddrID,
   177  					valid: true,
   178  				},
   179  				{
   180  					magic: SimNetParams.PubKeyHashAddrID,
   181  					valid: true,
   182  				},
   183  				{
   184  					magic: mockNetParams.PubKeyHashAddrID,
   185  					valid: true,
   186  				},
   187  				{
   188  					magic: 0xFF,
   189  					valid: false,
   190  				},
   191  			},
   192  			p2shMagics: []magicTest{
   193  				{
   194  					magic: MainNetParams.ScriptHashAddrID,
   195  					valid: true,
   196  				},
   197  				{
   198  					magic: TestNet3Params.ScriptHashAddrID,
   199  					valid: true,
   200  				},
   201  				{
   202  					magic: RegressionNetParams.ScriptHashAddrID,
   203  					valid: true,
   204  				},
   205  				{
   206  					magic: SimNetParams.ScriptHashAddrID,
   207  					valid: true,
   208  				},
   209  				{
   210  					magic: mockNetParams.ScriptHashAddrID,
   211  					valid: true,
   212  				},
   213  				{
   214  					magic: 0xFF,
   215  					valid: false,
   216  				},
   217  			},
   218  			hdMagics: []hdTest{
   219  				{
   220  					priv: mockNetParams.HDPrivateKeyID[:],
   221  					want: mockNetParams.HDPublicKeyID[:],
   222  					err:  nil,
   223  				},
   224  			},
   225  		},
   226  		{
   227  			name: "more duplicates",
   228  			register: []registerTest{
   229  				{
   230  					name:   "duplicate mainnet",
   231  					params: &MainNetParams,
   232  					err:    ErrDuplicateNet,
   233  				},
   234  				{
   235  					name:   "duplicate regtest",
   236  					params: &RegressionNetParams,
   237  					err:    ErrDuplicateNet,
   238  				},
   239  				{
   240  					name:   "duplicate testnet3",
   241  					params: &TestNet3Params,
   242  					err:    ErrDuplicateNet,
   243  				},
   244  				{
   245  					name:   "duplicate simnet",
   246  					params: &SimNetParams,
   247  					err:    ErrDuplicateNet,
   248  				},
   249  				{
   250  					name:   "duplicate mocknet",
   251  					params: &mockNetParams,
   252  					err:    ErrDuplicateNet,
   253  				},
   254  			},
   255  			p2pkhMagics: []magicTest{
   256  				{
   257  					magic: MainNetParams.PubKeyHashAddrID,
   258  					valid: true,
   259  				},
   260  				{
   261  					magic: TestNet3Params.PubKeyHashAddrID,
   262  					valid: true,
   263  				},
   264  				{
   265  					magic: RegressionNetParams.PubKeyHashAddrID,
   266  					valid: true,
   267  				},
   268  				{
   269  					magic: SimNetParams.PubKeyHashAddrID,
   270  					valid: true,
   271  				},
   272  				{
   273  					magic: mockNetParams.PubKeyHashAddrID,
   274  					valid: true,
   275  				},
   276  				{
   277  					magic: 0xFF,
   278  					valid: false,
   279  				},
   280  			},
   281  			p2shMagics: []magicTest{
   282  				{
   283  					magic: MainNetParams.ScriptHashAddrID,
   284  					valid: true,
   285  				},
   286  				{
   287  					magic: TestNet3Params.ScriptHashAddrID,
   288  					valid: true,
   289  				},
   290  				{
   291  					magic: RegressionNetParams.ScriptHashAddrID,
   292  					valid: true,
   293  				},
   294  				{
   295  					magic: SimNetParams.ScriptHashAddrID,
   296  					valid: true,
   297  				},
   298  				{
   299  					magic: mockNetParams.ScriptHashAddrID,
   300  					valid: true,
   301  				},
   302  				{
   303  					magic: 0xFF,
   304  					valid: false,
   305  				},
   306  			},
   307  			hdMagics: []hdTest{
   308  				{
   309  					priv: MainNetParams.HDPrivateKeyID[:],
   310  					want: MainNetParams.HDPublicKeyID[:],
   311  					err:  nil,
   312  				},
   313  				{
   314  					priv: TestNet3Params.HDPrivateKeyID[:],
   315  					want: TestNet3Params.HDPublicKeyID[:],
   316  					err:  nil,
   317  				},
   318  				{
   319  					priv: RegressionNetParams.HDPrivateKeyID[:],
   320  					want: RegressionNetParams.HDPublicKeyID[:],
   321  					err:  nil,
   322  				},
   323  				{
   324  					priv: SimNetParams.HDPrivateKeyID[:],
   325  					want: SimNetParams.HDPublicKeyID[:],
   326  					err:  nil,
   327  				},
   328  				{
   329  					priv: mockNetParams.HDPrivateKeyID[:],
   330  					want: mockNetParams.HDPublicKeyID[:],
   331  					err:  nil,
   332  				},
   333  				{
   334  					priv: []byte{0xff, 0xff, 0xff, 0xff},
   335  					err:  ErrUnknownHDKeyID,
   336  				},
   337  				{
   338  					priv: []byte{0xff},
   339  					err:  ErrUnknownHDKeyID,
   340  				},
   341  			},
   342  		},
   343  	}
   344  
   345  	for _, test := range tests {
   346  		for _, regTest := range test.register {
   347  			err := Register(regTest.params)
   348  			if err != regTest.err {
   349  				t.Errorf("%s:%s: Registered network with unexpected error: got %v expected %v",
   350  					test.name, regTest.name, err, regTest.err)
   351  			}
   352  		}
   353  		for i, magTest := range test.p2pkhMagics {
   354  			valid := IsPubKeyHashAddrID(magTest.magic)
   355  			if valid != magTest.valid {
   356  				t.Errorf("%s: P2PKH magic %d valid mismatch: got %v expected %v",
   357  					test.name, i, valid, magTest.valid)
   358  			}
   359  		}
   360  		for i, magTest := range test.p2shMagics {
   361  			valid := IsScriptHashAddrID(magTest.magic)
   362  			if valid != magTest.valid {
   363  				t.Errorf("%s: P2SH magic %d valid mismatch: got %v expected %v",
   364  					test.name, i, valid, magTest.valid)
   365  			}
   366  		}
   367  		for i, magTest := range test.hdMagics {
   368  			pubKey, err := HDPrivateKeyToPublicKeyID(magTest.priv[:])
   369  			if !reflect.DeepEqual(err, magTest.err) {
   370  				t.Errorf("%s: HD magic %d mismatched error: got %v expected %v ",
   371  					test.name, i, err, magTest.err)
   372  				continue
   373  			}
   374  			if magTest.err == nil && !bytes.Equal(pubKey, magTest.want[:]) {
   375  				t.Errorf("%s: HD magic %d private and public mismatch: got %v expected %v ",
   376  					test.name, i, pubKey, magTest.want[:])
   377  			}
   378  		}
   379  	}
   380  }