123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- package kcp
- import (
- "bytes"
- "crypto/rand"
- "hash/crc32"
- "io"
- "testing"
- )
- func TestSM4(t *testing.T) {
- bc, err := NewSM4BlockCrypt(pass[:16])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestAES(t *testing.T) {
- bc, err := NewAESBlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestTEA(t *testing.T) {
- bc, err := NewTEABlockCrypt(pass[:16])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestXOR(t *testing.T) {
- bc, err := NewSimpleXORBlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestBlowfish(t *testing.T) {
- bc, err := NewBlowfishBlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestNone(t *testing.T) {
- bc, err := NewNoneBlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestCast5(t *testing.T) {
- bc, err := NewCast5BlockCrypt(pass[:16])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func Test3DES(t *testing.T) {
- bc, err := NewTripleDESBlockCrypt(pass[:24])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestTwofish(t *testing.T) {
- bc, err := NewTwofishBlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestXTEA(t *testing.T) {
- bc, err := NewXTEABlockCrypt(pass[:16])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func TestSalsa20(t *testing.T) {
- bc, err := NewSalsa20BlockCrypt(pass[:32])
- if err != nil {
- t.Fatal(err)
- }
- cryptTest(t, bc)
- }
- func cryptTest(t *testing.T, bc BlockCrypt) {
- data := make([]byte, mtuLimit)
- io.ReadFull(rand.Reader, data)
- dec := make([]byte, mtuLimit)
- enc := make([]byte, mtuLimit)
- bc.Encrypt(enc, data)
- bc.Decrypt(dec, enc)
- if !bytes.Equal(data, dec) {
- t.Fail()
- }
- }
- func BenchmarkSM4(b *testing.B) {
- bc, err := NewSM4BlockCrypt(pass[:16])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkAES128(b *testing.B) {
- bc, err := NewAESBlockCrypt(pass[:16])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkAES192(b *testing.B) {
- bc, err := NewAESBlockCrypt(pass[:24])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkAES256(b *testing.B) {
- bc, err := NewAESBlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkTEA(b *testing.B) {
- bc, err := NewTEABlockCrypt(pass[:16])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkXOR(b *testing.B) {
- bc, err := NewSimpleXORBlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkBlowfish(b *testing.B) {
- bc, err := NewBlowfishBlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkNone(b *testing.B) {
- bc, err := NewNoneBlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkCast5(b *testing.B) {
- bc, err := NewCast5BlockCrypt(pass[:16])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func Benchmark3DES(b *testing.B) {
- bc, err := NewTripleDESBlockCrypt(pass[:24])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkTwofish(b *testing.B) {
- bc, err := NewTwofishBlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkXTEA(b *testing.B) {
- bc, err := NewXTEABlockCrypt(pass[:16])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func BenchmarkSalsa20(b *testing.B) {
- bc, err := NewSalsa20BlockCrypt(pass[:32])
- if err != nil {
- b.Fatal(err)
- }
- benchCrypt(b, bc)
- }
- func benchCrypt(b *testing.B, bc BlockCrypt) {
- b.ReportAllocs()
- data := make([]byte, mtuLimit)
- io.ReadFull(rand.Reader, data)
- dec := make([]byte, mtuLimit)
- enc := make([]byte, mtuLimit)
- for i := 0; i < b.N; i++ {
- bc.Encrypt(enc, data)
- bc.Decrypt(dec, enc)
- }
- b.SetBytes(int64(len(enc) * 2))
- }
- func BenchmarkCRC32(b *testing.B) {
- content := make([]byte, 1024)
- b.SetBytes(int64(len(content)))
- for i := 0; i < b.N; i++ {
- crc32.ChecksumIEEE(content)
- }
- }
|