Skip to content

Commit

Permalink
Merge pull request #13 from mrf345/testing
Browse files Browse the repository at this point in the history
Add better error messages and new test case
  • Loading branch information
mrf345 authored Sep 4, 2024
2 parents 5ba765d + 8351b39 commit 9daf154
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions benchmark/bench_and_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def decrypt():
exit(err)

os.chdir(os.path.expanduser("~"))
# encrypt()
# decrypt()
encrypt()
decrypt()
os.chdir(root)
plt.margins(3.5)

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var rootCmd = &cobra.Command{
Use: "safelock-cli",
Short: "Simple tool to encrypt/decrypt files with AES encryption",
Long: "Simple command-line tool to encrypt and decrypt files with AES encryption",
Version: "0.4.0",
Version: "0.4.1",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Expand Down
3 changes: 1 addition & 2 deletions safelock/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ag *asyncGcm) load() {
}

if gcm, err = getGCM(ag.pwd, nonce, ag.config); err != nil {
ag.errs <- fmt.Errorf("failed to create new GCM > %w", err)
ag.errs <- err
return
}

Expand Down Expand Up @@ -100,7 +100,6 @@ func decryptChunk(chunk []byte, pwd string, limit int, config EncryptionConfig)
nonce := chunk[limit-(config.NonceLength) : limit]

if gcm, err = getGCM(pwd, nonce, config); err != nil {
err = fmt.Errorf("failed to create new GCM > %w", err)
return
}

Expand Down
22 changes: 22 additions & 0 deletions safelock/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ func TestDecryptWithInvalidOutputPath(t *testing.T) {
assert.True(slErrs.Is[*slErrs.ErrInvalidOutputPath](err))
}

func TestDecryptWithFaultyEncryptedFile(t *testing.T) {
assert := assert.New(t)
password := "testing123456"
sl := GetQuietSafelock()
inputFile, _ := os.CreateTemp("", "input_file")
outputPath, _ := os.MkdirTemp("", "output_dir")
data := make([]byte, sl.HeaderRatio)
_, wErr := inputFile.Write(append(
data,
[]byte("faulty encryption file")...,
))

err := sl.Decrypt(context.TODO(), inputFile, outputPath, password)

assert.Nil(wErr)
assert.NotNil(err)
assert.True(slErrs.Is[*slErrs.ErrFailedToAuthenticate](err))

os.Remove(inputFile.Name())
os.RemoveAll(outputPath)
}

func TestDecryptFileWithTimeout(t *testing.T) {
assert := assert.New(t)
password := "testing123456"
Expand Down
12 changes: 12 additions & 0 deletions safelock/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package safelock
import (
"bytes"
"context"
"fmt"
"io"
"strconv"
"strings"

"github.com/mrf345/safelock-cli/slErrs"
"github.com/mrf345/safelock-cli/utils"
)

Expand Down Expand Up @@ -53,16 +55,23 @@ func (sr *safelockReader) ReadHeader() (err error) {
headerBytes := make([]byte, sr.headerSize)

if _, err = sr.reader.Seek(sr.diffSize(), io.SeekStart); err != nil {
err = fmt.Errorf("can't seek header > %w", err)
return sr.handleErr(err)
}

if _, err = sr.reader.Read(headerBytes); err != nil {
err = fmt.Errorf("can't read header > %w", err)
return sr.handleErr(err)
}

header := string(bytes.Trim(headerBytes, "\x00")[:])
sr.blocks = strings.Split(header, ";")[1:]

if len(sr.blocks) == 0 {
err = &slErrs.ErrFailedToAuthenticate{Msg: "missing header content"}
return
}

if _, err = sr.reader.Seek(0, io.SeekStart); err != nil {
return sr.handleErr(err)
}
Expand Down Expand Up @@ -90,16 +99,19 @@ func (sr *safelockReader) Read(chunk []byte) (read int, err error) {
block, sr.blocks = sr.blocks[0], sr.blocks[1:]

if blockSize, err = strconv.Atoi(block); err != nil {
err = fmt.Errorf("invalid header block size > %w", err)
return 0, sr.handleErr(err)
}

encrypted := make([]byte, blockSize)

if read, err = sr.reader.Read(encrypted); err != nil && err != io.EOF {
err = fmt.Errorf("cant't read encrypted chunk > %w", err)
return read, sr.handleErr(err)
}

if decrypted, err = decryptChunk(encrypted, sr.pwd, read, sr.config); err != nil {
err = fmt.Errorf("can't decrypt chunk > %w", err)
return read, sr.handleErr(err)
}

Expand Down
2 changes: 2 additions & 0 deletions safelock/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (sw *safelockWriter) Write(chunk []byte) (written int, err error) {
encrypted := sw.asyncGcm.encryptChunk(chunk)

if written, err = sw.writer.Write(encrypted); err != nil {
err = fmt.Errorf("can't write encrypted chunk > %w", err)
return written, sw.handleErr(err)
}

Expand All @@ -67,6 +68,7 @@ func (sw *safelockWriter) WriteHeader() (err error) {
headerBytes = append([]byte(header), headerBytes[len(header):]...)

if _, err = sw.writer.Write(headerBytes); err != nil {
err = fmt.Errorf("can't write header bytes > %w", err)
return sw.handleErr(err)
}

Expand Down

0 comments on commit 9daf154

Please sign in to comment.