github.com/core-coin/go-core/v2@v2.1.9/p2p/dnsdisc/tree_test.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package dnsdisc
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/davecgh/go-spew/spew"
    24  
    25  	"github.com/core-coin/go-core/v2/common/hexutil"
    26  	"github.com/core-coin/go-core/v2/p2p/enode"
    27  )
    28  
    29  func TestParseRoot(t *testing.T) {
    30  	tests := []struct {
    31  		input string
    32  		e     rootEntry
    33  		err   error
    34  	}{
    35  		{
    36  			input: "enrtree-root:v1 e=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3 sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtw",
    37  			err:   entryError{"root", errSyntax},
    38  		},
    39  		{
    40  			input: "enrtree-root:v1 e=TO4Q75OQ2N7DX4EOOR7X66A6OM l=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3 sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtw",
    41  			err:   entryError{"root", errInvalidSig},
    42  		},
    43  		{
    44  			input: "enrtree-root:v1 e=7525E77SHV6G7TBPKTRFYQKNEI l=TCMJVN7523OUUZZ5MYGACYBBIU seq=1 sig=hbftK14lRXy81Y1WtqhWgJ8VVOT2bYo31mTDhouxMCA-ENY3JA0Du6imZIEd6voIgm1yn4_k2aKAWsfLkyKs6-q6rZhOWd81CuDYg2eEPUcK1FyWaj1hMMXj3TGVPgSqfHLX6AU1xYKcchdwCeiY0hAAhR3c_l0JEw3XUFpFrCjAQu-MCmR7bhcFQ_-KFwrG6r_1oQ2Q_-glxs9NYZz2ueAgQ8Lm0te4tZsA",
    45  			e: rootEntry{
    46  				eroot: "7525E77SHV6G7TBPKTRFYQKNEI",
    47  				lroot: "TCMJVN7523OUUZZ5MYGACYBBIU",
    48  				seq:   1,
    49  				sig:   hexutil.MustDecode("0x85b7ed2b5e25457cbcd58d56b6a856809f1554e4f66d8a37d664c3868bb130203e10d637240d03bba8a664811deafa08826d729f8fe4d9a2805ac7cb9322acebeabaad984e59df350ae0d88367843d470ad45c966a3d6130c5e3dd31953e04aa7c72d7e80535c5829c72177009e898d21000851ddcfe5d09130dd7505a45ac28c042ef8c0a647b6e170543ff8a170ac6eabff5a10d90ffe825c6cf4d619cf6b9e02043c2e6d2d7b8b59b00"),
    50  			},
    51  		},
    52  	}
    53  	for i, test := range tests {
    54  		e, err := parseRoot(test.input)
    55  		if !reflect.DeepEqual(e, test.e) {
    56  			t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
    57  		}
    58  		if err != test.err {
    59  			t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
    60  		}
    61  	}
    62  }
    63  
    64  func TestParseEntry(t *testing.T) {
    65  	testkey := testKey(nodesSeed1)
    66  	pub := testkey.PublicKey()
    67  	tests := []struct {
    68  		input string
    69  		e     entry
    70  		err   error
    71  	}{
    72  		// Subtrees:
    73  		{
    74  			input: "enrtree-branch:1,2",
    75  			err:   entryError{"branch", errInvalidChild},
    76  		},
    77  		{
    78  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    79  			err:   entryError{"branch", errInvalidChild},
    80  		},
    81  		{
    82  			input: "enrtree-branch:",
    83  			e:     &branchEntry{},
    84  		},
    85  		{
    86  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAA",
    87  			e:     &branchEntry{[]string{"AAAAAAAAAAAAAAAAAAAA"}},
    88  		},
    89  		{
    90  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAA,BBBBBBBBBBBBBBBBBBBB",
    91  			e:     &branchEntry{[]string{"AAAAAAAAAAAAAAAAAAAA", "BBBBBBBBBBBBBBBBBBBB"}},
    92  		},
    93  		// Links
    94  		{
    95  			input: "enrtree://SALGRL5CVC6ZXZSAJ5HJWZ5X5I2MURYEE7MRPZFFS3FRN4OJJDC2ISGFNZOKZPAUC3B2SOWTTAX4F3GD6VOR777KM2AA@nodes.example.org",
    96  			e:     &linkEntry{"SALGRL5CVC6ZXZSAJ5HJWZ5X5I2MURYEE7MRPZFFS3FRN4OJJDC2ISGFNZOKZPAUC3B2SOWTTAX4F3GD6VOR777KM2AA@nodes.example.org", "nodes.example.org", pub},
    97  		},
    98  		{
    99  			input: "enrtree://nodes.example.org",
   100  			err:   entryError{"link", errNoPubkey},
   101  		},
   102  		{
   103  			input: "enrtree://AP62DT7WOTEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57@nodes.example.org",
   104  			err:   entryError{"link", errBadPubkey},
   105  		},
   106  		{
   107  			input: "enrtree://AP62DT7WONEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57TQHGIA@nodes.example.org",
   108  			err:   entryError{"link", errBadPubkey},
   109  		},
   110  		// ENRs
   111  		{
   112  			input: "enr:-Pm4qzzDbjdzIJFOmZh6d1MP3dEFux7I5rEVg5jJ9kJHHli36IDCVM5L0bm1_MmD-KdxejveeMGCJ05_AHeBVyk5UqI1s7z1Tl1fabjz2RKwFI_eO8kEYZTY__1uzGDKL7GmeVSaw1Yzdc13QGgRYQfks3UpAJAWaK-iqL2b5kBPTptnt-o0ykcEJ9kX5KWWyxbxyUjFpEjFblysvBQWw6k605gvwuzD9V0f_-pmgICCaWSCdjSJc2VjcDI1NmsxuDmQFmivoqi9m-ZAT06bZ7fqNMpHBCfZF-SllssW8clIxaRIxW5crLwUFsOpOtOYL8Lsw_VdH__qZoA",
   113  			e:     &enrEntry{node: testNode(nodesSeed1)},
   114  		},
   115  		{
   116  			input: "enr:-HW4QLZHjM4vZXkbp-5xJoHsKSbE7W39FPC8283X-y8oHcHPTnDDlIlzL5ArvDUlHZVDPgmFASrh7cWgLOLxj4wprRkHgmlkgnY0iXNlY3AyNTZrMaEC3t2jLMhDpCDX5mbSEwDn4L3iUfyXzoO8G28XvjGRkrAg=",
   117  			err:   entryError{"enr", errInvalidENR},
   118  		},
   119  		// Invalid:
   120  		{input: "", err: errUnknownEntry},
   121  		{input: "foo", err: errUnknownEntry},
   122  		{input: "enrtree", err: errUnknownEntry},
   123  		{input: "enrtree-x=", err: errUnknownEntry},
   124  	}
   125  	for i, test := range tests {
   126  		e, err := parseEntry(test.input, enode.ValidSchemes)
   127  		if !reflect.DeepEqual(e, test.e) {
   128  			t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
   129  		}
   130  		if err != test.err {
   131  			t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
   132  		}
   133  	}
   134  }
   135  
   136  func TestMakeTree(t *testing.T) {
   137  	nodes := testNodes(nodesSeed2, 50)
   138  	tree, err := MakeTree(2, nodes, nil)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	txt := tree.ToTXT("")
   143  	if len(txt) < len(nodes)+1 {
   144  		t.Fatal("too few TXT records in output")
   145  	}
   146  }