-
Notifications
You must be signed in to change notification settings - Fork 2
/
fields_test.go
215 lines (193 loc) · 5.35 KB
/
fields_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
package iso8583
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"strings"
"testing"
)
func TestNumericFixed(t *testing.T) {
n := NewNumeric("12345")
b, err := n.Encode(ASCII, 5, "", "N")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("12345")) {
t.Error("bad encoding")
}
n = NewNumeric("12345")
b, err = n.Encode(ASCII, 6, "", "N")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("012345")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12345", "string should be the same")
n = NewNumeric("12345")
b, err = n.Encode(ASCII, 10, "", "N")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("0000012345")) {
t.Error("bad encoding")
}
str = n.String()
equals(t, str, "12345", "string should be the same")
}
func TestNumericLLVAR(t *testing.T) {
n := NewNumeric("12345")
b, err := n.Encode(ASCII, 19, "LLVAR", "N")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("0512345")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12345", "string should be the same")
b, err = n.Encode(ASCII, 4, "LLVAR", "N")
if err == nil {
t.Error("expecting error, length 4 < len(n)")
}
b, err = n.Encode(ASCII, 5, "LLVAR", "N")
if err != nil {
t.Error(err)
}
}
func TestNumericLLLVAR(t *testing.T) {
n := NewNumeric("12345")
b, err := n.Encode(ASCII, 19, "LLLVAR", "N")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("00512345")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12345", "string should be the same")
b, err = n.Encode(ASCII, 4, "LLLVAR", "N")
if err == nil {
t.Error("expecting error, length 4 < len(n)")
}
b, err = n.Encode(ASCII, 5, "LLLVAR", "N")
if err != nil {
t.Error(err)
}
}
func TestAlphaNumericFixed(t *testing.T) {
n := NewAlphanumeric("12AN")
b, err := n.Encode(ASCII, 10, "", "AN")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("12AN ")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12AN", "string should be the same")
}
func TestAlphaNumericLLVAR(t *testing.T) {
n := NewAlphanumeric("12ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS")
b, err := n.Encode(ASCII, 99, "LLVAR", "AN")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("4812ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS", "string should be the same")
}
func TestAlphaNumericLLLVAR(t *testing.T) {
n := NewAlphanumeric("12ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS")
b, err := n.Encode(ASCII, 99, "LLLVAR", "AN")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("04812ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS")) {
t.Error("bad encoding")
}
str := n.String()
equals(t, str, "12ANABCDEFGHTCASDASSAASCSACSACSACSACSACACSACSACS", "string should be the same")
}
func TestNDecode(t *testing.T) {
b := []byte("0512345")
n := NewNumeric("")
_, err := n.Decode(b, ASCII, 19, "LLVAR", "N")
if err != nil {
t.Fatal(err)
}
t.Log(string(n.Value))
b = []byte("0000012356")
nn, err := n.Decode(b, ASCII, 10, "", "")
if err != nil {
t.Fatal(err)
}
t.Log(string(n.Value))
t.Log(nn)
}
func TestMarshalJSON(t *testing.T) {
m := Message{
DE2: NewNumeric("1234412"),
DE22: NewAlphanumeric("3123"),
DE35: NewTrack2Code("latrack2"),
DE37: NewANP("affa32"),
DE41: NewANS("ans433"),
DE52: NewBinary64("433"),
DE125: &SubMessage{
SE85: NewBN("123"),
},
}
result, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
expected := `{"DE2":"1234412","DE22":"3123","DE35":"latrack2","DE37":"affa32","DE41":"ans433","DE52":"00000000000001B1","DE125":{"SE85":"123"}}`
equals(t, string(result), expected, "")
}
func TestUnmarshalJSON(t *testing.T) {
m := Message{}
expected := `{"DE2":"1234412","DE22":"3123","DE35":"latrack2","DE37":"affa32","DE41":"ans433","DE52":"00000000000001B1","DE125":{"SE85":"123"}}`
// expected := `{"DE2":"1234412"}`
err := json.Unmarshal([]byte(expected), &m)
if err != nil {
t.Fatal(err)
}
equals(t, string(m.DE2.Value), "1234412", "")
equals(t, string(m.DE22.Value), "3123", "")
equals(t, string(m.DE35.Value), "latrack2", "")
equals(t, string(m.DE37.Value), "affa32", "")
equals(t, string(m.DE41.Value), "ans433", "")
equals(t, string(m.DE52.Value), "00000000000001B1", "")
equals(t, string(m.DE125.SE85.Value), "123", "")
//equals(t, string(result), expected, "")
}
// TestToLog will make sure if anyone runs ToLog that it will not show DE2 (primary account number)
func TestToLog(t *testing.T) {
// Test new message
m := New()
m.DE2 = NewNumeric(strconv.Itoa(rand.Intn((9999999999999999+1)-1111111111111111) + 1111111111111111))
newString := fmt.Sprint(m)
if !strings.Contains(newString, m.DE2.String()) {
t.Error("String should have contained the DE2 value but it didnt")
}
// Test new safe message
ms := NewSafe()
ms.DE2 = NewNumeric(strconv.Itoa(rand.Intn((9999999999999999+1)-1111111111111111) + 1111111111111111))
newSafeString := fmt.Sprint(ms)
if strings.Contains(newSafeString, ms.DE2.String()) {
t.Error("String should have NOT contained the DE2 value but it did")
}
}
func equals(t *testing.T, actual, expected, description string) {
if description == "" {
description = "empty description"
}
if actual != expected {
t.Errorf("Data should be %s, instead of %s [%s]", expected, actual, description)
}
}