github.com/ethersphere/bee/v2@v2.2.0/pkg/resolver/multiresolver/config_test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package multiresolver_test 6 7 import ( 8 "errors" 9 "testing" 10 11 "github.com/ethersphere/bee/v2/pkg/resolver/multiresolver" 12 ) 13 14 func TestParseConnectionStrings(t *testing.T) { 15 t.Parallel() 16 17 testCases := []struct { 18 desc string 19 conStrings []string 20 wantCfg []multiresolver.ConnectionConfig 21 wantErr error 22 }{ 23 { 24 // Defined as per RFC 1034. For reference, see: 25 // https://en.wikipedia.org/wiki/Domain_Name_System#cite_note-rfc1034-1 26 desc: "tld too long", 27 conStrings: []string{ 28 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff:example.com", 29 }, 30 wantErr: multiresolver.ErrTLDTooLong, 31 }, 32 { 33 desc: "single endpoint default tld", 34 conStrings: []string{ 35 "https://example.com", 36 }, 37 wantCfg: []multiresolver.ConnectionConfig{ 38 { 39 TLD: "", 40 Endpoint: "https://example.com", 41 }, 42 }, 43 }, 44 { 45 desc: "single endpoint explicit tld", 46 conStrings: []string{ 47 "tld:https://example.com", 48 }, 49 wantCfg: []multiresolver.ConnectionConfig{ 50 { 51 TLD: "tld", 52 Endpoint: "https://example.com", 53 }, 54 }, 55 }, 56 { 57 desc: "single endpoint with address default tld", 58 conStrings: []string{ 59 "0x314159265dD8dbb310642f98f50C066173C1259b@https://example.com", 60 }, 61 wantCfg: []multiresolver.ConnectionConfig{ 62 { 63 TLD: "", 64 Address: "0x314159265dD8dbb310642f98f50C066173C1259b", 65 Endpoint: "https://example.com", 66 }, 67 }, 68 }, 69 { 70 desc: "single endpoint with address explicit tld", 71 conStrings: []string{ 72 "tld:0x314159265dD8dbb310642f98f50C066173C1259b@https://example.com", 73 }, 74 wantCfg: []multiresolver.ConnectionConfig{ 75 { 76 TLD: "tld", 77 Address: "0x314159265dD8dbb310642f98f50C066173C1259b", 78 Endpoint: "https://example.com", 79 }, 80 }, 81 }, 82 { 83 desc: "mixed", 84 conStrings: []string{ 85 "tld:https://example.com", 86 "testdomain:wowzers.map", 87 "yesyesyes:0x314159265dD8dbb310642f98f50C066173C1259b@2.2.2.2", 88 "cloudflare-ethereum.org", 89 }, 90 wantCfg: []multiresolver.ConnectionConfig{ 91 { 92 TLD: "tld", 93 Endpoint: "https://example.com", 94 }, 95 { 96 TLD: "testdomain", 97 Endpoint: "wowzers.map", 98 }, 99 { 100 TLD: "yesyesyes", 101 Address: "0x314159265dD8dbb310642f98f50C066173C1259b", 102 Endpoint: "2.2.2.2", 103 }, 104 { 105 TLD: "", 106 Endpoint: "cloudflare-ethereum.org", 107 }, 108 }, 109 }, 110 { 111 desc: "mixed with error", 112 conStrings: []string{ 113 "tld:https://example.com", 114 "testdomain:wowzers.map", 115 "nonononononononononononononononononononononononononononononononononono:yes", 116 }, 117 wantErr: multiresolver.ErrTLDTooLong, 118 }, 119 } 120 for _, tC := range testCases { 121 tC := tC 122 t.Run(tC.desc, func(t *testing.T) { 123 t.Parallel() 124 125 got, err := multiresolver.ParseConnectionStrings(tC.conStrings) 126 if err != nil { 127 if !errors.Is(err, tC.wantErr) { 128 t.Errorf("got error %v", err) 129 } 130 return 131 } 132 133 for i, el := range got { 134 want := tC.wantCfg[i] 135 got := el 136 if got.TLD != want.TLD { 137 t.Errorf("got %q, want %q", got.TLD, want.TLD) 138 } 139 if got.Address != want.Address { 140 t.Errorf("got %q, want %q", got.Address, want.Address) 141 } 142 if got.Endpoint != want.Endpoint { 143 t.Errorf("got %q, want %q", got.Endpoint, want.Endpoint) 144 } 145 } 146 }) 147 } 148 }