@@ -19,110 +19,120 @@ namespace Exomia.Network.UnitTest
1919 public class SerializationTcpUnitTest1
2020 {
2121 [ TestMethod ]
22- public void SerializeTcpNoResponseTest ( )
22+ public unsafe void SerializeTcpNoResponseTest ( )
2323 {
2424 byte [ ] data = { 0b1000_0000 , 0b1000_0000 , 0b0000_0000 , 0b1000_0000 , 0b1000_0000 , 0b0000_0000 , 0b1000_0000 } ;
25-
26- Serialization . Serialization . SerializeTcp (
27- 1337u , data , 0 , data . Length , 0 , EncryptionMode . None , out byte [ ] send , out int size ) ;
28- Assert . AreEqual ( size , Constants . TCP_HEADER_SIZE + data . Length + 1 + 1 ) ;
29-
30- CircularBuffer cb = new CircularBuffer ( ) ;
31- cb . Write ( send , 0 , size ) ;
32-
33- Assert . IsTrue (
34- cb . PeekHeader ( 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
35-
36- Assert . AreEqual ( ( byte ) 0 , packetHeader ) ;
37- Assert . AreEqual ( 1337u , commandID ) ;
38- Assert . AreEqual ( data . Length + 1 + 1 , dataLength ) ;
39-
40- Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
41- Assert . AreEqual ( ( byte ) 0 , b ) ;
42-
43- cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
44-
45- byte [ ] deserializeBuffer = new byte [ 128 ] ;
46- /*int checksum2 = Serialization.Serialization.Deserialize(
47- send, 0, dataLength - 1, deserializeBuffer, out int bufferLength);
48-
49- Assert.AreEqual(checksum, checksum2);
50-
51- Assert.AreEqual(data.Length, bufferLength);
52- Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
25+ fixed ( byte * src = data )
26+ {
27+ Serialization . Serialization . SerializeTcp (
28+ 1337u , src , data . Length , 0 , EncryptionMode . None , out byte [ ] send , out int size ) ;
29+ Assert . AreEqual ( size , Constants . TCP_HEADER_SIZE + data . Length + 1 + 1 ) ;
30+
31+ CircularBuffer cb = new CircularBuffer ( ) ;
32+ cb . Write ( send , 0 , size ) ;
33+
34+ Assert . IsTrue (
35+ cb . PeekHeader (
36+ 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
37+
38+ Assert . AreEqual ( ( byte ) 0 , packetHeader ) ;
39+ Assert . AreEqual ( 1337u , commandID ) ;
40+ Assert . AreEqual ( data . Length + 1 + 1 , dataLength ) ;
41+
42+ Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
43+ Assert . AreEqual ( ( byte ) 0 , b ) ;
44+
45+ cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
46+
47+ byte [ ] deserializeBuffer = new byte [ 128 ] ;
48+ /*int checksum2 = Serialization.Serialization.Deserialize(
49+ send, 0, dataLength - 1, deserializeBuffer, out int bufferLength);
50+
51+ Assert.AreEqual(checksum, checksum2);
52+
53+ Assert.AreEqual(data.Length, bufferLength);
54+ Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
55+ }
5356 }
5457
5558 [ TestMethod ]
56- public void SerializeTcpResponseTest ( )
59+ public unsafe void SerializeTcpResponseTest ( )
5760 {
5861 byte [ ] data = { 0b0000_0000 , 0b0000_0000 , 0b0000_0000 , 0b0000_0000 , 0b0000_0000 , 0b0000_0000 , 0b0000_0000 } ;
59-
60- Serialization . Serialization . SerializeTcp (
61- 1337u , data , 0 , data . Length , 654584478 , EncryptionMode . None , out byte [ ] send ,
62- out int size ) ;
63- Assert . AreEqual ( size , Constants . TCP_HEADER_SIZE + 4 + data . Length + 1 + 1 ) ;
64-
65- CircularBuffer cb = new CircularBuffer ( ) ;
66- cb . Write ( send , 0 , size ) ;
67-
68- Assert . IsTrue (
69- cb . PeekHeader ( 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
70-
71- Assert . AreEqual ( ( byte ) 64 , packetHeader ) ;
72- Assert . AreEqual ( 1337u , commandID ) ;
73- Assert . AreEqual ( 4 + data . Length + 1 + 1 , dataLength ) ;
74-
75- Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
76- Assert . AreEqual ( ( byte ) 0 , b ) ;
77-
78- cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
79-
80- byte [ ] deserializeBuffer = new byte [ 128 ] ;
81- /*int checksum2 = Serialization.Serialization.Deserialize(
82- send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
83-
84- Assert.AreEqual(checksum, checksum2);
85-
86- Assert.AreEqual(data.Length, bufferLength);
87- Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
62+ fixed ( byte * src = data )
63+ {
64+ Serialization . Serialization . SerializeTcp (
65+ 1337u , src , data . Length , 654584478 , EncryptionMode . None , out byte [ ] send ,
66+ out int size ) ;
67+ Assert . AreEqual ( size , Constants . TCP_HEADER_SIZE + 4 + data . Length + 1 + 1 ) ;
68+
69+ CircularBuffer cb = new CircularBuffer ( ) ;
70+ cb . Write ( send , 0 , size ) ;
71+
72+ Assert . IsTrue (
73+ cb . PeekHeader (
74+ 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
75+
76+ Assert . AreEqual ( ( byte ) 64 , packetHeader ) ;
77+ Assert . AreEqual ( 1337u , commandID ) ;
78+ Assert . AreEqual ( 4 + data . Length + 1 + 1 , dataLength ) ;
79+
80+ Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
81+ Assert . AreEqual ( ( byte ) 0 , b ) ;
82+
83+ cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
84+
85+ byte [ ] deserializeBuffer = new byte [ 128 ] ;
86+ /*int checksum2 = Serialization.Serialization.Deserialize(
87+ send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
88+
89+ Assert.AreEqual(checksum, checksum2);
90+
91+ Assert.AreEqual(data.Length, bufferLength);
92+ Assert.IsTrue(data.SequenceEqual(deserializeBuffer.Take(bufferLength)));*/
93+ }
8894 }
8995
9096 [ TestMethod ]
91- public void SerializeTcpNoResponseBigPayloadTest ( )
97+ public unsafe void SerializeTcpNoResponseBigPayloadTest ( )
9298 {
9399 Random rnd = new Random ( 7576 ) ;
94100 byte [ ] data = new byte [ ( 1 << 12 ) + 200 ] ;
95101
96102 //rnd.NextBytes(data);
97103 int expectedLength = data . Length + Math2 . Ceiling ( data . Length / 7.0f ) ;
98104
99- Serialization . Serialization . SerializeTcp (
100- 1337u , data , 0 , data . Length , 0 , EncryptionMode . None , out byte [ ] send , out int size ) ;
101- Assert . IsTrue ( size < Constants . TCP_HEADER_SIZE + 4 + expectedLength + 1 + 1 ) ;
102-
103- CircularBuffer cb = new CircularBuffer ( data . Length ) ;
104- cb . Write ( send , 0 , size ) ;
105-
106- Assert . IsTrue (
107- cb . PeekHeader ( 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
108-
109- Assert . AreEqual ( ( byte ) 32 , packetHeader ) ;
110- Assert . AreEqual ( 1337u , commandID ) ;
111- Assert . IsTrue ( 4 + data . Length + 1 + 1 > dataLength ) ;
112-
113- Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
114- Assert . AreEqual ( ( byte ) 0 , b ) ;
115-
116- cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
117-
118- byte [ ] deserializeBuffer = new byte [ 128 ] ;
119- /*int checksum2 = Serialization.Serialization.Deserialize(
120- send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
121-
122- Assert.AreEqual(27, bufferLength);
123-
124- Assert.AreEqual(checksum, checksum2);
125- Assert.AreEqual(data.Length, BitConverter.ToInt32(send, 0));*/
105+ fixed ( byte * src = data )
106+ {
107+ Serialization . Serialization . SerializeTcp (
108+ 1337u , src , data . Length , 0 , EncryptionMode . None , out byte [ ] send , out int size ) ;
109+ Assert . IsTrue ( size < Constants . TCP_HEADER_SIZE + 4 + expectedLength + 1 + 1 ) ;
110+
111+ CircularBuffer cb = new CircularBuffer ( data . Length ) ;
112+ cb . Write ( send , 0 , size ) ;
113+
114+ Assert . IsTrue (
115+ cb . PeekHeader (
116+ 0 , out byte packetHeader , out uint commandID , out int dataLength , out ushort checksum ) ) ;
117+
118+ Assert . AreEqual ( ( byte ) 32 , packetHeader ) ;
119+ Assert . AreEqual ( 1337u , commandID ) ;
120+ Assert . IsTrue ( 4 + data . Length + 1 + 1 > dataLength ) ;
121+
122+ Assert . IsTrue ( cb . PeekByte ( ( Constants . TCP_HEADER_SIZE + dataLength ) - 1 , out byte b ) ) ;
123+ Assert . AreEqual ( ( byte ) 0 , b ) ;
124+
125+ cb . Read ( send , 0 , dataLength , Constants . TCP_HEADER_SIZE ) ;
126+
127+ byte [ ] deserializeBuffer = new byte [ 128 ] ;
128+ /*int checksum2 = Serialization.Serialization.Deserialize(
129+ send, 4, dataLength - 5, deserializeBuffer, out int bufferLength);
130+
131+ Assert.AreEqual(27, bufferLength);
132+
133+ Assert.AreEqual(checksum, checksum2);
134+ Assert.AreEqual(data.Length, BitConverter.ToInt32(send, 0));*/
135+ }
126136 }
127137 }
128138}
0 commit comments