-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathBerConverterTests.cs
263 lines (218 loc) · 15.2 KB
/
BerConverterTests.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.XUnitExtensions;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Xunit;
namespace System.DirectoryServices.Protocols.Tests
{
[ConditionalClass(typeof(DirectoryServicesTestHelpers), nameof(DirectoryServicesTestHelpers.IsWindowsOrLibLdapIsInstalled))]
public class BerConverterTests
{
public static IEnumerable<object[]> Encode_TestData()
{
yield return new object[] { "", null, new byte[0] };
yield return new object[] { "", new object[10], new byte[0] };
yield return new object[] { "b", new object[] { true, false, true, false }, new byte[] { 1, 1, 255 } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "{", new object[] { "a" }, new byte[] { 48, 0, 0, 0, 0, 0 } }; // This format is not supported by Linux OpenLDAP
}
yield return new object[] { "{}", new object[] { "a" }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 48, 132, 0, 0, 0, 0 } : new byte[] { 48, 0 } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "[", new object[] { "a" }, new byte[] { 49, 0, 0, 0, 0, 0 } }; // This format is not supported by Linux OpenLDAP
}
yield return new object[] { "[]", new object[] { "a" }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 49, 132, 0, 0, 0, 0 } : new byte[] { 49, 0 } };
yield return new object[] { "n", new object[] { "a" }, new byte[] { 5, 0 } };
yield return new object[] { "e", new object[] { 128 }, new byte[] { 10, 2, 0, 128 } };
yield return new object[] { "te", new object[] { 128, 0 }, new byte[] { 128, 1, 0 } };
yield return new object[] { "tet", new object[] { 128, 0, 133 }, new byte[] { 128, 1, 0 } };
yield return new object[] { "tetie", new object[] { 128, 0, 133, 2, 3 }, new byte[] { 128, 1, 0, 133, 1, 2, 10, 1, 3 } };
yield return new object[] { "{tetie}", new object[] { 128, 0, 133, 2, 3 }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 48, 132, 0, 0, 0, 9, 128, 1, 0, 133, 1, 2, 10, 1, 3 } : new byte[] { 48, 9, 128, 1, 0, 133, 1, 2, 10, 1, 3 } };
yield return new object[] { "bb", new object[] { true, false }, new byte[] { 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "{bb}", new object[] { true, false }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } : new byte[] { 48, 6, 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "ssss", new object[] { null, "", "abc", "\0" }, new byte[] { 4, 0, 4, 0, 4, 3, 97, 98, 99, 4, 1, 0 } };
yield return new object[] { "o", new object[] { null }, new byte[] { 4, 0} };
yield return new object[] { "X", new object[] { new byte[] { 0, 1, 2, 255 } }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 3, 4, 0, 1, 2, 255 } : new byte[] { 3, 2, 4, 0 } };
yield return new object[] { "oXo", new object[] { null, new byte[] { 0, 1, 2, 255 }, new byte[0] }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 4, 0, 3, 4, 0, 1, 2, 255, 4, 0 } : new byte[] { 4, 0, 3, 2, 4, 0, 4, 0 } };
yield return new object[] { "vv", new object[] { null, new string[] { "abc", "", null } }, new byte[] { 4, 3, 97, 98, 99, 4, 0, 4, 0 } };
yield return new object[] { "{vv}", new object[] { null, new string[] { "abc", "", null } }, (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ? new byte[] { 48, 132, 0, 0, 0, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 } : new byte[] { 48, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "VVVV", new object[] { null, new byte[][] { new byte[] { 0, 1, 2, 3 }, null }, new byte[][] { new byte[0] }, new byte[0][] }, new byte[] { 4, 4, 0, 1, 2, 3, 4, 0, 4, 0 } };
}
}
[Theory]
[MemberData(nameof(Encode_TestData))]
public void Encode_Objects_ReturnsExpected(string format, object[] values, byte[] expected)
{
AssertExtensions.Equal(expected, BerConverter.Encode(format, values));
}
[Fact]
public void Encode_NullFormat_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("format", () => BerConverter.Encode(null, new object[0]));
}
public static IEnumerable<object[]> Encode_Invalid_TestData()
{
yield return new object[] { "t", new object[0] };
yield return new object[] { "t", new object[] { "string" } };
yield return new object[] { "t", new object[] { null } };
yield return new object[] { "i", new object[0] };
yield return new object[] { "i", new object[] { "string" } };
yield return new object[] { "i", new object[] { null } };
yield return new object[] { "e", new object[0] };
yield return new object[] { "e", new object[] { "string" } };
yield return new object[] { "e", new object[] { null } };
yield return new object[] { "b", new object[0] };
yield return new object[] { "b", new object[0] };
yield return new object[] { "b", new object[] { "string" } };
yield return new object[] { "b", new object[] { null } };
yield return new object[] { "s", new object[0] };
yield return new object[] { "s", new object[] { 123 } };
yield return new object[] { "o", new object[0] };
yield return new object[] { "o", new object[] { "string" } };
yield return new object[] { "o", new object[] { 123 } };
yield return new object[] { "X", new object[0] };
yield return new object[] { "X", new object[] { "string" } };
yield return new object[] { "X", new object[] { 123 } };
yield return new object[] { "v", new object[0] };
yield return new object[] { "v", new object[] { "string" } };
yield return new object[] { "v", new object[] { 123 } };
yield return new object[] { "V", new object[0] };
yield return new object[] { "V", new object[] { "string" } };
yield return new object[] { "V", new object[] { new byte[0] } };
yield return new object[] { "a", new object[0] };
}
[Theory]
[MemberData(nameof(Encode_Invalid_TestData))]
public void Encode_Invalid_ThrowsArgumentException(string format, object[] values)
{
AssertExtensions.Throws<ArgumentException>(null, () => BerConverter.Encode(format, values));
}
[Theory]
[InlineData("]")]
[InlineData("}")]
[InlineData("{{}}}")]
public void Encode_InvalidFormat_ThrowsBerConversionException(string format)
{
Assert.Throws<BerConversionException>(() => BerConverter.Encode(format, new object[0]));
}
public static IEnumerable<object[]> Decode_TestData()
{
// Content: zero-length sequence
// Parsed as such
yield return new object[] { "{}", new byte[] { 48, 0, 0, 0, 0, 0 }, new object[0] };
// Content: sequence containing octet string
// Parsed as such
yield return new object[] { "{a}", new byte[] { 48, 132, 0, 0, 0, 5, 4, 3, 97, 98, 99 }, new object[] { "abc" } };
// Content: sequence containing integer
// Parsed as such
yield return new object[] { "{i}", new byte[] { 48, 132, 0, 0, 0, 3, 2, 1, 10 }, new object[] { 10 } };
// Content: sequence containing two booleans
// Parsed as a sequence containing an integer, followed by an enumerated value
yield return new object[] { "{ie}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { -1, 0 } };
// Content: sequence containing two booleans
// Parsed as such
yield return new object[] { "{bb}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { true, false } };
// Content: sequence containing two booleans
// Parsed as a sequence containing two octet strings
yield return new object[] { "{OO}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { new byte[] { 255 }, new byte[] { 0 } } };
// Content: sequence containing two booleans
// Parsed as a sequence containing two bitstrings
yield return new object[] { "{BB}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { new byte[] { 255 }, new byte[] { 0 } } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // vv and VV formats are not supported yet in Linux
{
// Content: sequence containing three octet strings
// Parsed as a sequence containing two sequences of octet strings
yield return new object[] { "{vv}", new byte[] { 48, 132, 0, 0, 0, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 }, new object[] { null, null } };
// Content: sequence containing three octet strings
// Parsed as two sequences of octet strings
yield return new object[] { "vv", new byte[] { 48, 132, 0, 0, 0, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 }, new object[] { new string[] { "abc", "" }, null } };
// Content: sequence containing two sequences of octet strings
// Parsed as such
yield return new object[] { "{vv}", new byte[] { 48, 14, 48, 5, 4, 3, 97, 98, 99, 48, 5, 4, 3, 100, 101, 102 }, new object[] { new string[] { "abc" }, new string[] { "def" } } };
// Content: sequence containing two booleans
// Parsed as a sequence containing two sequences of octet strings
yield return new object[] { "{vv}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { new string[] { "\x01" }, null } };
// Content: sequence containing two booleans. First boolean has a valid value which is also a valid UTF8 character
// Parsed as two sequences of octet strings
yield return new object[] { "vv", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 48, 1, 1, 0 }, new object[] { new string[] { "\x30", "\x00" }, null } };
// Content: sequence of octet strings
// Parsed as a sequence containing two sequences of octet strings (returned as bytes)
yield return new object[] { "{VV}", new byte[] { 48, 132, 0, 0, 0, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 }, new object[] { null, null } };
// Content: sequence of octet strings
// Parsed as two sequences of octet strings (returned as bytes)
yield return new object[] { "VV", new byte[] { 48, 132, 0, 0, 0, 9, 4, 3, 97, 98, 99, 4, 0, 4, 0 }, new object[] { new byte[][] { [97, 98, 99], [] }, null } };
// Content: sequence containing two booleans
// Parsed as a sequence containing two sequences of octet strings (returned as bytes)
yield return new object[] { "{VV}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { new byte[][] { [1] }, null } };
// Content: sequence containing two booleans
// Parsed as two sequences of octet strings (returned as bytes)
yield return new object[] { "VV", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 }, new object[] { new byte[][] { [255], [0] }, null } };
}
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/99725")]
[Theory]
[MemberData(nameof(Decode_TestData))]
public void Decode_Bytes_ReturnsExpected(string format, byte[] values, object[] expected)
{
object value = BerConverter.Decode(format, values);
Assert.Equal(expected, value);
}
[Fact]
public void Decode_NullFormat_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("format", () => BerConverter.Decode(null, new byte[0]));
}
[Theory]
[InlineData("p", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 })]
public void UnknownFormat_ThrowsArgumentException(string format, byte[] values)
{
AssertExtensions.Throws<ArgumentException>(null, () => BerConverter.Decode(format, values));
}
public static IEnumerable<object[]> Decode_Invalid_ThrowsBerConversionException_Data()
{
yield return new object[] { "n", null };
yield return new object[] { "n", new byte[0] };
yield return new object[] { "{", new byte[] { 1 } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "}", new byte[] { 1 } }; // This is considered a valid case in Linux
}
yield return new object[] { "{}{}{}{}{}{}{}", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "aaa", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
}
yield return new object[] { "iii", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "eee", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "bbb", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "OOO", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
yield return new object[] { "BBB", new byte[] { 48, 132, 0, 0, 0, 6, 1, 1, 255, 1, 1, 0 } };
}
[Theory]
[MemberData(nameof(Decode_Invalid_ThrowsBerConversionException_Data))]
public void Decode_Invalid_ThrowsBerConversionException(string format, byte[] values)
{
Assert.Throws<BerConversionException>(() => BerConverter.Decode(format, values));
}
public static IEnumerable<object[]> Manual_Wrapping_Required_Data()
{
// vv and VV formats are not supported yet in Linux
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return new object[] { "v", new object[] { new string[] { "abc", "def" } } };
yield return new object[] { "V", new object[] { new byte[][] { [97, 98, 99], [100, 101, 102] } } };
}
}
[Theory]
[MemberData(nameof(Manual_Wrapping_Required_Data))]
public void Must_Manually_Wrap_Several_OctetStrings_In_Sequence(string format, object[] values)
{
Assert.Throws<BerConversionException>(() => BerConverter.Decode(format, BerConverter.Encode(format, values)));
Assert.Equal(values, BerConverter.Decode(format, BerConverter.Encode("{" + format + "}", values)));
}
}
}