test_review #2

Merged
mihaim merged 2 commits from test_review into master 2022-03-24 00:08:56 +00:00
2 changed files with 52 additions and 13 deletions

View File

@ -5,29 +5,25 @@ import (
"net" "net"
) )
/* login to El */ /* Login to El */
func sendLogin(c net.Conn, user string, password string) { func sendLogin(c net.Conn, user string, password string) {
s := user + " " + password s := user + " " + password
lungime := len(s) + 3 length := len(s) + 3
b := make([]byte, lungime+2) b := make([]byte, length+2)
b[0] = 140 b[0] = 140
b[1] = byte(lungime % 256) b[1] = byte(length % 256)
b[2] = byte(lungime / 256) b[2] = byte(length / 256)
copy(b[3:], s) copy(b[3:], s)
b[lungime+1] = 0 b[length+1] = 0
binary.Write(c, binary.LittleEndian, b) binary.Write(c, binary.LittleEndian, b)
} }
/* Send back to server what we received */ /* Send back to server what we received */
func sendPingReply(c net.Conn, b []byte) { func sendPingReply(c net.Conn, b []byte) {
ping_reply := make([]byte, 7) ping_reply := make([]byte, 7)
ping_reply[0] = b[0] for i := 0; i < 7; i++ {
ping_reply[1] = b[1] ping_reply[i] = b[i]
ping_reply[2] = b[2] }
ping_reply[3] = b[3]
ping_reply[4] = b[4]
ping_reply[5] = b[5]
ping_reply[6] = b[6]
binary.Write(c, binary.LittleEndian, ping_reply) binary.Write(c, binary.LittleEndian, ping_reply)
} }

43
sending_test.go Normal file
View File

@ -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)
}
})
}
}