Skip to content

Commit

Permalink
Added tests for HMAC algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
paolo-rossi committed Jan 16, 2020
1 parent a441e74 commit 48c6128
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions Tests/Source/JOSE.Tests.Common.pas
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ interface
[TestFixture]
TTestJOSEBytes = class
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;

[Test]
[TestCase('TestImplicit', 'aBc')]
[TestCase('TestImplicitEmptyString', '')]
Expand Down Expand Up @@ -77,15 +72,9 @@ TTestJOSEBytes = class
procedure TestMerge(const AValue1, AValue2, _Result: string);
end;


[TestFixture]
TTestBase64 = class
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;

[Test]
[TestCase('TestEncodeString', 'paolo,cGFvbG8=')]
procedure TestEncodeString(const AValue1, _Expected: string);
Expand All @@ -98,24 +87,20 @@ TTestBase64 = class
[TestFixture]
TTestHMAC = class
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
end;

implementation

{ TTestBase64 }
[Test]
[TestCase('TestSignSHA256', 'plaintext,secret,XXv4q83DfQItSR7PCiZwWFlG10ah668c1cRsrKh6Ylg=')]
procedure TestSignSHA256(const AValue1, AValue2, _Expected: string);

procedure TTestBase64.Setup;
begin
[Test]
[TestCase('TestSignSHA384', 'plaintext,secret,u0uk1bjmjw3CqOTwcgWrbrvcoCQMGE8LQCuT8SfxJRXedF3PvGw4FWsoZvPtIl3B')]
procedure TestSignSHA384(const AValue1, AValue2, _Expected: string);

end;
[Test]
[TestCase('TestSignSHA512', 'plaintext,secret,3/adkEcz1vmOINXEEOxdtM119BDAnKgvIJyr7IxLpdQsaUWLu9vu1Wz4veWeKz7wrkKTzySUGFj6'#$D#$A'/rDBrJzRuQ==')]
procedure TestSignSHA512(const AValue1, AValue2, _Expected: string);
end;

procedure TTestBase64.TearDown;
begin
end;
implementation

procedure TTestBase64.TestDecodeString(const AValue1, _Expected: string);
begin
Expand All @@ -127,26 +112,28 @@ procedure TTestBase64.TestEncodeString(const AValue1, _Expected: string);
Assert.AreEqual(_Expected, TBase64.Encode(AValue1).AsString);
end;

procedure TTestHMAC.Setup;
procedure TTestHMAC.TestSignSHA256(const AValue1, AValue2, _Expected: string);
var
LSignature: TJOSEBytes;
begin

LSignature := THMAC.Sign(TEncoding.ANSI.GetBytes(AValue1), TEncoding.ANSI.GetBytes(AValue2), THMACAlgorithm.SHA256);
Assert.AreEqual(_Expected, TBase64.Encode(LSignature).AsString);
end;

procedure TTestHMAC.TearDown;
begin

end;

{ TTestJOSEBytes }

procedure TTestJOSEBytes.Setup;
procedure TTestHMAC.TestSignSHA384(const AValue1, AValue2, _Expected: string);
var
LSignature: TJOSEBytes;
begin

LSignature := THMAC.Sign(TEncoding.ANSI.GetBytes(AValue1), TEncoding.ANSI.GetBytes(AValue2), THMACAlgorithm.SHA384);
Assert.AreEqual(_Expected, TBase64.Encode(LSignature).AsString);
end;

procedure TTestJOSEBytes.TearDown;
procedure TTestHMAC.TestSignSHA512(const AValue1, AValue2, _Expected: string);
var
LSignature: TJOSEBytes;
begin

LSignature := THMAC.Sign(TEncoding.ANSI.GetBytes(AValue1), TEncoding.ANSI.GetBytes(AValue2), THMACAlgorithm.SHA512);
Assert.AreEqual(_Expected, TBase64.Encode(LSignature).AsString);
end;

procedure TTestJOSEBytes.TestAdd(const AValue1, AValue2, _Result: string);
Expand Down

0 comments on commit 48c6128

Please sign in to comment.