File: MessageReaderTest.cs

package info (click to toggle)
dbus-sharp 0.8.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 868 kB
  • sloc: cs: 7,690; sh: 494; makefile: 181
file content (193 lines) | stat: -rw-r--r-- 6,413 bytes parent folder | download | duplicates (3)
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

using System;
using System.Runtime.InteropServices;
using NUnit.Framework;
using DBus;
using DBus.Protocol;

namespace DBus.Tests
{
	[TestFixture]
	public class MessageReaderTest
	{
		[Test]
		public void ReadIntLittleEndian ()
		{
			MessageReader reader = new MessageReader (EndianFlag.Little, new byte[] { 8, 8, 0, 0});
			Assert.AreEqual (0x808, reader.ReadInt32 ());
			Assert.IsFalse (reader.DataAvailable);
		}

		[Test]
		public void ReadIntBigEndian ()
		{
			MessageReader reader = new MessageReader (EndianFlag.Big, new byte[] { 0, 0, 8, 8});
			Assert.AreEqual (0x808, reader.ReadInt32 ());
			Assert.IsFalse (reader.DataAvailable);
		}

		[Test]
		public void ReadIntArrayLittleEndian ()
		{
			byte[] data = new byte[] { 16, 0, 0, 0, 8, 8, 0, 0, 8, 8, 0, 0, 8, 8, 0, 0, 8, 8, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);
			
			int[] array = (int[])reader.ReadArray (typeof (int));
			Assert.IsNotNull (array);
			Assert.AreEqual (4, array.Length, "length");
			CollectionAssert.AreEqual (new int[] { 0x808, 0x808, 0x808, 0x808}, array, "elements");
			Assert.IsFalse (reader.DataAvailable);
		}

		[Test]
		public void ReadIntArrayBigEndian ()
		{
			byte[] data = new byte[] { 0, 0, 0, 16, 0, 0, 8, 8, 0, 0, 8, 8, 0, 0, 8, 8, 0, 0, 8, 8 };
			MessageReader reader = new MessageReader (EndianFlag.Big, data);
			
			int[] array = (int[])reader.ReadArray (typeof (int));
			Assert.IsNotNull (array);
			Assert.AreEqual (4, array.Length, "length");
			CollectionAssert.AreEqual (new int[] { 0x808, 0x808, 0x808, 0x808}, array, "elements");
			Assert.IsFalse (reader.DataAvailable);
		}

		[Test]
		public void ReadBooleanArrayLittleEndian ()
		{
			byte[] data = new byte[] { 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);
			
			bool[] array = (bool[])reader.ReadArray (typeof (bool));
			Assert.IsNotNull (array);
			Assert.AreEqual (4, array.Length, "length");
			CollectionAssert.AreEqual (new bool[] { true, false, true, true}, array, "elements");
			Assert.IsFalse (reader.DataAvailable);
		}

		[Test]
		public void ReadBooleanArrayBigEndian ()
		{
			byte[] data = new byte[] { 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
			MessageReader reader = new MessageReader (EndianFlag.Big, data);
			
			bool[] array = (bool[])reader.ReadArray (typeof (bool));
			Assert.IsNotNull (array);
			Assert.AreEqual (4, array.Length, "length");
			CollectionAssert.AreEqual (new bool[] { true, false, true, true}, array, "elements");
			Assert.IsFalse (reader.DataAvailable);
		}

		[StructLayout (LayoutKind.Sequential)]
		struct TestStruct {
			public int Item1;
			public long Item2;
			public int Item3;
		}

		[Test]
		public void ReadIntLongIntStructLittleEndian ()
		{
			// (ixi) and (1, 2, 3)
			byte[] data = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);

			TestStruct stct = (TestStruct)reader.ReadStruct (typeof (TestStruct));
			Assert.AreEqual (1, stct.Item1);
			Assert.AreEqual (2, stct.Item2);
			Assert.AreEqual (3, stct.Item3);
		}

		[Test, ExpectedException (typeof (MessageReader.PaddingException))]
		public void ReadIntLongIntStructNonAlignedLittleEndian ()
		{
			// (ixi) and (1, 2, 3)
			byte[] data = new byte[] { 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);

			TestStruct stct = (TestStruct)reader.ReadStruct (typeof (TestStruct));
		}

		[StructLayout (LayoutKind.Sequential)]
		struct TestStruct2 {
			public int Item1;
			public int Item2;
			public int Item3;
		}

		[Test]
		public void ReadIntIntIntStructLittleEndian ()
		{
			// Will test the fast path
			byte[] data = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);

			TestStruct2 stct = (TestStruct2)reader.ReadStruct (typeof (TestStruct2));
			Assert.AreEqual (1, stct.Item1);
			Assert.AreEqual (2, stct.Item2);
			Assert.AreEqual (3, stct.Item3);
		}

		[Test]
		public void ReadIntIntIntStructLittleEndianGeneric ()
		{
			// Will test the fast path
			byte[] data = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0 };
			MessageReader reader = new MessageReader (EndianFlag.Little, data);

			TestStruct2 stct = reader.ReadStruct<TestStruct2> ();
			Assert.AreEqual (1, stct.Item1);
			Assert.AreEqual (2, stct.Item2);
			Assert.AreEqual (3, stct.Item3);
		}

		[StructLayout (LayoutKind.Sequential)]
		struct TestStruct3 {
			public long Item1;
			public ulong Item2;
			public double Item3;
		}

		[Test]
		public void ReadSameAlignementStructNativeEndian ()
		{
			// Will test the fast path with mixed types but same alignement
			byte[] data = new byte[8 * 3];
			Array.Copy (BitConverter.GetBytes ((long)1), 0, data, 0, 8);
			Array.Copy (BitConverter.GetBytes (ulong.MaxValue), 0, data, 8, 8);
			Array.Copy (BitConverter.GetBytes ((double)3.3), 0, data, 16, 8);

			MessageReader reader = new MessageReader (BitConverter.IsLittleEndian ? EndianFlag.Little : EndianFlag.Big, data);

			TestStruct3 stct = (TestStruct3)reader.ReadStruct (typeof (TestStruct3));
			Assert.AreEqual (1, stct.Item1);
			Assert.AreEqual (ulong.MaxValue, stct.Item2);
			Assert.AreEqual (3.3, stct.Item3);
		}

		[Test]
		public void ReadSameAlignementStructNonNativeEndian ()
		{
			// Will test the fast path with mixed types but same alignement
			byte[] data = new byte[8 * 3];
			Array.Copy (BitConverter.GetBytes ((long)1), 0, data, 0, 8);
			Array.Copy (BitConverter.GetBytes (ulong.MaxValue), 0, data, 8, 8);
			Array.Copy (BitConverter.GetBytes ((double)3.3), 0, data, 16, 8);
			// Swap value to simulate other endianess
			for (int i = 0; i < data.Length; i += 8) {
				for (int j = 0; j < 4; j++) {
					data[i + j] = (byte)(data[i + j] ^ data[i + 7 - j]);
					data[i + 7 - j] = (byte)(data[i + j] ^ data[i + 7 - j]);
					data[i + j] = (byte)(data[i + j] ^ data[i + 7 - j]);
				}
			}

			MessageReader reader = new MessageReader (!BitConverter.IsLittleEndian ? EndianFlag.Little : EndianFlag.Big, data);

			TestStruct3 stct = (TestStruct3)reader.ReadStruct (typeof (TestStruct3));
			Assert.AreEqual (1, stct.Item1);
			Assert.AreEqual (ulong.MaxValue, stct.Item2);
			Assert.AreEqual (3.3, stct.Item3);
		}
	}
}