2014-12-31 08:39:33 -06:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-07 09:06:26 -06:00
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
|
|
|
ks := NewKeyStorePlain(DefaultDataDir())
|
2014-12-31 08:39:33 -06:00
|
|
|
pass := "" // not used but required by API
|
|
|
|
k1, err := ks.GenerateNewKey(pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
k2 := new(Key)
|
|
|
|
k2, err = ks.GetKey(k1.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.Id, k2.Id) {
|
|
|
|
fmt.Println("key Id mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if k1.Flags != k2.Flags {
|
|
|
|
fmt.Println("key Flags mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
|
|
|
fmt.Println("key PrivateKey mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k2.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
2015-01-07 09:06:26 -06:00
|
|
|
ks := NewKeyStorePassphrase(DefaultDataDir())
|
2014-12-31 08:39:33 -06:00
|
|
|
pass := "foo"
|
|
|
|
k1, err := ks.GenerateNewKey(pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
k2 := new(Key)
|
|
|
|
k2, err = ks.GetKey(k1.Id, pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.Id, k2.Id) {
|
|
|
|
fmt.Println("key Id mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if k1.Flags != k2.Flags {
|
|
|
|
fmt.Println("key Flags mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
|
|
|
fmt.Println("key PrivateKey mismatch")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k2.Id, pass) // also to clean up created files
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
2015-01-07 09:06:26 -06:00
|
|
|
ks := NewKeyStorePassphrase(DefaultDataDir())
|
2014-12-31 08:39:33 -06:00
|
|
|
pass := "foo"
|
|
|
|
k1, err := ks.GenerateNewKey(pass)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ks.GetKey(k1.Id, "bar") // wrong passphrase
|
2015-01-07 09:06:26 -06:00
|
|
|
// t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
if err == nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k1.Id, "bar") // wrong passphrase
|
|
|
|
if err == nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ks.DeleteKey(k1.Id, pass) // to clean up
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
t.Error(err)
|
2014-12-31 08:39:33 -06:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyMixedEntropy(t *testing.T) {
|
|
|
|
GetEntropyTinFoilHat()
|
|
|
|
}
|