Browse Source

Added first tests

main
Unbewohnte 3 years ago
parent
commit
d7665618de
  1. 11
      README.md
  2. 4
      checksum/checksum.go
  3. 42
      checksum/checksum_test.go
  4. 1
      encryption/encrypt.go
  5. 11
      encryption/encryption_test.go

11
README.md

@ -37,8 +37,7 @@ Thus, with a connection and a way of communication, the sender will send some pa
- No way to verify if the transferred file is not corrupted; FIXED via checksum- [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] - No encryption; FIXED via AES encryption of packets` body - [x]
- Messy and hard to follow code && file structure; FIXED? - [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 - [x]; Not every packet has its tests, but they are present
- No tests; FIXED - [ ]
## Good points ## Good points
- It works. - 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="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` - `./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
--- ---

4
checksum/checksum.go

@ -32,7 +32,7 @@ func GetPartialCheckSum(file *os.File) (CheckSum, error) {
fileSize := fileStats.Size() fileSize := fileStats.Size()
if fileSize < int64(CHUNKS*CHUNKSIZE+STEP*(CHUNKS-1)) { 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) checksum, err := getFullCheckSum(file)
if err != nil { if err != nil {
@ -85,6 +85,8 @@ func AreEqual(checksum1, checksum2 CheckSum) bool {
func BytesToChecksum(bytes []byte) (CheckSum, error) { func BytesToChecksum(bytes []byte) (CheckSum, error) {
if uint(len(bytes)) > CHECKSUMLEN { if uint(len(bytes)) > CHECKSUMLEN {
return CheckSum{}, fmt.Errorf("provided bytes` length is bigger than the checksum`s") 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 var checksum [CHECKSUMLEN]byte

42
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")
}
}
}

1
encryption/encrypt.go

@ -20,5 +20,6 @@ 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
} }

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