github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/p2p/discover/dht/dns_seeds_test.go (about) 1 package dht 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/bytom/bytom/consensus" 8 "github.com/davecgh/go-spew/spew" 9 ) 10 11 var testnetAddr = []string{"1.2.3.4", "5.6.7.8"} 12 var mainnetAddr = []string{"11.22.33.44", "55.66.77.88"} 13 var errAddr = []string{"a.b.ab.abc", "55.66.77.88"} 14 15 func lookupHostNormal(host string) ([]string, error) { 16 switch host { 17 case consensus.MainNetParams.DNSSeeds[0]: 18 return mainnetAddr, nil 19 case consensus.TestNetParams.DNSSeeds[0]: 20 return testnetAddr, nil 21 } 22 return nil, nil 23 } 24 25 func lookupHostErrIP(host string) ([]string, error) { 26 return errAddr, nil 27 } 28 29 var testCases = []struct { 30 chainID string 31 lookupHost func(host string) (addrs []string, err error) 32 wantErr error 33 wantAddr []string 34 }{ 35 { 36 chainID: "wisdom", 37 lookupHost: lookupHostNormal, 38 wantErr: nil, 39 wantAddr: []string{"1.2.3.4:46656", "5.6.7.8:46656"}, 40 }, 41 { 42 chainID: "mainnet", 43 lookupHost: lookupHostNormal, 44 wantErr: nil, 45 wantAddr: []string{"11.22.33.44:46657", "55.66.77.88:46657"}, 46 }, 47 { 48 chainID: "solonet", 49 lookupHost: lookupHostNormal, 50 wantErr: errDNSSeedsEmpty, 51 wantAddr: nil, 52 }, 53 { 54 chainID: "wisdom", 55 lookupHost: lookupHostErrIP, 56 wantErr: errDNSTimeout, 57 wantAddr: nil, 58 }, 59 } 60 61 func TestQueryDNSSeeds(t *testing.T) { 62 for i, tc := range testCases { 63 consensus.ActiveNetParams = consensus.NetParams[tc.chainID] 64 addresses, err := QueryDNSSeeds(tc.lookupHost) 65 if err != tc.wantErr { 66 t.Fatalf("test %d: error mismatch for query dns seed got %q want %q", i, err, tc.wantErr) 67 } 68 69 if !reflect.DeepEqual(addresses, tc.wantAddr) { 70 t.Fatalf("test %d: result mismatch for query dns seed got %s want %s", i, spew.Sdump(addresses), spew.Sdump(tc.wantAddr)) 71 } 72 } 73 }