github.com/theQRL/go-zond@v0.1.1/p2p/dnsdisc/tree_test.go (about) 1 // Copyright 2019 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/theQRL/go-zond/common/hexutil" 25 "github.com/theQRL/go-zond/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 testENRs := []string{"enr:-HW4QES8QIeXTYlDzbfr1WEzE-XKY4f8gJFJzjJL-9D7TC9lJb4Z3JPRRz1lP4pL_N_QpT6rGQjAU9Apnc-C1iMP36OAgmlkgnY0iXNlY3AyNTZrMaED5IdwfMxdmR8W37HqSFdQLjDkIwBd4Q_MjxgZifgKSdM"} 65 testNodes := parseNodes(testENRs) 66 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://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@nodes.example.org", 96 e: &linkEntry{ 97 str: "AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@nodes.example.org", 98 domain: "nodes.example.org", 99 pubkey: &signingKeyForTesting.PublicKey, 100 }, 101 }, 102 { 103 input: "enrtree://nodes.example.org", 104 err: entryError{"link", errNoPubkey}, 105 }, 106 { 107 input: "enrtree://AP62DT7WOTEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57@nodes.example.org", 108 err: entryError{"link", errBadPubkey}, 109 }, 110 { 111 input: "enrtree://AP62DT7WONEQZGQZOU474PP3KMEGVTTE7A7NPRXKX3DUD57TQHGIA@nodes.example.org", 112 err: entryError{"link", errBadPubkey}, 113 }, 114 // ENRs 115 { 116 input: testENRs[0], 117 e: &enrEntry{node: testNodes[0]}, 118 }, 119 { 120 input: "enr:-HW4QLZHjM4vZXkbp-5xJoHsKSbE7W39FPC8283X-y8oHcHPTnDDlIlzL5ArvDUlHZVDPgmFASrh7cWgLOLxj4wprRkHgmlkgnY0iXNlY3AyNTZrMaEC3t2jLMhDpCDX5mbSEwDn4L3iUfyXzoO8G28XvjGRkrAg=", 121 err: entryError{"enr", errInvalidENR}, 122 }, 123 // Invalid: 124 {input: "", err: errUnknownEntry}, 125 {input: "foo", err: errUnknownEntry}, 126 {input: "enrtree", err: errUnknownEntry}, 127 {input: "enrtree-x=", err: errUnknownEntry}, 128 } 129 for i, test := range tests { 130 e, err := parseEntry(test.input, enode.ValidSchemes) 131 if !reflect.DeepEqual(e, test.e) { 132 t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e)) 133 } 134 if err != test.err { 135 t.Errorf("test %d: wrong error %q, want %q", i, err, test.err) 136 } 137 } 138 } 139 140 func TestMakeTree(t *testing.T) { 141 keys := testKeys(50) 142 nodes := testNodes(keys) 143 tree, err := MakeTree(2, nodes, nil) 144 if err != nil { 145 t.Fatal(err) 146 } 147 txt := tree.ToTXT("") 148 if len(txt) < len(nodes)+1 { 149 t.Fatal("too few TXT records in output") 150 } 151 }