forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_test.go
541 lines (464 loc) · 17.2 KB
/
validation_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
package quickfix
import (
"testing"
"time"
"github.com/quickfixgo/quickfix/datadictionary"
)
type validateTest struct {
TestName string
Validator validator
MessageBytes []byte
ExpectedRejectReason int
ExpectedRefTagID *Tag
DoNotExpectReject bool
}
func TestValidate(t *testing.T) {
var tests = []validateTest{
tcInvalidTagNumberHeader(),
tcInvalidTagNumberBody(),
tcInvalidTagNumberTrailer(),
tcTagSpecifiedWithoutAValue(),
tcInvalidMsgType(),
tcValueIsIncorrect(),
tcIncorrectDataFormatForValue(),
tcTagSpecifiedOutOfRequiredOrderHeader(),
tcTagSpecifiedOutOfRequiredOrderTrailer(),
tcTagSpecifiedOutOfRequiredOrderDisabledHeader(),
tcTagSpecifiedOutOfRequiredOrderDisabledTrailer(),
tcTagAppearsMoreThanOnce(),
tcFloatValidation(),
tcTagNotDefinedForMessage(),
tcTagIsDefinedForMessage(),
tcFieldNotFoundBody(),
tcFieldNotFoundHeader(),
}
for _, test := range tests {
msg, _ := ParseMessage(test.MessageBytes)
reject := test.Validator.Validate(msg)
switch {
case reject == nil && test.DoNotExpectReject:
continue
case reject != nil && test.DoNotExpectReject:
t.Errorf("%v: Unexpected reject: %v", test.TestName, reject)
continue
case reject == nil:
t.Errorf("%v: Expected reject", test.TestName)
continue
}
if reject.RejectReason() != test.ExpectedRejectReason {
t.Errorf("%v: Expected reason %v got %v", test.TestName, test.ExpectedRejectReason, reject.RejectReason())
}
switch {
case reject.RefTagID() == nil && test.ExpectedRefTagID == nil:
//ok, expected and actual ref tag not set
case reject.RefTagID() != nil && test.ExpectedRefTagID == nil:
t.Errorf("%v: Unexpected RefTag '%v'", test.TestName, *reject.RefTagID())
case reject.RefTagID() == nil && test.ExpectedRefTagID != nil:
t.Errorf("%v: Expected RefTag '%v'", test.TestName, *test.ExpectedRefTagID)
case *reject.RefTagID() == *test.ExpectedRefTagID:
//ok, tags equal
default:
t.Errorf("%v: Expected RefTag '%v' got '%v'", test.TestName, *test.ExpectedRefTagID, *reject.RefTagID())
}
}
}
func createFIX40NewOrderSingle() Message {
msg := NewMessage()
msg.Header.SetField(tagMsgType, FIXString("D"))
msg.Header.SetField(tagBeginString, FIXString("FIX.4.0"))
msg.Header.SetField(tagBodyLength, FIXString("0"))
msg.Header.SetField(tagSenderCompID, FIXString("0"))
msg.Header.SetField(tagTargetCompID, FIXString("0"))
msg.Header.SetField(tagMsgSeqNum, FIXString("0"))
msg.Header.SetField(tagSendingTime, FIXUTCTimestamp{Time: time.Now()})
msg.Body.SetField(Tag(11), FIXString("A"))
msg.Body.SetField(Tag(21), FIXString("1"))
msg.Body.SetField(Tag(55), FIXString("A"))
msg.Body.SetField(Tag(54), FIXString("1"))
msg.Body.SetField(Tag(40), FIXString("1"))
msg.Body.SetField(Tag(38), FIXInt(5))
msg.Body.SetField(Tag(100), FIXString("0"))
msg.Trailer.SetField(tagCheckSum, FIXString("000"))
return msg
}
func createFIX43NewOrderSingle() Message {
msg := NewMessage()
msg.Header.SetField(tagMsgType, FIXString("D"))
msg.Header.SetField(tagBeginString, FIXString("FIX.4.3"))
msg.Header.SetField(tagBodyLength, FIXString("0"))
msg.Header.SetField(tagSenderCompID, FIXString("0"))
msg.Header.SetField(tagTargetCompID, FIXString("0"))
msg.Header.SetField(tagMsgSeqNum, FIXString("0"))
msg.Header.SetField(tagSendingTime, FIXUTCTimestamp{Time: time.Now()})
msg.Body.SetField(Tag(11), FIXString("A"))
msg.Body.SetField(Tag(21), FIXString("1"))
msg.Body.SetField(Tag(55), FIXString("A"))
msg.Body.SetField(Tag(54), FIXString("1"))
msg.Body.SetField(Tag(38), FIXInt(5))
msg.Body.SetField(Tag(40), FIXString("1"))
msg.Body.SetField(Tag(60), FIXUTCTimestamp{Time: time.Now()})
msg.Trailer.SetField(tagCheckSum, FIXString("000"))
return msg
}
func tcInvalidTagNumberHeader() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidHeaderFieldMessage := createFIX40NewOrderSingle()
tag := Tag(9999)
invalidHeaderFieldMessage.Header.SetField(tag, FIXString("hello"))
msgBytes, _ := invalidHeaderFieldMessage.Build()
return validateTest{
TestName: "Invalid Tag Number Header",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonInvalidTagNumber,
ExpectedRefTagID: &tag,
}
}
func tcInvalidTagNumberBody() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidBodyFieldMessage := createFIX40NewOrderSingle()
tag := Tag(9999)
invalidBodyFieldMessage.Body.SetField(tag, FIXString("hello"))
msgBytes, _ := invalidBodyFieldMessage.Build()
return validateTest{
TestName: "Invalid Tag Number Body",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonInvalidTagNumber,
ExpectedRefTagID: &tag,
}
}
func tcInvalidTagNumberTrailer() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidTrailerFieldMessage := createFIX40NewOrderSingle()
tag := Tag(9999)
invalidTrailerFieldMessage.Trailer.SetField(tag, FIXString("hello"))
msgBytes, _ := invalidTrailerFieldMessage.Build()
return validateTest{
TestName: "Invalid Tag Number Trailer",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonInvalidTagNumber,
ExpectedRefTagID: &tag,
}
}
func tcTagNotDefinedForMessage() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidMsg := createFIX40NewOrderSingle()
tag := Tag(41)
invalidMsg.Body.SetField(tag, FIXString("hello"))
msgBytes, _ := invalidMsg.Build()
return validateTest{
TestName: "Tag Not Defined For Message",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonTagNotDefinedForThisMessageType,
ExpectedRefTagID: &tag,
}
}
func tcTagIsDefinedForMessage() validateTest {
//compare to tcTagIsNotDefinedForMessage
dict, _ := datadictionary.Parse("spec/FIX43.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
validMsg := createFIX43NewOrderSingle()
msgBytes, _ := validMsg.Build()
return validateTest{
TestName: "TagIsDefinedForMessage",
Validator: validator,
MessageBytes: msgBytes,
DoNotExpectReject: true,
}
}
func tcFieldNotFoundBody() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidMsg1 := NewMessage()
invalidMsg1.Header.SetField(tagMsgType, FIXString("D")).
SetField(tagBeginString, FIXString("FIX.4.0")).
SetField(tagBodyLength, FIXString("0")).
SetField(tagSenderCompID, FIXString("0")).
SetField(tagTargetCompID, FIXString("0")).
SetField(tagMsgSeqNum, FIXString("0")).
SetField(tagSendingTime, FIXUTCTimestamp{Time: time.Now()})
invalidMsg1.Trailer.SetField(tagCheckSum, FIXString("000"))
invalidMsg1.Body.SetField(Tag(11), FIXString("A")).
SetField(Tag(21), FIXString("A")).
SetField(Tag(55), FIXString("A")).
SetField(Tag(54), FIXString("A")).
SetField(Tag(38), FIXString("A"))
tag := Tag(40)
//ord type is required
//invalidMsg1.Body.SetField(Tag(40), "A"))
msgBytes, _ := invalidMsg1.Build()
return validateTest{
TestName: "FieldNotFoundBody",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonRequiredTagMissing,
ExpectedRefTagID: &tag,
}
}
func tcFieldNotFoundHeader() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
invalidMsg2 := NewMessage()
invalidMsg2.Trailer.SetField(tagCheckSum, FIXString("000"))
invalidMsg2.Body.SetField(Tag(11), FIXString("A")).
SetField(Tag(21), FIXString("A")).
SetField(Tag(55), FIXString("A")).
SetField(Tag(54), FIXString("A")).
SetField(Tag(38), FIXString("A"))
invalidMsg2.Header.SetField(tagMsgType, FIXString("D")).
SetField(tagBeginString, FIXString("FIX.4.0")).
SetField(tagBodyLength, FIXString("0")).
SetField(tagSenderCompID, FIXString("0")).
SetField(tagTargetCompID, FIXString("0")).
SetField(tagMsgSeqNum, FIXString("0"))
//sending time is required
//invalidMsg2.Header.FieldMap.SetField(tag.SendingTime, "0"))
tag := tagSendingTime
msgBytes, _ := invalidMsg2.Build()
return validateTest{
TestName: "FieldNotFoundHeader",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonRequiredTagMissing,
ExpectedRefTagID: &tag,
}
}
func tcTagSpecifiedWithoutAValue() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
builder := createFIX40NewOrderSingle()
bogusTag := Tag(109)
builder.Body.SetField(bogusTag, FIXString(""))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "Tag SpecifiedWithoutAValue",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonTagSpecifiedWithoutAValue,
ExpectedRefTagID: &bogusTag,
}
}
func tcInvalidMsgType() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
builder := createFIX40NewOrderSingle()
builder.Header.SetField(tagMsgType, FIXString("z"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "Invalid MsgType",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonInvalidMsgType,
}
}
func tcValueIsIncorrect() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
tag := Tag(21)
builder := createFIX40NewOrderSingle()
builder.Body.SetField(tag, FIXString("4"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "ValueIsIncorrect",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonValueIsIncorrect,
ExpectedRefTagID: &tag,
}
}
func tcIncorrectDataFormatForValue() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
builder := createFIX40NewOrderSingle()
tag := Tag(38)
builder.Body.SetField(tag, FIXString("+200.00"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "IncorrectDataFormatForValue",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonIncorrectDataFormatForValue,
ExpectedRefTagID: &tag,
}
}
func tcTagSpecifiedOutOfRequiredOrderHeader() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
builder := createFIX40NewOrderSingle()
tag := tagOnBehalfOfCompID
//should be in header
builder.Body.SetField(tag, FIXString("CWB"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "Tag specified out of required order in Header",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonTagSpecifiedOutOfRequiredOrder,
ExpectedRefTagID: &tag,
}
}
func tcTagSpecifiedOutOfRequiredOrderTrailer() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
builder := createFIX40NewOrderSingle()
tag := tagSignature
//should be in trailer
builder.Body.SetField(tag, FIXString("SIG"))
msgBytes, _ := builder.Build()
refTag := Tag(100)
return validateTest{
TestName: "Tag specified out of required order in Trailer",
Validator: validator,
MessageBytes: msgBytes,
ExpectedRejectReason: rejectReasonTagSpecifiedOutOfRequiredOrder,
ExpectedRefTagID: &refTag,
}
}
func tcTagSpecifiedOutOfRequiredOrderDisabledHeader() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
validator.settings.CheckFieldsOutOfOrder = false
builder := createFIX40NewOrderSingle()
tag := tagOnBehalfOfCompID
//should be in header
builder.Body.SetField(tag, FIXString("CWB"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "Tag specified out of required order in Header - Disabled",
Validator: validator,
MessageBytes: msgBytes,
DoNotExpectReject: true,
}
}
func tcTagSpecifiedOutOfRequiredOrderDisabledTrailer() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
validator.settings.CheckFieldsOutOfOrder = false
builder := createFIX40NewOrderSingle()
tag := tagSignature
//should be in trailer
builder.Body.SetField(tag, FIXString("SIG"))
msgBytes, _ := builder.Build()
return validateTest{
TestName: "Tag specified out of required order in Trailer - Disabled",
Validator: validator,
MessageBytes: msgBytes,
DoNotExpectReject: true,
}
}
func tcTagAppearsMoreThanOnce() validateTest {
dict, _ := datadictionary.Parse("spec/FIX40.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
tag := Tag(40)
return validateTest{
TestName: "Tag appears more than once",
Validator: validator,
MessageBytes: []byte("8=FIX.4.09=10735=D34=249=TW52=20060102-15:04:0556=ISLD11=ID21=140=140=254=138=20055=INTC60=20060102-15:04:0510=234"),
ExpectedRejectReason: rejectReasonTagAppearsMoreThanOnce,
ExpectedRefTagID: &tag,
}
}
func tcFloatValidation() validateTest {
dict, _ := datadictionary.Parse("spec/FIX42.xml")
validator := &fixValidator{dict, defaultValidatorSettings}
tag := Tag(38)
return validateTest{
TestName: "FloatValidation",
Validator: validator,
MessageBytes: []byte("8=FIX.4.29=10635=D34=249=TW52=20140329-22:38:4556=ISLD11=ID21=140=154=138=+200.0055=INTC60=20140329-22:38:4510=178"),
ExpectedRejectReason: rejectReasonIncorrectDataFormatForValue,
ExpectedRefTagID: &tag,
}
}
func TestValidateVisitField(t *testing.T) {
fieldType0 := datadictionary.NewFieldType("myfield", 11, "STRING")
fieldDef0 := &datadictionary.FieldDef{FieldType: fieldType0}
fieldType1 := datadictionary.NewFieldType("myfield", 2, "STRING")
fieldDef1 := &datadictionary.FieldDef{FieldType: fieldType1, Fields: []*datadictionary.FieldDef{}}
fieldType2 := datadictionary.NewFieldType("myfield", 3, "STRING")
fieldDef2 := &datadictionary.FieldDef{FieldType: fieldType2, Fields: []*datadictionary.FieldDef{}}
groupFieldType := datadictionary.NewFieldType("mygroupfield", 1, "INT")
groupFieldDef := &datadictionary.FieldDef{FieldType: groupFieldType, Fields: []*datadictionary.FieldDef{fieldDef1, fieldDef2}}
var field TagValue
field.init(Tag(11), []byte("value"))
var repField1 TagValue
var repField2 TagValue
repField1.init(Tag(2), []byte("a"))
repField2.init(Tag(3), []byte("a"))
var groupID TagValue
groupID.init(Tag(1), []byte("1"))
var groupID2 TagValue
groupID2.init(Tag(1), []byte("2"))
var groupID3 TagValue
groupID3.init(Tag(1), []byte("3"))
var tests = []struct {
fieldDef *datadictionary.FieldDef
fields TagValues
expectedRemFields int
expectReject bool
expectedRejectReason int
}{
//non-repeating
{expectedRemFields: 0,
fieldDef: fieldDef0,
fields: TagValues{field}},
//single field group
{expectedRemFields: 0,
fieldDef: groupFieldDef,
fields: TagValues{groupID, repField1}},
//multiple field group
{expectedRemFields: 0,
fieldDef: groupFieldDef,
fields: TagValues{groupID, repField1, repField2}},
//test with trailing tag not in group
{expectedRemFields: 1,
fieldDef: groupFieldDef,
fields: TagValues{groupID, repField1, repField2, field}},
//repeats
{expectedRemFields: 1,
fieldDef: groupFieldDef,
fields: TagValues{groupID2, repField1, repField2, repField1, repField2, field}},
//REJECT: group size declared > actual group size
{expectReject: true,
fieldDef: groupFieldDef,
fields: TagValues{groupID3, repField1, repField2, repField1, repField2, field},
expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
},
{expectReject: true,
fieldDef: groupFieldDef,
fields: TagValues{groupID3, repField1, repField1, field},
expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
},
//REJECT: group size declared < actual group size
{expectReject: true,
fieldDef: groupFieldDef,
fields: TagValues{groupID, repField1, repField2, repField1, repField2, field},
expectedRejectReason: rejectReasonIncorrectNumInGroupCountForRepeatingGroup,
},
}
for _, test := range tests {
remFields, reject := validateVisitField(test.fieldDef, test.fields)
if test.expectReject {
if reject == nil {
t.Error("Expected Reject")
}
if reject.RejectReason() != test.expectedRejectReason {
t.Errorf("Expected reject reason %v got %v", test.expectedRejectReason, reject.RejectReason())
}
continue
}
if reject != nil {
t.Errorf("Unexpected reject: %v", reject)
}
if len(remFields) != test.expectedRemFields {
t.Errorf("Expected len %v got %v", test.expectedRemFields, len(remFields))
}
}
}