OSDN Git Service

Microsoft.NET port: Fix Mono compatibility of unit tests.
[mhash384/mhash384.git] / ports / dotnet / test / MHash384Test.cs
1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Simple fast portable secure hashing library                                        */
3 /* Copyright(c) 2016-2020 LoRd_MuldeR <mulder2@gmx.de>                                            */
4 /*                                                                                                */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software  */
6 /* and associated documentation files (the "Software"), to deal in the Software without           */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish,     */
8 /* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the  */
9 /* Software is furnished to do so, subject to the following conditions:                           */
10 /*                                                                                                */
11 /* The above copyright notice and this permission notice shall be included in all copies or       */
12 /* substantial portions of the Software.                                                          */
13 /*                                                                                                */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     */
16 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        */
19 /* ---------------------------------------------------------------------------------------------- */
20
21 using System;
22 using System.Text;
23 using System.Diagnostics;
24
25 #if !__MonoCS__
26 using Microsoft.VisualStudio.TestTools.UnitTesting;
27 #else
28 using com.muldersoft.mhash384.test.mono_compat;
29 #endif
30
31 namespace com.muldersoft.mhash384.test
32 {
33     [TestClass]
34     public class MHash384Test
35     {
36         private static bool m_success;
37
38         [ClassInitialize]
39         public static void Initialize(TestContext testContext)
40         {
41             Trace.WriteLine(String.Format("MHash-384 v{0:D}.{1:D2}-{2:D}, Microsoft.NET v{3} [{4:D}-Bit]\n", MHash384.VERSION_MAJOR, MHash384.VERSION_MINOR, MHash384.VERSION_PATCH, System.Environment.Version, System.Environment.Is64BitProcess ? 64 : 32));
42             m_success = true;
43         }
44
45         [ClassCleanup]
46         public static void Cleanup()
47         {
48             if(m_success)
49             {
50                 Trace.WriteLine("\nSelf-test completed successfully :-)");
51             }
52             else
53             {
54                 Trace.WriteLine("\nError: Self-test has failed! :-(");
55             }
56         }
57
58         //=======================================================================
59         // TEST VECTORS
60         //=======================================================================
61
62         [TestMethod]
63         public void TestVector_01()
64         {
65             RunTest
66             (
67                 1, "",
68                 "4C4B82D07B368E1C22D0DE3759C32D44DA71BE6283E8550A2468DC1FEC38919F7EDB6C1BA08378EC583AE612AB0E02BA"
69             );
70         }
71
72         [TestMethod]
73         public void TestVector_02()
74         {
75             RunTest
76             (
77                 1, "abc",
78                 "9171D83EE7DEDE36CAF27C2644897F3114A0F67B6E9193AA1AB23462EA815EDEA535002671E086493B41A528A26FD8B3"
79             );
80         }
81
82         [TestMethod]
83         public void TestVector_03()
84         {
85             RunTest
86             (
87                 1, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
88                 "290BD2162C2105A0824172A8875EE33BB65A98DC0928100441B41B9399F6A8EA09794834504A3E817D49D29BC20A520A"
89             );
90         }
91
92         [TestMethod]
93         public void TestVector_04()
94         {
95             RunTest
96             (
97                 1, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
98                 "0B3F13A68AA8D8F0C5B9BF8BE5AECCB73E0D13732C9290006B6DC939ADA79C48AE362E545A067D2C1FB0749C60A49243"
99             );
100         }
101
102         [TestMethod]
103         public void TestVector_05()
104         {
105             RunTest
106             (
107                 0x186A0, "aaaaaaaaaa",
108                 "56228E9432471B09A7F696D0DEFA15E664D3E7ACD27E2D39F864C05006F1F77012F4F4CCE7450C52B6C1CFAB84FAEC63"
109             );
110         }
111
112         [TestMethod]
113         public void TestVector_06()
114         {
115             RunTest
116             (
117                 0x1000000, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
118                 "3A199A673FAB2900AB80FEC1185F79359FEC44B88728E3D62DC31A936C62DB05EF35716FED3074E9310DDDF69ED5671C"
119             );
120         }
121
122         [TestMethod]
123         public void TestVector_07()
124         {
125             RunTest
126             (
127                 1, "The quick brown fox jumps over the lazy dog",
128                 "79F76CA53D529162E632152EDE82A403F8F996DEAA009CC512250BAFF910AC24DF1381F7EF1F43DAC26F63EE0CFF3CDF"
129             );
130         }
131
132         [TestMethod]
133         public void TestVector_08()
134         {
135             RunTest
136             (
137                 1, "The quick brown fox jumps over the lazy cog",
138                 "8A2A58B20020F7700FFF629B0D7238D3D5311AC2A9ADA606E69AD7BEBF2B6258AEC74080DEC04AD59F3B9326121DFF66"
139             );
140         }
141
142         [TestMethod]
143         public void TestVector_09()
144         {
145             RunTest
146             (
147                 1, "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern",
148                 "D2E07EA37EF1E0E52BB704DEC3330C3378B943FE242CF3B08B93D18DBD61D4AB7C42E581DBFDBFD2F5D8EDF82C3B35D6"
149             );
150         }
151
152         [TestMethod]
153         public void TestVector_10()
154         {
155             RunTest
156             (
157                 1, "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern",
158                 "E97C790B194532A59BC84090B5C68C5B0D050C6FE937ABDF480CC19C345B72FEF925D83BF9B42D1A8F572ADE7A509FF9"
159             );
160         }
161
162         [TestMethod]
163         public void TestVector_11()
164         {
165             RunTest
166             (
167                 1, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
168                 "A772D7B984ABC790A9FFF51F3BD7C6A53844A233A564A970872C41345AFE19983B8D3CE30B900FD7FDD66CED03D0CD6E"
169             );
170         }
171
172         [TestMethod]
173         public void TestVector_12()
174         {
175             RunTest
176             (
177                 1, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamc0 laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
178                 "614A6B25BD673216EDEAB6A051A8B4869F9AD80CC5DD4AE629DDFB70CAA70E49D51E7027FF35A183A278FE97F8759CF9"
179             );
180         }
181
182         //=======================================================================
183         // HELPER FUNCTIONS
184         //=======================================================================
185
186         private static void RunTest(uint repetitions, string test, string expected)
187         {
188             string digest = GetDigest(repetitions, test);
189             bool matches = digest.Equals(expected, StringComparison.OrdinalIgnoreCase);
190             m_success &= matches;
191             Trace.WriteLine(String.Format("{0} - {1}", digest, matches ? "OK" : "Error!"));
192             if(!matches)
193             {
194                 Assert.Fail("Computed digest does *not* match reference!");
195             }
196         }
197
198         private static string GetDigest(uint repetitions, string test)
199         {
200             MHash384 hash = new MHash384();
201             for(uint i = 0; i < repetitions; ++i)
202             {
203                 hash.Update(test);
204             }
205             return ToHexString(hash.GetDigest());
206         }
207
208         private static string ToHexString(byte[] bytes)
209         {
210             StringBuilder sb = new StringBuilder(bytes.Length * 2);
211             foreach (byte b in bytes)
212             {
213                 sb.AppendFormat("{0:X2}", b);
214             }
215             return sb.ToString();
216         }
217     }
218 }