asm_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package asm
  17. import (
  18. "testing"
  19. "encoding/hex"
  20. )
  21. // Tests disassembling the instructions for valid evm code
  22. func TestInstructionIteratorValid(t *testing.T) {
  23. cnt := 0
  24. script, _ := hex.DecodeString("61000000")
  25. it := NewInstructionIterator(script)
  26. for it.Next() {
  27. cnt++
  28. }
  29. if err := it.Error(); err != nil {
  30. t.Errorf("Expected 2, but encountered error %v instead.", err)
  31. }
  32. if cnt != 2 {
  33. t.Errorf("Expected 2, but got %v instead.", cnt)
  34. }
  35. }
  36. // Tests disassembling the instructions for invalid evm code
  37. func TestInstructionIteratorInvalid(t *testing.T) {
  38. cnt := 0
  39. script, _ := hex.DecodeString("6100")
  40. it := NewInstructionIterator(script)
  41. for it.Next() {
  42. cnt++
  43. }
  44. if it.Error() == nil {
  45. t.Errorf("Expected an error, but got %v instead.", cnt)
  46. }
  47. }
  48. // Tests disassembling the instructions for empty evm code
  49. func TestInstructionIteratorEmpty(t *testing.T) {
  50. cnt := 0
  51. script, _ := hex.DecodeString("")
  52. it := NewInstructionIterator(script)
  53. for it.Next() {
  54. cnt++
  55. }
  56. if err := it.Error(); err != nil {
  57. t.Errorf("Expected 0, but encountered error %v instead.", err)
  58. }
  59. if cnt != 0 {
  60. t.Errorf("Expected 0, but got %v instead.", cnt)
  61. }
  62. }