2025-09-24 08:25:11 -04:00
|
|
|
using System;
|
2025-09-26 13:15:55 -04:00
|
|
|
using SabreTools.Data.Extensions;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.ASN1;
|
2025-09-24 08:25:11 -04:00
|
|
|
using Xunit;
|
|
|
|
|
|
2026-01-27 12:03:01 -05:00
|
|
|
#pragma warning disable IDE0230 // Use UTF-8 string literal
|
2025-09-24 09:00:48 -04:00
|
|
|
namespace SabreTools.Serialization.Test.Extensions
|
2025-09-24 08:25:11 -04:00
|
|
|
{
|
|
|
|
|
public class TypeLengthValueTests
|
|
|
|
|
{
|
|
|
|
|
#region Formatting
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_EOC()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_EOC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_EOC, Length = 0, Value = null };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ZeroLength()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_NULL, Length: 0";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_NULL, Length = 0, Value = null };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidConstructed()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OBJECT, V_ASN1_CONSTRUCTED, Length: 1, Value: [INVALID DATA TYPE]";
|
2026-01-27 12:03:01 -05:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OBJECT | ASN1Type.V_ASN1_CONSTRUCTED, Length = 1, Value = false };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidConstructed()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OBJECT, V_ASN1_CONSTRUCTED, Length: 3, Value:\n Type: V_ASN1_BOOLEAN, Length: 1, Value: True";
|
2025-09-26 13:06:18 -04:00
|
|
|
var boolTlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BOOLEAN, Length = 1, Value = new byte[] { 0x01 } };
|
|
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OBJECT | ASN1Type.V_ASN1_CONSTRUCTED, Length = 3, Value = new Data.Models.ASN1.TypeLengthValue[] { boolTlv } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidDataType()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OBJECT, Length: 1, Value: [INVALID DATA TYPE]";
|
2026-01-27 12:03:01 -05:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OBJECT, Length = 1, Value = false };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidLength()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_NULL, Length: 1, Value: [NO DATA]";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_NULL, Length = 1, Value = Array.Empty<byte>() };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidBooleanLength()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BOOLEAN, Length: 2 [Expected length of 1], Value: True";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BOOLEAN, Length = 2, Value = new byte[] { 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidBooleanArrayLength()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BOOLEAN, Length: 1 [Expected value length of 1], Value: True";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BOOLEAN, Length = 1, Value = new byte[] { 0x01, 0x00 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidBoolean()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BOOLEAN, Length: 1, Value: True";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BOOLEAN, Length = 1, Value = new byte[] { 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidInteger()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_INTEGER, Length: 1, Value: 1";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_INTEGER, Length = 1, Value = new byte[] { 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidBitString_NoBits()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BIT_STRING, Length: 1, Value with 0 unused bits";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BIT_STRING, Length = 1, Value = new byte[] { 0x00 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidBitString_Bits()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BIT_STRING, Length: 1, Value with 1 unused bits: 01";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BIT_STRING, Length = 1, Value = new byte[] { 0x01, 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidOctetString()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OCTET_STRING, Length: 1, Value: 01";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OCTET_STRING, Length = 1, Value = new byte[] { 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidObject()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OBJECT, Length: 3, Value: 0.1.2.3 (/ITU-T/1/2/3)";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OBJECT, Length = 3, Value = new byte[] { 0x01, 0x02, 0x03 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidUTF8String()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_UTF8STRING, Length: 3, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_UTF8STRING, Length = 3, Value = new byte[] { 0x41, 0x42, 0x43 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidPrintableString()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_PRINTABLESTRING, Length: 3, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_PRINTABLESTRING, Length = 3, Value = new byte[] { 0x41, 0x42, 0x43 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidTeletexString()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_TELETEXSTRING, Length: 3, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_TELETEXSTRING, Length = 3, Value = new byte[] { 0x41, 0x42, 0x43 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidIA5String()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_IA5STRING, Length: 3, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_IA5STRING, Length = 3, Value = new byte[] { 0x41, 0x42, 0x43 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_InvalidUTCTime()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_UTCTIME, Length: 3, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_UTCTIME, Length = 3, Value = new byte[] { 0x41, 0x42, 0x43 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidUTCTime()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_UTCTIME, Length: 3, Value: 1980-01-01 00:00:00";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_UTCTIME, Length = 3, Value = new byte[] { 0x31, 0x39, 0x38, 0x30, 0x2D, 0x30, 0x31, 0x2D, 0x30, 0x31, 0x20, 0x30, 0x30, 0x3A, 0x30, 0x30, 0x3A, 0x30, 0x30 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidBmpString()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_BMPSTRING, Length: 6, Value: ABC";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_BMPSTRING, Length = 6, Value = new byte[] { 0x41, 0x00, 0x42, 0x00, 0x43, 0x00 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Format_ValidUnformatted()
|
|
|
|
|
{
|
|
|
|
|
string expected = "Type: V_ASN1_OBJECT_DESCRIPTOR, Length: 1, Value: 01";
|
2025-09-26 13:06:18 -04:00
|
|
|
var tlv = new Data.Models.ASN1.TypeLengthValue { Type = ASN1Type.V_ASN1_OBJECT_DESCRIPTOR, Length = 1, Value = new byte[] { 0x01 } };
|
2025-09-24 08:25:11 -04:00
|
|
|
string actual = tlv.Format();
|
|
|
|
|
Assert.Equal(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2025-11-14 09:06:59 -05:00
|
|
|
}
|