2017-10-18 05:19:09 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-20 04:01:07 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-10-18 05:19:09 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncrypt(t *testing.T) {
|
|
|
|
key := GetRandomName()
|
2017-10-18 19:29:41 +03:00
|
|
|
encrypted, salt, iv := Encrypt([]byte("hello, world"), key)
|
|
|
|
decrypted, err := Decrypt(encrypted, key, salt, iv)
|
2017-10-18 05:19:09 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if string(decrypted) != "hello, world" {
|
|
|
|
t.Error("problem decrypting")
|
|
|
|
}
|
2017-10-18 19:29:41 +03:00
|
|
|
_, err = Decrypt(encrypted, "wrong passphrase", salt, iv)
|
2017-10-18 05:19:09 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Error("should not work!")
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 04:01:07 +03:00
|
|
|
|
|
|
|
func TestEncryptFiles(t *testing.T) {
|
|
|
|
key := GetRandomName()
|
|
|
|
if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := EncryptFile("temp", "temp.enc", key); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := DecryptFile("temp.enc", "temp.dec", key); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile("temp.dec")
|
|
|
|
if string(data) != "hello, world!" {
|
|
|
|
t.Errorf("Got something weird: " + string(data))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err := DecryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
|
|
|
|
t.Error("should throw error!")
|
|
|
|
}
|
|
|
|
os.Remove("temp.dec")
|
|
|
|
os.Remove("temp.enc")
|
2017-10-20 16:44:42 +03:00
|
|
|
os.Remove("temp")
|
2017-10-20 04:01:07 +03:00
|
|
|
}
|