-
Notifications
You must be signed in to change notification settings - Fork 23
/
helper_test.go
219 lines (191 loc) · 4.92 KB
/
helper_test.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package connection_test
import (
"fmt"
"io"
"log"
"sync"
"time"
"github.com/moov-io/iso8583"
connection "github.com/moov-io/iso8583-connection"
"github.com/moov-io/iso8583-connection/server"
"github.com/moov-io/iso8583/encoding"
"github.com/moov-io/iso8583/field"
"github.com/moov-io/iso8583/network"
"github.com/moov-io/iso8583/prefix"
)
// here are the implementation of the provider protocol:
// * header reader and writer
// * spec
func readMessageLength(r io.Reader) (int, error) {
header := network.NewBinary2BytesHeader()
n, err := header.ReadFrom(r)
if err != nil {
return n, err
}
return header.Length(), nil
}
func writeMessageLength(w io.Writer, length int) (int, error) {
header := network.NewBinary2BytesHeader()
header.SetLength(length)
n, err := header.WriteTo(w)
if err != nil {
return n, fmt.Errorf("writing message header: %w", err)
}
return n, nil
}
var testSpec *iso8583.MessageSpec = &iso8583.MessageSpec{
Name: "ISO 8583 v1987 ASCII",
Fields: map[int]field.Field{
0: field.NewString(&field.Spec{
Length: 4,
Description: "Message Type Indicator",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
1: field.NewBitmap(&field.Spec{
Length: 8,
Description: "Bitmap",
Enc: encoding.Binary,
Pref: prefix.Binary.Fixed,
}),
2: field.NewString(&field.Spec{
Length: 3,
Description: "Test Case Code",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
7: field.NewString(&field.Spec{
Length: 10,
Description: "Transmission Date & Time",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
11: field.NewString(&field.Spec{
Length: 6,
Description: "Systems Trace Audit Number (STAN)",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
63: field.NewString(&field.Spec{
Length: 5,
Description: "Extra field",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
},
}
// create testServer for testing
type testServer struct {
Addr string
Server *server.Server
// to protect following
mutex sync.Mutex
receivedPings int
}
func (t *testServer) Ping() {
t.mutex.Lock()
t.receivedPings++
t.mutex.Unlock()
}
func (t *testServer) ReceivedPings() int {
t.mutex.Lock()
defer t.mutex.Unlock()
return t.receivedPings
}
const (
TestCaseReply string = "000"
// server waits for 500ms before reply
TestCaseDelayedResponse string = "001"
TestCasePingCounter string = "002"
// for sending incoming message with same STAN as
// received message
TestCaseSameSTANRequest string = "003"
TestCaseCloseConnection string = "004"
TestCaseRespondWithExtraField string = "005"
)
func NewTestServerWithAddr(addr string) (*testServer, error) {
var srv *testServer
// define logic for our test server
testServerLogic := func(c *connection.Connection, message *iso8583.Message) {
mti, err := message.GetMTI()
if err != nil {
log.Printf("getting MTI: %s", err.Error())
return
}
// we handle only 0800 messages
if mti != "0800" {
return
}
// update MTI for the response message
newMTI := "0810"
message.MTI(newMTI)
// check if PAN was set to specific test case value
f2 := message.GetField(2)
if f2 != nil {
code, err := f2.String()
if err != nil {
log.Printf("getting field 2: %s", err.Error())
return
}
switch code {
case TestCaseDelayedResponse:
// testing value to "sleep" for a 500ms
time.Sleep(5000 * time.Millisecond)
c.Reply(message)
case TestCaseSameSTANRequest:
// here we will send message to the client with
// the same STAN
stan, _ := message.GetString(11)
incomingMessage := iso8583.NewMessage(testSpec)
incomingMessage.MTI("0800")
incomingMessage.Field(11, stan)
_, err := c.Send(incomingMessage)
if err != nil {
log.Printf("sending message to client: %s", err.Error())
}
// and then delay the reply
time.Sleep(200 * time.Millisecond)
c.Reply(message)
case TestCasePingCounter:
// ping request received
srv.Ping()
c.Reply(message)
case TestCaseCloseConnection:
// reply
c.Reply(message)
// let client receive reply
time.Sleep(50 * time.Millisecond)
c.Close()
case TestCaseReply:
c.Reply(message)
case TestCaseRespondWithExtraField:
message.Field(63, "EXTRA")
c.Reply(message)
default:
c.Reply(message)
}
}
}
s := server.New(testSpec, readMessageLength, writeMessageLength, connection.InboundMessageHandler(testServerLogic))
s.SetOptions(
server.WithErrorHandler(func(err error) {
log.Printf("server error: %s", err.Error())
}),
)
// start on random port
err := s.Start(addr)
if err != nil {
return nil, err
}
srv = &testServer{
Server: s,
Addr: s.Addr,
}
return srv, nil
}
func NewTestServer() (*testServer, error) {
return NewTestServerWithAddr("127.0.0.1:")
}
func (t *testServer) Close() {
t.Server.Close()
}