-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package appcommon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package appserver contains facilities | ||
// for implementing the skywire app server. | ||
package appserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |