Skip to content

Commit

Permalink
[decrypt_test] Fix windows test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jbygdell committed Jan 11, 2024
1 parent 6ba6e26 commit ba0f87c
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions decrypt/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"testing"

createKey "github.com/NBISweden/sda-cli/create_key"
Expand Down Expand Up @@ -97,8 +98,12 @@ func (suite *DecryptTests) Testdecrypt() {
assert.NoError(suite.T(), err)

// Test decrypting a non-existent file
msg := "no such file or directory"
if runtime.GOOS == "windows" {
msg = "The system cannot find the file specified."
}
err = decryptFile(filepath.Join(suite.tempDir, "non-existent"), "output_file", *privateKey)
assert.ErrorContains(suite.T(), err, "no such file or directory")
assert.ErrorContains(suite.T(), err, msg)

// Test decryption with malformed key
fakeKey := [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Expand Down Expand Up @@ -131,19 +136,20 @@ func (suite *DecryptTests) TestDecrypt() {
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))
assert.NoError(suite.T(), os.Remove(suite.testFile.Name()))

os.Setenv("C4GH_PASSWORD", "")
os.Args = []string{"decrypt", "-key", fmt.Sprintf("%s.sec.pem", testKeyFile), fmt.Sprintf("%s.c4gh", suite.testFile.Name())}
err = Decrypt(os.Args)
assert.NoError(suite.T(), err, "decrypt failed unexpectedly")

// 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))
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())}
err = Decrypt(os.Args)
assert.NoError(suite.T(), err, "decrypt failed unexpectedly")

// 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 = []string{"decrypt", "-key", fmt.Sprintf("%s.sec.pem", testKeyFile), "--force-overwrite", fmt.Sprintf("%s.c4gh", suite.testFile.Name())}
err = Decrypt(os.Args)
Expand Down

0 comments on commit ba0f87c

Please sign in to comment.