add benchmark test for cipher/hash

master
fangdingjun 7 years ago
parent 89b099fdb3
commit a1a67d272c

@ -4,25 +4,55 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"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) {
key := []byte("0123456789abcdef")
iv := []byte("abcdefg123456789")
c, err := NewCipher(GNUTLS_CIPHER_AES_128_CBC, key, iv)
cipherName := GNUTLS_CIPHER_AES_256_CBC
keysize := GetCipherKeySize(cipherName)
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 {
t.Fatal(err)
}
defer c.Close()
c1, err := NewCipher(GNUTLS_CIPHER_AES_128_CBC, key, iv)
c1, err := NewCipher(cipherName, key, iv)
if err != nil {
t.Fatal(err)
}
defer c1.Close()
data := []byte("1234012121212121")
data := make([]byte, blocksize*10)
if c == nil {
t.Fatal("new ciphoer failed")
}
@ -45,3 +75,50 @@ func TestEncryptDecrypt(t *testing.T) {
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 (
"bytes"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"log"
@ -9,16 +10,40 @@ import (
)
func TestHashSHA(t *testing.T) {
h := NewHash(GNUTLS_SHA512)
h := NewHash(GNUTLS_HASH_SHA512)
defer h.Close()
data := []byte("1234")
h1 := h.Sum(data)
h3 := sha512.Sum512(data)
if !bytes.Equal(h3[:], h1) {
log.Printf("\n%s\n%s", hex.EncodeToString(h3[:]), hex.EncodeToString(h1))
h3 := sha512.New()
h3.Write(data)
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")
}
}
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 {
t.Fatal("gnutls dial ", err)
}

Loading…
Cancel
Save