Added test to Packets and more

This commit is contained in:
Pablu23
2023-12-06 17:13:37 +01:00
parent 38876f6577
commit 5ccd83c9da
7 changed files with 68 additions and 7 deletions

View File

@@ -120,6 +120,21 @@ func GetFile(path string, address string) {
var recvPackets bitmap.Bitmap
dataReceived := 0
go func() {
last := 0
fmt.Print("\033[s")
for {
fmt.Print("\033[u\033[K")
fmt.Printf("Throughput: %v Mbit/s\n", ((dataReceived-last)*8)/1024/1024)
last = dataReceived
time.Sleep(1 * time.Second)
}
}()
for {
pck := ReceivePacket(key, conn)
if pck.Flag == common.End {
@@ -132,13 +147,13 @@ func GetFile(path string, address string) {
}
recvPackets.Set(pck.Sync)
offset := (int64(pck.Sync) - int64(ackPck.Sync+1)) * int64(common.MaxDataSize)
_, err = file.WriteAt(pck.Data, offset)
if err != nil {
panic(err)
}
dataReceived += int(pck.DataLength)
}
lostPackets := make([]uint32, 0)

View File

@@ -4,4 +4,7 @@ go 1.21.1
require golang.org/x/crypto v0.15.0
require golang.org/x/sys v0.14.0 // indirect
require (
github.com/google/go-cmp v0.6.0
golang.org/x/sys v0.14.0 // indirect
)

View File

@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=

View File

@@ -0,0 +1,29 @@
package common
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPacketFromBytes(t *testing.T) {
sid := [8]byte{255, 255, 255, 255, 255, 255, 255, 255}
data := []byte{1, 0, 1}
dataLength := len(data)
want := Packet{
Flag: Request,
Sync: 0,
Data: data,
Sid: sid,
DataLength: uint32(dataLength),
}
bytes := []byte{0, 0, 0, 0, 0, 1, 0, 1}
pck := PacketFromBytes(bytes, uint32(dataLength), sid)
if !cmp.Equal(pck, want) {
t.Fail()
}
}

View File

@@ -37,6 +37,10 @@ func New() (*Server, error) {
return nil, err
}
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
})
return &Server{
sessions: make(map[common.SessionID]*info),
rsa: key,