cipher_test.go 1.0 KB

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