I hate commit messages
This commit is contained in:
1 parent
209af0d820
commit
8654c45e35
11 files changed
+278
-35
No files matched your search
@@ -1,3 +1,39 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
"github.com/didiercrunch/filou/helper"
|
||||
)
|
||||
|
||||
func getRSAPrivateKey() contract.GetRSAPrivateKey {
|
||||
return func() (*rsa.PrivateKey, error) {
|
||||
return helper.GetRSAPrivateKey(2048), nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSensibleAcceptedContract() *contract.AcceptedContract {
|
||||
ret := new(contract.AcceptedContract)
|
||||
ret.Hash = "sha512"
|
||||
ret.BlockCipher = "aes256"
|
||||
ret.BlockCipherMode = "ofb"
|
||||
ret.AsynchronousEncryptionScheme = "rsa_oaep"
|
||||
ret.SignatureScheme = "rsa_pss"
|
||||
ret.RsaPrivateKey = getRSAPrivateKey()
|
||||
ret.RsaPublicKey = helper.GetRSAPublicKey(2048)
|
||||
return ret
|
||||
}
|
||||
|
||||
func TestEncryptFile(t *testing.T) {
|
||||
buffer := bytes.NewBufferString("some secret text")
|
||||
w := helper.NewMockIoWriter()
|
||||
c := getSensibleAcceptedContract()
|
||||
if err := EncryptFile(buffer, w, c); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
fmt.Println(w.String())
|
||||
}
|
||||
+57
-4
@@ -1,15 +1,14 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type encryptor func(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error
|
||||
|
||||
type PathEncryptor struct {
|
||||
@@ -17,7 +16,61 @@ type PathEncryptor struct {
|
||||
EncryptDirectory encryptor
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) EncryptPath(path string, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||
func (this *PathEncryptor) fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) getDefaultPathToEncryptFile(originalFileName string) string {
|
||||
isFileWithSingleExtension := regexp.MustCompile(`\.\w+$`)
|
||||
isFileWithDoubleExtension := regexp.MustCompile(`\.\w+\.\w+$`)
|
||||
isHiddenDirectory := regexp.MustCompile(`\/.\w+$`)
|
||||
isHiddenFile := regexp.MustCompile(`\/\.\w+\.\w+$`)
|
||||
|
||||
if isHiddenFile.MatchString(originalFileName) {
|
||||
return isFileWithSingleExtension.ReplaceAllString(originalFileName, ".filou")
|
||||
}
|
||||
if isHiddenDirectory.MatchString(originalFileName) {
|
||||
return originalFileName + ".filou"
|
||||
}
|
||||
if isFileWithDoubleExtension.MatchString(originalFileName) {
|
||||
return isFileWithDoubleExtension.ReplaceAllString(originalFileName, ".filou")
|
||||
}
|
||||
if isFileWithSingleExtension.MatchString(originalFileName) {
|
||||
return isFileWithSingleExtension.ReplaceAllString(originalFileName, ".filou")
|
||||
}
|
||||
return originalFileName + ".filou"
|
||||
}
|
||||
|
||||
// create a file with permission set to 600
|
||||
func (this *PathEncryptor) createPrivateFile(name string) (*os.File, error) {
|
||||
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) EncryptPathToDefaultFile(path string, acceptedContract *contract.AcceptedContract) error {
|
||||
outputPathName := this.getDefaultPathToEncryptFile(path)
|
||||
if _, err := os.Stat(outputPathName); !os.IsNotExist(err) {
|
||||
return errors.New("the default path to encrypt already exists.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) EncryptPathToOtherPath(fromPath, toPath string, acceptedContract *contract.AcceptedContract) error {
|
||||
if this.fileExists(toPath) {
|
||||
return errors.New("output file already exists")
|
||||
}
|
||||
if f, err := this.createPrivateFile(toPath); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer f.Close()
|
||||
return this.EncryptPathToWriter(fromPath, f, acceptedContract)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) EncryptPathToWriter(path string, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||
if !this.fileExists(path) {
|
||||
return errors.New("input file does not exists")
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+47
-10
@@ -3,6 +3,7 @@ package files
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
@@ -20,11 +21,16 @@ func (this *mockEncryptor) Encrypt(reader io.Reader, writer io.Writer, acceptedC
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeFile(name string, t *testing.T) {
|
||||
if err := os.Remove(name); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestEncryptPathWrongPath(t *testing.T) {
|
||||
pe := new(PathEncryptor)
|
||||
|
||||
filePath := "/something_that_does_not_exists"
|
||||
if err := pe.EncryptPath(filePath, nil, nil); err == nil {
|
||||
if err := pe.EncryptPathToWriter(filePath, nil, nil); err == nil {
|
||||
t.Error("should say the file does not exists")
|
||||
}
|
||||
}
|
||||
@@ -34,7 +40,7 @@ func TestEncryptPathForDirectory(t *testing.T) {
|
||||
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||
|
||||
if err := pe.EncryptPath(dirName, nil, nil); err != nil {
|
||||
if err := pe.EncryptPathToWriter(dirName, nil, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if fileEncryptor.N != 0 {
|
||||
t.Error("should have 0", fileEncryptor.N)
|
||||
@@ -43,17 +49,48 @@ func TestEncryptPathForDirectory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptPathForFile(t *testing.T) {
|
||||
file := helper.GetTmpEmptyFile()
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
func TestEncryptPathToWriter(t *testing.T) {
|
||||
file := helper.GetTmpFileWithText("foo bar")
|
||||
defer removeFile(file.Name(), t)
|
||||
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||
|
||||
if err := pe.EncryptPath(file.Name(), nil, nil); err != nil {
|
||||
if err := pe.EncryptPathToWriter(file.Name(), nil, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if fileEncryptor.N != 1 {
|
||||
t.Error("should have 1", fileEncryptor.N)
|
||||
} else if dirEncryptor.N != 0 {
|
||||
t.Error("should have 0", dirEncryptor.N)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefaultPathToEncryptFile(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/foo/bar.txt": "/foo/bar.filou", // regular file
|
||||
"/foo/bar.tar.gz": "/foo/bar.filou", // file with two extensions
|
||||
"/foo/bar": "/foo/bar.filou", // directory
|
||||
"/foo/.bar": "/foo/.bar.filou", // hidden directory
|
||||
"/foo/.bar.txt": "/foo/.bar.filou", // hidden file
|
||||
}
|
||||
pe := new(PathEncryptor)
|
||||
for input, expectedOutput := range cases {
|
||||
if output := pe.getDefaultPathToEncryptFile(input); output != expectedOutput {
|
||||
t.Error("expected", expectedOutput, "but received", output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptPathToOtherPath(t *testing.T) {
|
||||
fromFile := helper.GetTmpFileWithText("foo bar")
|
||||
defer removeFile(fromFile.Name(), t)
|
||||
|
||||
toFile := helper.GetTmpFileName()
|
||||
defer removeFile(toFile, t)
|
||||
|
||||
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||
|
||||
if err := pe.EncryptPathToOtherPath(fromFile.Name(), toFile, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if fileEncryptor.N != 1 {
|
||||
t.Error("should have 1", fileEncryptor.N)
|
||||
|
||||
Reference in new issue
Block a user