From 74f66ffce2005ed40c7048204603e060b6b7c84d Mon Sep 17 00:00:00 2001 From: Mihai Moldovanu Date: Wed, 23 Mar 2022 03:22:18 +0200 Subject: [PATCH] refactor sendPingReply and added first unit test for login --- sending_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sending_test.go diff --git a/sending_test.go b/sending_test.go new file mode 100644 index 0000000..3ca67da --- /dev/null +++ b/sending_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "net" + "testing" +) + +// Test scafolding +func TestSendLogin(t *testing.T) { + testTable := []struct { + name string + pass string + resultBuffer []byte + resultLenght int + // input, output + }{ + { + name: "happy", + pass: "happy2", + resultBuffer: []byte{140, 15, 0, 104, 97, 112, 112, 121, 32, 104, 97, 112, 112, 121, 50, 0, 0}, + resultLenght: 17, + }, + } + + for _, tt := range testTable { + t.Run(tt.name, func(t *testing.T) { + // call function or method under test + // .... + // check respose + server, client := net.Pipe() + + go func() { + sendLogin(client, tt.name, tt.pass) + }() + + reply := make([]byte, 4096) + length, _ := server.Read(reply) + if length != tt.resultLenght { + t.Errorf("Expected length %v, got %v buffer: %v for %s / %s", tt.resultLenght, length, reply[0:length], tt.name, tt.pass) + } + }) + } +}