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