-
Notifications
You must be signed in to change notification settings - Fork 108
/
message_test.go
2647 lines (2358 loc) · 80.3 KB
/
message_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package iso8583
import (
"encoding/hex"
"encoding/json"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/moov-io/iso8583/encoding"
iso8583errors "github.com/moov-io/iso8583/errors"
"github.com/moov-io/iso8583/field"
"github.com/moov-io/iso8583/padding"
"github.com/moov-io/iso8583/prefix"
"github.com/moov-io/iso8583/sort"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMessage(t *testing.T) {
spec := &MessageSpec{
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{
Description: "Bitmap",
Enc: encoding.BytesToASCIIHex,
Pref: prefix.Hex.Fixed,
}),
2: field.NewString(&field.Spec{
Length: 19,
Description: "Primary Account Number",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
3: field.NewComposite(&field.Spec{
Length: 6,
Description: "Processing Code",
Pref: prefix.ASCII.Fixed,
Tag: &field.TagSpec{
Sort: sort.StringsByInt,
},
Subfields: map[string]field.Field{
"1": field.NewString(&field.Spec{
Length: 2,
Description: "Transaction Type",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"2": field.NewString(&field.Spec{
Length: 2,
Description: "From Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"3": field.NewString(&field.Spec{
Length: 2,
Description: "To Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
},
}),
4: field.NewString(&field.Spec{
Length: 12,
Description: "Transaction Amount",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
// this field will be ignored when packing and
// unpacking, as bit 65 is a bitmap presence indicator
65: field.NewString(&field.Spec{
Length: 1,
Description: "Settlement Code",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
// this is a field of the third bitmap
130: field.NewString(&field.Spec{
Length: 1,
Description: "Additional Data",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
},
}
// this test most probably will fail in regular mode,
// and should fail when is run with -race flag
t.Run("No data race when accessing fields concurrently", func(t *testing.T) {
message := NewMessage(spec)
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// calling GetString writes into the map of the
// set fields
message.GetString(0)
}()
}
wg.Wait()
})
t.Run("Test packing and unpacking untyped fields", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("0100")
require.NoError(t, message.Field(2, "4242424242424242"))
require.NoError(t, message.Field(3, "123456"))
require.NoError(t, message.Field(4, "100"))
got, err := message.Pack()
want := "01007000000000000000164242424242424242123456000000000100"
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, want, string(got))
message = NewMessage(spec)
err = message.Unpack([]byte(want))
require.NoError(t, err)
s, err := message.GetMTI()
require.NoError(t, err)
require.Equal(t, "0100", s)
s, err = message.GetString(2)
require.NoError(t, err)
require.Equal(t, "4242424242424242", s)
s, err = message.GetString(3)
require.NoError(t, err)
require.Equal(t, "123456", s)
s, err = message.GetString(4)
require.NoError(t, err)
require.Equal(t, "100", s)
})
t.Run("Do not pack fields that match the bitmap presence indicator", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("0100")
require.NoError(t, message.Field(65, "1"))
require.NoError(t, message.Field(130, "1")) // field of third bitmap
got, err := message.Pack()
want := "01008000000000000000800000000000000040000000000000001"
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, want, string(got))
message = NewMessage(spec)
err = message.Unpack([]byte(want))
require.NoError(t, err)
s, err := message.GetMTI()
require.NoError(t, err)
require.Equal(t, "0100", s)
s, err = message.GetString(65)
require.NoError(t, err)
require.Equal(t, "", s)
s, err = message.GetString(130)
require.NoError(t, err)
require.Equal(t, "1", s)
})
t.Run("Does not fail when packing and unpacking message with three bitmaps", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("0100")
require.NoError(t, message.Field(130, "1")) // field of third bitmap
got, err := message.Pack()
require.NoError(t, err)
require.NotNil(t, got)
want := "01008000000000000000800000000000000040000000000000001"
require.Equal(t, want, string(got))
message = NewMessage(spec)
err = message.Unpack([]byte(want))
require.NoError(t, err)
s, err := message.GetMTI()
require.NoError(t, err)
require.Equal(t, "0100", s)
s, err = message.GetString(130)
require.NoError(t, err)
require.Equal(t, "1", s)
})
t.Run("Test unpacking with typed fields", func(t *testing.T) {
type TestISOF3Data struct {
F1 *field.String
F2 *field.String
F3 *field.String
}
type ISO87Data struct {
F0 *field.String
F2 *field.String
F3 *TestISOF3Data
F4 *field.String
}
message := NewMessage(spec)
rawMsg := []byte("01007000000000000000164242424242424242123456000000000100")
err := message.Unpack([]byte(rawMsg))
require.NoError(t, err)
s, err := message.GetString(2)
require.NoError(t, err)
require.Equal(t, "4242424242424242", s)
s, err = message.GetString(3)
require.NoError(t, err)
require.Equal(t, "123456", s)
s, err = message.GetString(4)
require.NoError(t, err)
require.Equal(t, "100", s)
data := &ISO87Data{}
require.NoError(t, message.Unmarshal(data))
require.Equal(t, "0100", data.F0.Value())
require.Equal(t, "4242424242424242", data.F2.Value())
require.Equal(t, "12", data.F3.F1.Value())
require.Equal(t, "34", data.F3.F2.Value())
require.Equal(t, "56", data.F3.F3.Value())
require.Equal(t, "100", data.F4.Value())
})
t.Run("Test unpacking message with fields that have native types", func(t *testing.T) {
type TestISOF3Data struct {
F1 *string
F2 string
F3 string
}
type ISO87Data struct {
F0 *string
F2 string
F3 *TestISOF3Data
F4 string
}
message := NewMessage(spec)
rawMsg := []byte("01007000000000000000164242424242424242123456000000000100")
err := message.Unpack([]byte(rawMsg))
require.NoError(t, err)
s, err := message.GetString(2)
require.NoError(t, err)
require.Equal(t, "4242424242424242", s)
s, err = message.GetString(3)
require.NoError(t, err)
require.Equal(t, "123456", s)
s, err = message.GetString(4)
require.NoError(t, err)
require.Equal(t, "100", s)
data := &ISO87Data{}
require.NoError(t, message.Unmarshal(data))
require.NotNil(t, data.F0)
require.Equal(t, "0100", *data.F0)
require.Equal(t, "4242424242424242", data.F2)
require.NotNil(t, data.F3.F1)
require.Equal(t, "12", *data.F3.F1)
require.Equal(t, "34", data.F3.F2)
require.Equal(t, "56", data.F3.F3)
require.Equal(t, "100", data.F4)
})
t.Run("Test packing with typed fields", func(t *testing.T) {
type TestISOF3Data struct {
F1 *field.String
F2 *field.String
F3 *field.String
}
type ISO87Data struct {
F0 *field.String
F2 *field.String
F3 *TestISOF3Data
F4 *field.String
}
message := NewMessage(spec)
err := message.Marshal(&ISO87Data{
F0: field.NewStringValue("0100"),
F2: field.NewStringValue("4242424242424242"),
F3: &TestISOF3Data{
F1: field.NewStringValue("12"),
F2: field.NewStringValue("34"),
F3: field.NewStringValue("56"),
},
F4: field.NewStringValue("100"),
})
require.NoError(t, err)
rawMsg, err := message.Pack()
require.NoError(t, err)
wantMsg := []byte("01007000000000000000164242424242424242123456000000000100")
require.Equal(t, wantMsg, rawMsg)
})
t.Run("Test packing message with fields that have native types", func(t *testing.T) {
type TestISOF3Data struct {
F1 string
F2 string
F3 string
}
type ISO87Data struct {
F0 *string
F2 string
F3 *TestISOF3Data
F4 string
}
messageCode := "0100"
message := NewMessage(spec)
err := message.Marshal(&ISO87Data{
F0: &messageCode,
F2: "4242424242424242",
F3: &TestISOF3Data{
F1: "12",
F2: "34",
F3: "56",
},
F4: "100",
})
require.NoError(t, err)
rawMsg, err := message.Pack()
require.NoError(t, err)
wantMsg := []byte("01007000000000000000164242424242424242123456000000000100")
require.Equal(t, wantMsg, rawMsg)
})
t.Run("Clone, set zero values", func(t *testing.T) {
type TestISOF3Data struct {
F1 *field.String
F2 *field.String
F3 *field.String
}
type ISO87Data struct {
F0 *field.String
F2 *field.String
F3 *TestISOF3Data
F4 *field.String
}
message := NewMessage(spec)
err := message.Marshal(&ISO87Data{
F0: field.NewStringValue("0100"),
F2: field.NewStringValue("4242424242424242"),
F3: &TestISOF3Data{
F1: field.NewStringValue("12"),
F2: field.NewStringValue("34"),
F3: field.NewStringValue("56"),
},
F4: field.NewStringValue("100"),
})
require.NoError(t, err)
// clone the message and reset some values
clone, err := message.Clone()
require.NoError(t, err)
// reset the values
// first, check that the fields are set
data := &ISO87Data{}
require.NoError(t, clone.Unmarshal(data))
require.Equal(t, "0100", data.F0.Value())
require.Equal(t, "4242424242424242", data.F2.Value())
require.Equal(t, "12", data.F3.F1.Value())
require.Equal(t, "34", data.F3.F2.Value())
require.Equal(t, "56", data.F3.F3.Value())
require.Equal(t, "100", data.F4.Value())
// reset the fields
err = clone.Marshal(&struct {
F2 *field.String `iso8583:",keepzero"`
F3 *struct {
F2 *field.String `iso8583:",keepzero"`
} `iso8583:",keepzero"`
}{})
require.NoError(t, err)
// check that the field values are set to zero values
data = &ISO87Data{}
require.NoError(t, clone.Unmarshal(data))
// check the zero values
require.Equal(t, "", data.F2.Value())
require.Equal(t, "", data.F3.F2.Value())
// check the reset fields in the message
require.Equal(t, "0100", data.F0.Value())
require.Equal(t, "12", data.F3.F1.Value())
require.Equal(t, "56", data.F3.F3.Value())
require.Equal(t, "100", data.F4.Value())
})
t.Run("Unset doesn't return error for fields that are not set", func(t *testing.T) {
message := NewMessage(spec)
err := message.UnsetFields("2", "3", "4")
require.NoError(t, err)
})
t.Run("Unset unsets fields", func(t *testing.T) {
type TestISOF3Data struct {
F1 *field.String
F2 *field.String
F3 *field.String
}
type ISO87Data struct {
F0 *field.String
F2 *field.String
F3 *TestISOF3Data
F4 *field.String
}
messageCode := "0100"
message := NewMessage(spec)
err := message.Marshal(&ISO87Data{
F0: field.NewStringValue(messageCode),
F2: field.NewStringValue("4242424242424242"),
F3: &TestISOF3Data{
F1: field.NewStringValue("12"),
F2: field.NewStringValue("34"),
F3: field.NewStringValue("56"),
},
F4: field.NewStringValue("100"),
})
require.NoError(t, err)
// unset fields
err = message.UnsetFields("2", "3.3")
require.NoError(t, err)
data := &ISO87Data{}
err = message.Unmarshal(data)
require.NoError(t, err)
require.Nil(t, data.F2)
require.Nil(t, data.F3.F3)
// unset field 3
err = message.UnsetFields("3")
require.NoError(t, err)
data = &ISO87Data{}
err = message.Unmarshal(data)
require.NoError(t, err)
require.Nil(t, data.F3)
// let's set the field 3.3 again
// only subfield 3 should be set in the field 3, the rest should be unset
err = message.Marshal(&ISO87Data{
F3: &TestISOF3Data{
F3: field.NewStringValue("56"),
},
})
require.NoError(t, err)
data = &ISO87Data{}
err = message.Unmarshal(data)
require.NoError(t, err)
require.Nil(t, data.F3.F1)
require.Nil(t, data.F3.F2)
require.Equal(t, "56", data.F3.F3.Value())
})
}
func TestPackUnpack(t *testing.T) {
spec := &MessageSpec{
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{
Description: "Bitmap",
Enc: encoding.Binary,
Pref: prefix.ASCII.Fixed,
}),
2: field.NewString(&field.Spec{
Length: 19,
Description: "Primary Account Number",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
3: field.NewComposite(&field.Spec{
Length: 6,
Description: "Processing Code",
Pref: prefix.ASCII.Fixed,
Tag: &field.TagSpec{
Sort: sort.StringsByInt,
},
Subfields: map[string]field.Field{
"1": field.NewString(&field.Spec{
Length: 2,
Description: "Transaction Type",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"2": field.NewString(&field.Spec{
Length: 2,
Description: "From Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"3": field.NewString(&field.Spec{
Length: 2,
Description: "To Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
},
}),
4: field.NewNumeric(&field.Spec{
Length: 12,
Description: "Field 4",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
7: field.NewNumeric(&field.Spec{
Length: 10,
Description: "Field 7",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
11: field.NewNumeric(&field.Spec{
Length: 6,
Description: "Field 11",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
12: field.NewNumeric(&field.Spec{
Length: 6,
Description: "Field 12",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
13: field.NewNumeric(&field.Spec{
Length: 4,
Description: "Field 13",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
14: field.NewNumeric(&field.Spec{
Length: 4,
Description: "Field 14",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
19: field.NewNumeric(&field.Spec{
Length: 3,
Description: "Field 19",
Enc: encoding.BCD,
Pref: prefix.BCD.Fixed,
}),
22: field.NewNumeric(&field.Spec{
Length: 3,
Description: "Field 22",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
25: field.NewNumeric(&field.Spec{
Length: 2,
Description: "Field 25",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
32: field.NewNumeric(&field.Spec{
Length: 11,
Description: "Field 32",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
35: field.NewString(&field.Spec{
Length: 37,
Description: "Field 35",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
37: field.NewString(&field.Spec{
Length: 12,
Description: "Field 37",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
39: field.NewString(&field.Spec{
Length: 2,
Description: "Field 39",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left('0'),
}),
41: field.NewString(&field.Spec{
Length: 8,
Description: "Field 41",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
42: field.NewString(&field.Spec{
Length: 15,
Description: "Field 42",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
43: field.NewString(&field.Spec{
Length: 40,
Description: "Field 43",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left(' '),
}),
49: field.NewNumeric(&field.Spec{
Length: 3,
Description: "Field 49",
Enc: encoding.LBCD,
Pref: prefix.BCD.Fixed,
}),
50: field.NewNumeric(&field.Spec{
Length: 3,
Description: "Field 50",
Enc: encoding.LBCD,
Pad: padding.Left('0'),
}),
// this one should be binary...
52: field.NewString(&field.Spec{
Length: 8,
Description: "Field 52",
Enc: encoding.Binary,
Pref: prefix.ASCII.Fixed,
}),
53: field.NewNumeric(&field.Spec{
Length: 16,
Description: "Field 53",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
Pad: padding.Left(' '),
}),
// TLV
55: field.NewComposite(&field.Spec{
Length: 999,
Description: "ICC Data – EMV Having Multiple Tags",
Pref: prefix.ASCII.LLL,
Tag: &field.TagSpec{
Enc: encoding.BerTLVTag,
Sort: sort.StringsByHex,
},
Subfields: map[string]field.Field{
"9A": field.NewString(&field.Spec{
Description: "Transaction Date",
Enc: encoding.Binary,
Pref: prefix.BerTLV,
}),
"9F02": field.NewString(&field.Spec{
Description: "Amount, Authorized (Numeric)",
Enc: encoding.Binary,
Pref: prefix.BerTLV,
}),
},
}),
120: field.NewString(&field.Spec{
Length: 999,
Description: "Field 120",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LLL,
}),
},
}
type TestISOF3Data struct {
F1 *field.String
F2 *field.String
F3 *field.String
}
type TestISOF55Data struct {
F9A *field.String
F9F02 *field.String
}
type TestISOData struct {
F2 *field.String
F3 *TestISOF3Data
F4 *field.Numeric
F7 *field.Numeric
F11 *field.Numeric
F12 *field.Numeric
F13 *field.Numeric
F14 *field.Numeric
F19 *field.Numeric
F22 *field.Numeric
F25 *field.Numeric
F32 *field.Numeric
F35 *field.String
F37 *field.String
F39 *field.String
F41 *field.String
F42 *field.String
F43 *field.String
F49 *field.Numeric
F50 *field.Numeric
F52 *field.String
F53 *field.Numeric
F55 *TestISOF55Data
F120 *field.String
}
t.Run("Pack data", func(t *testing.T) {
message := NewMessage(spec)
err := message.Marshal(&TestISOData{
F2: field.NewStringValue("4276555555555555"),
F3: &TestISOF3Data{
F1: field.NewStringValue("00"),
F2: field.NewStringValue("00"),
F3: field.NewStringValue("00"),
},
F4: field.NewNumericValue(77700),
F7: field.NewNumericValue(701111844),
F11: field.NewNumericValue(123),
F12: field.NewNumericValue(131844),
F13: field.NewNumericValue(701),
F14: field.NewNumericValue(1902),
F19: field.NewNumericValue(643),
F22: field.NewNumericValue(901),
F25: field.NewNumericValue(2),
F32: field.NewNumericValue(123456),
F35: field.NewStringValue("4276555555555555=12345678901234567890"),
F37: field.NewStringValue("987654321001"),
F41: field.NewStringValue("00000321"),
F42: field.NewStringValue("120000000000034"),
F43: field.NewStringValue("Test text"),
F49: field.NewNumericValue(643),
// F50 left nil to ensure that it has not been populated in the bitmap
F52: field.NewStringValue(string([]byte{1, 2, 3, 4, 5, 6, 7, 8})),
F53: field.NewNumericValue(1234000000000000),
F55: &TestISOF55Data{
F9A: field.NewStringValue("210720"),
F9F02: field.NewStringValue("000000000501"),
},
F120: field.NewStringValue("Another test text"),
})
require.NoError(t, err)
message.MTI("0100")
got, err := message.Pack()
want := []byte{0x30, 0x31, 0x30, 0x30, 0xf2, 0x3c, 0x24, 0x81, 0x28, 0xe0, 0x9a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x31, 0x36, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x37, 0x37, 0x30, 0x30, 0x30, 0x37, 0x30, 0x31, 0x31, 0x31, 0x31, 0x38, 0x34, 0x34, 0x30, 0x30, 0x30, 0x31, 0x32, 0x33, 0x31, 0x33, 0x31, 0x38, 0x34, 0x34, 0x30, 0x37, 0x30, 0x31, 0x31, 0x39, 0x30, 0x32, 0x6, 0x43, 0x39, 0x30, 0x31, 0x30, 0x32, 0x30, 0x36, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x33, 0x37, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x3d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x32, 0x31, 0x31, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x34, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x64, 0x30, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x31, 0x32, 0x33, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x33, 0x9a, 0x6, 0x32, 0x31, 0x30, 0x37, 0x32, 0x30, 0x9f, 0x2, 0xc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x35, 0x30, 0x31, 0x30, 0x31, 0x37, 0x41, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74}
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, want, got)
})
t.Run("Unpack data", func(t *testing.T) {
message := NewMessage(spec)
rawMsg := []byte{0x30, 0x31, 0x30, 0x30, 0xf2, 0x3c, 0x24, 0x81, 0x28, 0xe0, 0x9a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x31, 0x36, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x37, 0x37, 0x30, 0x30, 0x30, 0x37, 0x30, 0x31, 0x31, 0x31, 0x31, 0x38, 0x34, 0x34, 0x30, 0x30, 0x30, 0x31, 0x32, 0x33, 0x31, 0x33, 0x31, 0x38, 0x34, 0x34, 0x30, 0x37, 0x30, 0x31, 0x31, 0x39, 0x30, 0x32, 0x6, 0x43, 0x39, 0x30, 0x31, 0x30, 0x32, 0x30, 0x36, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x33, 0x37, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x3d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x32, 0x31, 0x31, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x34, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x64, 0x30, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x31, 0x32, 0x33, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x33, 0x9a, 0x6, 0x32, 0x31, 0x30, 0x37, 0x32, 0x30, 0x9f, 0x2, 0xc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x35, 0x30, 0x31, 0x30, 0x31, 0x37, 0x41, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74}
err := message.Unpack([]byte(rawMsg))
require.NoError(t, err)
s, err := message.GetString(2)
require.NoError(t, err)
require.Equal(t, "4276555555555555", s)
s, err = message.GetString(3)
require.NoError(t, err)
require.Equal(t, "000000", s)
s, err = message.GetString(4)
require.NoError(t, err)
require.Equal(t, "77700", s)
data := &TestISOData{}
require.NoError(t, message.Unmarshal(data))
assert.Equal(t, "4276555555555555", data.F2.Value())
assert.Equal(t, "00", data.F3.F1.Value())
assert.Equal(t, "00", data.F3.F2.Value())
assert.Equal(t, "00", data.F3.F3.Value())
assert.Equal(t, int64(77700), data.F4.Value())
assert.Equal(t, int64(701111844), data.F7.Value())
assert.Equal(t, int64(123), data.F11.Value())
assert.Equal(t, int64(131844), data.F12.Value())
assert.Equal(t, int64(701), data.F13.Value())
assert.Equal(t, int64(1902), data.F14.Value())
assert.Equal(t, int64(643), data.F19.Value())
assert.Equal(t, int64(901), data.F22.Value())
assert.Equal(t, int64(2), data.F25.Value())
assert.Equal(t, int64(123456), data.F32.Value())
assert.Equal(t, "4276555555555555=12345678901234567890", data.F35.Value())
assert.Equal(t, "987654321001", data.F37.Value())
assert.Equal(t, "00000321", data.F41.Value())
assert.Equal(t, "120000000000034", data.F42.Value())
assert.Equal(t, "Test text", data.F43.Value())
assert.Equal(t, int64(643), data.F49.Value())
assert.Nil(t, data.F50)
assert.Equal(t, string([]byte{1, 2, 3, 4, 5, 6, 7, 8}), data.F52.Value())
assert.Equal(t, int64(1234000000000000), data.F53.Value())
assert.Equal(t, "210720", data.F55.F9A.Value())
assert.Equal(t, "000000000501", data.F55.F9F02.Value())
assert.Equal(t, "Another test text", data.F120.Value())
})
t.Run("Pack invalid message returns error of *PackError type", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("1")
_, err := message.Pack()
require.Error(t, err)
var packErr *iso8583errors.PackError
require.ErrorAs(t, err, &packErr)
})
t.Run("Unpack nil", func(t *testing.T) {
message := NewMessage(spec)
err := message.Unpack(nil)
require.Error(t, err)
var unpackError *iso8583errors.UnpackError
require.ErrorAs(t, err, &unpackError)
})
t.Run("Unpack short mti", func(t *testing.T) {
message := NewMessage(spec)
rawMsg := []byte{0x30, 0x31}
err := message.Unpack([]byte(rawMsg))
require.Error(t, err)
var unpackError *iso8583errors.UnpackError
require.ErrorAs(t, err, &unpackError)
require.Equal(t, rawMsg, unpackError.RawMessage)
})
t.Run("Unpack data field error on field returns partial message", func(t *testing.T) {
message := NewMessage(spec)
// One byte has been removed from field 120 which will make it fail the length check
rawMsg := []byte{0x30, 0x31, 0x30, 0x30, 0xf2, 0x3c, 0x24, 0x81, 0x28, 0xe0, 0x9a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x31, 0x36, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x37, 0x37, 0x37, 0x30, 0x30, 0x30, 0x37, 0x30, 0x31, 0x31, 0x31, 0x31, 0x38, 0x34, 0x34, 0x30, 0x30, 0x30, 0x31, 0x32, 0x33, 0x31, 0x33, 0x31, 0x38, 0x34, 0x34, 0x30, 0x37, 0x30, 0x31, 0x31, 0x39, 0x30, 0x32, 0x6, 0x43, 0x39, 0x30, 0x31, 0x30, 0x32, 0x30, 0x36, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x33, 0x37, 0x34, 0x32, 0x37, 0x36, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x3d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x32, 0x31, 0x31, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x34, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74, 0x64, 0x30, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x31, 0x32, 0x33, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x33, 0x9a, 0x6, 0x32, 0x31, 0x30, 0x37, 0x32, 0x30, 0x9f, 0x2, 0xc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x35, 0x30, 0x31, 0x30, 0x31, 0x37, 0x41, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78}
err := message.Unpack([]byte(rawMsg))
require.Error(t, err)
var unpackError *iso8583errors.UnpackError
require.ErrorAs(t, err, &unpackError)
assert.Equal(t, unpackError.FieldID, "120")
s, err := message.GetString(2)
require.NoError(t, err)
require.Equal(t, "4276555555555555", s)
s, err = message.GetString(3)
require.NoError(t, err)
require.Equal(t, "000000", s)
s, err = message.GetString(4)
require.NoError(t, err)
require.Equal(t, "77700", s)
data := &TestISOData{}
require.NoError(t, message.Unmarshal(data))
assert.Equal(t, "4276555555555555", data.F2.Value())
assert.Equal(t, "00", data.F3.F1.Value())
assert.Equal(t, "00", data.F3.F2.Value())
assert.Equal(t, "00", data.F3.F3.Value())
assert.Equal(t, int64(77700), data.F4.Value())
assert.Equal(t, int64(701111844), data.F7.Value())
assert.Equal(t, int64(123), data.F11.Value())
assert.Equal(t, int64(131844), data.F12.Value())
assert.Equal(t, int64(701), data.F13.Value())
assert.Equal(t, int64(1902), data.F14.Value())
assert.Equal(t, int64(643), data.F19.Value())
assert.Equal(t, int64(901), data.F22.Value())
assert.Equal(t, int64(2), data.F25.Value())
assert.Equal(t, int64(123456), data.F32.Value())
assert.Equal(t, "4276555555555555=12345678901234567890", data.F35.Value())
assert.Equal(t, "987654321001", data.F37.Value())
assert.Equal(t, "00000321", data.F41.Value())
assert.Equal(t, "120000000000034", data.F42.Value())
assert.Equal(t, "Test text", data.F43.Value())
assert.Equal(t, int64(643), data.F49.Value())
assert.Nil(t, data.F50)
assert.Equal(t, string([]byte{1, 2, 3, 4, 5, 6, 7, 8}), data.F52.Value())
assert.Equal(t, int64(1234000000000000), data.F53.Value())
assert.Equal(t, "210720", data.F55.F9A.Value())
assert.Equal(t, "000000000501", data.F55.F9F02.Value())
assert.Empty(t, data.F120)
})
t.Run("Unpack data field error on subfield returns both numbers and partial message", func(t *testing.T) {
type TestISOF3MixedData struct {
F1 *field.String
F2 *field.Numeric
F3 *field.String
}
type TestISOShortData struct {
F2 *field.String
F3 *TestISOF3MixedData
}
shortSpec := &MessageSpec{
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{
Description: "Bitmap",
Enc: encoding.Binary,
Pref: prefix.ASCII.Fixed,
}),
2: field.NewString(&field.Spec{
Length: 19,
Description: "Primary Account Number",
Enc: encoding.ASCII,
Pref: prefix.ASCII.LL,
}),
3: field.NewComposite(&field.Spec{
Length: 6,
Description: "Processing Code",
Pref: prefix.ASCII.Fixed,
Tag: &field.TagSpec{
Sort: sort.StringsByInt,
},
Subfields: map[string]field.Field{
"1": field.NewString(&field.Spec{
Length: 2,
Description: "Transaction Type",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,
}),
"2": field.NewNumeric(&field.Spec{
Length: 2,
Description: "From Account",
Enc: encoding.ASCII,
Pref: prefix.ASCII.Fixed,