github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/swarm/swarm_test.go (about)

     1  // Copyright 2017 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 swarm
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  func TestParseEnsAPIAddress(t *testing.T) {
    26  	for _, x := range []struct {
    27  		description string
    28  		value       string
    29  		tld         string
    30  		endpoint    string
    31  		addr        common.Address
    32  	}{
    33  		{
    34  			description: "IPC endpoint",
    35  			value:       "/data/testnet/geth.ipc",
    36  			endpoint:    "/data/testnet/geth.ipc",
    37  		},
    38  		{
    39  			description: "HTTP endpoint",
    40  			value:       "http://127.0.0.1:1234",
    41  			endpoint:    "http://127.0.0.1:1234",
    42  		},
    43  		{
    44  			description: "WS endpoint",
    45  			value:       "ws://127.0.0.1:1234",
    46  			endpoint:    "ws://127.0.0.1:1234",
    47  		},
    48  		{
    49  			description: "IPC Endpoint and TLD",
    50  			value:       "test:/data/testnet/geth.ipc",
    51  			endpoint:    "/data/testnet/geth.ipc",
    52  			tld:         "test",
    53  		},
    54  		{
    55  			description: "HTTP endpoint and TLD",
    56  			value:       "test:http://127.0.0.1:1234",
    57  			endpoint:    "http://127.0.0.1:1234",
    58  			tld:         "test",
    59  		},
    60  		{
    61  			description: "WS endpoint and TLD",
    62  			value:       "test:ws://127.0.0.1:1234",
    63  			endpoint:    "ws://127.0.0.1:1234",
    64  			tld:         "test",
    65  		},
    66  		{
    67  			description: "IPC Endpoint and contract address",
    68  			value:       "314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
    69  			endpoint:    "/data/testnet/geth.ipc",
    70  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
    71  		},
    72  		{
    73  			description: "HTTP endpoint and contract address",
    74  			value:       "314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
    75  			endpoint:    "http://127.0.0.1:1234",
    76  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
    77  		},
    78  		{
    79  			description: "WS endpoint and contract address",
    80  			value:       "314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
    81  			endpoint:    "ws://127.0.0.1:1234",
    82  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
    83  		},
    84  		{
    85  			description: "IPC Endpoint, TLD and contract address",
    86  			value:       "test:314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
    87  			endpoint:    "/data/testnet/geth.ipc",
    88  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
    89  			tld:         "test",
    90  		},
    91  		{
    92  			description: "HTTP endpoint, TLD and contract address",
    93  			value:       "eth:314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
    94  			endpoint:    "http://127.0.0.1:1234",
    95  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
    96  			tld:         "eth",
    97  		},
    98  		{
    99  			description: "WS endpoint, TLD and contract address",
   100  			value:       "eth:314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
   101  			endpoint:    "ws://127.0.0.1:1234",
   102  			addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
   103  			tld:         "eth",
   104  		},
   105  	} {
   106  		t.Run(x.description, func(t *testing.T) {
   107  			tld, endpoint, addr := parseEnsAPIAddress(x.value)
   108  			if endpoint != x.endpoint {
   109  				t.Errorf("expected Endpoint %q, got %q", x.endpoint, endpoint)
   110  			}
   111  			if addr != x.addr {
   112  				t.Errorf("expected ContractAddress %q, got %q", x.addr.String(), addr.String())
   113  			}
   114  			if tld != x.tld {
   115  				t.Errorf("expected TLD %q, got %q", x.tld, tld)
   116  			}
   117  		})
   118  	}
   119  }