github.com/xraypb/xray-core@v1.6.6/proxy/shadowsocks/config_test.go (about)

     1  package shadowsocks_test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/xraypb/xray-core/common"
     9  	"github.com/xraypb/xray-core/common/buf"
    10  	"github.com/xraypb/xray-core/proxy/shadowsocks"
    11  )
    12  
    13  func TestAEADCipherUDP(t *testing.T) {
    14  	rawAccount := &shadowsocks.Account{
    15  		CipherType: shadowsocks.CipherType_AES_128_GCM,
    16  		Password:   "test",
    17  	}
    18  	account, err := rawAccount.AsAccount()
    19  	common.Must(err)
    20  
    21  	cipher := account.(*shadowsocks.MemoryAccount).Cipher
    22  
    23  	key := make([]byte, cipher.KeySize())
    24  	common.Must2(rand.Read(key))
    25  
    26  	payload := make([]byte, 1024)
    27  	common.Must2(rand.Read(payload))
    28  
    29  	b1 := buf.New()
    30  	common.Must2(b1.ReadFullFrom(rand.Reader, cipher.IVSize()))
    31  	common.Must2(b1.Write(payload))
    32  	common.Must(cipher.EncodePacket(key, b1))
    33  
    34  	common.Must(cipher.DecodePacket(key, b1))
    35  	if diff := cmp.Diff(b1.Bytes(), payload); diff != "" {
    36  		t.Error(diff)
    37  	}
    38  }