diff --git a/README.md b/README.md index 8db9a6a..8fe083b 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,9 @@ Thus, with a connection and a way of communication, the sender will send some pa In FTU directory: -- `go test ./...` - to test recursively -- `go test -v ./...` - to test recursively, with additional information +- `go test ./...` - to test everything +- `go test -v ./...` - to test everything, with additional information +- `go test ./NAME_OF_THE_PACKAGE` - to test a certain package --- diff --git a/encryption/encrypt.go b/encryption/encrypt.go index 0ccebbf..339f379 100644 --- a/encryption/encrypt.go +++ b/encryption/encrypt.go @@ -20,6 +20,5 @@ func Encrypt(key, dataToEncrypt []byte) ([]byte, error) { nonce := make([]byte, aesGCM.NonceSize()) encryptedData := aesGCM.Seal(nonce, nonce, dataToEncrypt, nil) - fmt.Printf("EEEEEEEEEEEEEEEEEE %v\n", encryptedData) return encryptedData, nil } diff --git a/protocol/packet.go b/protocol/packet.go index 2144a15..e066e7d 100644 --- a/protocol/packet.go +++ b/protocol/packet.go @@ -52,36 +52,47 @@ func BytesToPacket(packetbytes []byte) Packet { } } -// Sends given packet to connection, following all the protocol`s rules. -// ALL packets MUST be sent by this method -func SendPacket(connection net.Conn, packetToSend Packet) error { - packetSize := MeasurePacketSize(packetToSend) +// Converts given packet struct into ready-to-transfer bytes, constructed by following the protocol +func PacketToBytes(packet Packet) ([]byte, error) { + packetSize := MeasurePacketSize(packet) if packetSize > uint64(MAXPACKETSIZE) { - return fmt.Errorf("invalid packet!: EXCEEDED MAX PACKETSIZE") + return nil, fmt.Errorf("invalid packet!: EXCEEDED MAX PACKETSIZE") } - // packetsize between delimeters (ie: |17|) packetSizeBytes := []byte(strconv.Itoa(int(packetSize))) // creating a buffer and writing the whole packet into it - packet := new(bytes.Buffer) - - packet.Write([]byte(PACKETSIZEDELIMETER)) - packet.Write(packetSizeBytes) - packet.Write([]byte(PACKETSIZEDELIMETER)) + packetBuffer := new(bytes.Buffer) - packet.Write([]byte(packetToSend.Header)) - packet.Write([]byte(HEADERDELIMETER)) - packet.Write(packetToSend.Body) + // packetsize between delimeters (ie: |17|) + packetBuffer.Write([]byte(PACKETSIZEDELIMETER)) + packetBuffer.Write(packetSizeBytes) + packetBuffer.Write([]byte(PACKETSIZEDELIMETER)) - // write the result (ie: |17|FILENAME~file.png) - connection.Write(packet.Bytes()) + // ie: FILENAME~file.txt + packetBuffer.Write([]byte(packet.Header)) + packetBuffer.Write([]byte(HEADERDELIMETER)) + packetBuffer.Write(packet.Body) // for debug purposes (ᗜˬᗜ) // fmt.Printf("SENDING PACKET: %s%s%s%s%s%s\n", // []byte(PACKETSIZEDELIMETER), packetSizeBytes, []byte(PACKETSIZEDELIMETER), // []byte(packetToSend.Header), []byte(HEADERDELIMETER), packetToSend.Body) + + return packetBuffer.Bytes(), nil +} + +// Sends given packet to connection, following all the protocol`s rules. +// ALL packets MUST be sent by this method +func SendPacket(connection net.Conn, packetToSend Packet) error { + packetBytes, err := PacketToBytes(packetToSend) + if err != nil { + return fmt.Errorf("could not convert given packet to bytes: %s", err) + } + // write the result (ie: |17|FILENAME~file.png) + connection.Write(packetBytes) + return nil } diff --git a/protocol/protocol_test.go b/protocol/protocol_test.go new file mode 100644 index 0000000..a488b87 --- /dev/null +++ b/protocol/protocol_test.go @@ -0,0 +1,78 @@ +package protocol + +import ( + "bytes" + "net" + "testing" +) + +// Practically tests the whole protocol +func TestTransfer(t *testing.T) { + packet := Packet{ + Header: HeaderFilename, + Body: []byte("fIlEnAmE.txt"), + } + + packetBuffer := new(bytes.Buffer) + packetBuffer.Write([]byte(packet.Header)) + packetBuffer.Write([]byte(HEADERDELIMETER)) + packetBuffer.Write(packet.Body) + + // a valid representation of received packet`s bytes + packetBytes := packetBuffer.Bytes() + + // imitating a connection + l, err := net.Listen("tcp", ":9999") + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + c, err := net.Dial("tcp", "localhost:9999") + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + cc, err := l.Accept() + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + defer c.Close() + defer cc.Close() + + // sending packet + err = SendPacket(cc, packet) + if err != nil { + t.Errorf("SendPacket failed: %s", err) + } + + // + receivedPacket, err := ReadFromConn(c) + if err != nil { + t.Errorf("ReadFromConn failed: %s", err) + } + + for index, b := range receivedPacket { + if b != packetBytes[index] { + t.Errorf("Failed: wanted: %v, got: %v", packetBytes[index], b) + } + } +} + +func TestBytesToPacket(t *testing.T) { + packet := Packet{ + Header: HeaderFilename, + Body: []byte("fIlEnAmE.txt"), + } + + packetBuffer := new(bytes.Buffer) + packetBuffer.Write([]byte(packet.Header)) + packetBuffer.Write([]byte(HEADERDELIMETER)) + packetBuffer.Write(packet.Body) + + // a valid representation of received packet`s bytes + packetBytes := packetBuffer.Bytes() + + convertedPacket := BytesToPacket(packetBytes) + + if convertedPacket.Header != packet.Header || string(convertedPacket.Body) != string(packet.Body) { + t.Errorf("BytesToPacket failed") + } +}