frame_test.go 2.0 KB
Newer Older
D
Derek Parker 已提交
1 2 3 4 5 6 7 8 9 10 11
package frame

import (
	"bytes"
	"debug/elf"
	"os"
	"path/filepath"
	"testing"
)

func grabDebugFrameSection(fp string, t *testing.T) []byte {
12
	p, err := filepath.Abs("../_fixtures/testprog")
D
Derek Parker 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	if err != nil {
		t.Fatal(err)
	}

	f, err := os.Open(p)
	if err != nil {
		t.Fatal(err)
	}

	ef, err := elf.NewFile(f)
	if err != nil {
		t.Fatal(err)
	}

	data, err := ef.Section(".debug_frame").Data()
	if err != nil {
		t.Fatal(err)
	}

	return data
}

35
func TestDecodeULEB128(t *testing.T) {
D
Derek Parker 已提交
36 37
	var leb128 = bytes.NewBuffer([]byte{0xE5, 0x8E, 0x26})

D
Derek Parker 已提交
38
	n, c := decodeULEB128(leb128)
D
Derek Parker 已提交
39 40 41 42 43
	if n != 624485 {
		t.Fatal("Number was not decoded properly, got: ", n, c)
	}
}

44 45 46 47 48 49 50 51 52
func TestDecodeSLEB128(t *testing.T) {
	sleb128 := bytes.NewBuffer([]byte{0x9b, 0xf1, 0x59})

	n, c := decodeSLEB128(sleb128)
	if n != -624485 {
		t.Fatal("Number was not decoded properly, got: ", n, c)
	}
}

D
Derek Parker 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
func TestParseString(t *testing.T) {
	bstr := bytes.NewBuffer([]byte{'h', 'i', 0x0, 0xFF, 0xCC})
	str, _ := parseString(bstr)

	if str != "hi" {
		t.Fatal("String was not parsed correctly")
	}
}

func TestParse(t *testing.T) {
	data := grabDebugFrameSection("../fixtures/testprog", t)
	ce := Parse(data)[0]

	if ce.Length != 16 {
		t.Fatal("Length was not parsed correctly")
	}

	if ce.CIE_id != 0xffffffff {
		t.Fatal("CIE id was not parsed correctly")
	}

	if ce.Version != 0x3 {
		t.Fatalf("Version was not parsed correctly expected %#v got %#v, data was %#v", 0x3, ce.Version, data[0:40])
	}

	if ce.Augmentation != "" {
		t.Fatal("Augmentation was not parsed correctly")
	}

	if ce.CodeAlignmentFactor != 0x1 {
		t.Fatal("Code Alignment Factor was not parsed correctly")
	}

86 87
	if ce.DataAlignmentFactor != -4 {
		t.Fatalf("Data Alignment Factor was not parsed correctly got %#v", ce.DataAlignmentFactor)
D
Derek Parker 已提交
88 89 90 91 92 93 94 95 96 97 98 99
	}

	fe := ce.FrameDescriptorEntries[0]

	if fe.Length != 32 {
		t.Fatal("Length was not parsed correctly")
	}

	if fe.CIE_pointer != ce {
		t.Fatal("Frame entry does not point to parent CIE")
	}

D
Derek Parker 已提交
100 101
	if fe.InitialLocation != 0x400c00 {
		t.Fatalf("Initial location not parsed correctly, got %#v", fe.InitialLocation)
D
Derek Parker 已提交
102 103
	}
}