forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserTest.cs
executable file
·121 lines (94 loc) · 4.55 KB
/
ParserTest.cs
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
using NUnit.Framework;
using QuickFix;
using System;
namespace UnitTests
{
[TestFixture]
public class ParserTest
{
const string normalLength = "8=FIX.4.2\x01" + "9=12\x01" + "35=A\x01" + " 108=30\x01" + "10=31\x01";
const string badLength = "8=FIX.4.2\x01" + "9=A\x01" + "35=A\x01" + "108=30\x01" + "10=31\x01";
const string negativeLength = "8=FIX.4.2\x01" + "9=-1\x01" + "35=A\x01" + "108=30\x01" + "10=31\x01";
const string incomplete_1 = "8=FIX.4.2";
const string incomplete_2 = "8=FIX.4.2\x01" + "9=12";
[Test]
public void ExtractLength()
{
Parser parser = new Parser();
int len = 0;
int pos = 0;
Assert.True(parser.ExtractLength(out len, out pos, normalLength));
Assert.AreEqual(12, len);
Assert.AreEqual(15, pos);
pos = 0;
Assert.Throws<QuickFix.MessageParseError>(delegate { parser.ExtractLength(out len, out pos, badLength); });
Assert.AreEqual(0, pos);
Assert.Throws<QuickFix.MessageParseError>(delegate { parser.ExtractLength(out len, out pos, negativeLength); });
Assert.AreEqual(0, pos);
parser.ExtractLength(out len, out pos, incomplete_1);
parser.ExtractLength(out len, out pos, incomplete_2);
Assert.AreEqual(0, pos);
Assert.False(parser.ExtractLength(out len, out pos, ""));
}
[Test]
public void ReadCompleteFixMessages()
{
const string fixMsg1 = "8=FIX.4.2\x01" + "9=12\x01" + "35=A\x01" + "108=30\x01" + "10=31\x01";
const string fixMsg2 = "8=FIX.4.2\x01" + "9=17\x01" + "35=4\x01" + "36=88\x01" + "123=Y\x01" + "10=34\x01";
const string fixMsg3 = "8=FIX.4.2\x01" + "9=19\x01" + "35=A\x01" + "108=30\x01" + "9710=8\x01" + "10=31\x01";
Parser parser = new Parser();
parser.AddToStream(fixMsg1 + fixMsg2 + fixMsg3);
string readFixMsg1;
Assert.True(parser.ReadFixMessage(out readFixMsg1));
Assert.AreEqual(fixMsg1, readFixMsg1);
string readFixMsg2;
Assert.True(parser.ReadFixMessage(out readFixMsg2));
Assert.AreEqual(fixMsg2, readFixMsg2);
string readFixMsg3;
Assert.True(parser.ReadFixMessage(out readFixMsg3));
Assert.AreEqual(fixMsg3, readFixMsg3);
}
[Test]
public void ReadPartialFixMessage()
{
string partFixMsg1 = "8=FIX.4.2\x01" + "9=17\x01" + "35=4\x01" + "36=";
string partFixMsg2 = "88\x01" + "123=Y\x01" + "10=34\x01";
Parser parser = new Parser();
parser.AddToStream(partFixMsg1);
string readPartFixMsg;
Assert.False(parser.ReadFixMessage(out readPartFixMsg));
parser.AddToStream(partFixMsg2);
Assert.True(parser.ReadFixMessage(out readPartFixMsg));
Assert.AreEqual(partFixMsg1 + partFixMsg2, readPartFixMsg);
}
[Test]
public void ReadFixMessageWithBadLength()
{
string fixMsg = "8=TEST\x01" + "9=TEST\x01" + "35=TEST\x01" + "49=SS1\x01" + "56=RORE\x01" + "34=3\x01" + "52=20050222-16:45:53\x01" + "10=TEST\x01";
Parser parser = new Parser();
parser.AddToStream(fixMsg);
string readFixMsg;
Assert.Throws<QuickFix.MessageParseError>(delegate { parser.ReadFixMessage(out readFixMsg); });
// nothing thrown now because the previous call removes bad data from buffer:
Assert.DoesNotThrow(delegate { parser.ReadFixMessage(out readFixMsg); });
}
[Test]
public void ReadFixMessageWithNonAscii()
{
string[] fixMsgFields1 = { "8=FIX.4.4", "9=19", "35=B", "148=Ole!", "33=0", "10=0" };
string fixMsg1 = String.Join("\x01", fixMsgFields1) + "\x01";
Assert.AreEqual("é", "\x00E9");
Assert.AreEqual("é", "\xE9");
string[] fixMsgFields2 = { "8=FIX.4.4", "9=20", "35=B", "148=Olé!", "33=0", "10=0" };
string fixMsg2 = String.Join("\x01", fixMsgFields2) + "\x01";
Parser parser = new Parser();
parser.AddToStream(fixMsg1 + fixMsg2);
string readFixMsg1;
Assert.True(parser.ReadFixMessage(out readFixMsg1));
Assert.AreEqual(fixMsg1, readFixMsg1);
string readFixMsg2;
Assert.True(parser.ReadFixMessage(out readFixMsg2));
Assert.AreEqual(fixMsg2, readFixMsg2);
}
}
}