2020-03-11 01:52:06 +03:00
|
|
|
package secretbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/config/secrets"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretBox(t *testing.T) {
|
|
|
|
secretKey, err := base64.StdEncoding.DecodeString("4jbVgq8FsAV7vy+n8WqEZrl7BUtNqh3fYT5RXzXOPFY=")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:57:57 +03:00
|
|
|
s := NewSecrets()
|
2020-03-11 01:52:06 +03:00
|
|
|
|
|
|
|
if err := s.Init(); err == nil {
|
|
|
|
t.Error("Secretbox accepted an empty secret key")
|
|
|
|
}
|
2020-04-27 16:57:57 +03:00
|
|
|
if err := s.Init(secrets.Key([]byte("invalid"))); err == nil {
|
2020-03-11 01:52:06 +03:00
|
|
|
t.Error("Secretbox accepted a secret key that is invalid")
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:57:57 +03:00
|
|
|
if err := s.Init(secrets.Key(secretKey)); err != nil {
|
2020-03-11 01:52:06 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := s.Options()
|
2020-04-27 16:57:57 +03:00
|
|
|
if !reflect.DeepEqual(o.Key, secretKey) {
|
2020-03-11 01:52:06 +03:00
|
|
|
t.Error("Init() didn't set secret key correctly")
|
|
|
|
}
|
|
|
|
if s.String() != "nacl-secretbox" {
|
|
|
|
t.Error(s.String() + " should be nacl-secretbox")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try 10 times to get different nonces
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
message := []byte(`Can you hear me, Major Tom?`)
|
|
|
|
|
|
|
|
encrypted, err := s.Encrypt(message)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to encrypt message (%s)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted, err := s.Decrypt(encrypted)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to decrypt encrypted message (%s)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(message, decrypted) {
|
|
|
|
t.Errorf("Decrypted Message dod not match encrypted message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|