json_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package codec
  17. import (
  18. "bytes"
  19. "io"
  20. "net"
  21. "testing"
  22. "time"
  23. )
  24. type jsonTestConn struct {
  25. buffer *bytes.Buffer
  26. }
  27. func newJsonTestConn(data []byte) *jsonTestConn {
  28. return &jsonTestConn{
  29. buffer: bytes.NewBuffer(data),
  30. }
  31. }
  32. func (self *jsonTestConn) Read(p []byte) (n int, err error) {
  33. return self.buffer.Read(p)
  34. }
  35. func (self *jsonTestConn) Write(p []byte) (n int, err error) {
  36. return self.buffer.Write(p)
  37. }
  38. func (self *jsonTestConn) Close() error {
  39. // not implemented
  40. return nil
  41. }
  42. func (self *jsonTestConn) LocalAddr() net.Addr {
  43. // not implemented
  44. return nil
  45. }
  46. func (self *jsonTestConn) RemoteAddr() net.Addr {
  47. // not implemented
  48. return nil
  49. }
  50. func (self *jsonTestConn) SetDeadline(t time.Time) error {
  51. return nil
  52. }
  53. func (self *jsonTestConn) SetReadDeadline(t time.Time) error {
  54. return nil
  55. }
  56. func (self *jsonTestConn) SetWriteDeadline(t time.Time) error {
  57. return nil
  58. }
  59. func TestJsonDecoderWithValidRequest(t *testing.T) {
  60. reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","params":[],"id":64}`)
  61. decoder := newJsonTestConn(reqdata)
  62. jsonDecoder := NewJsonCoder(decoder)
  63. requests, batch, err := jsonDecoder.ReadRequest()
  64. if err != nil {
  65. t.Errorf("Read valid request failed - %v", err)
  66. }
  67. if len(requests) != 1 {
  68. t.Errorf("Expected to get a single request but got %d", len(requests))
  69. }
  70. if batch {
  71. t.Errorf("Got batch indication while expecting single request")
  72. }
  73. if requests[0].Id != float64(64) {
  74. t.Errorf("Expected req.Id == 64 but got %v", requests[0].Id)
  75. }
  76. if requests[0].Method != "modules" {
  77. t.Errorf("Expected req.Method == 'modules' got '%s'", requests[0].Method)
  78. }
  79. }
  80. func TestJsonDecoderWithValidBatchRequest(t *testing.T) {
  81. reqdata := []byte(`[{"jsonrpc":"2.0","method":"modules","params":[],"id":64},
  82. {"jsonrpc":"2.0","method":"modules","params":[],"id":64}]`)
  83. decoder := newJsonTestConn(reqdata)
  84. jsonDecoder := NewJsonCoder(decoder)
  85. requests, batch, err := jsonDecoder.ReadRequest()
  86. if err != nil {
  87. t.Errorf("Read valid batch request failed - %v", err)
  88. }
  89. if len(requests) != 2 {
  90. t.Errorf("Expected to get two requests but got %d", len(requests))
  91. }
  92. if !batch {
  93. t.Errorf("Got no batch indication while expecting batch request")
  94. }
  95. for i := 0; i < len(requests); i++ {
  96. if requests[i].Id != float64(64) {
  97. t.Errorf("Expected req.Id == 64 but got %v", requests[i].Id)
  98. }
  99. if requests[i].Method != "modules" {
  100. t.Errorf("Expected req.Method == 'modules' got '%s'", requests[i].Method)
  101. }
  102. }
  103. }
  104. func TestJsonDecoderWithInvalidIncompleteMessage(t *testing.T) {
  105. reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","pa`)
  106. decoder := newJsonTestConn(reqdata)
  107. jsonDecoder := NewJsonCoder(decoder)
  108. requests, batch, err := jsonDecoder.ReadRequest()
  109. if err != io.ErrUnexpectedEOF {
  110. t.Errorf("Expected to read an incomplete request err but got %v", err)
  111. }
  112. // remaining message
  113. decoder.Write([]byte(`rams":[],"id:64"}`))
  114. requests, batch, err = jsonDecoder.ReadRequest()
  115. if err == nil {
  116. t.Errorf("Expected an error but got nil")
  117. }
  118. if len(requests) != 0 {
  119. t.Errorf("Expected to get no requests but got %d", len(requests))
  120. }
  121. if batch {
  122. t.Errorf("Got batch indication while expecting non batch")
  123. }
  124. }