1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package codec
import (
"bytes"
"io"
"net"
"testing"
"time"
)
type jsonTestConn struct {
buffer *bytes.Buffer
}
func newJsonTestConn(data []byte) *jsonTestConn {
return &jsonTestConn{
buffer: bytes.NewBuffer(data),
}
}
func (self *jsonTestConn) Read(p []byte) (n int, err error) {
return self.buffer.Read(p)
}
func (self *jsonTestConn) Write(p []byte) (n int, err error) {
return self.buffer.Write(p)
}
func (self *jsonTestConn) Close() error {
// not implemented
return nil
}
func (self *jsonTestConn) LocalAddr() net.Addr {
// not implemented
return nil
}
func (self *jsonTestConn) RemoteAddr() net.Addr {
// not implemented
return nil
}
func (self *jsonTestConn) SetDeadline(t time.Time) error {
return nil
}
func (self *jsonTestConn) SetReadDeadline(t time.Time) error {
return nil
}
func (self *jsonTestConn) SetWriteDeadline(t time.Time) error {
return nil
}
func TestJsonDecoderWithValidRequest(t *testing.T) {
reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","params":[],"id":64}`)
decoder := newJsonTestConn(reqdata)
jsonDecoder := NewJsonCoder(decoder)
requests, batch, err := jsonDecoder.ReadRequest()
if err != nil {
t.Errorf("Read valid request failed - %v", err)
}
if len(requests) != 1 {
t.Errorf("Expected to get a single request but got %d", len(requests))
}
if batch {
t.Errorf("Got batch indication while expecting single request")
}
if requests[0].Id != float64(64) {
t.Errorf("Expected req.Id == 64 but got %v", requests[0].Id)
}
if requests[0].Method != "modules" {
t.Errorf("Expected req.Method == 'modules' got '%s'", requests[0].Method)
}
}
func TestJsonDecoderWithValidBatchRequest(t *testing.T) {
reqdata := []byte(`[{"jsonrpc":"2.0","method":"modules","params":[],"id":64},
{"jsonrpc":"2.0","method":"modules","params":[],"id":64}]`)
decoder := newJsonTestConn(reqdata)
jsonDecoder := NewJsonCoder(decoder)
requests, batch, err := jsonDecoder.ReadRequest()
if err != nil {
t.Errorf("Read valid batch request failed - %v", err)
}
if len(requests) != 2 {
t.Errorf("Expected to get two requests but got %d", len(requests))
}
if !batch {
t.Errorf("Got no batch indication while expecting batch request")
}
for i := 0; i < len(requests); i++ {
if requests[i].Id != float64(64) {
t.Errorf("Expected req.Id == 64 but got %v", requests[i].Id)
}
if requests[i].Method != "modules" {
t.Errorf("Expected req.Method == 'modules' got '%s'", requests[i].Method)
}
}
}
func TestJsonDecoderWithInvalidIncompleteMessage(t *testing.T) {
reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","pa`)
decoder := newJsonTestConn(reqdata)
jsonDecoder := NewJsonCoder(decoder)
requests, batch, err := jsonDecoder.ReadRequest()
if err != io.ErrUnexpectedEOF {
t.Errorf("Expected to read an incomplete request err but got %v", err)
}
// remaining message
decoder.Write([]byte(`rams":[],"id:64"}`))
requests, batch, err = jsonDecoder.ReadRequest()
if err == nil {
t.Errorf("Expected an error but got nil")
}
if len(requests) != 0 {
t.Errorf("Expected to get no requests but got %d", len(requests))
}
if batch {
t.Errorf("Got batch indication while expecting non batch")
}
}
|