|
| 1 | +{***************************************************************************** |
| 2 | + The DEC team (see file NOTICE.txt) licenses this file |
| 3 | + to you under the Apache License, Version 2.0 (the |
| 4 | + "License"); you may not use this file except in compliance |
| 5 | + with the License. A copy of this licence is found in the root directory of |
| 6 | + this project in the file LICENCE.txt or alternatively at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, |
| 11 | + software distributed under the License is distributed on an |
| 12 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | + KIND, either express or implied. See the License for the |
| 14 | + specific language governing permissions and limitations |
| 15 | + under the License. |
| 16 | +*****************************************************************************} |
| 17 | + |
| 18 | +{$M+} // DUnitX would add it anyway |
| 19 | +unit TestDECZIPHelper; |
| 20 | + |
| 21 | +interface |
| 22 | + |
| 23 | +// Needs to be included before any other statements |
| 24 | +{$INCLUDE TestDefines.inc} |
| 25 | + |
| 26 | +uses |
| 27 | + System.SysUtils, System.Classes, |
| 28 | + {$IFDEF DUnitX} |
| 29 | + DUnitX.TestFramework,DUnitX.DUnitCompatibility, |
| 30 | + {$ELSE} |
| 31 | + TestFramework, |
| 32 | + {$ENDIF} |
| 33 | + DECUtil; |
| 34 | + |
| 35 | +type |
| 36 | + /// <summary> |
| 37 | + /// Test cases for the various helper functions |
| 38 | + /// </summary> |
| 39 | + TestZIPHelpers = class(TTestCase) |
| 40 | + strict private |
| 41 | + /// <summary> |
| 42 | + /// Perform a signle algorithm create test |
| 43 | + /// </summary> |
| 44 | + /// <param name="AlgorithmID"> |
| 45 | + /// ID of the algorithm instance to create |
| 46 | + /// </param> |
| 47 | + /// <param name="Name"> |
| 48 | + /// Short class name of the instance to be created, must match to pass |
| 49 | + /// </param> |
| 50 | + procedure DoTestCreateZIPCryptoAlgorithmInstance(AlgorithmID: UInt16; |
| 51 | + const Name : string); |
| 52 | + /// <summary> |
| 53 | + /// Test for the "unknown" algorithm ID as parameter |
| 54 | + /// </summary> |
| 55 | + procedure DoTestCreateZIPUnknownCryptoAlgorithmException; |
| 56 | + /// <summary> |
| 57 | + /// Test for an arbitrary unknown algorithm ID as parameter |
| 58 | + /// </summary> |
| 59 | + procedure DoTestCreateZIPUnknCryptoAlgorithmException; |
| 60 | + published |
| 61 | + procedure TestCreateZIPCryptoAlgorithmInstance; |
| 62 | + procedure TestCreateZIPUnknownCryptoAlgorithmInstanceException; |
| 63 | + procedure TestCreateZIPUnknCryptoAlgorithmInstanceException; |
| 64 | + end; |
| 65 | + |
| 66 | +implementation |
| 67 | + |
| 68 | +uses |
| 69 | + DECTypes, |
| 70 | + DECCipherFormats, |
| 71 | + DECZIPHelper; |
| 72 | + |
| 73 | +procedure TestZIPHelpers.DoTestCreateZIPUnknCryptoAlgorithmException; |
| 74 | +var |
| 75 | + Instance : TDECFormattedCipher; |
| 76 | +begin |
| 77 | + Instance := CreateZIPCryptoAlgorithmInstance($1000); |
| 78 | + Instance.Free; // Should not be reached but suppresses compiler warning |
| 79 | +end; |
| 80 | + |
| 81 | +procedure TestZIPHelpers.DoTestCreateZIPUnknownCryptoAlgorithmException; |
| 82 | +var |
| 83 | + Instance : TDECFormattedCipher; |
| 84 | +begin |
| 85 | + Instance := CreateZIPCryptoAlgorithmInstance($FFFF); |
| 86 | + Instance.Free; // Should not be reached but suppresses compiler warning |
| 87 | +end; |
| 88 | + |
| 89 | +procedure TestZIPHelpers.DoTestCreateZIPCryptoAlgorithmInstance(AlgorithmID: UInt16; |
| 90 | + const Name: string); |
| 91 | +var |
| 92 | + Instance : TDECFormattedCipher; |
| 93 | +begin |
| 94 | + Instance := CreateZIPCryptoAlgorithmInstance(AlgorithmID); |
| 95 | + try |
| 96 | + CheckEquals(Name, Instance.GetShortClassName); |
| 97 | + finally |
| 98 | + Instance.Free; |
| 99 | + end; |
| 100 | +end; |
| 101 | + |
| 102 | +procedure TestZIPHelpers.TestCreateZIPCryptoAlgorithmInstance; |
| 103 | +begin |
| 104 | + DoTestCreateZIPCryptoAlgorithmInstance($6601, '1DES'); |
| 105 | + DoTestCreateZIPCryptoAlgorithmInstance($6603, '3DES'); |
| 106 | + DoTestCreateZIPCryptoAlgorithmInstance($6609, '2DES'); |
| 107 | + DoTestCreateZIPCryptoAlgorithmInstance($660E, 'AES128'); |
| 108 | + DoTestCreateZIPCryptoAlgorithmInstance($660F, 'AES192'); |
| 109 | + DoTestCreateZIPCryptoAlgorithmInstance($6610, 'AES256'); |
| 110 | + DoTestCreateZIPCryptoAlgorithmInstance($6702, 'RC2'); |
| 111 | + DoTestCreateZIPCryptoAlgorithmInstance($6720, 'Blowfish'); |
| 112 | + DoTestCreateZIPCryptoAlgorithmInstance($6721, 'Twofish'); |
| 113 | + DoTestCreateZIPCryptoAlgorithmInstance($6801, 'RC4'); |
| 114 | + |
| 115 | +// $6602 : Result := TCipher_RC2.Create; // (version needed to extract < 5.2) |
| 116 | +// // This has to do with a faulty RC2 |
| 117 | +// // implementation in XP SP1 and earlier |
| 118 | +// // Unsupported as we do not know the |
| 119 | +// // details of the fault |
| 120 | +end; |
| 121 | + |
| 122 | +procedure TestZIPHelpers.TestCreateZIPUnknCryptoAlgorithmInstanceException; |
| 123 | +begin |
| 124 | + CheckException(DoTestCreateZIPUnknCryptoAlgorithmException, EDECClassNotRegisteredException); |
| 125 | +end; |
| 126 | + |
| 127 | +procedure TestZIPHelpers.TestCreateZIPUnknownCryptoAlgorithmInstanceException; |
| 128 | +begin |
| 129 | + CheckException(DoTestCreateZIPUnknownCryptoAlgorithmException, EDECClassNotRegisteredException); |
| 130 | +end; |
| 131 | + |
| 132 | +initialization |
| 133 | + // Register any test cases with the test runner |
| 134 | + {$IFDEF DUnitX} |
| 135 | + TDUnitX.RegisterTestFixture(TestZIPHelpers); |
| 136 | + {$ELSE} |
| 137 | + RegisterTest('DECZIPHelper', TestZIPHelpers.Suite); |
| 138 | + {$ENDIF} |
| 139 | +end. |
0 commit comments