| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x86asm |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/hex" |
| 9 |
"io/ioutil" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
func TestDecode(t *testing.T) { |
| 16 |
data, err := ioutil.ReadFile("testdata/decode.txt") |
| 17 |
if err != nil { |
| 18 |
t.Fatal(err) |
| 19 |
} |
| 20 |
all := string(data) |
| 21 |
for strings.Contains(all, "\t\t") { |
| 22 |
all = strings.Replace(all, "\t\t", "\t", -1) |
| 23 |
} |
| 24 |
for _, line := range strings.Split(all, "\n") { |
| 25 |
line = strings.TrimSpace(line) |
| 26 |
if line == "" || strings.HasPrefix(line, "#") { |
| 27 |
continue |
| 28 |
} |
| 29 |
f := strings.SplitN(line, "\t", 4) |
| 30 |
i := strings.Index(f[0], "|") |
| 31 |
if i < 0 { |
| 32 |
t.Errorf("parsing %q: missing | separator", f[0]) |
| 33 |
continue |
| 34 |
} |
| 35 |
if i%2 != 0 { |
| 36 |
t.Errorf("parsing %q: misaligned | separator", f[0]) |
| 37 |
} |
| 38 |
size := i / 2 |
| 39 |
code, err := hex.DecodeString(f[0][:i] + f[0][i+1:]) |
| 40 |
if err != nil { |
| 41 |
t.Errorf("parsing %q: %v", f[0], err) |
| 42 |
continue |
| 43 |
} |
| 44 |
mode, err := strconv.Atoi(f[1]) |
| 45 |
if err != nil { |
| 46 |
t.Errorf("invalid mode %q in: %s", f[1], line) |
| 47 |
continue |
| 48 |
} |
| 49 |
syntax, asm := f[2], f[3] |
| 50 |
inst, err := Decode(code, mode) |
| 51 |
var out string |
| 52 |
if err != nil { |
| 53 |
out = "error: " + err.Error() |
| 54 |
} else { |
| 55 |
switch syntax { |
| 56 |
case "gnu": |
| 57 |
out = GNUSyntax(inst, 0, nil) |
| 58 |
case "intel": |
| 59 |
out = IntelSyntax(inst, 0, nil) |
| 60 |
case "plan9": |
| 61 |
out = GoSyntax(inst, 0, nil) |
| 62 |
default: |
| 63 |
t.Errorf("unknown syntax %q", syntax) |
| 64 |
continue |
| 65 |
} |
| 66 |
} |
| 67 |
if out != asm || inst.Len != size { |
| 68 |
t.Errorf("Decode(%s) [%s] = %s, %d, want %s, %d", f[0], syntax, out, inst.Len, asm, size) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestDecodeDoesNotCrash(t *testing.T) { |
| 74 |
cases := [...][]byte{ |
| 75 |
[]byte{}, |
| 76 |
[]byte{0xc5}, |
| 77 |
[]byte{0xc4}, |
| 78 |
} |
| 79 |
for _, test := range cases { |
| 80 |
_, err := Decode([]byte(test), 64) |
| 81 |
if err == nil { |
| 82 |
t.Errorf("expected error on invalid instruction %x", test) |
| 83 |
} |
| 84 |
} |
| 85 |
} |