Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.Mobile.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ uses
Asn1IntegerTests in '..\src\Asn1\Asn1IntegerTests.pas',
Asn1GeneratorTests in '..\src\Asn1\Asn1GeneratorTests.pas',
Asn1SequenceParserTests in '..\src\Asn1\Asn1SequenceParserTests.pas',
Asn1SequenceCursorTests in '..\src\Asn1\Asn1SequenceCursorTests.pas',
Asn1TimeFormatTests in '..\src\Asn1\Asn1TimeFormatTests.pas',
BitStringTests in '..\src\Asn1\BitStringTests.pas',
DerUtf8StringTests in '..\src\Asn1\DerUtf8StringTests.pas',
Expand Down Expand Up @@ -864,6 +865,7 @@ uses
CtrDrbgTests in '..\src\Rngs\Drbg\CtrDrbgTests.pas',
//FixedPointTests in '..\src\Math\EC\FixedPointTests.pas',
AESTests in '..\src\Crypto\AESTests.pas',
BlockCipherTestBase in '..\src\Crypto\BlockCipherTestBase.pas',
AesBlockCipherTestBase in '..\src\Crypto\AesBlockCipherTestBase.pas',
AesLightTests in '..\src\Crypto\AesLightTests.pas',
FusedExternalRegistrationTests in '..\src\Crypto\FusedExternalRegistrationTests.pas',
Expand Down
2 changes: 2 additions & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.Mobile.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@
<DCCReference Include="..\src\Asn1\Asn1IntegerTests.pas"/>
<DCCReference Include="..\src\Asn1\Asn1GeneratorTests.pas"/>
<DCCReference Include="..\src\Asn1\Asn1SequenceParserTests.pas"/>
<DCCReference Include="..\src\Asn1\Asn1SequenceCursorTests.pas"/>
<DCCReference Include="..\src\Asn1\Asn1TimeFormatTests.pas"/>
<DCCReference Include="..\src\Asn1\BitStringTests.pas"/>
<DCCReference Include="..\src\Asn1\DerUtf8StringTests.pas"/>
Expand Down Expand Up @@ -1203,6 +1204,7 @@
<DCCReference Include="..\src\Rngs\Drbg\HMacDrbgTests.pas"/>
<DCCReference Include="..\src\Rngs\Drbg\CtrDrbgTests.pas"/>
<DCCReference Include="..\src\Crypto\AESTests.pas"/>
<DCCReference Include="..\src\Crypto\BlockCipherTestBase.pas"/>
<DCCReference Include="..\src\Crypto\AesBlockCipherTestBase.pas"/>
<DCCReference Include="..\src\Crypto\AesLightTests.pas"/>
<DCCReference Include="..\src\Crypto\FusedExternalRegistrationTests.pas"/>
Expand Down
2 changes: 2 additions & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ uses
Asn1IntegerTests in '..\src\Asn1\Asn1IntegerTests.pas',
Asn1GeneratorTests in '..\src\Asn1\Asn1GeneratorTests.pas',
Asn1SequenceParserTests in '..\src\Asn1\Asn1SequenceParserTests.pas',
Asn1SequenceCursorTests in '..\src\Asn1\Asn1SequenceCursorTests.pas',
Asn1TimeFormatTests in '..\src\Asn1\Asn1TimeFormatTests.pas',
BitStringTests in '..\src\Asn1\BitStringTests.pas',
DerUtf8StringTests in '..\src\Asn1\DerUtf8StringTests.pas',
Expand Down Expand Up @@ -883,6 +884,7 @@ uses
DigestRandomNumberTests in '..\src\Crypto\DigestRandomNumberTests.pas',
FixedPointTests in '..\src\Math\EC\FixedPointTests.pas',
AESTests in '..\src\Crypto\AESTests.pas',
BlockCipherTestBase in '..\src\Crypto\BlockCipherTestBase.pas',
AesBlockCipherTestBase in '..\src\Crypto\AesBlockCipherTestBase.pas',
AesLightTests in '..\src\Crypto\AesLightTests.pas',
FusedExternalRegistrationTests in '..\src\Crypto\FusedExternalRegistrationTests.pas',
Expand Down
10 changes: 9 additions & 1 deletion CryptoLib.Tests/FreePascal.Tests/CryptoLib.Tests.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<PackageName Value="FCL"/>
</Item4>
</RequiredPackages>
<Units Count="189">
<Units Count="191">
<Unit0>
<Filename Value="CryptoLib.lpr"/>
<IsPartOfProject Value="True"/>
Expand Down Expand Up @@ -837,6 +837,14 @@
<Filename Value="..\src\Asn1\Asn1StreamReadTests.pas"/>
<IsPartOfProject Value="True"/>
</Unit188>
<Unit189>
<Filename Value="..\src\Asn1\Asn1SequenceCursorTests.pas"/>
<IsPartOfProject Value="True"/>
</Unit189>
<Unit190>
<Filename Value="..\src\Crypto\BlockCipherTestBase.pas"/>
<IsPartOfProject Value="True"/>
</Unit190>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand Down
4 changes: 3 additions & 1 deletion CryptoLib.Tests/FreePascal.Tests/CryptoLib.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

uses
Interfaces, Forms, GuiTestRunner, Asn1GeneratorTests, Asn1SequenceParserTests,
Asn1SequenceCursorTests,
Asn1TimeFormatTests, EqualsAndHashCodeTests, OIDTests, EnumeratedTests,
ExternalTests, ParsingTests, ParseTests, StringTests, TagTests,
BigIntegerTests, ECAlgorithmsTests, ECPointTests, BinPolyTests,
Expand All @@ -12,7 +13,8 @@
DigestRandomNumberTests, FixedPointTests, AESTests, AesLightTests,
FusedExternalRegistrationTests, AesHardwareEngineTests, AESSICTests,
SicBulkParityTests, EcbBulkParityTests, CbcBulkParityTests,
GcmSivBulkParityTests, SpeckBlockCipherTestBase, SpeckLegacyTests, SpeckTests,
GcmSivBulkParityTests, BlockCipherTestBase, SpeckBlockCipherTestBase,
SpeckLegacyTests, SpeckTests,
IESCipherTests, HMacTests, Pkcs5Tests, HkdfGeneratorTests, ECIESTests,
PascalCoinECIESTests, ECNRTests, PrimesTests, ECEncodingTests, GF256AesTests,
PaddingTests, DSATests, DeterministicDsaTests, Salsa20Tests, XSalsa20Tests,
Expand Down
10 changes: 9 additions & 1 deletion CryptoLib.Tests/FreePascal.Tests/CryptoLibConsole.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageName Value="FCL"/>
</Item2>
</RequiredPackages>
<Units Count="189">
<Units Count="191">
<Unit0>
<Filename Value="CryptoLibConsole.lpr"/>
<IsPartOfProject Value="True"/>
Expand Down Expand Up @@ -796,6 +796,14 @@
<Filename Value="..\src\Asn1\Asn1StreamReadTests.pas"/>
<IsPartOfProject Value="True"/>
</Unit188>
<Unit189>
<Filename Value="..\src\Asn1\Asn1SequenceCursorTests.pas"/>
<IsPartOfProject Value="True"/>
</Unit189>
<Unit190>
<Filename Value="..\src\Crypto\BlockCipherTestBase.pas"/>
<IsPartOfProject Value="True"/>
</Unit190>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand Down
4 changes: 3 additions & 1 deletion CryptoLib.Tests/FreePascal.Tests/CryptoLibConsole.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
uses
{$IFDEF UNIX}cwstring,{$ENDIF}
consoletestrunner, Asn1GeneratorTests, Asn1SequenceParserTests,
Asn1SequenceCursorTests,
Asn1TimeFormatTests, EqualsAndHashCodeTests, OIDTests, EnumeratedTests,
ExternalTests, ParsingTests, ParseTests, StringTests, TagTests,
BigIntegerTests, ECAlgorithmsTests, ECPointTests, BinPolyTests,
Expand All @@ -13,7 +14,8 @@
DigestRandomNumberTests, FixedPointTests, AESTests, AesLightTests,
FusedExternalRegistrationTests, AesHardwareEngineTests, AESSICTests,
SicBulkParityTests, EcbBulkParityTests, CbcBulkParityTests,
GcmSivBulkParityTests, SpeckBlockCipherTestBase, SpeckLegacyTests, SpeckTests,
GcmSivBulkParityTests, BlockCipherTestBase, SpeckBlockCipherTestBase,
SpeckLegacyTests, SpeckTests,
IESCipherTests, HMacTests, Pkcs5Tests, HkdfGeneratorTests, ECIESTests,
PascalCoinECIESTests, ECNRTests, PrimesTests, ECEncodingTests, GF256AesTests,
PaddingTests, DSATests, DeterministicDsaTests, Salsa20Tests, XSalsa20Tests,
Expand Down
232 changes: 232 additions & 0 deletions CryptoLib.Tests/src/Asn1/Asn1SequenceCursorTests.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{ *********************************************************************************** }
{ * CryptoLib Library * }
{ * Author - Ugochukwu Mmaduekwe * }
{ * Github Repository <https://github.com/Xor-el> * }
{ * * }
{ * Distributed under the MIT software license, see the accompanying file LICENSE * }
{ * or visit http://www.opensource.org/licenses/mit-license.php. * }
{ * * }
{ * Acknowledgements: * }
{ * * }
{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
{ * the development of this library * }
{ * ******************************************************************************* * }

(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)

unit Asn1SequenceCursorTests;

interface

{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF FPC}

uses
Classes,
SysUtils,
{$IFDEF FPC}
fpcunit,
testregistry,
{$ELSE}
TestFramework,
{$ENDIF FPC}
ClpAsn1Objects,
ClpIAsn1Objects,
ClpIAsn1Core,
ClpAsn1Utilities,
ClpCryptoLibTypes,
CryptoLibTestBase;

type

/// <summary>
/// Direct edge-case tests for the TAsn1Utilities sequence cursor helpers
/// (Read / ReadOptional / ReadContextTagged / CheckSequenceSize / RequireEndOfSequence).
/// </summary>
TTestAsn1SequenceCursor = class(TCryptoLibAlgorithmTestCase)
private
function IntSeq(const AValues: array of Int32): IAsn1Sequence;
published
procedure TestReadMandatoryAndRequireEnd;
procedure TestCheckSequenceSizeRejectsWrongSize;
procedure TestCheckSequenceSizeRejectsNil;
procedure TestReadRejectsPrematureEnd;
procedure TestRequireEndRejectsTrailing;
procedure TestReadEncodableReturnsRawElement;
procedure TestReadOptionalPresentAdvances;
procedure TestReadOptionalAbsentDoesNotAdvance;
procedure TestReadContextTaggedExplicit;
end;

implementation

function TTestAsn1SequenceCursor.IntSeq(const AValues: array of Int32): IAsn1Sequence;
var
LElements: TCryptoLibGenericArray<IAsn1Encodable>;
LI: Int32;
begin
LElements := nil;
System.SetLength(LElements, System.Length(AValues));
for LI := 0 to System.Length(AValues) - 1 do
LElements[LI] := TDerInteger.ValueOf(AValues[LI]);
Result := TDerSequence.FromElements(LElements);
end;

procedure TTestAsn1SequenceCursor.TestReadMandatoryAndRequireEnd;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LA, LB: IDerInteger;
begin
LSeq := IntSeq([7, 11]);
LPos := 0;
TAsn1Utilities.CheckSequenceSize(LSeq, 2, 2);
LA := TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
LB := TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
TAsn1Utilities.RequireEndOfSequence(LSeq, LPos);
CheckTrue(LA.HasValue(7), 'first element');
CheckTrue(LB.HasValue(11), 'second element');
CheckEquals(2, LPos, 'position after two reads');
end;

procedure TTestAsn1SequenceCursor.TestCheckSequenceSizeRejectsWrongSize;
var
LRaised: Boolean;
begin
LRaised := False;
try
TAsn1Utilities.CheckSequenceSize(IntSeq([1, 2, 3]), 2, 2);
except
on E: EArgumentCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'expected bad-sequence-size exception');
end;

procedure TTestAsn1SequenceCursor.TestCheckSequenceSizeRejectsNil;
var
LSeq: IAsn1Sequence;
LRaised: Boolean;
begin
LSeq := nil;
LRaised := False;
try
TAsn1Utilities.CheckSequenceSize(LSeq, 0, 3);
except
on E: EArgumentNilCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'expected nil-sequence exception');
end;

procedure TTestAsn1SequenceCursor.TestReadRejectsPrematureEnd;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LRaised: Boolean;
begin
LSeq := IntSeq([1]);
LPos := 0;
TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
LRaised := False;
try
TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
except
on E: EArgumentCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'expected premature-end exception');
end;

procedure TTestAsn1SequenceCursor.TestRequireEndRejectsTrailing;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LRaised: Boolean;
begin
LSeq := IntSeq([1, 2]);
LPos := 0;
TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
LRaised := False;
try
TAsn1Utilities.RequireEndOfSequence(LSeq, LPos);
except
on E: EArgumentCryptoLibException do
LRaised := True;
end;
CheckTrue(LRaised, 'expected unexpected-elements exception');
end;

procedure TTestAsn1SequenceCursor.TestReadEncodableReturnsRawElement;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LElement: IAsn1Encodable;
begin
LSeq := IntSeq([42]);
LPos := 0;
LElement := TAsn1Utilities.ReadEncodable(LSeq, LPos);
CheckNotNull(LElement, 'raw element');
CheckEquals(1, LPos, 'position advanced');
CheckTrue(TDerInteger.GetInstance(LElement).HasValue(42), 'element value');
end;

procedure TTestAsn1SequenceCursor.TestReadOptionalPresentAdvances;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LValue: IDerInteger;
begin
LSeq := IntSeq([9]);
LPos := 0;
LValue := TAsn1Utilities.ReadOptional<IDerInteger>(LSeq, LPos, TDerInteger.GetOptional);
CheckNotNull(LValue, 'optional present');
CheckTrue(LValue.HasValue(9), 'optional value');
CheckEquals(1, LPos, 'position advanced');
end;

procedure TTestAsn1SequenceCursor.TestReadOptionalAbsentDoesNotAdvance;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LBits: IDerBitString;
LInt: IDerInteger;
begin
// sequence holds a DerInteger; an optional DerBitString read must not consume it
LSeq := IntSeq([5]);
LPos := 0;
LBits := TAsn1Utilities.ReadOptional<IDerBitString>(LSeq, LPos, TDerBitString.GetOptional);
CheckNull(LBits, 'optional absent');
CheckEquals(0, LPos, 'position not advanced');
LInt := TAsn1Utilities.Read<IDerInteger>(LSeq, LPos, TDerInteger.GetInstance);
CheckTrue(LInt.HasValue(5), 'element still readable');
CheckEquals(1, LPos, 'position advanced after mandatory read');
end;

procedure TTestAsn1SequenceCursor.TestReadContextTaggedExplicit;
var
LSeq: IAsn1Sequence;
LPos: Int32;
LValue: IDerInteger;
begin
LSeq := TDerSequence.FromElement(TDerTaggedObject.Create(True, 0, TDerInteger.ValueOf(5)) as IDerTaggedObject);
LPos := 0;
LValue := TAsn1Utilities.ReadContextTagged<IDerInteger>(LSeq, LPos, 0, True, TDerInteger.GetTagged);
TAsn1Utilities.RequireEndOfSequence(LSeq, LPos);
CheckNotNull(LValue, 'context-tagged value');
CheckTrue(LValue.HasValue(5), 'context-tagged value content');
CheckEquals(1, LPos, 'position advanced');
end;

initialization

// Register any test cases with the test runner

{$IFDEF FPC}
RegisterTest(TTestAsn1SequenceCursor);
{$ELSE}
RegisterTest(TTestAsn1SequenceCursor.Suite);
{$ENDIF FPC}

end.
2 changes: 1 addition & 1 deletion CryptoLib.Tests/src/Asn1/Asn1StreamReadTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function TAsn1StreamReadTest.BuildBerIndefiniteConstructedBitString

procedure TAsn1StreamReadTest.TestConstructedOctetReadAllRoundTrip;
var
LPayload, LEncoding, LActual: TCryptoLibByteArray;
LPayload, LEncoding: TCryptoLibByteArray;
LParser: IAsn1StreamParser;
LOctets: IBerOctetString;
LI: Int32;
Expand Down
Loading
Loading