2014-12-31 14:39:33 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2015-01-15 16:45:45 +00:00
|
|
|
crand "crypto/rand"
|
2014-12-31 14:39:33 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-07 15:06:26 +00:00
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
|
|
|
ks := NewKeyStorePlain(DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "" // not used but required by API
|
2015-01-15 16:45:45 +00:00
|
|
|
k1, err := ks.GenerateNewKey(crand.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k2 := new(Key)
|
|
|
|
k2, err = ks.GetKey(k1.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.Id, k2.Id) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k2.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
2015-01-07 15:06:26 +00:00
|
|
|
ks := NewKeyStorePassphrase(DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "foo"
|
2015-01-15 16:45:45 +00:00
|
|
|
k1, err := ks.GenerateNewKey(crand.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
k2 := new(Key)
|
|
|
|
k2, err = ks.GetKey(k1.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(k1.Id, k2.Id) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k2.Id, pass) // also to clean up created files
|
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
2015-01-07 15:06:26 +00:00
|
|
|
ks := NewKeyStorePassphrase(DefaultDataDir())
|
2014-12-31 14:39:33 +00:00
|
|
|
pass := "foo"
|
2015-01-15 16:45:45 +00:00
|
|
|
k1, err := ks.GenerateNewKey(crand.Reader, pass)
|
2014-12-31 14:39:33 +00:00
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ks.GetKey(k1.Id, "bar") // wrong passphrase
|
|
|
|
if err == nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k1.Id, "bar") // wrong passphrase
|
|
|
|
if err == nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k1.Id, pass) // to clean up
|
|
|
|
if err != nil {
|
2015-01-15 16:45:45 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 14:39:33 +00:00
|
|
|
}
|
|
|
|
}
|