package opcodes import ( "bytes" "crypto/sha256" "encoding/hex" "testing" ) func TestEval(t *testing.T) { 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"}} { s, _ := hex.DecodeString(c.s) w, _ := hex.DecodeString(c.w) v, e := Eval(s) if e != nil || len(v) == 0 || !bytes.Equal(v[len(v)-1], w) { t.Errorf("%s: %x %v", c.s, v, e) } } } func TestReject(t *testing.T) { for _, s := range []string{"63", "67", "68", "5163", "6a", "76", "6c", "0201", "0063ff68", "00 69", "515288"} { s = string(bytes.ReplaceAll([]byte(s), []byte(" "), nil)) b, _ := hex.DecodeString(s) if _, e := Eval(b); e == nil { t.Errorf("accepted %s", s) } } } func TestHash(t *testing.T) { v, e := Eval([]byte{1, 42, 0xaa}) h := sha256.Sum256([]byte{42}) h = sha256.Sum256(h[:]) if e != nil || !bytes.Equal(v[0], h[:]) { t.Fatal(v, e) } } func FuzzEval(f *testing.F) { f.Add([]byte{0x51, 0x76}) f.Fuzz(func(t *testing.T, b []byte) { Eval(b) }) }