cordawyn-iso8583 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/util.rb ADDED
@@ -0,0 +1,94 @@
1
+ # Copyright 2009 by Tim Becker (tim.becker@kuriostaet.de)
2
+ # MIT License, for details, see the LICENSE file accompaning
3
+ # this distribution
4
+
5
+ module ISO8583
6
+
7
+ # general utilities
8
+ #
9
+ # Convert a String of bytes to their hex representation
10
+ # E.g.:
11
+ #
12
+ # b2hex "\x12\x34\xab" => "1234AB"
13
+ #
14
+ def b2hex(byte_string)
15
+ r = byte_string.unpack("H*")[0]
16
+ r.length > 1 ? r : " "
17
+ end
18
+
19
+ #
20
+ # Convert a String containing hex data to
21
+ # a String containing the corresponding bytes:
22
+ #
23
+ # hex2b "abcd12" => "\xa\cd\12"
24
+ #
25
+ def hex2b(hex_string)
26
+ string = hex_string.gsub(/\s+/, "")
27
+ raise ISO8583Exception.new("Invalid Hex chars: #{hex_string}") unless string =~ /^[A-Fa-f0-9]*$/
28
+ raise ISO8583Exception.new("Uneven number of Hex chars #{hex_string}") unless ( (string.length % 2) == 0)
29
+ [string].pack("H*")
30
+ end
31
+
32
+ def _conv(str, mapping)
33
+ _str = ""
34
+ str.each_byte{|byte|
35
+ _str << mapping[byte]
36
+ }
37
+ _str
38
+ end
39
+
40
+ #
41
+ # Convert a String of ASCII chars to EBCDIC
42
+ #
43
+ def ascii2ebcdic(ascii)
44
+ _conv(ascii, US_ASCII2IBM037)
45
+ end
46
+
47
+ #
48
+ # Convert an EBCDIC String to ASCII
49
+ #
50
+ def ebcdic2ascii(ebcdic)
51
+ _conv(ebcdic, IBM0372US_ASCII)
52
+ end
53
+
54
+ # The charsets supported by iconv aren't guranteed. At the very least MACs don't support ebcdic,
55
+ # so providing rudimentary mappings here.
56
+ US_ASCII2IBM037 = [
57
+ 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
58
+ 0x10, 0x11, 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ib/iso8583'
2
+ require 'test/unit'
3
+
4
+ include ISO8583
5
+
6
+ class BitmapTests < Test::Unit::TestCase
7
+ def test_create_empty
8
+ b = Bitmap.new
9
+ assert_equal(b.to_s.size, 64, "proper length: 64")
10
+ b.set(112)
11
+ assert_equal(b.to_s.size, 128, "proper length: 128")
12
+ assert_equal(b.to_s, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000")
13
+ b.unset(112)
14
+ 5.step(20,2){|i| b.set(i)}
15
+ assert(b.to_s.size==64, "proper length: 64")
16
+ assert_equal(b.to_s, "0000101010101010101000000000000000000000000000000000000000000000")
17
+ assert b[5]
18
+ assert b[7]
19
+ assert b[11]
20
+ assert !b[12]
21
+ assert !b[199]
22
+
23
+ assert_raises(ISO8583Exception) {b.set 1000 }
24
+ assert_raises(ISO8583Exception) { b.set 1 }
25
+ assert_raises(ISO8583Exception) { b.set -1 }
26
+ end
27
+
28
+ def test_out_of_bounds_errors
29
+ b = Bitmap.new
30
+ assert_raises(ISO8583Exception) {b.set 1000 }
31
+ assert_raises(ISO8583Exception) { b.set 1 }
32
+ assert_raises(ISO8583Exception) { b.set -1 }
33
+ end
34
+
35
+ def test_parse_bmp
36
+ # 0000000001001001001001001001001001001001001001001001001001000000
37
+ # generated by: 10.step(60,3) {|i| mp.set(i)}
38
+
39
+ tst = "\x00\x49\x24\x92\x49\x24\x92\x40"
40
+ b = Bitmap.new tst
41
+ 10.step(60,3) {|i|
42
+ assert(b[i], "bit #{i} is not set.")
43
+ assert(!b[i+i], "bit #{i+i} is set.")
44
+ }
45
+
46
+ #10000000000000000001000000100000010000001000000100000010000001000000100000010000001000000100000010000001000000100000010000001000
47
+ # generated by: 20.step(128,7) {|i| mp.set(i)}
48
+ tst = "\x80\x00\x10\x20\x40\x81\x02\x04\x08\x10\x20\x40\x81\x02\x04\x08"
49
+ b = Bitmap.new tst
50
+ 20.step(128,7) {|i|
51
+ assert(b[i], "bit #{i} is not set. (128 bit map)")
52
+ assert(!b[i+i], "bit #{i+i} is set. (128 bit map)")
53
+ }
54
+ end
55
+
56
+ def test_parse_rest
57
+ tst = "\x00\x49\x24\x92\x49\x24\x92\x40\x31\x32\x33\x34"
58
+ b, rest = Bitmap.parse tst
59
+ 10.step(60,3) {|i|
60
+ assert(b[i], "bit #{i} is not set.")
61
+ assert(!b[i+i], "bit #{i+i} is set.")
62
+ }
63
+ assert_equal("1234", rest)
64
+ end
65
+
66
+ def test_each
67
+ bmp = Bitmap.new
68
+ bmp.set(2)
69
+ bmp.set(3)
70
+ bmp.set(5)
71
+ bmp.set(6)
72
+ arr = []
73
+ bmp.each{|bit|
74
+ arr.push bit
75
+ }
76
+ assert_equal [2,3,5,6], arr
77
+ end
78
+
79
+ end
80
+
@@ -0,0 +1,163 @@
1
+ require 'lib/iso8583'
2
+ require 'lib/berlin'
3
+ require 'test/unit'
4
+
5
+ include ISO8583
6
+
7
+ class MessageTest < Test::Unit::TestCase
8
+ def test_create_empty
9
+ mes = BerlinMessage.new
10
+ mes.mti = 1100
11
+ pan = 474747474747
12
+ mes.pan = pan
13
+ assert_equal pan, mes.pan
14
+ assert_equal pan, mes[2]
15
+ assert_equal pan, mes["Primary Account Number (PAN)"]
16
+ assert_equal "1100@\000\000\000\000\000\000\00012474747474747", mes.to_b
17
+
18
+ mes = BerlinMessage.new
19
+ mes.mti = "Authorization Request Response Issuer Gateway"
20
+ pan = 474747474747
21
+ mes[2] = pan
22
+ assert_equal pan, mes.pan
23
+ assert_equal pan, mes[2]
24
+ assert_equal pan, mes["Primary Account Number (PAN)"]
25
+ assert_equal "1110@\000\000\000\000\000\000\00012474747474747", mes.to_b
26
+
27
+ mes = BerlinMessage.new
28
+ mes.mti = 1420
29
+ pan = 474747474747
30
+ mes["Primary Account Number (PAN)"] = pan
31
+ assert_equal pan, mes.pan
32
+ assert_equal pan, mes[2]
33
+ assert_equal pan, mes["Primary Account Number (PAN)"]
34
+ assert_equal "1420@\000\000\000\000\000\000\00012474747474747", mes.to_b
35
+ end
36
+
37
+ def test_parse
38
+ pan = 474747474747
39
+
40
+ assert_raises(ISO8583Exception) {
41
+ mes = BerlinMessage.parse "@\000\000\000\000\000\000\00012474747474747"
42
+ }
43
+ mes = BerlinMessage.parse "1430@\000\000\000\000\000\000\00012474747474747"
44
+ assert_equal pan, mes.pan
45
+ assert_equal 1430, mes.mti
46
+ end
47
+
48
+ def test_to_s
49
+ mes = BerlinMessage.new
50
+ mes.mti = "Network Management Request Response Issuer Gateway or Acquirer Gateway"
51
+ mes[2] = 12341234
52
+ mes[3] = 1111
53
+ mes[4] = 100
54
+ mes[6] = 101
55
+ mes[7] = "0808120000"
56
+ mes[10] = 100
57
+ mes[11] = 0
58
+ mes[12] = "740808120000"
59
+ mes[14] = "1010"
60
+ mes[22] = "POSDATACODE"
61
+ mes[23] = 0
62
+ mes[24] = 1
63
+ mes[25] = 90
64
+ mes[26] = 4444
65
+ mes[30] = 150
66
+ mes[32] = 321321
67
+ mes[35] = ";123123123=123?5"
68
+ mes[37] = "123 123"
69
+ mes[38] = "90"
70
+ mes[39] = 90
71
+ mes[41] = "TermLoc!"
72
+ mes[42] = "ID Code!"
73
+ mes[43] = "Card Acceptor Name Location"
74
+ mes[49] = "840"
75
+ mes[51] = 978
76
+ mes[52] = '\x00\x01\x02\x03'
77
+ mes[53] = '\x07\x06\x05\x04'
78
+ mes[54] = "No additional amount"
79
+ mes[55] = '\x07\x06\x05\x04'
80
+ mes[56] = 88888888888
81
+ mes[59] = "I'm you're private data, data for money..."
82
+ mes[64] = "\xF0\xF0\xF0\xF0"
83
+
84
+ expected = <<-END
85
+ MTI:1814 (Network Management Request Response Issuer Gateway or Acquirer Gateway)
86
+
87
+ 002 Primary Account Number (PAN) : 12341234
88
+ 003 Processing Code : 1111
89
+ 004 Amount (Transaction) : 100
90
+ 006 Amount, Cardholder Billing : 101
91
+ 007 Date and Time, Transmission : 0808120000
92
+ 010 Conversion Rate, Cardholder Billing : 100
93
+ 011 System Trace Audit Number (STAN) : 0
94
+ 012 Date and Time, Local Transaction : 740808120000
95
+ 014 Date, Expiration : 1010
96
+ 022 POS Data Code : POSDATACODE
97
+ 023 Card Sequence Number : 0
98
+ 024 Function Code : 1
99
+ 025 Message Reason Code : 90
100
+ 026 Card Acceptor Business Code : 4444
101
+ 030 Amounts, Original : 150
102
+ 032 Acquiring Institution Identification Code : 321321
103
+ 035 Track 2 Data : ;123123123=123?5
104
+ 037 Retrieval Reference Number : 123 123
105
+ 038 Approval Code : 90
106
+ 039 Action Code : 90
107
+ 041 Card Acceptor Terminal Identification : TermLoc!
108
+ 042 Card Acceptor Identification Code : ID Code!
109
+ 043 Card Acceptor Name/Location : Card Acceptor Name Location
110
+ 049 Currency Code, Transaction : 840
111
+ 051 Currency Code, Cardholder Billing : 978
112
+ 052 Personal Identification Number (PIN) Data : \\x00\\x01\\x02\\x03
113
+ 053 Security Related Control Information : \\x07\\x06\\x05\\x04
114
+ 054 Amounts, Additional : No additional amount
115
+ 055 Integrated Circuit Card (ICC) System Related Data : \\x07\\x06\\x05\\x04
116
+ 056 Original Data Elements : 88888888888
117
+ 059 Additional Data - Private : I'm you're private data, data for money...
118
+ 064 Message Authentication Code (MAC) Field : \360\360\360\360
119
+ END
120
+ assert_equal expected, mes.to_s
121
+ end
122
+
123
+ def test_round_trip
124
+ mes = BerlinMessage.new
125
+ mes.mti = "Network Management Request Response Issuer Gateway or Acquirer Gateway"
126
+ mes[2] = 12341234
127
+ mes[3] = 1111
128
+ mes[4] = 100
129
+ mes[6] = 101
130
+ mes[7] = "0808120000"
131
+ mes[10] = 100
132
+ mes[11] = 0
133
+ mes[12] = "740808120000"
134
+ mes[14] = "1010"
135
+ mes[22] = "POSDATACODE"
136
+ mes[23] = 0
137
+ mes[24] = 1
138
+ mes[25] = 90
139
+ mes[26] = 4444
140
+ mes[30] = 150
141
+ mes[32] = 321321
142
+ mes[35] = ";123123123=123?5"
143
+ mes[37] = "123 123"
144
+ mes[38] = "90"
145
+ mes[39] = 90
146
+ mes[41] = "TermLoc!"
147
+ mes[42] = "ID Code!"
148
+ mes[43] = "Card Acceptor Name Location"
149
+ mes[49] = "840"
150
+ mes[51] = 978
151
+ mes[52] = "\x00\x01\x02\x03"
152
+ mes[53] = "\x07\x06\x05\x04"
153
+ mes[54] = "No additional amount"
154
+ mes[55] = '\x07\x06\x05\x04'
155
+ mes[56] = 88888888888
156
+ mes[59] = "I'm you're private data, data for money..."
157
+ mes[64] = "\xF0\xF0\xF0\xF0"
158
+
159
+ bytes = mes.to_b
160
+ mes2 = BerlinMessage.parse(mes.to_b)
161
+ assert_equal(mes.to_b, mes2.to_b)
162
+ end
163
+ end
@@ -0,0 +1,97 @@
1
+ require 'test/unit'
2
+ require 'lib/iso8583'
3
+
4
+ include ISO8583
5
+
6
+ class FieldTest < Test::Unit::TestCase
7
+ def test_MMDDhhmmssCodec
8
+ dt = MMDDhhmmssCodec.decode "1212121212"
9
+ assert_equal DateTime, dt.class
10
+ assert_equal 12, dt.month
11
+ assert_equal 12, dt.day
12
+ assert_equal 12, dt.hour
13
+ assert_equal 12, dt.min
14
+ assert_equal 12, dt.sec
15
+
16
+ assert_raise(ISO8583Exception) {
17
+ dt = MMDDhhmmssCodec.decode "1312121212"
18
+ }
19
+
20
+ assert_raise(ISO8583Exception) {
21
+ dt = MMDDhhmmssCodec.encode "1312121212"
22
+ }
23
+
24
+ assert_equal "1212121212", MMDDhhmmssCodec.encode("1212121212")
25
+ end
26
+
27
+ def test_YYMMDDhhmmssCodec
28
+ dt = YYMMDDhhmmssCodec.decode "081212121212"
29
+ assert_equal DateTime, dt.class
30
+ assert_equal 2008, dt.year
31
+ assert_equal 12, dt.month
32
+ assert_equal 12, dt.day
33
+ assert_equal 12, dt.hour
34
+ assert_equal 12, dt.min
35
+ assert_equal 12, dt.sec
36
+
37
+ assert_raise(ISO8583Exception) {
38
+ dt = YYMMDDhhmmssCodec.decode "091312121212"
39
+ }
40
+
41
+ assert_raise(ISO8583Exception) {
42
+ dt = YYMMDDhhmmssCodec.encode "091312121212"
43
+ }
44
+
45
+ assert_equal "091212121212", YYMMDDhhmmssCodec.encode("091212121212")
46
+ end
47
+
48
+ def test_YYMMCodec
49
+ dt = YYMMCodec.decode "0812"
50
+ assert_equal DateTime, dt.class
51
+ assert_equal 2008, dt.year
52
+ assert_equal 12, dt.month
53
+
54
+ assert_raise(ISO8583Exception) {
55
+ dt = YYMMCodec.decode "0913"
56
+ }
57
+
58
+ assert_raise(ISO8583Exception) {
59
+ dt = YYMMCodec.encode "0913"
60
+ }
61
+
62
+ assert_equal "0912", YYMMCodec.encode("0912")
63
+ end
64
+
65
+ def test_AN_Codec
66
+ assert_raise(ISO8583Exception) {
67
+ dt = AN_Codec.encode "!!!"
68
+ }
69
+ assert_equal "bla", AN_Codec.encode("bla")
70
+ assert_equal "bla", AN_Codec.decode("bla")
71
+ end
72
+
73
+ def test_Track2_Codec
74
+ assert_raise(ISO8583Exception) {
75
+ dt = Track2.encode "!!!"
76
+ }
77
+ assert_raise(ISO8583Exception) {
78
+ dt = Track2.encode ";12312312=123?5"
79
+ }
80
+ assert_equal ";123123123=123?5", Track2.encode(";123123123=123?5")
81
+ assert_equal ";123123123=123?5", Track2.decode(";123123123=123?5")
82
+ end
83
+
84
+ def test_packed_codec
85
+ assert_equal "\x12", Packed_Number.encode(12)
86
+ assert_equal "\x12", Packed_Number.encode("12")
87
+ assert_equal "\x02", Packed_Number.encode("2")
88
+ assert_equal "\x02", Packed_Number.encode(2)
89
+ assert_equal "\x02\x55", Packed_Number.encode(0xff)
90
+ assert_raise(ISO8583Exception) {
91
+ dt = Packed_Number.encode ";12312312=123?5"
92
+ }
93
+ assert_raise(ISO8583Exception) {
94
+ dt = Packed_Number.encode "F"
95
+ }
96
+ end
97
+ end
@@ -0,0 +1,188 @@
1
+ require 'test/unit'
2
+ require 'lib/iso8583'
3
+
4
+ include ISO8583
5
+
6
+ class FieldTest < Test::Unit::TestCase
7
+ def test_LLL
8
+ value, rest = LLL.parse "123456"
9
+ assert_equal 123, value
10
+ assert_equal "456", rest
11
+
12
+ assert_raise(ISO8583ParseException) {
13
+ l,rest = LLL.parse "12"
14
+ }
15
+
16
+ enc = LLL.encode 123
17
+ assert_equal "\x31\x32\x33", enc
18
+
19
+ enc = LLL.encode "123"
20
+ assert_equal "\x31\x32\x33", enc
21
+
22
+ enc = LLL.encode 12
23
+ assert_equal "\x30\x31\x32", enc
24
+
25
+ #enc = LLL.encode "012"
26
+ #assert_equal "\x30\x31\x32", enc
27
+
28
+
29
+ assert_raise(ISO8583Exception) {
30
+ enc = LLL.encode 1234
31
+ }
32
+
33
+ assert_raise(ISO8583Exception) {
34
+ enc = LLL.encode "1234"
35
+ }
36
+ end
37
+
38
+ def test_LL_BCD
39
+ value, rest = LL_BCD.parse "\x123456"
40
+ assert_equal 12, value
41
+ assert_equal "3456", rest
42
+ end
43
+
44
+ def test_LLVAR_N
45
+ value, rest = LLVAR_N.parse "021234"
46
+ assert_equal 12, value
47
+ assert_equal "34", rest
48
+
49
+ value, rest = LLLVAR_N.parse "0041234"
50
+ assert_equal 1234, value
51
+ assert_equal "", rest
52
+ assert_raise(ISO8583ParseException) {
53
+ l,rest = LLLVAR_N.parse "12"
54
+ }
55
+ assert_raise(ISO8583ParseException) {
56
+ l,rest = LLVAR_N.parse "12123"
57
+ }
58
+
59
+ enc = LLVAR_N.encode 1234
60
+ assert_equal "041234", enc
61
+
62
+ enc = LLVAR_N.encode 123412341234
63
+ assert_equal "12123412341234", enc
64
+
65
+ assert_raise(ISO8583Exception) {
66
+ enc = LLVAR_N.encode "1234ABCD"
67
+ }
68
+
69
+ enc = LLLVAR_N.encode "123412341234"
70
+ assert_equal "012123412341234", enc
71
+
72
+ assert_raise(ISO8583Exception) {
73
+ enc = LLLVAR_N.encode "1234ABCD"
74
+ }
75
+ end
76
+
77
+ def test_LLVAR_Z
78
+ value, rest = LLVAR_Z.parse "16;123123123=123?5"+"021234"
79
+ assert_equal ";123123123=123?5", value
80
+ assert_equal "021234", rest
81
+
82
+ value, rest = LLVAR_Z.parse "16;123123123=123?5"
83
+ assert_equal ";123123123=123?5", value
84
+ assert_equal "", rest
85
+ assert_raise(ISO8583ParseException) {
86
+ l,rest = LLVAR_Z.parse "12"
87
+ }
88
+ assert_raise(ISO8583ParseException) {
89
+ l,rest = LLVAR_Z.parse "17;123123123=123?5"
90
+ }
91
+
92
+ enc = LLVAR_Z.encode ";123123123=123?5"
93
+ assert_equal "16;123123123=123?5", enc
94
+
95
+
96
+ assert_raise(ISO8583Exception) {
97
+ enc = LLVAR_Z.encode "1234ABCD"
98
+ }
99
+ end
100
+
101
+ def test_AN
102
+ fld = AN.dup
103
+ fld.length = 3
104
+ value, rest = fld.parse "1234"
105
+ assert_equal "123", value
106
+ assert_equal "4", rest
107
+
108
+ assert_raise(ISO8583ParseException) {
109
+ fld.parse "12"
110
+ }
111
+
112
+ assert_raise(ISO8583Exception) {
113
+ fld.encode "888810"
114
+ }
115
+ end
116
+
117
+ def test_ANP
118
+ fld = ANP.dup
119
+ fld.length = 3
120
+ value, rest = fld.parse "1234"
121
+ assert_equal "123", value
122
+ assert_equal "4", rest
123
+
124
+ assert_raise(ISO8583ParseException) {
125
+ fld.parse "12"
126
+ }
127
+
128
+ assert_equal "10 ", fld.encode("10")
129
+ end
130
+
131
+ def test_ANS
132
+ fld = ANS.dup
133
+ fld.length = 3
134
+ value, rest = fld.parse "1234"
135
+ assert_equal "123", value
136
+ assert_equal "4", rest
137
+
138
+ assert_raise(ISO8583ParseException) {
139
+ fld.parse "12"
140
+ }
141
+
142
+ assert_equal "10 ", fld.encode("10")
143
+ assert_equal ["1!", "a"], fld.parse("1! a")
144
+ end
145
+
146
+ def test_B
147
+ fld = B.dup
148
+ fld.length = 3
149
+ value, rest = fld.parse "\000234"
150
+ assert_equal "\00023", value
151
+ assert_equal "4", rest
152
+
153
+ assert_raise(ISO8583ParseException) {
154
+ fld.parse "12"
155
+ }
156
+
157
+ assert_equal "10\000", fld.encode("10")
158
+ assert_equal ["1! ", "a"], fld.parse("1! a")
159
+ assert_equal ["1!", ""], fld.parse("1!\000")
160
+ end
161
+
162
+ def test_N_BCD
163
+ fld = N_BCD.dup
164
+ fld.length=3
165
+ value, rest = fld.parse "\x01\x23\x45"
166
+ assert_equal 123, value
167
+
168
+ assert_equal "\x01\x23", fld.encode(123)
169
+ assert_equal "\x01\x23", fld.encode("123")
170
+ assert_equal "\x01\x23", fld.encode("0123")
171
+
172
+ assert_raise(ISO8583Exception) {
173
+ fld.encode 12345
174
+ }
175
+
176
+ # There's a bug here. A 4 digit value encodes to 2 digits encoded,
177
+ # which passes the test for length ... This test doesn't pass:
178
+
179
+ #asssert_raise (ISO8583Exception) {
180
+ # fld.encode 1234
181
+ #}
182
+ end
183
+
184
+ def test_YYMMDDhhmmss
185
+ fld = YYMMDDhhmmss
186
+ assert_equal "740808120000", fld.encode("740808120000")
187
+ end
188
+ end