@@ -6,6 +6,8 @@ namespace SabreTools.ASN1.Test
6
6
{
7
7
public class TypeLengthValueTests
8
8
{
9
+ #region Construction
10
+
9
11
[ Fact ]
10
12
public void Constructor_EmptyArray_Throws ( )
11
13
{
@@ -31,7 +33,7 @@ public void Constructor_ValidArrayOverIndex_Throws()
31
33
}
32
34
33
35
[ Fact ]
34
- public void Constructor_ValidMinimalArray_Returns ( )
36
+ public void Constructor_ValidMinimalArray ( )
35
37
{
36
38
int index = 0 ;
37
39
byte [ ] data = [ 0x00 ] ;
@@ -50,7 +52,7 @@ public void Constructor_EmptyStream_Throws()
50
52
}
51
53
52
54
[ Fact ]
53
- public void Constructor_ValidMinimalStream_Returns ( )
55
+ public void Constructor_ValidMinimalStream ( )
54
56
{
55
57
Stream data = new MemoryStream ( [ 0x00 ] ) ;
56
58
var tlv = new TypeLengthValue ( data ) ;
@@ -61,7 +63,7 @@ public void Constructor_ValidMinimalStream_Returns()
61
63
}
62
64
63
65
[ Fact ]
64
- public void Constructor_ValidBoolean_Returns ( )
66
+ public void Constructor_ValidBoolean ( )
65
67
{
66
68
Stream data = new MemoryStream ( [ 0x01 , 0x01 , 0x01 ] ) ;
67
69
var tlv = new TypeLengthValue ( data ) ;
@@ -85,7 +87,7 @@ public void Constructor_ValidBoolean_Returns()
85
87
[ InlineData ( new byte [ ] { 0x26 , 0x86 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , 0x01 , 0x01 , 0x01 } ) ]
86
88
[ InlineData ( new byte [ ] { 0x26 , 0x87 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , 0x01 , 0x01 , 0x01 } ) ]
87
89
[ InlineData ( new byte [ ] { 0x26 , 0x88 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 , 0x01 , 0x01 , 0x01 } ) ]
88
- public void Constructor_ComplexValue_Returns ( byte [ ] arr )
90
+ public void Constructor_ComplexValue ( byte [ ] arr )
89
91
{
90
92
Stream data = new MemoryStream ( arr ) ;
91
93
var tlv = new TypeLengthValue ( data ) ;
@@ -111,5 +113,210 @@ public void Constructor_ComplexValueInvalidLength_Throws(byte[] arr)
111
113
Stream data = new MemoryStream ( arr ) ;
112
114
Assert . Throws < InvalidOperationException > ( ( ) => new TypeLengthValue ( data ) ) ;
113
115
}
116
+
117
+ #endregion
118
+
119
+ #region Formatting
120
+
121
+ [ Fact ]
122
+ public void Format_EOC ( )
123
+ {
124
+ string expected = "Type: V_ASN1_EOC" ;
125
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_EOC , 0 , null ) ;
126
+ string actual = tlv . Format ( ) ;
127
+ Assert . Equal ( expected , actual ) ;
128
+ }
129
+
130
+ [ Fact ]
131
+ public void Format_ZeroLength ( )
132
+ {
133
+ string expected = "Type: V_ASN1_NULL, Length: 0" ;
134
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_NULL , 0 , null ) ;
135
+ string actual = tlv . Format ( ) ;
136
+ Assert . Equal ( expected , actual ) ;
137
+ }
138
+
139
+ [ Fact ]
140
+ public void Format_InvalidConstructed ( )
141
+ {
142
+ string expected = "Type: V_ASN1_OBJECT, V_ASN1_CONSTRUCTED, Length: 1, Value: [INVALID DATA TYPE]" ;
143
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OBJECT | ASN1Type . V_ASN1_CONSTRUCTED , 1 , ( object ? ) false ) ;
144
+ string actual = tlv . Format ( ) ;
145
+ Assert . Equal ( expected , actual ) ;
146
+ }
147
+
148
+ [ Fact ]
149
+ public void Format_ValidConstructed ( )
150
+ {
151
+ string expected = "Type: V_ASN1_OBJECT, V_ASN1_CONSTRUCTED, Length: 3, Value:\n Type: V_ASN1_BOOLEAN, Length: 1, Value: True" ;
152
+ var boolTlv = new TypeLengthValue ( ASN1Type . V_ASN1_BOOLEAN , 1 , new byte [ ] { 0x01 } ) ;
153
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OBJECT | ASN1Type . V_ASN1_CONSTRUCTED , 3 , new TypeLengthValue [ ] { boolTlv } ) ;
154
+ string actual = tlv . Format ( ) ;
155
+ Assert . Equal ( expected , actual ) ;
156
+ }
157
+
158
+ [ Fact ]
159
+ public void Format_InvalidDataType ( )
160
+ {
161
+ string expected = "Type: V_ASN1_OBJECT, Length: 1, Value: [INVALID DATA TYPE]" ;
162
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OBJECT , 1 , ( object ? ) false ) ;
163
+ string actual = tlv . Format ( ) ;
164
+ Assert . Equal ( expected , actual ) ;
165
+ }
166
+
167
+ [ Fact ]
168
+ public void Format_InvalidLength ( )
169
+ {
170
+ string expected = "Type: V_ASN1_NULL, Length: 1, Value: [NO DATA]" ;
171
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_NULL , 1 , Array . Empty < byte > ( ) ) ;
172
+ string actual = tlv . Format ( ) ;
173
+ Assert . Equal ( expected , actual ) ;
174
+ }
175
+
176
+ [ Fact ]
177
+ public void Format_InvalidBooleanLength ( )
178
+ {
179
+ string expected = "Type: V_ASN1_BOOLEAN, Length: 2 [Expected length of 1], Value: True" ;
180
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BOOLEAN , 2 , new byte [ ] { 0x01 } ) ;
181
+ string actual = tlv . Format ( ) ;
182
+ Assert . Equal ( expected , actual ) ;
183
+ }
184
+
185
+ [ Fact ]
186
+ public void Format_InvalidBooleanArrayLength ( )
187
+ {
188
+ string expected = "Type: V_ASN1_BOOLEAN, Length: 1 [Expected value length of 1], Value: True" ;
189
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BOOLEAN , 1 , new byte [ ] { 0x01 , 0x00 } ) ;
190
+ string actual = tlv . Format ( ) ;
191
+ Assert . Equal ( expected , actual ) ;
192
+ }
193
+
194
+ [ Fact ]
195
+ public void Format_ValidBoolean ( )
196
+ {
197
+ string expected = "Type: V_ASN1_BOOLEAN, Length: 1, Value: True" ;
198
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BOOLEAN , 1 , new byte [ ] { 0x01 } ) ;
199
+ string actual = tlv . Format ( ) ;
200
+ Assert . Equal ( expected , actual ) ;
201
+ }
202
+
203
+ [ Fact ]
204
+ public void Format_ValidInteger ( )
205
+ {
206
+ string expected = "Type: V_ASN1_INTEGER, Length: 1, Value: 1" ;
207
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_INTEGER , 1 , new byte [ ] { 0x01 } ) ;
208
+ string actual = tlv . Format ( ) ;
209
+ Assert . Equal ( expected , actual ) ;
210
+ }
211
+
212
+ [ Fact ]
213
+ public void Format_ValidBitString_NoBits ( )
214
+ {
215
+ string expected = "Type: V_ASN1_BIT_STRING, Length: 1, Value with 0 unused bits" ;
216
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BIT_STRING , 1 , new byte [ ] { 0x00 } ) ;
217
+ string actual = tlv . Format ( ) ;
218
+ Assert . Equal ( expected , actual ) ;
219
+ }
220
+
221
+ [ Fact ]
222
+ public void Format_ValidBitString_Bits ( )
223
+ {
224
+ string expected = "Type: V_ASN1_BIT_STRING, Length: 1, Value with 1 unused bits: 01" ;
225
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BIT_STRING , 1 , new byte [ ] { 0x01 , 0x01 } ) ;
226
+ string actual = tlv . Format ( ) ;
227
+ Assert . Equal ( expected , actual ) ;
228
+ }
229
+
230
+ [ Fact ]
231
+ public void Format_ValidOctetString ( )
232
+ {
233
+ string expected = "Type: V_ASN1_OCTET_STRING, Length: 1, Value: 01" ;
234
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OCTET_STRING , 1 , new byte [ ] { 0x01 } ) ;
235
+ string actual = tlv . Format ( ) ;
236
+ Assert . Equal ( expected , actual ) ;
237
+ }
238
+
239
+ [ Fact ]
240
+ public void Format_ValidObject ( )
241
+ {
242
+ string expected = "Type: V_ASN1_OBJECT, Length: 3, Value: 0.1.2.3 (/ITU-T/1/2/3)" ;
243
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OBJECT , 3 , new byte [ ] { 0x01 , 0x02 , 0x03 } ) ;
244
+ string actual = tlv . Format ( ) ;
245
+ Assert . Equal ( expected , actual ) ;
246
+ }
247
+
248
+ [ Fact ]
249
+ public void Format_ValidUTF8String ( )
250
+ {
251
+ string expected = "Type: V_ASN1_UTF8STRING, Length: 3, Value: ABC" ;
252
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_UTF8STRING , 3 , new byte [ ] { 0x41 , 0x42 , 0x43 } ) ;
253
+ string actual = tlv . Format ( ) ;
254
+ Assert . Equal ( expected , actual ) ;
255
+ }
256
+
257
+ [ Fact ]
258
+ public void Format_ValidPrintableString ( )
259
+ {
260
+ string expected = "Type: V_ASN1_PRINTABLESTRING, Length: 3, Value: ABC" ;
261
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_PRINTABLESTRING , 3 , new byte [ ] { 0x41 , 0x42 , 0x43 } ) ;
262
+ string actual = tlv . Format ( ) ;
263
+ Assert . Equal ( expected , actual ) ;
264
+ }
265
+
266
+ [ Fact ]
267
+ public void Format_ValidTeletexString ( )
268
+ {
269
+ string expected = "Type: V_ASN1_TELETEXSTRING, Length: 3, Value: ABC" ;
270
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_TELETEXSTRING , 3 , new byte [ ] { 0x41 , 0x42 , 0x43 } ) ;
271
+ string actual = tlv . Format ( ) ;
272
+ Assert . Equal ( expected , actual ) ;
273
+ }
274
+
275
+ [ Fact ]
276
+ public void Format_ValidIA5String ( )
277
+ {
278
+ string expected = "Type: V_ASN1_IA5STRING, Length: 3, Value: ABC" ;
279
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_IA5STRING , 3 , new byte [ ] { 0x41 , 0x42 , 0x43 } ) ;
280
+ string actual = tlv . Format ( ) ;
281
+ Assert . Equal ( expected , actual ) ;
282
+ }
283
+
284
+ [ Fact ]
285
+ public void Format_InvalidUTCTime ( )
286
+ {
287
+ string expected = "Type: V_ASN1_UTCTIME, Length: 3, Value: ABC" ;
288
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_UTCTIME , 3 , new byte [ ] { 0x41 , 0x42 , 0x43 } ) ;
289
+ string actual = tlv . Format ( ) ;
290
+ Assert . Equal ( expected , actual ) ;
291
+ }
292
+
293
+ [ Fact ]
294
+ public void Format_ValidUTCTime ( )
295
+ {
296
+ string expected = "Type: V_ASN1_UTCTIME, Length: 3, Value: 1980-01-01 00:00:00" ;
297
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_UTCTIME , 3 , new byte [ ] { 0x31 , 0x39 , 0x38 , 0x30 , 0x2D , 0x30 , 0x31 , 0x2D , 0x30 , 0x31 , 0x20 , 0x30 , 0x30 , 0x3A , 0x30 , 0x30 , 0x3A , 0x30 , 0x30 } ) ;
298
+ string actual = tlv . Format ( ) ;
299
+ Assert . Equal ( expected , actual ) ;
300
+ }
301
+
302
+ [ Fact ]
303
+ public void Format_ValidBmpString ( )
304
+ {
305
+ string expected = "Type: V_ASN1_BMPSTRING, Length: 6, Value: ABC" ;
306
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_BMPSTRING , 6 , new byte [ ] { 0x41 , 0x00 , 0x42 , 0x00 , 0x43 , 0x00 } ) ;
307
+ string actual = tlv . Format ( ) ;
308
+ Assert . Equal ( expected , actual ) ;
309
+ }
310
+
311
+ [ Fact ]
312
+ public void Format_ValidUnformatted ( )
313
+ {
314
+ string expected = "Type: V_ASN1_OBJECT_DESCRIPTOR, Length: 1, Value: 01" ;
315
+ var tlv = new TypeLengthValue ( ASN1Type . V_ASN1_OBJECT_DESCRIPTOR , 1 , new byte [ ] { 0x01 } ) ;
316
+ string actual = tlv . Format ( ) ;
317
+ Assert . Equal ( expected , actual ) ;
318
+ }
319
+
320
+ #endregion
114
321
}
115
322
}
0 commit comments