gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/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 := "tokenpassphrase"
    10  	data := []byte("supersecret")
    11  
    12  	cipherText, err := Encrypt(data, key)
    13  	if err != nil {
    14  		t.Errorf("failed to encrypt data: %v", err)
    15  	}
    16  
    17  	// verify the cipherText is not the same as data
    18  	if bytes.Equal(data, cipherText) {
    19  		t.Error("encrypted data are the same as plaintext")
    20  	}
    21  }
    22  
    23  func TestDecrypt(t *testing.T) {
    24  	key := "tokenpassphrase"
    25  	data := []byte("supersecret")
    26  
    27  	cipherText, err := Encrypt(data, key)
    28  	if err != nil {
    29  		t.Errorf("failed to encrypt data: %v", err)
    30  	}
    31  
    32  	plainText, err := Decrypt(cipherText, key)
    33  	if err != nil {
    34  		t.Errorf("failed to decrypt data: %v", err)
    35  	}
    36  
    37  	// verify the plainText is the same as data
    38  	if !bytes.Equal(data, plainText) {
    39  		t.Error("decrypted data not the same as plaintext")
    40  	}
    41  }