/
validators_test.go
188 lines (175 loc) · 4.56 KB
/
validators_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
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ach
import (
"fmt"
"testing"
)
func TestValidators__checkDigit(t *testing.T) {
cases := map[string]int{
// invalid
"": -1,
"123456": -1,
"1a8ab": -1,
"0730002a": -1,
"0730A002": -1,
"YYYYYYYYY": -1, // users often mask ABA numbers
// valid
"07300022": 8, // Wells Fargo - Iowa
"10200007": 6, // Wells Fargo - Colorado
}
v := validator{}
for rtn, check := range cases {
answer := v.CalculateCheckDigit(rtn)
if check != answer {
t.Errorf("input=%s answer=%d expected=%d", rtn, answer, check)
}
if err := CheckRoutingNumber(fmt.Sprintf("%s%d", rtn, check)); err != nil && check >= 0 {
t.Errorf("input=%s answer=%d expected=%d: %v", rtn, answer, check, err)
}
}
}
func TestValidators__isCreditCardYear(t *testing.T) {
cases := map[string]bool{
// invalid (or out of range)
"10": false,
"00": false,
"51": false,
"17": false,
// valid
"20": true,
"19": true,
}
v := validator{}
for yy, valid := range cases {
err := v.isCreditCardYear(yy)
if valid && err != nil {
t.Errorf("yy=%s failed: %v", yy, err)
}
if !valid && err == nil {
t.Errorf("yy=%s should have failed", yy)
}
}
}
func TestValidators__validateSimpleDate(t *testing.T) {
cases := map[string]string{
// invalid
"": "",
"01": "",
"001520": "", // no 15th month
"001240": "", // no 40th Day
"190001": "", // no 0th month
"190100": "", // no 0th day
// valid
"190101": "190101", // Jan 1st
"201231": "201231", // Dec 31st
"220731": "220731", // July 31st
"350430": "350430", // April 30th
"500229": "500229", // Feb 29th
}
v := validator{}
for input, expected := range cases {
answer := v.validateSimpleDate(input)
if expected != answer {
t.Errorf("input=%q got=%q expected=%q", input, answer, expected)
}
}
}
func TestValidators__validateSimpleTime(t *testing.T) {
cases := map[string]string{
// invalid
"": "",
"01": "",
"012": "",
"123142": "",
// valid
"0000": "0000",
"0100": "0100",
"2359": "2359",
"1201": "1201",
"1238": "1238",
}
v := validator{}
for input, expected := range cases {
answer := v.validateSimpleTime(input)
if expected != answer {
t.Errorf("input=%q got=%q expected=%q", input, answer, expected)
}
}
}
func TestValidators__isAlphanumeric(t *testing.T) {
v := validator{}
tests := []struct {
name string
checkFunc func(string) error
shouldErr func(i int) bool
}{
// Ensure that ASCII characters from 0x20 to 0x7E are considered alphanumeric.
{
name: "is alphanumeric",
checkFunc: v.isAlphanumeric,
shouldErr: func(i int) bool {
return i < 0x20 || i > 0x7E
},
},
// Ensure that ASCII characters from 0x20 to 0x60 and 0x7B to 0x7E are considered upper case alphanumeric.
{
name: "is upper alphanumeric",
checkFunc: v.isUpperAlphanumeric,
shouldErr: func(i int) bool {
return i < 0x20 || i > 0x7E || (i > 0x60 && i < 0x7B)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for i := 0; i < 255; i++ {
chr := string(rune(i))
err := tt.checkFunc(chr)
shouldError := tt.shouldErr(i)
if shouldError && err == nil {
t.Errorf("expected rune %x (%s) to be non-alphanumeric", i, chr)
} else if !shouldError && err != nil {
t.Errorf("expected rune %x (%s) to be alphanumeric: %v", i, chr, err)
}
}
})
}
}
func TestValidators__validateJulianDay(t *testing.T) {
empty := " "
cases := map[string]string{
// invalid
"": empty,
" ": empty,
"01": empty,
"01234": empty,
"XXX": empty,
"000": empty,
"367": empty,
// valid
"001": "001",
"020": "020",
"366": "366",
}
v := validator{}
for input, valid := range cases {
if v.validateSettlementDate(input) != valid {
t.Errorf("julian day=%s failed", input)
}
}
}