Refactored IEventListener and IEventSink to handle garbage data according to configur...
[lwes-dotnet/github-mirror.git] / Org.Lwes.Tests / SerializerTests.cs
blobc3995e6f3d0b897c88122cc92c023b5267a6e92e
1 namespace Org.Lwes.Tests
3 using System;
4 using System.Collections.Generic;
5 using System.Net;
6 using System.Text;
8 using Microsoft.VisualStudio.TestTools.UnitTesting;
10 using Moq;
12 using Org.Lwes.DB;
14 [TestClass]
15 public class SerializerTests
17 #region Fields
19 Random _rand = new Random(Environment.TickCount);
21 #endregion Fields
23 #region Methods
25 [TestMethod]
26 public void RoundTripSerialize_Boolean()
28 var values = new bool[]
29 { (_rand.Next() % 2 == 0)
30 , (_rand.Next() % 2 == 0)
31 , (_rand.Next() % 2 == 0)
32 , (_rand.Next() % 2 == 0)
33 , (_rand.Next() % 2 == 0)
34 , (_rand.Next() % 2 == 0)
35 , (_rand.Next() % 2 == 0)
38 byte[] data = new byte[values.Length * sizeof(UInt64)];
39 int cursor = 0;
41 // Serialize all of the values...
42 for (int i = 0; i < values.Length; i++)
44 // Write the value and assert the length written...
45 Assert.AreEqual(sizeof(byte), LwesSerializer.Write(data, ref cursor, values[i]));
46 // Verify that the cursor moved appropriately...
47 Assert.AreEqual(i + 1, cursor);
50 // Reset the cursor
51 cursor = 0;
52 // Deserialize all of the values...
53 for (int i = 0; i < values.Length; i++)
55 Assert.AreEqual(values[i], LwesSerializer.ReadBoolean(data, ref cursor));
56 Assert.AreEqual(i + 1, cursor);
60 [TestMethod]
61 public void RoundTripSerialize_Byte()
63 var values = new byte[]
64 { Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
65 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
66 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
67 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
68 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
69 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
70 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
71 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
72 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
73 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
74 , Convert.ToByte(_rand.Next(byte.MinValue, byte.MaxValue))
77 byte[] data = new byte[values.Length * sizeof(UInt64)];
78 int cursor = 0;
80 // Serialize all of the values...
81 for (int i = 0; i < values.Length; i++)
83 // Write the value and assert the length written...
84 Assert.AreEqual(sizeof(byte), LwesSerializer.Write(data, ref cursor, values[i]));
85 // Verify that the cursor moved appropriately...
86 Assert.AreEqual(i + sizeof(byte), cursor);
89 // Reset the cursor
90 cursor = 0;
91 // Deserialize all of the values...
92 for (int i = 0; i < values.Length; i++)
94 Assert.AreEqual(values[i], LwesSerializer.ReadByte(data, ref cursor));
95 Assert.AreEqual(i + sizeof(byte), cursor);
99 [TestMethod]
100 [ExpectedException(typeof(BadLwesDataException))]
101 public void RoundTripSerialize_DeserializeGarbageData()
103 byte[] bytes = new byte[400];
104 Random _rand = new Random(Environment.TickCount);
105 _rand.NextBytes(bytes);
107 var mock = new Mock<IEventTemplateDB>();
108 Event dummy;
109 mock.Setup(db => db.TryCreateEvent(It.IsAny<string>(), out dummy, It.IsAny<bool>(), It.IsAny<SupportedEncoding>()))
110 .Returns(false);
112 LwesSerializer.Deserialize(bytes, 0, bytes.Length, mock.Object);
115 [TestMethod]
116 public void RoundTripSerialize_Event()
118 var e = new
120 EventName = "UserLogin",
121 Attributes = new
123 UserName = new { Name = "username", Token = TypeToken.STRING, Value = "bob" },
124 Password = new { Name = "password", Token = TypeToken.UINT64, Value = 0xfeedabbadeadbeefUL },
125 ClientIP = new { Name = "clientIP", Token = TypeToken.IP_ADDR, Value = IPAddress.Parse("127.0.0.1") },
126 Successful = new { Name = "successful", Token = TypeToken.BOOLEAN, Value = false }
130 // Create the event...
131 Event ev = new Event(e.EventName)
132 .SetValue(e.Attributes.UserName.Name, e.Attributes.UserName.Value)
133 .SetValue(e.Attributes.Password.Name, e.Attributes.Password.Value)
134 .SetValue(e.Attributes.ClientIP.Name, e.Attributes.ClientIP.Value)
135 .SetValue(e.Attributes.Successful.Name, e.Attributes.Successful.Value);
137 // Ensure the token types match...
138 Assert.AreEqual(e.Attributes.UserName.Token, ev[e.Attributes.UserName.Name].TypeToken);
139 Assert.AreEqual(e.Attributes.Password.Token, ev[e.Attributes.Password.Name].TypeToken);
140 Assert.AreEqual(e.Attributes.ClientIP.Token, ev[e.Attributes.ClientIP.Name].TypeToken);
141 Assert.AreEqual(e.Attributes.Successful.Token, ev[e.Attributes.Successful.Name].TypeToken);
143 // Ensure the values match...
144 Assert.AreEqual(e.Attributes.UserName.Value, ev[e.Attributes.UserName.Name].GetValue<string>());
145 Assert.AreEqual(e.Attributes.Password.Value, ev[e.Attributes.Password.Name].GetValue<UInt64>());
146 Assert.AreEqual(e.Attributes.ClientIP.Value, ev[e.Attributes.ClientIP.Name].GetValue<IPAddress>());
147 Assert.AreEqual(e.Attributes.Successful.Value, ev[e.Attributes.Successful.Name].GetValue<bool>());
149 // Serialization requires an IEventTemplateDB, we're gonna mock it here...
150 var mock = new Mock<IEventTemplateDB>();
151 Event dummy;
152 mock.Setup(db => db.TryCreateEvent(It.IsAny<string>(), out dummy, It.IsAny<bool>(), It.IsAny<SupportedEncoding>()))
153 .Returns(false);
155 // Perform a roundtrip serialization...
156 byte[] data = LwesSerializer.Serialize(ev);
157 Event ev2 = LwesSerializer.Deserialize(data, 0, data.Length, mock.Object);
159 // Ensure the token types match on the deserialized object...
160 Assert.AreEqual(e.Attributes.UserName.Token, ev2[e.Attributes.UserName.Name].TypeToken);
161 Assert.AreEqual(e.Attributes.Password.Token, ev2[e.Attributes.Password.Name].TypeToken);
162 Assert.AreEqual(e.Attributes.ClientIP.Token, ev2[e.Attributes.ClientIP.Name].TypeToken);
163 Assert.AreEqual(e.Attributes.Successful.Token, ev2[e.Attributes.Successful.Name].TypeToken);
165 // Ensure the values match on the deserialized object...
166 Assert.AreEqual(e.Attributes.UserName.Value, ev2[e.Attributes.UserName.Name].GetValue<string>());
167 Assert.AreEqual(e.Attributes.Password.Value, ev2[e.Attributes.Password.Name].GetValue<UInt64>());
168 Assert.AreEqual(e.Attributes.ClientIP.Value, ev2[e.Attributes.ClientIP.Name].GetValue<IPAddress>());
169 Assert.AreEqual(e.Attributes.Successful.Value, ev2[e.Attributes.Successful.Name].GetValue<bool>());
172 [TestMethod]
173 public void RoundTripSerialize_Int16()
175 var values = new Int16[]
176 { Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
177 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
178 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
179 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
180 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
181 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
182 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
183 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
184 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
185 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
186 , Convert.ToInt16(_rand.Next(Int16.MinValue, Int16.MaxValue))
189 byte[] data = new byte[values.Length * sizeof(UInt64)];
190 int cursor = 0;
192 // Serialize all of the values...
193 for (int i = 0; i < values.Length; i++)
195 // Write the value and assert the length written...
196 Assert.AreEqual(sizeof(Int16), LwesSerializer.Write(data, ref cursor, values[i]));
197 // Verify that the cursor moved appropriately...
198 Assert.AreEqual((i + 1) * sizeof(Int16), cursor);
201 // Reset the cursor
202 cursor = 0;
203 // Deserialize all of the values...
204 for (int i = 0; i < values.Length; i++)
206 // Read the and assert the value...
207 Assert.AreEqual(values[i], LwesSerializer.ReadInt16(data, ref cursor));
208 // Verify that the cursor moved appropriately...
209 Assert.AreEqual((i + 1) * sizeof(Int16), cursor);
213 [TestMethod]
214 public void RoundTripSerialize_Int32()
216 var values = new Int32[]
217 { Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
218 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
219 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
220 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
221 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
222 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
223 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
224 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
225 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
226 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
227 , Convert.ToInt32(_rand.Next(Int32.MinValue, Int32.MaxValue))
230 byte[] data = new byte[values.Length * sizeof(UInt64)];
231 int cursor = 0;
233 // Serialize all of the values...
234 for (int i = 0; i < values.Length; i++)
236 // Write the value and assert the length written...
237 Assert.AreEqual(sizeof(Int32), LwesSerializer.Write(data, ref cursor, values[i]));
238 // Verify that the cursor moved appropriately...
239 Assert.AreEqual((i + 1) * sizeof(Int32), cursor);
242 // Reset the cursor
243 cursor = 0;
244 // Deserialize all of the values...
245 for (int i = 0; i < values.Length; i++)
247 // Read the and assert the value...
248 Assert.AreEqual(values[i], LwesSerializer.ReadInt32(data, ref cursor));
249 // Verify that the cursor moved appropriately...
250 Assert.AreEqual((i + 1) * sizeof(Int32), cursor);
254 [TestMethod]
255 public void RoundTripSerialize_Int64()
257 var values = new Int64[]
258 { Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
259 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
260 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
261 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
262 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
263 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
264 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
265 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
266 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
267 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
268 , Convert.ToInt64(_rand.Next(Int32.MinValue, Int32.MaxValue))
271 byte[] data = new byte[values.Length * sizeof(UInt64)];
272 int cursor = 0;
274 // Serialize all of the values...
275 for (int i = 0; i < values.Length; i++)
277 // Write the value and assert the length written...
278 Assert.AreEqual(sizeof(Int64), LwesSerializer.Write(data, ref cursor, values[i]));
279 // Verify that the cursor moved appropriately...
280 Assert.AreEqual((i + 1) * sizeof(Int64), cursor);
283 // Reset the cursor
284 cursor = 0;
285 // Deserialize all of the values...
286 for (int i = 0; i < values.Length; i++)
288 // Read the and assert the value...
289 Assert.AreEqual(values[i], LwesSerializer.ReadInt64(data, ref cursor));
290 // Verify that the cursor moved appropriately...
291 Assert.AreEqual((i + 1) * sizeof(Int64), cursor);
295 [TestMethod]
296 public void RoundTripSerialize_String_ISO_8859_1()
298 var values = new string[]
299 { EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.ISO_8859_1)
300 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.ISO_8859_1)
301 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.ISO_8859_1)
302 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.ISO_8859_1)
305 byte[] data = new byte[values.Length * 400 * sizeof(UInt64)];
306 int cursor = 0, accumulatedSize = 0, encodedSize;
308 Queue<int> encodedSizes = new Queue<int>();
309 // Serialize all of the values...
310 for (int i = 0; i < values.Length; i++)
312 encodedSize = Constants.ISO8859_1Encoding.GetByteCount(values[i]) + sizeof(UInt16);
313 accumulatedSize += encodedSize;
314 // Write the value and assert the length written...
315 Assert.AreEqual(encodedSize, LwesSerializer.Write(data, ref cursor, values[i], Constants.ISO8859_1Encoding.GetEncoder()));
316 // Verify that the cursor moved appropriately...
317 Assert.AreEqual(accumulatedSize, cursor);
318 encodedSizes.Enqueue(accumulatedSize);
321 // Reset the cursor
322 cursor = 0;
323 // Deserialize all of the values...
324 for (int i = 0; i < values.Length; i++)
326 // Read the and assert the value...
327 Assert.AreEqual(values[i], LwesSerializer.ReadString(data, ref cursor, Constants.ISO8859_1Encoding.GetDecoder()));
328 // Verify that the cursor moved appropriately...
329 Assert.AreEqual(encodedSizes.Dequeue(), cursor);
333 [TestMethod]
334 public void RoundTripSerialize_String_UTF_8()
336 var values = new string[]
337 { EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.UTF_8)
338 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.UTF_8)
339 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.UTF_8)
340 , EventUtils.GenerateRandomString(_rand.Next(1, 400), SupportedEncoding.UTF_8)
343 byte[] data = new byte[values.Length * 400 * sizeof(UInt64)];
344 int cursor = 0, accumulatedSize = 0, encodedSize;
346 Queue<int> encodedSizes = new Queue<int>();
347 // Serialize all of the values...
348 for (int i = 0; i < values.Length; i++)
350 encodedSize = Constants.DefaultEncoding.GetByteCount(values[i]) + sizeof(UInt16);
351 accumulatedSize += encodedSize;
352 // Write the value and assert the length written...
353 Assert.AreEqual(encodedSize, LwesSerializer.Write(data, ref cursor, values[i], Constants.DefaultEncoding.GetEncoder()));
354 // Verify that the cursor moved appropriately...
355 Assert.AreEqual(accumulatedSize, cursor);
356 encodedSizes.Enqueue(accumulatedSize);
359 // Reset the cursor
360 cursor = 0;
361 // Deserialize all of the values...
362 for (int i = 0; i < values.Length; i++)
364 // Read the and assert the value...
365 Assert.AreEqual(values[i], LwesSerializer.ReadString(data, ref cursor, Constants.DefaultEncoding.GetDecoder()));
366 // Verify that the cursor moved appropriately...
367 Assert.AreEqual(encodedSizes.Dequeue(), cursor);
371 [TestMethod]
372 public void RoundTripSerialize_UInt16()
374 var values = new UInt16[]
375 { Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
376 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
377 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
378 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
379 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
380 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
381 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
382 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
383 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
384 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
385 , Convert.ToUInt16(_rand.Next(UInt16.MinValue, UInt16.MaxValue))
388 byte[] data = new byte[values.Length * sizeof(UInt64)];
389 int cursor = 0;
391 // Serialize all of the values...
392 for (int i = 0; i < values.Length; i++)
394 // Write the value and assert the length written...
395 Assert.AreEqual(sizeof(UInt16), LwesSerializer.Write(data, ref cursor, values[i]));
396 // Verify that the cursor moved appropriately...
397 Assert.AreEqual((i + 1) * sizeof(UInt16), cursor);
400 // Reset the cursor
401 cursor = 0;
402 // Deserialize all of the values...
403 for (int i = 0; i < values.Length; i++)
405 // Read the and assert the value...
406 Assert.AreEqual(values[i], LwesSerializer.ReadUInt16(data, ref cursor));
407 // Verify that the cursor moved appropriately...
408 Assert.AreEqual((i + 1) * sizeof(UInt16), cursor);
412 [TestMethod]
413 public void RoundTripSerialize_UInt32()
415 var values = new UInt32[]
416 { Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
417 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
418 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
419 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
420 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
421 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
422 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
423 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
424 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
425 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
426 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
427 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
428 , Convert.ToUInt32(_rand.Next(0, Int32.MaxValue))
431 byte[] data = new byte[values.Length * sizeof(UInt64)];
432 int cursor = 0;
434 // Serialize all of the values...
435 for (int i = 0; i < values.Length; i++)
437 // Write the value and assert the length written...
438 Assert.AreEqual(sizeof(UInt32), LwesSerializer.Write(data, ref cursor, values[i]));
439 // Verify that the cursor moved appropriately...
440 Assert.AreEqual((i + 1) * sizeof(UInt32), cursor);
443 // Reset the cursor
444 cursor = 0;
445 // Deserialize all of the values...
446 for (int i = 0; i < values.Length; i++)
448 // Read the and assert the value...
449 Assert.AreEqual(values[i], LwesSerializer.ReadUInt32(data, ref cursor));
450 // Verify that the cursor moved appropriately...
451 Assert.AreEqual((i + 1) * sizeof(UInt32), cursor);
455 [TestMethod]
456 public void RoundTripSerialize_UInt64()
458 var values = new UInt64[]
459 { Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
460 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
461 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
462 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
463 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
464 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
465 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
466 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
467 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
468 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
469 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
470 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
471 , Convert.ToUInt64(_rand.Next(0, Int32.MaxValue))
474 byte[] data = new byte[values.Length * sizeof(UInt64)];
475 int cursor = 0;
477 // Serialize all of the values...
478 for (int i = 0; i < values.Length; i++)
480 // Write the value and assert the length written...
481 Assert.AreEqual(sizeof(UInt64), LwesSerializer.Write(data, ref cursor, values[i]));
482 // Verify that the cursor moved appropriately...
483 Assert.AreEqual((i + 1) * sizeof(UInt64), cursor);
486 // Reset the cursor
487 cursor = 0;
488 // Deserialize all of the values...
489 for (int i = 0; i < values.Length; i++)
491 // Read the and assert the value...
492 Assert.AreEqual(values[i], LwesSerializer.ReadUInt64(data, ref cursor));
493 // Verify that the cursor moved appropriately...
494 Assert.AreEqual((i + 1) * sizeof(UInt64), cursor);
498 #endregion Methods