Browse Source

Added more tests; Probably the last commit for this project; Stopped gaining fun and it's already complete imo

main 1.2.2
Unbewohnte 3 years ago
parent
commit
82a1391c9c
  1. 5
      README.md
  2. 1
      encryption/encrypt.go
  3. 43
      protocol/packet.go
  4. 78
      protocol/protocol_test.go

5
README.md

@ -85,8 +85,9 @@ Thus, with a connection and a way of communication, the sender will send some pa
In FTU directory: In FTU directory:
- `go test ./...` - to test recursively - `go test ./...` - to test everything
- `go test -v ./...` - to test recursively, with additional information - `go test -v ./...` - to test everything, with additional information
- `go test ./NAME_OF_THE_PACKAGE` - to test a certain package
--- ---

1
encryption/encrypt.go

@ -20,6 +20,5 @@ func Encrypt(key, dataToEncrypt []byte) ([]byte, error) {
nonce := make([]byte, aesGCM.NonceSize()) nonce := make([]byte, aesGCM.NonceSize())
encryptedData := aesGCM.Seal(nonce, nonce, dataToEncrypt, nil) encryptedData := aesGCM.Seal(nonce, nonce, dataToEncrypt, nil)
fmt.Printf("EEEEEEEEEEEEEEEEEE %v\n", encryptedData)
return encryptedData, nil return encryptedData, nil
} }

43
protocol/packet.go

@ -52,36 +52,47 @@ func BytesToPacket(packetbytes []byte) Packet {
} }
} }
// Sends given packet to connection, following all the protocol`s rules. // Converts given packet struct into ready-to-transfer bytes, constructed by following the protocol
// ALL packets MUST be sent by this method func PacketToBytes(packet Packet) ([]byte, error) {
func SendPacket(connection net.Conn, packetToSend Packet) error { packetSize := MeasurePacketSize(packet)
packetSize := MeasurePacketSize(packetToSend)
if packetSize > uint64(MAXPACKETSIZE) { 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))) packetSizeBytes := []byte(strconv.Itoa(int(packetSize)))
// creating a buffer and writing the whole packet into it // creating a buffer and writing the whole packet into it
packet := new(bytes.Buffer) packetBuffer := new(bytes.Buffer)
packet.Write([]byte(PACKETSIZEDELIMETER))
packet.Write(packetSizeBytes)
packet.Write([]byte(PACKETSIZEDELIMETER))
packet.Write([]byte(packetToSend.Header)) // packetsize between delimeters (ie: |17|)
packet.Write([]byte(HEADERDELIMETER)) packetBuffer.Write([]byte(PACKETSIZEDELIMETER))
packet.Write(packetToSend.Body) packetBuffer.Write(packetSizeBytes)
packetBuffer.Write([]byte(PACKETSIZEDELIMETER))
// write the result (ie: |17|FILENAME~file.png) // ie: FILENAME~file.txt
connection.Write(packet.Bytes()) packetBuffer.Write([]byte(packet.Header))
packetBuffer.Write([]byte(HEADERDELIMETER))
packetBuffer.Write(packet.Body)
// for debug purposes (ᗜˬᗜ) // for debug purposes (ᗜˬᗜ)
// fmt.Printf("SENDING PACKET: %s%s%s%s%s%s\n", // fmt.Printf("SENDING PACKET: %s%s%s%s%s%s\n",
// []byte(PACKETSIZEDELIMETER), packetSizeBytes, []byte(PACKETSIZEDELIMETER), // []byte(PACKETSIZEDELIMETER), packetSizeBytes, []byte(PACKETSIZEDELIMETER),
// []byte(packetToSend.Header), []byte(HEADERDELIMETER), packetToSend.Body) // []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 return nil
} }

78
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")
}
}
Loading…
Cancel
Save