Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pahatz committed Oct 21, 2024
1 parent 050920c commit 740785f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
15 changes: 11 additions & 4 deletions decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Decrypt(args []string) error {
// decrypt the input files
numFiles := len(files)
removedCount := 0
decryptedCount := 0
for i, file := range files {
switch {
case !helpers.FileIsReadable(file.Encrypted):
Expand All @@ -93,28 +94,34 @@ func Decrypt(args []string) error {
err := decryptFile(file.Encrypted, file.Unencrypted, *privateKey)
if err != nil {
fmt.Fprintf(os.Stderr, "Error decrypting file %s: %v\n", file.Encrypted, err)
decryptedCount--
}
decryptedCount++
case helpers.FileExists(file.Unencrypted):
fmt.Fprintf(os.Stderr, "Warning: file %s is already decrypted, skipping\n", file.Unencrypted)
default:
fmt.Printf("Decrypting file %v/%v: %s\n", i+1, numFiles, file.Encrypted)
err := decryptFile(file.Encrypted, file.Unencrypted, *privateKey)
if err != nil {
fmt.Fprintf(os.Stderr, "Error decrypting file %s: %v\n", file.Encrypted, err)
decryptedCount--
}
decryptedCount++
}
// remove the encrypted file if the clean flag is set
if *clean {
if *clean && helpers.FileIsReadable(file.Encrypted) {
err = os.Remove(file.Encrypted)
if err != nil {
return fmt.Errorf("could not remove encrypted file %s: %s", file.Encrypted, err)
fmt.Fprintf(os.Stderr, "Could not remove encrypted file %s: %s", file.Encrypted, err)
continue

Check failure on line 116 in decrypt/decrypt.go

View workflow job for this annotation

GitHub Actions / Lint code (1.22)

continue with no blank line before (nlreturn)
}
removedCount++
}

}
fmt.Printf("Decryption completed, %v files decrypted\n", numFiles)
fmt.Printf("Decryption completed.\n%v files decrypted\n", decryptedCount)
if *clean {
fmt.Printf("Removed %v encrypted files\n", removedCount)
fmt.Printf("%v encrypted files removed\n", removedCount)
}

return nil
Expand Down
82 changes: 69 additions & 13 deletions decrypt/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,9 @@ func (suite *DecryptTests) TestDecrypt() {
assert.Equal(suite.T(), string(suite.fileContent), string(fileData))
}

os.Args = []string{"decrypt", "-key", fmt.Sprintf("%s.sec.pem", testKeyFile), "--force-overwrite", "--clean", fmt.Sprintf("%s.c4gh", suite.testFile.Name())}
os.Args = []string{"decrypt", "-key", fmt.Sprintf("%s.sec.pem", testKeyFile), "--force-overwrite", fmt.Sprintf("%s.c4gh", suite.testFile.Name())}
err = Decrypt(os.Args)
assert.NoError(suite.T(), err, "decrypt failed unexpectedly")
// Check that the encrypted file was removed
_, err = os.Stat(fmt.Sprintf("%s.c4gh", suite.testFile.Name()))
msg := "no such file or directory"
if runtime.GOOS == "windows" {
msg = "The system cannot find the file specified"
}
assert.ErrorContains(suite.T(), err, msg)
// check that the decrypted file was created
_, err = os.Stat(suite.testFile.Name())
assert.NoError(suite.T(), err, "decrypted file was not created")
os.Args = nil

}

func (suite *DecryptTests) TestDecryptMultipleFiles() {
Expand Down Expand Up @@ -282,3 +270,71 @@ func (suite *DecryptTests) TestDecryptMultipleFiles() {
// Cleanup
os.Args = nil
}

// create a new suite for testing decrypt with the clean flag
type DecryptCleanTests struct {
suite.Suite
tempDir string
fileContent []byte
testFile *os.File
}

func TestDecryptCleanTestSuite(t *testing.T) {
suite.Run(t, new(DecryptCleanTests))
}

// test decrypt with clean flag
func (suite *DecryptCleanTests) TestDecryptClean() {

var err error
// Create a temporary directory for our files
suite.tempDir, err = os.MkdirTemp(os.TempDir(), "sda-cli-test-decrypt-clean")
assert.NoError(suite.T(), err)

// create a test file...
suite.testFile, err = os.CreateTemp(suite.tempDir, "testfile-")
assert.NoError(suite.T(), err)

// ... create some content ...
suite.fileContent = []byte("This is some fine content right here.")
// ... and write the known content to it
err = os.WriteFile(suite.testFile.Name(), suite.fileContent, 0600)
assert.NoError(suite.T(), err)

testKeyFile := filepath.Join(suite.tempDir, "testkey")
err = createKey.GenerateKeyPair(testKeyFile, "")
assert.NoError(suite.T(), err)

// Encrypt a file using the encrypt module. change to the test directory to
// make sure that the checksum files end up there.
cwd, err := os.Getwd()
assert.NoError(suite.T(), err)
err = os.Chdir(suite.tempDir)
assert.NoError(suite.T(), err)
encryptArgs := []string{"encrypt", "-key", fmt.Sprintf("%s.pub.pem", testKeyFile), suite.testFile.Name()}
assert.NoError(suite.T(), encrypt.Encrypt(encryptArgs), "encrypting file for testing failed")
assert.NoError(suite.T(), os.Chdir(cwd))
os.Setenv("C4GH_PASSWORD", "")
if runtime.GOOS != "windows" {
assert.NoError(suite.T(), os.Remove(suite.testFile.Name()))
os.Args = []string{"decrypt", "-key", fmt.Sprintf("%s.sec.pem", testKeyFile), fmt.Sprintf("%s.c4gh", suite.testFile.Name()), "--clean"}
err = Decrypt(os.Args)
assert.NoError(suite.T(), err, "decrypt failed unexpectedly")
// Check that the encrypted file was removed
_, err = os.Stat(fmt.Sprintf("%s.c4gh", suite.testFile.Name()))
assert.ErrorContains(suite.T(), err, "no such file or directory")

// Check content of the decrypted file
inFile, err := os.Open(suite.testFile.Name())
assert.NoError(suite.T(), err, "unable to open decrypted file")
fileData, err := io.ReadAll(inFile)
assert.NoError(suite.T(), err, "unable to read decrypted file")
assert.Equal(suite.T(), string(suite.fileContent), string(fileData))
}

os.Args = nil
}

func (suite *DecryptCleanTests) TearDownTest() {
os.Remove(suite.tempDir)
}

0 comments on commit 740785f

Please sign in to comment.