2019-09-04 16:02:24 +00:00
|
|
|
package frame
|
2014-06-29 16:52:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-04 16:02:24 +00:00
|
|
|
"bytes"
|
2016-01-24 08:47:22 +00:00
|
|
|
"encoding/binary"
|
2014-09-19 04:00:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-06-29 16:52:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-04-17 21:13:00 +00:00
|
|
|
"github.com/pkg/profile"
|
2014-06-29 16:52:21 +00:00
|
|
|
)
|
|
|
|
|
2019-09-04 16:02:24 +00:00
|
|
|
func TestParseCIE(t *testing.T) {
|
|
|
|
ctx := &parseContext{
|
|
|
|
buf : bytes.NewBuffer([]byte{3,0,1,124,16,12,7,8,5,16,2,0,36,0,0,0,0,0,0,0,0,16,64,0,0,0,0,0}),
|
|
|
|
common: &CommonInformationEntry{Length:12},
|
|
|
|
length: 12,
|
|
|
|
}
|
|
|
|
_ = parseCIE(ctx)
|
|
|
|
|
|
|
|
common := ctx.common
|
|
|
|
|
|
|
|
if common.Version != 3 {
|
|
|
|
t.Fatalf("Expected Version 3, but get %d", common.Version)
|
|
|
|
}
|
|
|
|
if common.Augmentation != "" {
|
|
|
|
t.Fatalf("Expected Augmentation \"\", but get %s", common.Augmentation)
|
|
|
|
}
|
|
|
|
if common.CodeAlignmentFactor != 1 {
|
|
|
|
t.Fatalf("Expected CodeAlignmentFactor 1, but get %d", common.CodeAlignmentFactor)
|
|
|
|
}
|
|
|
|
if common.DataAlignmentFactor != -4 {
|
|
|
|
t.Fatalf("Expected DataAlignmentFactor -4, but get %d", common.DataAlignmentFactor)
|
|
|
|
}
|
|
|
|
if common.ReturnAddressRegister != 16 {
|
|
|
|
t.Fatalf("Expected ReturnAddressRegister 16, but get %d", common.ReturnAddressRegister)
|
|
|
|
}
|
|
|
|
initialInstructions := []byte{12,7,8,5,16,2,0}
|
|
|
|
if !bytes.Equal(common.InitialInstructions, initialInstructions){
|
|
|
|
t.Fatalf("Expected InitialInstructions %v, but get %v", initialInstructions, common.InitialInstructions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 18:30:04 +00:00
|
|
|
func BenchmarkParse(b *testing.B) {
|
|
|
|
defer profile.Start(profile.CPUProfile).Stop()
|
2014-09-19 04:00:41 +00:00
|
|
|
f, err := os.Open("testdata/frame")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2014-07-14 18:30:04 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-09-04 16:02:24 +00:00
|
|
|
Parse(data, binary.BigEndian, 0)
|
2014-07-14 18:30:04 +00:00
|
|
|
}
|
|
|
|
}
|