go.dedis.ch/onet/v4@v4.0.0-pre1/network/address_test.go (about)

     1  package network
     2  
     3  import (
     4  	"testing"
     5  
     6  	"net"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"go.dedis.ch/onet/v4/log"
    11  )
    12  
    13  func TestConnType(t *testing.T) {
    14  	var tests = []struct {
    15  		Value    string
    16  		Expected ConnType
    17  	}{
    18  		{"tcp", PlainTCP},
    19  		{"tls", TLS},
    20  		{"tcp4", InvalidConnType},
    21  		{"_tls", InvalidConnType},
    22  	}
    23  
    24  	for _, str := range tests {
    25  		if connType(str.Value) != str.Expected {
    26  			t.Error("Wrong ConnType for " + str.Value)
    27  		}
    28  	}
    29  }
    30  
    31  var staticHostIPMapping = make(map[string]string)
    32  
    33  func dummyResolver(s string) ([]string, error) {
    34  	return []string{staticHostIPMapping[s]}, nil
    35  }
    36  
    37  func TestAddress(t *testing.T) {
    38  	lookupHost = dummyResolver
    39  	defer func() {
    40  		lookupHost = net.LookupHost
    41  	}()
    42  	staticHostIPMapping["google.com"] = "8.8.8.8"
    43  	staticHostIPMapping["facebook.com"] = "20.20.20.20"
    44  	staticHostIPMapping["epfl.ch"] = "100.100.100.100"
    45  	staticHostIPMapping["localhost"] = "127.0.0.1"
    46  	staticHostIPMapping["ipv6.localhost"] = "::1"
    47  	staticHostIPMapping["ipv6.epfl.ch"] = "2001:620:618:10f:1:80b2:f08:1"
    48  	staticHostIPMapping["ipv6.locala"] = "fd::1"
    49  	staticHostIPMapping["ipv6.localb"] = "fda::1"
    50  	staticHostIPMapping["ipv6.localc"] = "fda9::1"
    51  	var tests = []struct {
    52  		Value           string
    53  		Valid           bool
    54  		Type            ConnType
    55  		Address         string
    56  		Host            string
    57  		Port            string
    58  		Public          bool
    59  		ResolvedHost    string
    60  		ResolvedAddress string
    61  	}{
    62  		{"tls://10.0.0.4:2000", true, TLS, "10.0.0.4:2000", "10.0.0.4", "2000", false, "10.0.0.4", "10.0.0.4:2000"},
    63  		{"tcp://10.0.0.4:2000", true, PlainTCP, "10.0.0.4:2000", "10.0.0.4", "2000", false, "10.0.0.4", "10.0.0.4:2000"},
    64  		{"tcp://67.43.129.85:2000", true, PlainTCP, "67.43.129.85:2000", "67.43.129.85", "2000", true, "67.43.129.85", "67.43.129.85:2000"},
    65  		{"tls://[::]:1000", true, TLS, "[::]:1000", "::", "1000", true, "::", "[::]:1000"},
    66  		{"tls4://10.0.0.4:2000", false, InvalidConnType, "", "", "", false, "", ""},
    67  		{"tls://1000.0.0.4:2000", false, InvalidConnType, "", "", "", false, "", ""},
    68  		{"tls://10.0.0.4:20000000", false, InvalidConnType, "", "", "", false, "", ""},
    69  		{"tls://10.0.0.4:-10", false, InvalidConnType, "", "", "", false, "", ""},
    70  		{"tlsx10.0.0.4:2000", false, InvalidConnType, "", "", "", false, "", ""},
    71  		{"tls:10.0.0.4x2000", false, InvalidConnType, "", "", "", false, "", ""},
    72  		{"tlsx10.0.0.4x2000", false, InvalidConnType, "", "", "", false, "", ""},
    73  		{"tlxblurdie", false, InvalidConnType, "", "", "", false, "", ""},
    74  		{"tls://blublublu", false, InvalidConnType, "", "", "", false, "", ""},
    75  		// dummy values for the IP addresses, defined by dummyResolver
    76  		{"tcp://localhost:80", true, PlainTCP, "localhost:80", "localhost", "80", false, "127.0.0.1", "127.0.0.1:80"},
    77  		{"tcp://ipv6.localhost:80", true, PlainTCP, "ipv6.localhost:80", "ipv6.localhost", "80", false, "::1", "[::1]:80"},
    78  		{"tcp://facebook.com:8080", true, PlainTCP, "facebook.com:8080", "facebook.com", "8080", true, "20.20.20.20", "20.20.20.20:8080"},
    79  		{"tls://google.com:80", true, TLS, "google.com:80", "google.com", "80", true, "8.8.8.8", "8.8.8.8:80"},
    80  		{"tcp://epfl.ch:8080", true, PlainTCP, "epfl.ch:8080", "epfl.ch", "8080", true, "100.100.100.100", "100.100.100.100:8080"},
    81  		{"tcp://ipv6.epfl.ch:8080", true, PlainTCP, "ipv6.epfl.ch:8080", "ipv6.epfl.ch", "8080", true, "2001:620:618:10f:1:80b2:f08:1", "[2001:620:618:10f:1:80b2:f08:1]:8080"},
    82  		{"tcp://ipv6.locala:80", true, PlainTCP, "ipv6.locala:80", "ipv6.locala", "80", false, "fd::1", "[fd::1]:80"},
    83  		{"tcp://ipv6.localb:80", true, PlainTCP, "ipv6.localb:80", "ipv6.localb", "80", false, "fda::1", "[fda::1]:80"},
    84  		{"tcp://ipv6.localc:80", true, PlainTCP, "ipv6.localc:80", "ipv6.localc", "80", false, "fda9::1", "[fda9::1]:80"},
    85  	}
    86  
    87  	for i, str := range tests {
    88  		log.Lvlf1("Testing %+v", str)
    89  		add := Address(str.Value)
    90  		require.Equal(t, str.Valid, add.Valid(), "Address (%d) %s", i, str.Value)
    91  		require.Equal(t, str.Type, add.ConnType(), "Address (%d) %s", i, str.Value)
    92  		require.Equal(t, str.Address, add.NetworkAddress())
    93  		require.Equal(t, str.Host, add.Host())
    94  		require.Equal(t, str.Port, add.Port())
    95  		require.Equal(t, str.Public, add.Public())
    96  		require.Equal(t, str.ResolvedHost, add.Resolve())
    97  		require.Equal(t, str.ResolvedAddress, add.NetworkAddressResolved())
    98  	}
    99  }
   100  
   101  // Isolated test case for validHostname
   102  func TestDNSNames(t *testing.T) {
   103  	assert.True(t, validHostname("myhost.secondlabel.org"))
   104  	assert.True(t, validHostname("www.asd.lol.xd"))
   105  	assert.True(t, validHostname("a.a"))
   106  	assert.True(t, validHostname("localhost"))
   107  	assert.True(t, validHostname("www.asd.lol.xd"))
   108  	assert.True(t, validHostname("randomtext"))
   109  	assert.True(t, validHostname("conode1"))
   110  	assert.False(t, validHostname("www.asd.lol.x-d"))
   111  	assert.False(t, validHostname("192.168.1.1"))
   112  	assert.False(t, validHostname("..a"))
   113  	assert.False(t, validHostname("a..a"))
   114  	assert.False(t, validHostname("123213.213"))
   115  	assert.False(t, validHostname("-23.dwe"))
   116  	assert.False(t, validHostname("..."))
   117  	assert.False(t, validHostname("www.asd.lol.xd-"))
   118  }