2019-11-25 14:58:12 +00:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncrypt(t *testing.T) {
|
2020-03-09 20:10:08 +03:00
|
|
|
key := []byte("tokenpassphrase")
|
2020-03-11 20:55:39 +03:00
|
|
|
gcm, err := newCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-25 14:58:12 +00:00
|
|
|
data := []byte("supersecret")
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
cipherText, err := Encrypt(gcm, data)
|
2019-11-25 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to encrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the cipherText is not the same as data
|
|
|
|
if bytes.Equal(data, cipherText) {
|
|
|
|
t.Error("encrypted data are the same as plaintext")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrypt(t *testing.T) {
|
2020-03-09 20:10:08 +03:00
|
|
|
key := []byte("tokenpassphrase")
|
2020-03-11 20:55:39 +03:00
|
|
|
gcm, err := newCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-25 14:58:12 +00:00
|
|
|
data := []byte("supersecret")
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
cipherText, err := Encrypt(gcm, data)
|
2019-11-25 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to encrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
plainText, err := Decrypt(gcm, cipherText)
|
2019-11-25 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to decrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the plainText is the same as data
|
|
|
|
if !bytes.Equal(data, plainText) {
|
|
|
|
t.Error("decrypted data not the same as plaintext")
|
|
|
|
}
|
|
|
|
}
|