From d7665618de4885e1021e7364782f85e6890bf0d5 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Thu, 17 Jun 2021 13:59:32 +0300 Subject: [PATCH] Added first tests --- README.md | 13 ++++++++--- checksum/checksum.go | 4 +++- checksum/checksum_test.go | 42 +++++++++++++++++++++++++++++++++++ encryption/encrypt.go | 1 + encryption/encryption_test.go | 11 +++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 checksum/checksum_test.go create mode 100644 encryption/encryption_test.go diff --git a/README.md b/README.md index f864767..8db9a6a 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,8 @@ Thus, with a connection and a way of communication, the sender will send some pa - Lack of information about the process of transferring; FIXED - [x] - No way to verify if the transferred file is not corrupted; FIXED via checksum- [x] - No encryption; FIXED via AES encryption of packets` body - [x] -- Messy and hard to follow code && file structure; FIXED? - [x] -- No way to stop the download/upload and resume it later or even during the next connection; FIXED - [ ] -- No tests; FIXED - [ ] +- Messy and hard to follow code && file structure; FIXED? - [x] +- No tests; FIXED - [x]; Not every packet has its tests, but they are present ## Good points - It works. @@ -80,6 +79,14 @@ Thus, with a connection and a way of communication, the sender will send some pa - `./FTU -downloadto="/home/some_path_here/Downloads/" -addr="192.168.1.104"` - creates a client (receiver) that will try to connect to `192.168.1.104` (local device) on port `8080` and if successful - downloads a file to given path - `./FTU -downloadto="/home/some_path_here/Downloads/" -addr=145.125.53.212 -port=8888` - same as before, but will try to connect to `145.125.53.212` on port `8888` +--- + +## Testing + +In FTU directory: + +- `go test ./...` - to test recursively +- `go test -v ./...` - to test recursively, with additional information --- diff --git a/checksum/checksum.go b/checksum/checksum.go index 4329bc5..609d84d 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -32,7 +32,7 @@ func GetPartialCheckSum(file *os.File) (CheckSum, error) { fileSize := fileStats.Size() if fileSize < int64(CHUNKS*CHUNKSIZE+STEP*(CHUNKS-1)) { - // file is too small, doing full checksum + // file is too small to chop it in chunks, so just doing full checksum checksum, err := getFullCheckSum(file) if err != nil { @@ -85,6 +85,8 @@ func AreEqual(checksum1, checksum2 CheckSum) bool { func BytesToChecksum(bytes []byte) (CheckSum, error) { if uint(len(bytes)) > CHECKSUMLEN { return CheckSum{}, fmt.Errorf("provided bytes` length is bigger than the checksum`s") + } else if uint(len(bytes)) < CHECKSUMLEN { + return CheckSum{}, fmt.Errorf("provided bytes` length is smaller than needed") } var checksum [CHECKSUMLEN]byte diff --git a/checksum/checksum_test.go b/checksum/checksum_test.go new file mode 100644 index 0000000..8bacdde --- /dev/null +++ b/checksum/checksum_test.go @@ -0,0 +1,42 @@ +package checksum + +import ( + "testing" +) + +func TestBytesToChecksum(t *testing.T) { + invalidChecksumBytes := []byte("LESSTHAN32") + _, err := BytesToChecksum(invalidChecksumBytes) + if err == nil { + t.Error("BytesToChecksum failed: expected an error") + } + + invalidChecksumBytes = []byte("BIGGERTHAN32_IFJOWIJFOIHJGLVKNS'O[DFJQWG[OJHNE[OJGNJOREG") + _, err = BytesToChecksum(invalidChecksumBytes) + if err == nil { + t.Error("BytesToChecksum failed: expected an error") + } + + validChecksumBytes := []byte{5, 194, 47, 217, 251, 195, 69, 230, 216, 121, 253, 38, + 116, 68, 152, 68, 103, 226, 16, 58, 235, 47, 6, 55, 27, 20, 83, 152, 89, 38, 59, 29} + _, err = BytesToChecksum(validChecksumBytes) + if err != nil { + t.Errorf("BytesToChecksum failed: not expected an error, got : %s; length of given bytes: %d", err, len(validChecksumBytes)) + } +} + +func TestChecksumToBytes(t *testing.T) { + validChecksumBytes := []byte{5, 194, 47, 217, 251, 195, 69, 230, 216, 121, 253, 38, + 116, 68, 152, 68, 103, 226, 16, 58, 235, 47, 6, 55, 27, 20, 83, 152, 89, 38, 59, 29} + + var validChecksum CheckSum = CheckSum{5, 194, 47, 217, 251, 195, 69, 230, 216, 121, 253, 38, + 116, 68, 152, 68, 103, 226, 16, 58, 235, 47, 6, 55, 27, 20, 83, 152, 89, 38, 59, 29} + + result := ChecksumToBytes(validChecksum) + + for index, b := range result { + if b != validChecksumBytes[index] { + t.Errorf("ChecksumToBytes failed, invalid result") + } + } +} diff --git a/encryption/encrypt.go b/encryption/encrypt.go index 339f379..0ccebbf 100644 --- a/encryption/encrypt.go +++ b/encryption/encrypt.go @@ -20,5 +20,6 @@ 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/encryption/encryption_test.go b/encryption/encryption_test.go new file mode 100644 index 0000000..8ed3afe --- /dev/null +++ b/encryption/encryption_test.go @@ -0,0 +1,11 @@ +package encryption + +import "testing" + +func TestGenerate32AESkey(t *testing.T) { + generatedKey := Generate32AESkey() + + if len(generatedKey) != int(KEYLEN) { + t.Errorf("Generate32AESkey failed: generated key`s length does not equal KEYLEN const (32)") + } +}