@@ -34,6 +34,10 @@ class TestSerialization : public TestBase {
34
34
void testSerializeWideString ();
35
35
void testSerializeLongString ();
36
36
void testSerializeJsonString ();
37
+ void testSerializeHex ();
38
+ void testDeSerializeString ();
39
+ void testDeSerializeWideString ();
40
+ void testDeSerializeLongString ();
37
41
38
42
std::string teststring2;
39
43
std::wstring teststring2_w;
@@ -47,9 +51,13 @@ void TestSerialization::runTests(IGameDef *gamedef)
47
51
buildTestStrings ();
48
52
49
53
TEST (testSerializeString);
54
+ TEST (testDeSerializeString);
50
55
TEST (testSerializeWideString);
56
+ TEST (testDeSerializeWideString);
51
57
TEST (testSerializeLongString);
58
+ TEST (testDeSerializeLongString);
52
59
TEST (testSerializeJsonString);
60
+ TEST (testSerializeHex);
53
61
}
54
62
55
63
// //////////////////////////////////////////////////////////////////////////////
@@ -80,20 +88,37 @@ void TestSerialization::buildTestStrings()
80
88
void TestSerialization::testSerializeString ()
81
89
{
82
90
// Test blank string
83
- UASSERT (serializeString (" Hello world! " ) == mkstr (" \0\14 Hello world! " ));
91
+ UASSERT (serializeString (" " ) == mkstr (" \0\0 " ));
84
92
85
93
// Test basic string
86
- UASSERT (serializeString (" " ) == mkstr (" \0\0 " ));
94
+ UASSERT (serializeString (" Hello world! " ) == mkstr (" \0\14 Hello world! " ));
87
95
88
96
// Test character range
89
97
UASSERT (serializeString (teststring2) == mkstr (" \1\0 " ) + teststring2);
98
+ }
90
99
100
+ void TestSerialization::testDeSerializeString ()
101
+ {
91
102
// Test deserialize
92
- std::istringstream is (serializeString (teststring2), std::ios::binary);
93
- UASSERT (deSerializeString (is) == teststring2);
94
- UASSERT (!is.eof ());
95
- is.get ();
96
- UASSERT (is.eof ());
103
+ {
104
+ std::istringstream is (serializeString (teststring2), std::ios::binary);
105
+ UASSERT (deSerializeString (is) == teststring2);
106
+ UASSERT (!is.eof ());
107
+ is.get ();
108
+ UASSERT (is.eof ());
109
+ }
110
+
111
+ // Test deserialize an incomplete length specifier
112
+ {
113
+ std::istringstream is (mkstr (" \x53 " ), std::ios::binary);
114
+ EXCEPTION_CHECK (SerializationError, deSerializeString (is));
115
+ }
116
+
117
+ // Test deserialize a string with incomplete data
118
+ {
119
+ std::istringstream is (mkstr (" \x00\x55 abcdefg" ), std::ios::binary);
120
+ EXCEPTION_CHECK (SerializationError, deSerializeString (is));
121
+ }
97
122
}
98
123
99
124
void TestSerialization::testSerializeWideString ()
@@ -108,13 +133,36 @@ void TestSerialization::testSerializeWideString()
108
133
// Test character range
109
134
UASSERT (serializeWideString (teststring2_w) ==
110
135
mkstr (" \1\0 " ) + teststring2_w_encoded);
136
+ }
111
137
138
+ void TestSerialization::testDeSerializeWideString ()
139
+ {
112
140
// Test deserialize
113
- std::istringstream is (serializeWideString (teststring2_w), std::ios::binary);
114
- UASSERT (deSerializeWideString (is) == teststring2_w);
115
- UASSERT (!is.eof ());
116
- is.get ();
117
- UASSERT (is.eof ());
141
+ {
142
+ std::istringstream is (serializeWideString (teststring2_w), std::ios::binary);
143
+ UASSERT (deSerializeWideString (is) == teststring2_w);
144
+ UASSERT (!is.eof ());
145
+ is.get ();
146
+ UASSERT (is.eof ());
147
+ }
148
+
149
+ // Test deserialize an incomplete length specifier
150
+ {
151
+ std::istringstream is (mkstr (" \x53 " ), std::ios::binary);
152
+ EXCEPTION_CHECK (SerializationError, deSerializeWideString (is));
153
+ }
154
+
155
+ // Test deserialize a string with an incomplete character
156
+ {
157
+ std::istringstream is (mkstr (" \x00\x07\0 a\0 b\0 c\0 d\0 e\0 f\0 " ), std::ios::binary);
158
+ EXCEPTION_CHECK (SerializationError, deSerializeWideString (is));
159
+ }
160
+
161
+ // Test deserialize a string with incomplete data
162
+ {
163
+ std::istringstream is (mkstr (" \x00\x08\0 a\0 b\0 c\0 d\0 e\0 f" ), std::ios::binary);
164
+ EXCEPTION_CHECK (SerializationError, deSerializeWideString (is));
165
+ }
118
166
}
119
167
120
168
void TestSerialization::testSerializeLongString ()
@@ -127,15 +175,39 @@ void TestSerialization::testSerializeLongString()
127
175
128
176
// Test character range
129
177
UASSERT (serializeLongString (teststring2) == mkstr (" \0\0\1\0 " ) + teststring2);
178
+ }
130
179
180
+ void TestSerialization::testDeSerializeLongString ()
181
+ {
131
182
// Test deserialize
132
- std::istringstream is (serializeLongString (teststring2), std::ios::binary);
133
- UASSERT (deSerializeLongString (is) == teststring2);
134
- UASSERT (!is.eof ());
135
- is.get ();
136
- UASSERT (is.eof ());
183
+ {
184
+ std::istringstream is (serializeLongString (teststring2), std::ios::binary);
185
+ UASSERT (deSerializeLongString (is) == teststring2);
186
+ UASSERT (!is.eof ());
187
+ is.get ();
188
+ UASSERT (is.eof ());
189
+ }
190
+
191
+ // Test deserialize an incomplete length specifier
192
+ {
193
+ std::istringstream is (mkstr (" \x53 " ), std::ios::binary);
194
+ EXCEPTION_CHECK (SerializationError, deSerializeLongString (is));
195
+ }
196
+
197
+ // Test deserialize a string with incomplete data
198
+ {
199
+ std::istringstream is (mkstr (" \x00\x00\x00\x05 abc" ), std::ios::binary);
200
+ EXCEPTION_CHECK (SerializationError, deSerializeLongString (is));
201
+ }
202
+
203
+ // Test deserialize a string with a length too large
204
+ {
205
+ std::istringstream is (mkstr (" \xFF\xFF\xFF\xFF blah" ), std::ios::binary);
206
+ EXCEPTION_CHECK (SerializationError, deSerializeLongString (is));
207
+ }
137
208
}
138
209
210
+
139
211
void TestSerialization::testSerializeJsonString ()
140
212
{
141
213
// Test blank string
@@ -180,3 +252,22 @@ void TestSerialization::testSerializeJsonString()
180
252
is.get ();
181
253
UASSERT (is.eof ());
182
254
}
255
+
256
+ void TestSerialization::testSerializeHex ()
257
+ {
258
+ // Test blank string
259
+ UASSERT (serializeHexString (" " ) == " " );
260
+ UASSERT (serializeHexString (" " , true ) == " " );
261
+
262
+ // Test basic string
263
+ UASSERT (serializeHexString (" Hello world!" ) ==
264
+ " 48656c6c6f20776f726c6421" );
265
+ UASSERT (serializeHexString (" Hello world!" , true ) ==
266
+ " 48 65 6c 6c 6f 20 77 6f 72 6c 64 21" );
267
+
268
+ // Test binary string
269
+ UASSERT (serializeHexString (mkstr (" \x00\x0a\xb0\x63\x1f\x00\xff " )) ==
270
+ " 000ab0631f00ff" );
271
+ UASSERT (serializeHexString (mkstr (" \x00\x0a\xb0\x63\x1f\x00\xff " ), true ) ==
272
+ " 00 0a b0 63 1f 00 ff" );
273
+ }
0 commit comments