protocol/encryptor_aes_test.go

55 lines
1.3 KiB
Go

package protocol
import (
"bytes"
"fmt"
"git.viry.cc/gomod/util"
"testing"
)
func TestEncryptorAes(t *testing.T) {
var ori, data, key []byte
var err error
for i := 1; i < 1024; i++ {
ori = util.RandomKey(i).Bytes()
data = make([]byte, len(ori))
copy(data, ori)
for j := 0; j < 3; j++ {
key = util.RandomKey(16 + j*8).Bytes()
// check
err = SetEncryptorAesKey(key)
if err != nil {
t.Errorf("set aes key failed, %v", err)
}
pkg := newPackage(0, 0, 0, data)
// fmt.Printf("body %d", pkg.bodySize)
pkg.aesEncrypt()
// fmt.Printf(" -> %d", pkg.bodySize)
pkg.aesDecrypt()
// fmt.Printf(" -> %d\n", pkg.bodySize)
if uint32(len(pkg.body)) != pkg.bodySize || pkg.bodySize != uint32(len(ori)) {
t.Errorf("expected [%d] got [%d, %d]\n", uint32(len(ori)), pkg.bodySize, uint32(len(pkg.body)))
}
if !bytes.Equal(pkg.body, ori) {
t.Errorf("expected [%0#2v] got [%0#2v]\n", ori, data)
}
}
}
}
func BenchmarkEncryptorAesEncode(b *testing.B) {
data := util.RandomKey(256 * 1024 * 1024).Bytes()
key := util.RandomKey(16).Bytes()
pkg := newPackage(0, 0, 0, data)
err := SetEncryptorAesKey(key)
if err != nil {
panic(fmt.Sprintf("set aes key failed, %v", err))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
pkg.aesEncrypt()
pkg.aesDecrypt()
}
}