my computer is about to die!!!
This commit is contained in:
1 parent
d863dc3bd1
commit
e80f2601ab
13 files changed
+244
-11
No files matched your search
@@ -0,0 +1,3 @@
|
||||
package files
|
||||
|
||||
// package that manage the encryption of files and directory
|
||||
@@ -0,0 +1,15 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
"github.com/didiercrunch/filou/envelop"
|
||||
"github.com/didiercrunch/filou/payload"
|
||||
)
|
||||
|
||||
func EncryptFile(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||
payload_ := payload.GetDefaultPayload(reader)
|
||||
envelop := envelop.NewEnveloper(nil, payload_, nil)
|
||||
return envelop.WriteToWriter(writer)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type encryptor func(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error
|
||||
|
||||
type PathEncryptor struct {
|
||||
EncryptFile encryptor
|
||||
EncryptDirectory encryptor
|
||||
}
|
||||
|
||||
func (this *PathEncryptor) EncryptPath(path string, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if fd, err := f.Stat(); err != nil {
|
||||
return err
|
||||
} else if fd.IsDir() {
|
||||
return this.EncryptDirectory(f, writer, acceptedContract)
|
||||
} else {
|
||||
return this.EncryptFile(f, writer, acceptedContract)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/didiercrunch/filou/contract"
|
||||
"github.com/didiercrunch/filou/helper"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type mockEncryptor struct {
|
||||
N int
|
||||
}
|
||||
|
||||
func (this *mockEncryptor) Encrypt(reader io.Reader, writer io.Writer, acceptedContract *contract.AcceptedContract) error {
|
||||
this.N += 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestEncryptPathWrongPath(t *testing.T) {
|
||||
pe := new(PathEncryptor)
|
||||
|
||||
filePath := "/something_that_does_not_exists"
|
||||
if err := pe.EncryptPath(filePath, nil, nil); err == nil {
|
||||
t.Error("should say the file does not exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptPathForDirectory(t *testing.T) {
|
||||
dirName := helper.GetTmpEmptyDir()
|
||||
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||
|
||||
if err := pe.EncryptPath(dirName, nil, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if fileEncryptor.N != 0 {
|
||||
t.Error("should have 0", fileEncryptor.N)
|
||||
} else if dirEncryptor.N != 1 {
|
||||
t.Error("should have 1", dirEncryptor.N)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptPathForFile(t *testing.T) {
|
||||
file := helper.GetTmpEmptyFile()
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
fileEncryptor, dirEncryptor := new(mockEncryptor), new(mockEncryptor)
|
||||
pe := &PathEncryptor{fileEncryptor.Encrypt, dirEncryptor.Encrypt}
|
||||
|
||||
if err := pe.EncryptPath(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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user