From 740785fc4e3cd087a65a4b713150b05a511482e0 Mon Sep 17 00:00:00 2001 From: Panos Chatzopoulos Date: Mon, 21 Oct 2024 14:11:06 +0200 Subject: [PATCH] fix tests --- decrypt/decrypt.go | 15 ++++++-- decrypt/decrypt_test.go | 82 ++++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/decrypt/decrypt.go b/decrypt/decrypt.go index e275567..8b4af67 100644 --- a/decrypt/decrypt.go +++ b/decrypt/decrypt.go @@ -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): @@ -93,7 +94,9 @@ 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: @@ -101,20 +104,24 @@ 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++ } // 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 } 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 diff --git a/decrypt/decrypt_test.go b/decrypt/decrypt_test.go index 16022e0..e0e0a08 100644 --- a/decrypt/decrypt_test.go +++ b/decrypt/decrypt_test.go @@ -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() { @@ -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) +}