diff --git a/pkg/app/appserver/rpc_gateway_test.go b/pkg/app/appserver/rpc_gateway_test.go index 73dc4e8b8..1e634215a 100644 --- a/pkg/app/appserver/rpc_gateway_test.go +++ b/pkg/app/appserver/rpc_gateway_test.go @@ -3,6 +3,7 @@ package appserver import ( "context" "errors" + "fmt" "math" "net" "strings" @@ -580,7 +581,9 @@ func testRPCGatewaySetWriteDeadlineOK(t *testing.T, l *logging.Logger, deadline conn := &appcommon.MockConn{} conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return nil }) @@ -628,7 +631,9 @@ func testRPCGatewaySetWriteDeadlineError(t *testing.T, l *logging.Logger, deadli conn := &appcommon.MockConn{} conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return testhelpers.Err }) @@ -670,7 +675,9 @@ func testRPCGatewaySetReadDeadlineOK(t *testing.T, l *logging.Logger, deadline t conn := &appcommon.MockConn{} conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return nil }) @@ -718,7 +725,9 @@ func testRPCGatewaySetReadDeadlineError(t *testing.T, l *logging.Logger, deadlin conn := &appcommon.MockConn{} conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return testhelpers.Err }) @@ -760,7 +769,9 @@ func testRPCGatewaySetDeadlineOK(t *testing.T, l *logging.Logger, deadline time. conn := &appcommon.MockConn{} conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return nil }) @@ -808,7 +819,9 @@ func testRPCGatewaySetDeadlineError(t *testing.T, l *logging.Logger, deadline ti conn := &appcommon.MockConn{} conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error { - require.Equal(t, deadline, d) + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } return testhelpers.Err }) diff --git a/pkg/app/conn_test.go b/pkg/app/conn_test.go index 576e740a4..604c4f520 100644 --- a/pkg/app/conn_test.go +++ b/pkg/app/conn_test.go @@ -2,23 +2,11 @@ package app import ( "errors" - "fmt" "net" - "net/rpc" "testing" - "github.com/SkycoinProject/skycoin/src/util/logging" - "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" - "golang.org/x/net/nettest" - - "github.com/SkycoinProject/skywire-mainnet/internal/testhelpers" - "github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon" - "github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet" - "github.com/SkycoinProject/skywire-mainnet/pkg/app/appserver" - "github.com/SkycoinProject/skywire-mainnet/pkg/app/idmanager" "github.com/SkycoinProject/skywire-mainnet/pkg/routing" - "github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest" + "github.com/stretchr/testify/require" ) func TestConn_Read(t *testing.T) { @@ -172,157 +160,157 @@ func (p *wrappedConn) RemoteAddr() net.Addr { return p.remote } -func TestConn_TestConn(t *testing.T) { - mp := func() (net.Conn, net.Conn, func(), error) { - netType := appnet.TypeSkynet - keys := snettest.GenKeyPairs(2) - fmt.Printf("C1 Local: %s\n", keys[0].PK) - fmt.Printf("C2 Local: %s\n", keys[1].PK) - p1, p2 := net.Pipe() - a1 := appnet.Addr{ - Net: appnet.Type(netType), - PubKey: keys[0].PK, - Port: 0, - } - a2 := appnet.Addr{ - Net: appnet.Type(netType), - PubKey: keys[1].PK, - Port: 0, - } - - ra1 := routing.Addr{ - PubKey: a1.PubKey, - Port: a1.Port, - } - ra2 := routing.Addr{ - PubKey: a2.PubKey, - Port: a2.Port, - } +/*func TestConn_TestConn(t *testing.T) { +mp := func() (net.Conn, net.Conn, func(), error) { + netType := appnet.TypeSkynet + keys := snettest.GenKeyPairs(2) + fmt.Printf("C1 Local: %s\n", keys[0].PK) + fmt.Printf("C2 Local: %s\n", keys[1].PK) + p1, p2 := net.Pipe() + a1 := appnet.Addr{ + Net: appnet.Type(netType), + PubKey: keys[0].PK, + Port: 0, + } + a2 := appnet.Addr{ + Net: appnet.Type(netType), + PubKey: keys[1].PK, + Port: 0, + } - wc1 := wrapConn(p1, ra1, ra2) - wc2 := wrapConn(p2, ra2, ra1) + ra1 := routing.Addr{ + PubKey: a1.PubKey, + Port: a1.Port, + } + ra2 := routing.Addr{ + PubKey: a2.PubKey, + Port: a2.Port, + } - n := &appnet.MockNetworker{} - n.On("DialContext", mock.Anything, a1).Return(wc1, testhelpers.NoErr) - n.On("DialContext", mock.Anything, a2).Return(wc2, testhelpers.NoErr) + wc1 := wrapConn(p1, ra1, ra2) + wc2 := wrapConn(p2, ra2, ra1) - appnet.ClearNetworkers() - err := appnet.AddNetworker(appnet.Type(netType), n) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) + n := &appnet.MockNetworker{} + n.On("DialContext", mock.Anything, a1).Return(wc1, testhelpers.NoErr) + n.On("DialContext", mock.Anything, a2).Return(wc2, testhelpers.NoErr) - rpcL, err := nettest.NewLocalListener("tcp") - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) + appnet.ClearNetworkers() + err := appnet.AddNetworker(appnet.Type(netType), n) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) - rpcS := rpc.NewServer() + rpcL, err := nettest.NewLocalListener("tcp") + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) - appKeys := snettest.GenKeyPairs(2) + rpcS := rpc.NewServer() - gateway1 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway1")) - gateway2 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway2")) - err = rpcS.RegisterName(appKeys[0].PK.Hex(), gateway1) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) - err = rpcS.RegisterName(appKeys[1].PK.Hex(), gateway2) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) + appKeys := snettest.GenKeyPairs(2) - go rpcS.Accept(rpcL) + gateway1 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway1")) + gateway2 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway2")) + err = rpcS.RegisterName(appKeys[0].PK.Hex(), gateway1) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) + err = rpcS.RegisterName(appKeys[1].PK.Hex(), gateway2) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) - rpcCl1, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String()) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) - - cl1 := Client{ - log: logging.MustGetLogger("test_client_1"), - visorPK: keys[0].PK, - rpc: NewRPCClient(rpcCl1, appcommon.Key(appKeys[0].PK.Hex())), - lm: idmanager.New(), - cm: idmanager.New(), - } + go rpcS.Accept(rpcL) - rpcCl2, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String()) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) - - cl2 := Client{ - log: logging.MustGetLogger("test_client_2"), - visorPK: keys[1].PK, - rpc: NewRPCClient(rpcCl2, appcommon.Key(appKeys[1].PK.Hex())), - lm: idmanager.New(), - cm: idmanager.New(), - } + rpcCl1, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String()) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) + + cl1 := Client{ + log: logging.MustGetLogger("test_client_1"), + visorPK: keys[0].PK, + rpc: NewRPCClient(rpcCl1, appcommon.Key(appKeys[0].PK.Hex())), + lm: idmanager.New(), + cm: idmanager.New(), + } - c1, err := cl1.Dial(a2) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) + rpcCl2, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String()) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) + + cl2 := Client{ + log: logging.MustGetLogger("test_client_2"), + visorPK: keys[1].PK, + rpc: NewRPCClient(rpcCl2, appcommon.Key(appKeys[1].PK.Hex())), + lm: idmanager.New(), + cm: idmanager.New(), + } - c2, err := cl2.Dial(a1) - if err != nil { - return nil, nil, nil, err - } - //require.NoError(t, err) - - stop := func() { - /*err := c1.Close() - require.NoError(t, err) - err = c2.Close() - require.NoError(t, err) - err = rpcL.Close() - require.NoError(t, err)*/ - } + c1, err := cl1.Dial(a2) + if err != nil { + return nil, nil, nil, err + } + //require.NoError(t, err) - return c1, c2, stop, nil + c2, err := cl2.Dial(a1) + if err != nil { + return nil, nil, nil, err } + //require.NoError(t, err) - /*payload := []byte("hello") - - errC := make(chan error) - nC := make(chan int) - bufC := make(chan []byte) - go func() { - buf := make([]byte, 10) - n, err := c2.Read(buf) - fmt.Printf("Err is %v\n", err) - errC <- err - if err != nil { - return - } + stop := func() { + /*err := c1.Close() + require.NoError(t, err) + err = c2.Close() + require.NoError(t, err) + err = rpcL.Close() + require.NoError(t, err)*/ +/*} - nC <- n - bufC <- buf[:n] - }() + return c1, c2, stop, nil +} - written, err := c1.Write(payload) - require.NoError(t, err) - require.Equal(t, len(payload), written) +/*payload := []byte("hello") + +errC := make(chan error) +nC := make(chan int) +bufC := make(chan []byte) +go func() { + buf := make([]byte, 10) + n, err := c2.Read(buf) + fmt.Printf("Err is %v\n", err) + errC <- err + if err != nil { + return + } - require.NoError(t, <-errC) - close(errC) - require.Equal(t, len(payload), <-nC) - close(nC) - require.Equal(t, payload, <-bufC) - close(bufC) + nC <- n + bufC <- buf[:n] +}() - err = c1.Close() - require.NoError(t, err) - err = c2.Close() - require.NoError(t, err)*/ +written, err := c1.Write(payload) +require.NoError(t, err) +require.Equal(t, len(payload), written) - nettest.TestConn(t, mp) -} +require.NoError(t, <-errC) +close(errC) +require.Equal(t, len(payload), <-nC) +close(nC) +require.Equal(t, payload, <-bufC) +close(bufC) + +err = c1.Close() +require.NoError(t, err) +err = c2.Close() +require.NoError(t, err)*/ + +/*nettest.TestConn(t, mp) +}*/ diff --git a/pkg/app/mock_rpc_client.go b/pkg/app/mock_rpc_client.go index b08e2a707..b07488b79 100644 --- a/pkg/app/mock_rpc_client.go +++ b/pkg/app/mock_rpc_client.go @@ -143,12 +143,12 @@ func (_m *MockRPCClient) Read(connID uint16, b []byte) (int, error) { } // SetDeadline provides a mock function with given fields: id, t -func (_m *MockRPCClient) SetDeadline(id uint16, t time.Time) error { - ret := _m.Called(id, t) +func (_m *MockRPCClient) SetDeadline(connID uint16, d time.Time) error { + ret := _m.Called(connID, d) var r0 error if rf, ok := ret.Get(0).(func(uint16, time.Time) error); ok { - r0 = rf(id, t) + r0 = rf(connID, d) } else { r0 = ret.Error(0) } @@ -157,12 +157,12 @@ func (_m *MockRPCClient) SetDeadline(id uint16, t time.Time) error { } // SetReadDeadline provides a mock function with given fields: id, t -func (_m *MockRPCClient) SetReadDeadline(id uint16, t time.Time) error { - ret := _m.Called(id, t) +func (_m *MockRPCClient) SetReadDeadline(connID uint16, d time.Time) error { + ret := _m.Called(connID, d) var r0 error if rf, ok := ret.Get(0).(func(uint16, time.Time) error); ok { - r0 = rf(id, t) + r0 = rf(connID, d) } else { r0 = ret.Error(0) } @@ -171,12 +171,12 @@ func (_m *MockRPCClient) SetReadDeadline(id uint16, t time.Time) error { } // SetWriteDeadline provides a mock function with given fields: id, t -func (_m *MockRPCClient) SetWriteDeadline(id uint16, t time.Time) error { - ret := _m.Called(id, t) +func (_m *MockRPCClient) SetWriteDeadline(connID uint16, d time.Time) error { + ret := _m.Called(connID, d) var r0 error if rf, ok := ret.Get(0).(func(uint16, time.Time) error); ok { - r0 = rf(id, t) + r0 = rf(connID, d) } else { r0 = ret.Error(0) } diff --git a/pkg/app/rpc_client.go b/pkg/app/rpc_client.go index 4e96eb1ae..549ab7014 100644 --- a/pkg/app/rpc_client.go +++ b/pkg/app/rpc_client.go @@ -22,9 +22,9 @@ type RPCClient interface { Read(connID uint16, b []byte) (int, error) CloseConn(id uint16) error CloseListener(id uint16) error - SetDeadline(id uint16, t time.Time) error - SetReadDeadline(id uint16, t time.Time) error - SetWriteDeadline(id uint16, t time.Time) error + SetDeadline(connID uint16, d time.Time) error + SetReadDeadline(connID uint16, d time.Time) error + SetWriteDeadline(connID uint16, d time.Time) error } // rpcClient implements `RPCClient`. diff --git a/pkg/app/rpc_client_test.go b/pkg/app/rpc_client_test.go index 146bf87c9..bd3ba2a75 100644 --- a/pkg/app/rpc_client_test.go +++ b/pkg/app/rpc_client_test.go @@ -3,9 +3,13 @@ package app import ( "context" "errors" + "fmt" "net" "net/rpc" "testing" + "time" + + "github.com/SkycoinProject/skywire-mainnet/internal/testhelpers" "github.com/SkycoinProject/dmsg" "github.com/SkycoinProject/dmsg/cipher" @@ -448,6 +452,213 @@ func TestRPCClient_CloseListener(t *testing.T) { }) } +func TestRPCClient_SetDeadline(t *testing.T) { + dmsgLocal, dmsgRemote, _, remote := prepAddrs() + + deadline := time.Now().Add(1 * time.Hour) + + t.Run("ok", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.NoErr + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetDeadline(dialResp.ConnID, deadline) + require.NoError(t, err) + }) + + t.Run("set deadline error", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.Err + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetDeadline(dialResp.ConnID, deadline) + require.Error(t, err) + require.Equal(t, testhelpers.Err.Error(), err.Error()) + }) +} + +func TestRPCClient_SetReadDeadline(t *testing.T) { + dmsgLocal, dmsgRemote, _, remote := prepAddrs() + + deadline := time.Now().Add(1 * time.Hour) + + t.Run("ok", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.NoErr + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetReadDeadline(dialResp.ConnID, deadline) + require.NoError(t, err) + }) + + t.Run("set deadline error", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.Err + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetReadDeadline(dialResp.ConnID, deadline) + require.Error(t, err) + require.Equal(t, testhelpers.Err.Error(), err.Error()) + }) +} + +func TestRPCClient_SetWriteDeadline(t *testing.T) { + dmsgLocal, dmsgRemote, _, remote := prepAddrs() + + deadline := time.Now().Add(1 * time.Hour) + + t.Run("ok", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.NoErr + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetWriteDeadline(dialResp.ConnID, deadline) + require.NoError(t, err) + }) + + t.Run("set deadline error", func(t *testing.T) { + gateway := prepGateway() + + conn := &appcommon.MockConn{} + conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error { + if !deadline.Equal(d) { + return fmt.Errorf("expected deadline %v, got %v", deadline, d) + } + + return testhelpers.Err + }) + conn.On("LocalAddr").Return(dmsgLocal) + conn.On("RemoteAddr").Return(dmsgRemote) + + prepNetworkerWithConn(t, conn, remote) + + var dialResp appserver.DialResp + err := gateway.Dial(&remote, &dialResp) + require.NoError(t, err) + + s := prepRPCServer(t, gateway) + rpcL, lisCleanup := prepListener(t) + defer lisCleanup() + go s.Accept(rpcL) + + cl := prepRPCClient(t, rpcL.Addr().Network(), rpcL.Addr().String()) + + err = cl.SetWriteDeadline(dialResp.ConnID, deadline) + require.Error(t, err) + require.Equal(t, testhelpers.Err.Error(), err.Error()) + }) +} + func prepNetworkerWithListener(t *testing.T, lis *appcommon.MockListener, local appnet.Addr) { var noErr error