github.com/csquan/dpos-go-ethereum@v1.9.7/p2p/dnsdisc/tree_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum 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  	"github.com/ethereum/go-ethereum/common/hexutil"
    25  	"github.com/ethereum/go-ethereum/p2p/enode"
    26  )
    27  
    28  func TestParseRoot(t *testing.T) {
    29  	tests := []struct {
    30  		input string
    31  		e     rootEntry
    32  		err   error
    33  	}{
    34  		{
    35  			input: "enrtree-root:v1 e=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3 sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtw",
    36  			err:   entryError{"root", errSyntax},
    37  		},
    38  		{
    39  			input: "enrtree-root:v1 e=TO4Q75OQ2N7DX4EOOR7X66A6OM l=TO4Q75OQ2N7DX4EOOR7X66A6OM seq=3 sig=N-YY6UB9xD0hFx1Gmnt7v0RfSxch5tKyry2SRDoLx7B4GfPXagwLxQqyf7gAMvApFn_ORwZQekMWa_pXrcGCtw",
    40  			err:   entryError{"root", errInvalidSig},
    41  		},
    42  		{
    43  			input: "enrtree-root:v1 e=QFT4PBCRX4XQCV3VUYJ6BTCEPU l=JGUFMSAGI7KZYB3P7IZW4S5Y3A seq=3 sig=3FmXuVwpa8Y7OstZTx9PIb1mt8FrW7VpDOFv4AaGCsZ2EIHmhraWhe4NxYhQDlw5MjeFXYMbJjsPeKlHzmJREQE",
    44  			e: rootEntry{
    45  				eroot: "QFT4PBCRX4XQCV3VUYJ6BTCEPU",
    46  				lroot: "JGUFMSAGI7KZYB3P7IZW4S5Y3A",
    47  				seq:   3,
    48  				sig:   hexutil.MustDecode("0xdc5997b95c296bc63b3acb594f1f4f21bd66b7c16b5bb5690ce16fe006860ac6761081e686b69685ee0dc588500e5c393237855d831b263b0f78a947ce62511101"),
    49  			},
    50  		},
    51  	}
    52  	for i, test := range tests {
    53  		e, err := parseRoot(test.input)
    54  		if !reflect.DeepEqual(e, test.e) {
    55  			t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
    56  		}
    57  		if err != test.err {
    58  			t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
    59  		}
    60  	}
    61  }
    62  
    63  func TestParseEntry(t *testing.T) {
    64  	testkey := testKey(signingKeySeed)
    65  	tests := []struct {
    66  		input string
    67  		e     entry
    68  		err   error
    69  	}{
    70  		// Subtrees:
    71  		{
    72  			input: "enrtree-branch:1,2",
    73  			err:   entryError{"branch", errInvalidChild},
    74  		},
    75  		{
    76  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    77  			err:   entryError{"branch", errInvalidChild},
    78  		},
    79  		{
    80  			input: "enrtree-branch:",
    81  			e:     &branchEntry{},
    82  		},
    83  		{
    84  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAA",
    85  			e:     &branchEntry{[]string{"AAAAAAAAAAAAAAAAAAAA"}},
    86  		},
    87  		{
    88  			input: "enrtree-branch:AAAAAAAAAAAAAAAAAAAA,BBBBBBBBBBBBBBBBBBBB",
    89  			e:     &branchEntry{[]string{"AAAAAAAAAAAAAAAAAAAA", "BBBBBBBBBBBBBBBBBBBB"}},
    90  		},
    91  		// Links
    92  		{
    93  			input: "enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@nodes.example.org",
    94  			e:     &linkEntry{"nodes.example.org", &testkey.PublicKey},
    95  		},
    96  		{
    97  			input: "enrtree://nodes.example.org",
    98  			err:   entryError{"link", errNoPubkey},
    99  		},
   100  		{
   101  			input: "enrtree://AP62DT7WOTEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57@nodes.example.org",
   102  			err:   entryError{"link", errBadPubkey},
   103  		},
   104  		{
   105  			input: "enrtree://AP62DT7WONEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57TQHGIA@nodes.example.org",
   106  			err:   entryError{"link", errBadPubkey},
   107  		},
   108  		// ENRs
   109  		{
   110  			input: "enr:-HW4QES8QIeXTYlDzbfr1WEzE-XKY4f8gJFJzjJL-9D7TC9lJb4Z3JPRRz1lP4pL_N_QpT6rGQjAU9Apnc-C1iMP36OAgmlkgnY0iXNlY3AyNTZrMaED5IdwfMxdmR8W37HqSFdQLjDkIwBd4Q_MjxgZifgKSdM",
   111  			e:     &enrEntry{node: testNode(nodesSeed1)},
   112  		},
   113  		{
   114  			input: "enr:-HW4QLZHjM4vZXkbp-5xJoHsKSbE7W39FPC8283X-y8oHcHPTnDDlIlzL5ArvDUlHZVDPgmFASrh7cWgLOLxj4wprRkHgmlkgnY0iXNlY3AyNTZrMaEC3t2jLMhDpCDX5mbSEwDn4L3iUfyXzoO8G28XvjGRkrAg=",
   115  			err:   entryError{"enr", errInvalidENR},
   116  		},
   117  		// Invalid:
   118  		{input: "", err: errUnknownEntry},
   119  		{input: "foo", err: errUnknownEntry},
   120  		{input: "enrtree", err: errUnknownEntry},
   121  		{input: "enrtree-x=", err: errUnknownEntry},
   122  	}
   123  	for i, test := range tests {
   124  		e, err := parseEntry(test.input, enode.ValidSchemes)
   125  		if !reflect.DeepEqual(e, test.e) {
   126  			t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
   127  		}
   128  		if err != test.err {
   129  			t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
   130  		}
   131  	}
   132  }
   133  
   134  func TestMakeTree(t *testing.T) {
   135  	nodes := testNodes(nodesSeed2, 50)
   136  	tree, err := MakeTree(2, nodes, nil)
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	txt := tree.ToTXT("")
   141  	if len(txt) < len(nodes)+1 {
   142  		t.Fatal("too few TXT records in output")
   143  	}
   144  }