add benchmark test for cipher/hash

master
fangdingjun 6 years ago
parent 89b099fdb3
commit a1a67d272c

@ -4,25 +4,55 @@ import (
"bytes" "bytes"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand"
"testing" "testing"
) )
func TestCipherSize(t *testing.T) {
data := []struct {
t int
bsize int
isize int
}{
{GNUTLS_CIPHER_AES_128_CBC, 16, 16},
{GNUTLS_CIPHER_AES_192_CBC, 24, 16},
{GNUTLS_CIPHER_AES_256_CBC, 32, 16},
}
for _, d := range data {
blocksize := GetCipherKeySize(d.t)
if blocksize != d.bsize {
t.Errorf("%d block size expect: %d, got: %d", d.t, d.bsize, blocksize)
}
ivsize := GetCipherIVSize(d.t)
if ivsize != d.isize {
t.Errorf("%d iv size expect: %d, got: %d", d.t, d.bsize, ivsize)
}
}
}
func TestEncryptDecrypt(t *testing.T) { func TestEncryptDecrypt(t *testing.T) {
key := []byte("0123456789abcdef") cipherName := GNUTLS_CIPHER_AES_256_CBC
iv := []byte("abcdefg123456789") keysize := GetCipherKeySize(cipherName)
c, err := NewCipher(GNUTLS_CIPHER_AES_128_CBC, key, iv) ivsize := GetCipherIVSize(cipherName)
blocksize := GetCipherBlockSize(cipherName)
key := make([]byte, keysize)
iv := make([]byte, ivsize)
rand.Reader.Read(key)
rand.Reader.Read(iv)
c, err := NewCipher(cipherName, key, iv)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close() defer c.Close()
c1, err := NewCipher(GNUTLS_CIPHER_AES_128_CBC, key, iv) c1, err := NewCipher(cipherName, key, iv)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c1.Close() defer c1.Close()
data := []byte("1234012121212121") data := make([]byte, blocksize*10)
if c == nil { if c == nil {
t.Fatal("new ciphoer failed") t.Fatal("new ciphoer failed")
} }
@ -45,3 +75,50 @@ func TestEncryptDecrypt(t *testing.T) {
t.Fatal("cipher text not equal to cypto/aes") t.Fatal("cipher text not equal to cypto/aes")
} }
} }
func BenchmarkAESEncrypt(b *testing.B) {
cipherName := GNUTLS_CIPHER_AES_256_CBC
keysize := GetCipherKeySize(cipherName)
ivsize := GetCipherIVSize(cipherName)
blocksize := GetCipherBlockSize(cipherName)
datalen := blocksize * 500
key := make([]byte, keysize)
iv := make([]byte, ivsize)
rand.Reader.Read(key)
rand.Reader.Read(iv)
buf := make([]byte, datalen)
for i := 0; i < b.N; i++ {
c, err := NewCipher(cipherName, key, iv)
if err != nil {
b.Fatal(err)
}
c.Encrypt(buf)
c.Close()
}
}
func BenchmarkAESEncrypt2(b *testing.B) {
cipherName := GNUTLS_CIPHER_AES_256_CBC
keysize := GetCipherKeySize(cipherName)
ivsize := GetCipherIVSize(cipherName)
blocksize := GetCipherBlockSize(cipherName)
datalen := blocksize * 500
key := make([]byte, keysize)
iv := make([]byte, ivsize)
buf := make([]byte, datalen)
rand.Reader.Read(buf)
rand.Reader.Read(key)
rand.Reader.Read(iv)
dst := make([]byte, datalen)
for i := 0; i < b.N; i++ {
block, _ := aes.NewCipher(key)
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(dst, buf)
}
}

@ -2,6 +2,7 @@ package gnutls
import ( import (
"bytes" "bytes"
"crypto/rand"
"crypto/sha512" "crypto/sha512"
"encoding/hex" "encoding/hex"
"log" "log"
@ -9,16 +10,40 @@ import (
) )
func TestHashSHA(t *testing.T) { func TestHashSHA(t *testing.T) {
h := NewHash(GNUTLS_SHA512) h := NewHash(GNUTLS_HASH_SHA512)
defer h.Close() defer h.Close()
data := []byte("1234") data := []byte("1234")
h1 := h.Sum(data) h1 := h.Sum(data)
h3 := sha512.Sum512(data) h3 := sha512.New()
if !bytes.Equal(h3[:], h1) { h3.Write(data)
log.Printf("\n%s\n%s", hex.EncodeToString(h3[:]), hex.EncodeToString(h1)) h4 := h3.Sum(nil)
if !bytes.Equal(h4[:], h1) {
log.Printf("\n%s\n%s", hex.EncodeToString(h4[:]), hex.EncodeToString(h1))
t.Fatal("hash not equal") t.Fatal("hash not equal")
} }
} }
func BenchmarkHashSHA512(b *testing.B) {
buf := make([]byte, 1000*1024)
rand.Reader.Read(buf)
for i := 0; i < b.N; i++ {
h := NewHash(GNUTLS_HASH_SHA512)
h.Write(buf)
h.Sum(nil)
h.Close()
}
}
func BenchmarkHashSHA512s(b *testing.B) {
buf := make([]byte, 1000*1024)
rand.Reader.Read(buf)
for i := 0; i < b.N; i++ {
h := sha512.New()
h.Write(buf)
h.Sum(nil)
//h.Close()
}
}

@ -45,7 +45,7 @@ func TestTLSClient(t *testing.T) {
} }
}() }()
c, err := Dial("tcp", addr, &Config{}) c, err := Dial("tcp", addr, &Config{InsecureSkipVerify: true})
if err != nil { if err != nil {
t.Fatal("gnutls dial ", err) t.Fatal("gnutls dial ", err)
} }

Loading…
Cancel
Save