-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMD5UnitTest.pas
130 lines (86 loc) · 5.21 KB
/
MD5UnitTest.pas
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
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
TMD5UnitTest = class(TAbstractUnitTest)
private
Buffer: Pointer;
BufferLen: Integer;
public
constructor Create;
procedure ExecuteTest; override;
destructor Destroy; override;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
constructor TMD5UnitTest.Create;
begin
inherited Create;
Self.Buffer := TMemoryManager.GetMemory(MAX_DNS_PACKET_LEN);
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
procedure TMD5UnitTest.ExecuteTest;
var
TimeStamp: TDateTime; InitSeed: Integer; i, j: Integer; MD5Digest: TMD5Digest;
begin
TimeStamp := Now;
InitSeed := Round(Frac(TimeStamp) * 8640000.0);
TTracer.Trace(TracePriorityInfo, Self.ClassName + ': Testing vectors...');
Move('abc', Self.Buffer^, 3); MD5Digest := TMD5.Compute(Self.Buffer, 3);
if (MD5Digest[0] <> $98500190) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $B04FD23C) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $7D3F96D6) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $727FE128) then raise FailedUnitTestException.Create;
Move('xyz', Self.Buffer^, 3); MD5Digest := TMD5.Compute(Self.Buffer, 3);
if (MD5Digest[0] <> $6FB36FD1) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $78F81109) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $61138c99) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $5E70AF91) then raise FailedUnitTestException.Create;
Move('1234567890', Self.Buffer^, 10); MD5Digest := TMD5.Compute(Self.Buffer, 10);
if (MD5Digest[0] <> $FCF107E8) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $2F132DF8) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $CA18B09B) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $9FA13867) then raise FailedUnitTestException.Create;
Move('The quick brown fox jumps over the lazy dog', Self.Buffer^, 43); MD5Digest := TMD5.Compute(Self.Buffer, 43);
if (MD5Digest[0] <> $9D7D109E) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $82B62B37) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $351DD86B) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $D619A442) then raise FailedUnitTestException.Create;
Move('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', Self.Buffer^, 56); MD5Digest := TMD5.Compute(Self.Buffer, 56);
if (MD5Digest[0] <> $07EF1582) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $CA0BA296) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $D316E1AA) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $4A666C87) then raise FailedUnitTestException.Create;
Move('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in ultrices nulla, sed scelerisque tortor. In hendrerit semper dui interdum lobortis. Nullam aliquet pulvinar ligula sed porta. Duis vitae arcu a augue dignissim venenatis.', Self.Buffer^, 236); MD5Digest := TMD5.Compute(Self.Buffer, 236);
if (MD5Digest[0] <> $C76BB2F4) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $8F188FE1) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $F2924EB7) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $ACDF3470) then raise FailedUnitTestException.Create;
Move('Sed dapibus posuere vestibulum. Vivamus sed mollis tellus. Pellentesque ultrices convallis nisl, non congue arcu gravida vitae. Suspendisse tristique dapibus mi ullamcorper tristique. Nam mattis vestibulum quam, nec vehicula felis blandit eu.', Self.Buffer^, 242); MD5Digest := TMD5.Compute(Self.Buffer, 242);
if (MD5Digest[0] <> $6C98C911) then raise FailedUnitTestException.Create;
if (MD5Digest[1] <> $E4C6933E) then raise FailedUnitTestException.Create;
if (MD5Digest[2] <> $5EA02B94) then raise FailedUnitTestException.Create;
if (MD5Digest[3] <> $23B408E5) then raise FailedUnitTestException.Create;
TTracer.Trace(TracePriorityInfo, Self.ClassName + ': Done.');
TTracer.Trace(TracePriorityInfo, Self.ClassName + ': Calculating hashes...');
RandSeed := InitSeed;
for i := 1 to 1000000 do begin
BufferLen := Random(MAX_DNS_PACKET_LEN - MIN_DNS_PACKET_LEN + 1) + MIN_DNS_PACKET_LEN; for j := 0 to (BufferLen - 1) do PByteArray(Buffer)^[j] := Random(256);
MD5Digest := TMD5.Compute(Self.Buffer, Self.BufferLen);
end;
TTracer.Trace(TracePriorityInfo, Self.ClassName + ': Done.');
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
destructor TMD5UnitTest.Destroy;
begin
TMemoryManager.FreeMemory(Buffer, MAX_DNS_PACKET_LEN);
inherited Destroy;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------