github.com/micro/go-micro/v2@v2.9.1/tunnel/crypto_test.go (about) 1 package tunnel 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestEncrypt(t *testing.T) { 9 key := []byte("tokenpassphrase") 10 gcm, err := newCipher(key) 11 if err != nil { 12 t.Fatal(err) 13 } 14 15 data := []byte("supersecret") 16 17 cipherText, err := Encrypt(gcm, data) 18 if err != nil { 19 t.Errorf("failed to encrypt data: %v", err) 20 } 21 22 // verify the cipherText is not the same as data 23 if bytes.Equal(data, cipherText) { 24 t.Error("encrypted data are the same as plaintext") 25 } 26 } 27 28 func TestDecrypt(t *testing.T) { 29 key := []byte("tokenpassphrase") 30 gcm, err := newCipher(key) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 data := []byte("supersecret") 36 37 cipherText, err := Encrypt(gcm, data) 38 if err != nil { 39 t.Errorf("failed to encrypt data: %v", err) 40 } 41 42 plainText, err := Decrypt(gcm, cipherText) 43 if err != nil { 44 t.Errorf("failed to decrypt data: %v", err) 45 } 46 47 // verify the plainText is the same as data 48 if !bytes.Equal(data, plainText) { 49 t.Error("decrypted data not the same as plaintext") 50 } 51 }