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

     1  package network
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"go.dedis.ch/kyber/v4/util/key"
     8  	"go.dedis.ch/onet/v4/log"
     9  )
    10  
    11  func TestServerIdentity(t *testing.T) {
    12  	log.OutputToBuf()
    13  	defer log.OutputToOs()
    14  	kp1 := key.NewKeyPair(tSuite)
    15  	kp2 := key.NewKeyPair(tSuite)
    16  
    17  	si1 := NewServerIdentity(kp1.Public, NewLocalAddress("1"))
    18  	si2 := NewServerIdentity(kp2.Public, NewLocalAddress("2"))
    19  
    20  	if si1.Equal(si2) || !si1.Equal(si1) {
    21  		t.Error("Stg's wrong with ServerIdentity")
    22  	}
    23  
    24  	if si1.ID.Equal(si2.ID) || !si1.ID.Equal(si1.ID) {
    25  		t.Error("Stg's wrong with ServerIdentityID")
    26  	}
    27  
    28  	t1 := si1.Toml(tSuite)
    29  	if t1.Address != si1.Address || t1.Address == "" {
    30  		t.Error("stg wrong with Toml()")
    31  	}
    32  
    33  	si11 := t1.ServerIdentity(tSuite)
    34  	if si11.Address != si1.Address || !si11.Public.Equal(si1.Public) {
    35  		t.Error("Stg wrong with toml -> Si")
    36  	}
    37  	t1.Public = ""
    38  	si12 := t1.ServerIdentity(tSuite)
    39  	if si12.Public != nil && si12.Public.Equal(si1.Public) {
    40  		t.Error("stg wrong with wrong toml -> wrong si")
    41  	}
    42  
    43  }
    44  
    45  func TestGlobalBind(t *testing.T) {
    46  	gb, err := GlobalBind("127.0.0.1:2000")
    47  	if err != nil {
    48  		t.Fatal("global bind err", err)
    49  	}
    50  	if gb != ":2000" {
    51  		t.Fatal("Wrong result", gb)
    52  	}
    53  
    54  	_, err = GlobalBind("127.0.0.12000")
    55  	if err == nil {
    56  		t.Fatal("Missing error for global bind")
    57  	}
    58  
    59  	// IPv6
    60  	gb, err = GlobalBind("[::1]:2000")
    61  	if err != nil {
    62  		t.Fatal("global bind err", err)
    63  	}
    64  	if gb != ":2000" {
    65  		t.Fatal("Wrong result", gb)
    66  	}
    67  }
    68  
    69  // TestServiceIdentity checks that service identities are instantiated
    70  // correctly and that we can access the keys
    71  func TestServiceIdentity(t *testing.T) {
    72  	kp := key.NewKeyPair(tSuite)
    73  	si := NewServerIdentity(kp.Public, NewLocalAddress("1"))
    74  	si.SetPrivate(kp.Private)
    75  
    76  	pub := tSuite.Point()
    77  	priv := tSuite.Scalar()
    78  	kp2 := key.NewKeyPair(tSuite)
    79  	si.ServiceIdentities = append(si.ServiceIdentities, NewServiceIdentity("a", tSuite, pub, priv))
    80  	si.ServiceIdentities = append(si.ServiceIdentities, NewServiceIdentityFromPair("b", tSuite, kp2))
    81  	si.ServiceIdentities = append(si.ServiceIdentities, NewServiceIdentity("d", tSuite, pub, nil))
    82  
    83  	require.Equal(t, pub, si.ServicePublic("a"))
    84  	require.Equal(t, priv, si.ServicePrivate("a"))
    85  	require.Equal(t, kp2.Public, si.ServicePublic("b"))
    86  	require.Equal(t, kp2.Private, si.ServicePrivate("b"))
    87  	require.Equal(t, kp.Public, si.ServicePublic("c"))
    88  	require.Equal(t, kp.Private, si.ServicePrivate("c"))
    89  	require.True(t, si.HasServiceKeyPair("a"))
    90  	require.True(t, si.HasServiceKeyPair("b"))
    91  	require.False(t, si.HasServiceKeyPair("c"))
    92  
    93  	require.True(t, si.HasServicePublic("a"))
    94  	require.True(t, si.HasServicePublic("b"))
    95  	require.False(t, si.HasServicePublic("c"))
    96  	require.True(t, si.HasServicePublic("d"))
    97  }