-
Notifications
You must be signed in to change notification settings - Fork 43
/
test_test.go
77 lines (63 loc) · 1.82 KB
/
test_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
/*
Gapstone is a Go binding for the Capstone disassembly library. For examples,
try reading the *_test.go files.
Library Author: Nguyen Anh Quynh
Binding Author: Ben Nagy
License: BSD style - see LICENSE file for details
(c) 2013 COSEINC. All Rights Reserved.
*/
package gapstone
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)
func TestTest(t *testing.T) {
final := new(bytes.Buffer)
spec_file := "test.SPEC"
var maj, min int
if ver, err := New(0, 0); err == nil {
maj, min = ver.Version()
ver.Close()
}
t.Logf("Basic Test. Capstone Version: %v.%v", maj, min)
for i, platform := range basicTests {
t.Logf("%2d> %s", i, platform.comment)
engine, err := New(platform.arch, platform.mode)
if err != nil {
t.Errorf("Failed to initialize engine %v", err)
return
}
defer engine.Close()
for _, opt := range platform.options {
engine.SetOption(opt.ty, opt.value)
}
insns, err := engine.Disasm([]byte(platform.code), address, 0)
if err == nil {
fmt.Fprintf(final, "****************\n")
fmt.Fprintf(final, "Platform: %s\n", platform.comment)
fmt.Fprintf(final, "Code: ")
dumpHex([]byte(platform.code), final)
fmt.Fprintf(final, "Disasm:\n")
for _, insn := range insns {
fmt.Fprintf(final, "0x%x:\t%s\t\t%s\n", insn.Address, insn.Mnemonic, insn.OpStr)
}
fmt.Fprintf(final, "0x%x:\n", insns[len(insns)-1].Address+insns[len(insns)-1].Size)
fmt.Fprintf(final, "\n")
} else {
t.Errorf("Disassembly error: %v\n", err)
}
}
spec, err := ioutil.ReadFile(spec_file)
if err != nil {
t.Errorf("Cannot read spec file %v: %v", spec_file, err)
}
if fs := final.String(); string(spec) != fs {
// Debugging - uncomment below and run the test | diff - test.SPEC
// fmt.Println(fs)
t.Errorf("Output failed to match spec!")
} else {
t.Logf("Clean diff with %v.\n", spec_file)
}
}