opcodes_test.go
TEXT FILE1.2 KB · Public source code for developers, search crawlers and AI agents
1package opcodes
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/hex"
7 "testing"
8)
9
10func TestEval(t *testing.T) {
11 for _, c := range []struct{ s, w string }{{"516352675368", "02"}, {"006352675368", "03"}, {"006351635268675468", "04"}, {"516b6c", "01"}, {"51527c75", "02"}, {"51527887", ""}, {"517687", "01"}, {"00519b", "01"}, {"00519a", ""}, {"02010282", "02"}, {"00636a6851", "01"}, {"516452675368", "03"}} {
12 s, _ := hex.DecodeString(c.s)
13 w, _ := hex.DecodeString(c.w)
14 v, e := Eval(s)
15 if e != nil || len(v) == 0 || !bytes.Equal(v[len(v)-1], w) {
16 t.Errorf("%s: %x %v", c.s, v, e)
17 }
18 }
19}
20func TestReject(t *testing.T) {
21 for _, s := range []string{"63", "67", "68", "5163", "6a", "76", "6c", "0201", "0063ff68", "00 69", "515288"} {
22 s = string(bytes.ReplaceAll([]byte(s), []byte(" "), nil))
23 b, _ := hex.DecodeString(s)
24 if _, e := Eval(b); e == nil {
25 t.Errorf("accepted %s", s)
26 }
27 }
28}
29func TestHash(t *testing.T) {
30 v, e := Eval([]byte{1, 42, 0xaa})
31 h := sha256.Sum256([]byte{42})
32 h = sha256.Sum256(h[:])
33 if e != nil || !bytes.Equal(v[0], h[:]) {
34 t.Fatal(v, e)
35 }
36}
37func FuzzEval(f *testing.F) {
38 f.Add([]byte{0x51, 0x76})
39 f.Fuzz(func(t *testing.T, b []byte) { Eval(b) })
40}Read-only source · Select a line number to link directly · Raw download above
