github.com/status-im/status-go@v1.1.0/server/pairing/connection_test.go (about)

     1  package pairing
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"sort"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/suite"
    11  
    12  	"github.com/status-im/status-go/server"
    13  	"github.com/status-im/status-go/server/servertest"
    14  )
    15  
    16  const (
    17  	connectionString = "cs3:kDDauj5:Q4:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6:XxovYsfDefUHFJy8U98wtV:3BM1jGMPFuHMhJqEB"
    18  
    19  	connectionString229Compatibility = "cs3:kDDauj5:Q4:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6"
    20  	port                             = 1337
    21  )
    22  
    23  func TestConnectionParamsSuite(t *testing.T) {
    24  	suite.Run(t, new(ConnectionParamsSuite))
    25  }
    26  
    27  type ConnectionParamsSuite struct {
    28  	suite.Suite
    29  	servertest.TestKeyComponents
    30  	servertest.TestCertComponents
    31  	servertest.TestLoggerComponents
    32  
    33  	server *BaseServer
    34  }
    35  
    36  func (s *ConnectionParamsSuite) SetupSuite() {
    37  	s.SetupKeyComponents(s.T())
    38  	s.SetupCertComponents(s.T())
    39  	s.SetupLoggerComponents()
    40  
    41  	ip := server.LocalHostIP
    42  	ips := []net.IP{ip}
    43  
    44  	cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, ips, []string{})
    45  	s.Require().NoError(err)
    46  
    47  	sc := ServerConfig{
    48  		PK:             &s.PK.PublicKey,
    49  		EK:             s.AES,
    50  		Cert:           &cert,
    51  		IPAddresses:    ips,
    52  		ListenIP:       net.IPv4zero,
    53  		InstallationID: "fabcfc11-6ed9-46d1-b723-de5d4ad1657a",
    54  		KeyUID:         "some-key-uid",
    55  	}
    56  
    57  	bs := server.NewServer(&cert, net.IPv4zero.String(), nil, s.Logger)
    58  	err = bs.SetPort(port)
    59  	s.Require().NoError(err)
    60  
    61  	s.server = &BaseServer{
    62  		Server: bs,
    63  		config: sc,
    64  	}
    65  }
    66  
    67  func (s *ConnectionParamsSuite) TestConnectionParams_ToString() {
    68  	keep229Compatibility = false
    69  	cp, err := s.server.MakeConnectionParams()
    70  	s.Require().NoError(err)
    71  
    72  	cps := cp.ToString()
    73  	s.Require().Equal(connectionString, cps)
    74  }
    75  
    76  func (s *ConnectionParamsSuite) TestConnectionParams_Generate() {
    77  
    78  	testCases := []struct {
    79  		description string
    80  		cs          string
    81  	}{
    82  		{description: "ConnectionString", cs: connectionString},
    83  	}
    84  
    85  	for _, tc := range testCases {
    86  		s.T().Run(tc.description, func(t *testing.T) {
    87  			cp := new(ConnectionParams)
    88  			err := cp.FromString(connectionString)
    89  			s.Require().NoError(err)
    90  
    91  			u, err := cp.URL(0)
    92  			s.Require().NoError(err)
    93  
    94  			expectedURL := fmt.Sprintf("https://%s:%d", server.LocalHostIP.String(), port)
    95  
    96  			s.Require().Equal(expectedURL, u.String())
    97  			s.Require().Equal(server.LocalHostIP.String(), u.Hostname())
    98  			s.Require().Equal(strconv.Itoa(port), u.Port())
    99  
   100  			s.Require().True(cp.publicKey.Equal(&s.PK.PublicKey))
   101  			s.Require().Equal(s.AES, cp.aesKey)
   102  		})
   103  	}
   104  }
   105  
   106  func (s *ConnectionParamsSuite) TestConnectionParams_ParseNetIps() {
   107  
   108  	in := []net.IP{
   109  		{192, 168, 1, 42},
   110  		net.ParseIP("fe80::6fd7:5ce4:554f:165a"),
   111  		{172, 16, 9, 1},
   112  		net.ParseIP("fe80::ffa5:98e1:285c:42eb"),
   113  		net.ParseIP("fe80::c1f:ee0d:1476:dd9a"),
   114  	}
   115  	bytes := SerializeNetIps(in)
   116  
   117  	s.Require().Equal(bytes,
   118  		[]byte{
   119  			2,               // v4 count
   120  			192, 168, 1, 42, // v4 1
   121  			172, 16, 9, 1, // v4 2
   122  			3,                                                                            // v6 count
   123  			0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x6f, 0xd7, 0x5c, 0xe4, 0x55, 0x4f, 0x16, 0x5a, // v6 1
   124  			0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0xff, 0xa5, 0x98, 0xe1, 0x28, 0x5c, 0x42, 0xeb, // v6 2
   125  			0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x0c, 0x1f, 0xee, 0x0d, 0x14, 0x76, 0xdd, 0x9a, // v6 3
   126  		})
   127  
   128  	out, err := ParseNetIps(bytes)
   129  
   130  	s.Require().NoError(err)
   131  	s.Require().Len(in, 5)
   132  
   133  	sort.SliceStable(in, func(i, j int) bool {
   134  		return in[i].String() < in[j].String()
   135  	})
   136  	sort.SliceStable(out, func(i, j int) bool {
   137  		return out[i].String() < out[j].String()
   138  	})
   139  
   140  	s.Require().Equal(in, out)
   141  }
   142  
   143  func (s *ConnectionParamsSuite) TestParse229() {
   144  	cp := new(ConnectionParams)
   145  	s.Require().NoError(cp.FromString(connectionString229Compatibility))
   146  }
   147  
   148  func (s *ConnectionParamsSuite) TestParseConnectionStringWithKeyUIDAndInstallationID() {
   149  	cp := new(ConnectionParams)
   150  	err := cp.FromString(connectionString)
   151  	s.Require().NoError(err)
   152  
   153  	s.Require().NotEmpty(cp.InstallationID)
   154  	s.Require().NotEmpty(cp.KeyUID)
   155  }