init.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package tests
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. )
  11. var (
  12. baseDir = filepath.Join(".", "files")
  13. blockTestDir = filepath.Join(baseDir, "BlockchainTests")
  14. stateTestDir = filepath.Join(baseDir, "StateTests")
  15. transactionTestDir = filepath.Join(baseDir, "TransactionTests")
  16. vmTestDir = filepath.Join(baseDir, "VMTests")
  17. BlockSkipTests = []string{
  18. "SimpleTx3",
  19. // TODO: check why these fail
  20. "BLOCK__RandomByteAtTheEnd",
  21. "TRANSCT__RandomByteAtTheEnd",
  22. "BLOCK__ZeroByteAtTheEnd",
  23. "TRANSCT__ZeroByteAtTheEnd",
  24. // TODO: why does this fail? should be check in ethash now
  25. "DifficultyIsZero",
  26. // TODO: why does this fail?
  27. "wrongMixHash",
  28. }
  29. TransSkipTests = []string{"TransactionWithHihghNonce256"}
  30. StateSkipTests = []string{"mload32bitBound_return", "mload32bitBound_return2"}
  31. VmSkipTests = []string{}
  32. )
  33. func readJson(reader io.Reader, value interface{}) error {
  34. data, err := ioutil.ReadAll(reader)
  35. if err != nil {
  36. return fmt.Errorf("Error reading JSON file", err.Error())
  37. }
  38. if err = json.Unmarshal(data, &value); err != nil {
  39. if syntaxerr, ok := err.(*json.SyntaxError); ok {
  40. line := findLine(data, syntaxerr.Offset)
  41. return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
  42. }
  43. return fmt.Errorf("JSON unmarshal error: %v", err)
  44. }
  45. return nil
  46. }
  47. func readJsonHttp(uri string, value interface{}) error {
  48. resp, err := http.Get(uri)
  49. if err != nil {
  50. return err
  51. }
  52. defer resp.Body.Close()
  53. err = readJson(resp.Body, value)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. func readJsonFile(fn string, value interface{}) error {
  60. file, err := os.Open(fn)
  61. if err != nil {
  62. return err
  63. }
  64. defer file.Close()
  65. err = readJson(file, value)
  66. if err != nil {
  67. return fmt.Errorf("%s in file %s", err.Error(), fn)
  68. }
  69. return nil
  70. }
  71. // findLine returns the line number for the given offset into data.
  72. func findLine(data []byte, offset int64) (line int) {
  73. line = 1
  74. for i, r := range string(data) {
  75. if int64(i) >= offset {
  76. return
  77. }
  78. if r == '\n' {
  79. line++
  80. }
  81. }
  82. return
  83. }