github.com/skyduy/mysocks@v0.0.0-20200109073638-34b4ff361833/cipher/cipher_test.go (about)

     1  package cipher
     2  
     3  import (
     4  	"crypto/rand"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  const (
    10  	MB = 1024 * 1024
    11  )
    12  
    13  // 测试 cipher 加密解密
    14  func TestCipher(t *testing.T) {
    15  	password := RandPassword()
    16  	t.Log(password)
    17  	p, _ := ParsePassword(password)
    18  	cipher := NewCipher(p)
    19  	// 原数据
    20  	org := make([]byte, passwordLength)
    21  	for i := 0; i < passwordLength; i++ {
    22  		org[i] = byte(i)
    23  	}
    24  	// 复制一份原数据到 tmp
    25  	tmp := make([]byte, passwordLength)
    26  	copy(tmp, org)
    27  	t.Log(tmp)
    28  	// 加密 tmp
    29  	cipher.Encode(tmp)
    30  	t.Log(tmp)
    31  	// 解密 tmp
    32  	cipher.Decode(tmp)
    33  	t.Log(tmp)
    34  	if !reflect.DeepEqual(org, tmp) {
    35  		t.Error("解码编码数据后无法还原数据,数据不对应")
    36  	}
    37  }
    38  
    39  func BenchmarkEncode(b *testing.B) {
    40  	password := RandPassword()
    41  	p, _ := ParsePassword(password)
    42  	cipher := NewCipher(p)
    43  	bs := make([]byte, MB)
    44  	b.ResetTimer()
    45  	rand.Read(bs)
    46  	cipher.Encode(bs)
    47  }
    48  
    49  func BenchmarkDecode(b *testing.B) {
    50  	password := RandPassword()
    51  	p, _ := ParsePassword(password)
    52  	cipher := NewCipher(p)
    53  	bs := make([]byte, MB)
    54  	b.ResetTimer()
    55  	rand.Read(bs)
    56  	cipher.Decode(bs)
    57  }