github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/codec/json_test.go (about) 1 // Copyright 2015 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 17 package codec 18 19 import ( 20 "bytes" 21 "io" 22 "net" 23 "testing" 24 "time" 25 ) 26 27 type jsonTestConn struct { 28 buffer *bytes.Buffer 29 } 30 31 func newJsonTestConn(data []byte) *jsonTestConn { 32 return &jsonTestConn{ 33 buffer: bytes.NewBuffer(data), 34 } 35 } 36 37 func (self *jsonTestConn) Read(p []byte) (n int, err error) { 38 return self.buffer.Read(p) 39 } 40 41 func (self *jsonTestConn) Write(p []byte) (n int, err error) { 42 return self.buffer.Write(p) 43 } 44 45 func (self *jsonTestConn) Close() error { 46 // not implemented 47 return nil 48 } 49 50 func (self *jsonTestConn) LocalAddr() net.Addr { 51 // not implemented 52 return nil 53 } 54 55 func (self *jsonTestConn) RemoteAddr() net.Addr { 56 // not implemented 57 return nil 58 } 59 60 func (self *jsonTestConn) SetDeadline(t time.Time) error { 61 return nil 62 } 63 64 func (self *jsonTestConn) SetReadDeadline(t time.Time) error { 65 return nil 66 } 67 68 func (self *jsonTestConn) SetWriteDeadline(t time.Time) error { 69 return nil 70 } 71 72 func TestJsonDecoderWithValidRequest(t *testing.T) { 73 reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","params":[],"id":64}`) 74 decoder := newJsonTestConn(reqdata) 75 76 jsonDecoder := NewJsonCoder(decoder) 77 requests, batch, err := jsonDecoder.ReadRequest() 78 79 if err != nil { 80 t.Errorf("Read valid request failed - %v", err) 81 } 82 83 if len(requests) != 1 { 84 t.Errorf("Expected to get a single request but got %d", len(requests)) 85 } 86 87 if batch { 88 t.Errorf("Got batch indication while expecting single request") 89 } 90 91 if requests[0].Id != float64(64) { 92 t.Errorf("Expected req.Id == 64 but got %v", requests[0].Id) 93 } 94 95 if requests[0].Method != "modules" { 96 t.Errorf("Expected req.Method == 'modules' got '%s'", requests[0].Method) 97 } 98 } 99 100 func TestJsonDecoderWithValidBatchRequest(t *testing.T) { 101 reqdata := []byte(`[{"jsonrpc":"2.0","method":"modules","params":[],"id":64}, 102 {"jsonrpc":"2.0","method":"modules","params":[],"id":64}]`) 103 decoder := newJsonTestConn(reqdata) 104 105 jsonDecoder := NewJsonCoder(decoder) 106 requests, batch, err := jsonDecoder.ReadRequest() 107 108 if err != nil { 109 t.Errorf("Read valid batch request failed - %v", err) 110 } 111 112 if len(requests) != 2 { 113 t.Errorf("Expected to get two requests but got %d", len(requests)) 114 } 115 116 if !batch { 117 t.Errorf("Got no batch indication while expecting batch request") 118 } 119 120 for i := 0; i < len(requests); i++ { 121 if requests[i].Id != float64(64) { 122 t.Errorf("Expected req.Id == 64 but got %v", requests[i].Id) 123 } 124 125 if requests[i].Method != "modules" { 126 t.Errorf("Expected req.Method == 'modules' got '%s'", requests[i].Method) 127 } 128 } 129 } 130 131 func TestJsonDecoderWithInvalidIncompleteMessage(t *testing.T) { 132 reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","pa`) 133 decoder := newJsonTestConn(reqdata) 134 135 jsonDecoder := NewJsonCoder(decoder) 136 requests, batch, err := jsonDecoder.ReadRequest() 137 138 if err != io.ErrUnexpectedEOF { 139 t.Errorf("Expected to read an incomplete request err but got %v", err) 140 } 141 142 // remaining message 143 decoder.Write([]byte(`rams":[],"id:64"}`)) 144 requests, batch, err = jsonDecoder.ReadRequest() 145 146 if err == nil { 147 t.Errorf("Expected an error but got nil") 148 } 149 150 if len(requests) != 0 { 151 t.Errorf("Expected to get no requests but got %d", len(requests)) 152 } 153 154 if batch { 155 t.Errorf("Got batch indication while expecting non batch") 156 } 157 }