From 504cc375a91eba6f8680c40d692f4fab051a5cba Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Tue, 18 Jun 2019 15:59:10 +0300 Subject: [PATCH] Add test for sent/received msg consistency --- pkg/dmsg/server_test.go | 71 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/pkg/dmsg/server_test.go b/pkg/dmsg/server_test.go index 58ae86d59a..f64a122b48 100644 --- a/pkg/dmsg/server_test.go +++ b/pkg/dmsg/server_test.go @@ -539,7 +539,7 @@ func TestServer_Serve(t *testing.T) { } }) - t.Run("test failed_accept_should_not_hang_established_transport", func(t *testing.T) { + t.Run("test failed accept not hanging already established transport", func(t *testing.T) { // generate keys for both clients aPK, aSK := cipher.GenerateKeyPair() bPK, bSK := cipher.GenerateKeyPair() @@ -590,7 +590,7 @@ func TestServer_Serve(t *testing.T) { for { ctx := context.Background() //ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) - if _, err = a.Dial(ctx, bPK); err != nil { + if _, err := a.Dial(ctx, bPK); err != nil { break } } @@ -628,6 +628,73 @@ func TestServer_Serve(t *testing.T) { b.log.Println("AFTER CLOSING") require.NoError(t, err) }) + + t.Run("test sent/received message consistency", func(t *testing.T) { + // generate keys for both clients + aPK, aSK := cipher.GenerateKeyPair() + bPK, bSK := cipher.GenerateKeyPair() + + // create remote + a := NewClient(aPK, aSK, dc) + a.SetLogger(logging.MustGetLogger("A")) + err = a.InitiateServerConnections(context.Background(), 1) + require.NoError(t, err) + + // create initiator + b := NewClient(bPK, bSK, dc) + b.SetLogger(logging.MustGetLogger("B")) + err = b.InitiateServerConnections(context.Background(), 1) + require.NoError(t, err) + + bTransport, err := b.Dial(context.Background(), aPK) + require.NoError(t, err) + + aTransport, err := a.Accept(context.Background()) + require.NoError(t, err) + + msgCount := 100 + for i := 0; i < msgCount; i++ { + msg := "Hello there!" + + _, err := bTransport.Write([]byte(msg)) + require.NoError(t, err) + + recMsg := make([]byte, 5) + n, err := aTransport.Read(recMsg) + require.NoError(t, err) + + received := string(recMsg[:n]) + + log.Printf("Received: %v , bytes: %v", received, n) + + n, err = aTransport.Read(recMsg) + require.NoError(t, err) + + received += string(recMsg[:n]) + + log.Printf("Received: %v , bytes: %v", received, n) + + n, err = aTransport.Read(recMsg) + require.NoError(t, err) + + received += string(recMsg[:n]) + log.Printf("Last bytes count: %v", n) + + require.Equal(t, received, msg) + } + + err = bTransport.Close() + require.NoError(t, err) + + err = aTransport.Close() + require.NoError(t, err) + + err = a.Close() + require.NoError(t, err) + + err = b.Close() + require.NoError(t, err) + }) } // Given two client instances (a & b) and a server instance (s),