From 96b6b70f792106a622802ced209f17cfe7ed48e4 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Mon, 28 Oct 2019 20:38:22 +0300 Subject: [PATCH] Add app server tests, lint --- pkg/app2/appcommon/doc.go | 1 + pkg/app2/appserver/doc.go | 3 + pkg/app2/appserver/proc.go | 4 +- pkg/app2/appserver/proc_manager.go | 3 +- pkg/app2/appserver/rpc_gateway_test.go | 5 +- pkg/app2/appserver/server.go | 4 +- pkg/app2/appserver/server_test.go | 101 +++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 pkg/app2/appcommon/doc.go create mode 100644 pkg/app2/appserver/doc.go create mode 100644 pkg/app2/appserver/server_test.go diff --git a/pkg/app2/appcommon/doc.go b/pkg/app2/appcommon/doc.go new file mode 100644 index 0000000000..f60350b0fc --- /dev/null +++ b/pkg/app2/appcommon/doc.go @@ -0,0 +1 @@ +package appcommon diff --git a/pkg/app2/appserver/doc.go b/pkg/app2/appserver/doc.go new file mode 100644 index 0000000000..b4f4829b9e --- /dev/null +++ b/pkg/app2/appserver/doc.go @@ -0,0 +1,3 @@ +// Package appserver contains facilities +// for implementing the skywire app server. +package appserver diff --git a/pkg/app2/appserver/proc.go b/pkg/app2/appserver/proc.go index 21d0e2873b..4ba9873cf9 100644 --- a/pkg/app2/appserver/proc.go +++ b/pkg/app2/appserver/proc.go @@ -6,9 +6,9 @@ import ( "os/exec" "path/filepath" - "github.com/skycoin/skywire/pkg/app2/appcommon" - "github.com/skycoin/skycoin/src/util/logging" + + "github.com/skycoin/skywire/pkg/app2/appcommon" ) // Proc is a wrapper for a skywire app. Encapsulates diff --git a/pkg/app2/appserver/proc_manager.go b/pkg/app2/appserver/proc_manager.go index c5f5c128bd..4d92b425a6 100644 --- a/pkg/app2/appserver/proc_manager.go +++ b/pkg/app2/appserver/proc_manager.go @@ -6,11 +6,10 @@ import ( "os/exec" "sync" - "github.com/skycoin/skywire/pkg/app2/appcommon" - "github.com/pkg/errors" "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/app2/appcommon" ) var ( diff --git a/pkg/app2/appserver/rpc_gateway_test.go b/pkg/app2/appserver/rpc_gateway_test.go index b3e42dad4b..9d3294141c 100644 --- a/pkg/app2/appserver/rpc_gateway_test.go +++ b/pkg/app2/appserver/rpc_gateway_test.go @@ -7,16 +7,15 @@ import ( "strings" "testing" - "github.com/skycoin/skywire/pkg/app2/appcommon" - "github.com/skycoin/skywire/pkg/app2/idmanager" - "github.com/pkg/errors" "github.com/skycoin/dmsg" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/skycoin/src/util/logging" "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/pkg/app2/appcommon" "github.com/skycoin/skywire/pkg/app2/appnet" + "github.com/skycoin/skywire/pkg/app2/idmanager" "github.com/skycoin/skywire/pkg/routing" ) diff --git a/pkg/app2/appserver/server.go b/pkg/app2/appserver/server.go index 86221315f7..10a6fde1d1 100644 --- a/pkg/app2/appserver/server.go +++ b/pkg/app2/appserver/server.go @@ -6,10 +6,10 @@ import ( "net/rpc" "sync" - "github.com/skycoin/skywire/pkg/app2/appcommon" - "github.com/pkg/errors" "github.com/skycoin/skycoin/src/util/logging" + + "github.com/skycoin/skywire/pkg/app2/appcommon" ) // Server is a server for app/visor communication. diff --git a/pkg/app2/appserver/server_test.go b/pkg/app2/appserver/server_test.go new file mode 100644 index 0000000000..15ef5e8558 --- /dev/null +++ b/pkg/app2/appserver/server_test.go @@ -0,0 +1,101 @@ +package appserver_test + +import ( + "fmt" + "strings" + "testing" + "time" + + "github.com/skycoin/dmsg" + "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skycoin/src/util/logging" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + + "github.com/skycoin/skywire/pkg/app2" + "github.com/skycoin/skywire/pkg/app2/appcommon" + "github.com/skycoin/skywire/pkg/app2/appnet" + "github.com/skycoin/skywire/pkg/app2/appserver" + "github.com/skycoin/skywire/pkg/routing" +) + +func TestServer_ListenAndServe(t *testing.T) { + l := logging.MustGetLogger("app_server") + sockFile := "app.sock" + appKey := appcommon.GenerateAppKey() + + s, err := appserver.New(l, sockFile, appKey) + require.NoError(t, err) + + visorPK, _ := cipher.GenerateKeyPair() + clientConfig := app2.ClientConfig{ + VisorPK: visorPK, + SockFile: sockFile, + AppKey: appKey, + } + + errCh := make(chan error) + go func() { + err := s.ListenAndServe() + if err != nil { + fmt.Printf("ListenAndServe error: %v\n", err) + } + errCh <- err + }() + + time.Sleep(500 * time.Millisecond) + + dmsgLocal, dmsgRemote, remote := prepAddrs() + + var noErr error + + conn := &appcommon.MockConn{} + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + conn.On("Close").Return(noErr) + + appnet.ClearNetworkers() + n := &appnet.MockNetworker{} + n.On("DialContext", mock.Anything, remote).Return(conn, noErr) + + err = appnet.AddNetworker(appnet.TypeDMSG, n) + require.NoError(t, err) + + cl, err := app2.NewClient(logging.MustGetLogger("app_client"), clientConfig) + require.NoError(t, err) + + gotConn, err := cl.Dial(remote) + require.NoError(t, err) + require.NotNil(t, gotConn) + require.Equal(t, remote, gotConn.RemoteAddr()) + + err = s.Close() + require.NoError(t, err) + + err = <-errCh + require.Error(t, err) + require.True(t, strings.Contains(err.Error(), "use of closed network connection")) +} + +func prepAddrs() (dmsgLocal, dmsgRemote dmsg.Addr, remote appnet.Addr) { + localPK, _ := cipher.GenerateKeyPair() + localPort := uint16(10) + dmsgLocal = dmsg.Addr{ + PK: localPK, + Port: localPort, + } + + remotePK, _ := cipher.GenerateKeyPair() + remotePort := uint16(11) + dmsgRemote = dmsg.Addr{ + PK: remotePK, + Port: remotePort, + } + remote = appnet.Addr{ + Net: appnet.TypeDMSG, + PubKey: remotePK, + Port: routing.Port(remotePort), + } + + return +}