OSDN Git Service

Makefile: Make it possible to specify a custom 'strip' and 'windres' command.
[mhash384/mhash384.git] / ports / c-sharp / test / MHash384Test.cs
1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Simple fast portable secure hashing library                                        */
3 /* Copyright(c) 2016-2019 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 Microsoft.VisualStudio.TestTools.UnitTesting;
24 using System.Diagnostics;
25
26 namespace com.muldersoft.mhash384.test
27 {
28     [TestClass]
29     public class MHash384Test
30     {
31         private static bool m_success;
32
33         [ClassInitialize()]
34         public static void Initialize(TestContext testContext)
35         {
36             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));
37             m_success = true;
38         }
39
40         [ClassCleanup()]
41         public static void Cleanup()
42         {
43             if(m_success)
44             {
45                 Trace.WriteLine("\nSelf-test completed successfully :-)");
46             }
47             else
48             {
49                 Trace.WriteLine("\nError: Self-test has failed! :-(");
50             }
51         }
52
53         //=======================================================================
54         // TEST VECTORS
55         //=======================================================================
56
57         [TestMethod]
58         public void TestVector_01()
59         {
60             RunTest
61             (
62                 1, "",
63                 "D629CB37F76156621E09C93B8D1029C5F0F7B778DE5BD9578230673FE7FA4C45A2308AB4E9D99AA0EA194CBF6230C202"
64             );
65         }
66
67         [TestMethod]
68         public void TestVector_02()
69         {
70             RunTest
71             (
72                 1, "abc",
73                 "1D7B1593D7AA4DD7DC8DA6D6BD591D8B507CA514E1C7EC37F43E78DD6B83253C355403F61009582890480C4CD527F405"
74             );
75         }
76
77         [TestMethod]
78         public void TestVector_03()
79         {
80             RunTest
81             (
82                 1, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
83                 "9B98CE4C3F4EA9018E1ED3034717E3E5D8DA20AB375BAFE372D07780FFDEFDA32BA3C822155E2883E333AFCAAEAFA614"
84             );
85         }
86
87         [TestMethod]
88         public void TestVector_04()
89         {
90             RunTest
91             (
92                 1, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
93                 "45416D1FFD44D3940C5AF5B425B5DA07FD8C06BAB7C58CD13389BC284650ED6090A9A1952C29FEB1B2C6AFBF5F996108"
94             );
95         }
96
97         [TestMethod]
98         public void TestVector_05()
99         {
100             RunTest
101             (
102                 0x186A0, "aaaaaaaaaa",
103                 "20774C570AE2E6A49A01DAE948672AE94D465D9167130FE87BF24148649CF121E8A50759507BF97134F0D28CEE91DCA2"
104             );
105         }
106
107         [TestMethod]
108         public void TestVector_06()
109         {
110             RunTest
111             (
112                 0x1000000, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
113                 "C18843D4DD9E0479D38A55E8C75E2201F0E93B6EF1F916EBAF3C4074D188945C7B9657F45269A1DAAB4A8FB6F63D0BEE"
114             );
115         }
116
117         [TestMethod]
118         public void TestVector_07()
119         {
120             RunTest
121             (
122                 1, "The quick brown fox jumps over the lazy dog",
123                 "E2901EDB76AA83F2D52FEA9A34EB2BC05EDFA9571C812620245BBEBA95FA2D82CBB5BAD93EB64D4B72B46635D565400B"
124             );
125         }
126
127         [TestMethod]
128         public void TestVector_08()
129         {
130             RunTest
131             (
132                 1, "The quick brown fox jumps over the lazy cog",
133                 "8B81CD9C1DEE8BCC642F8B1EA982CE79D15F50E66939FF34294E63D93AE3C610F9464FEFEF30297409B62231F028ABC0"
134             );
135         }
136
137         [TestMethod]
138         public void TestVector_09()
139         {
140             RunTest
141             (
142                 1, "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern",
143                 "A545E6319F6ACE0CEF62BCAAF5D488858751B96C2A488D5BE6EC63D50006C9D54DAD66168488F03B4A868D3BA610CCF8"
144             );
145         }
146
147         [TestMethod]
148         public void TestVector_10()
149         {
150             RunTest
151             (
152                 1, "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern",
153                 "2BECB89DD55FC9F2F3384DF61D6240DDA54FC275D186A45A883C03AA873DDAF4FAD4744212B92D26054B92852DEA5F1D"
154             );
155         }
156
157         [TestMethod]
158         public void TestVector_11()
159         {
160             RunTest
161             (
162                 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.",
163                 "6283CE91DA8AF5091C7B06E1352DC947B9E3B89614807C04C43DA322321FBADAD3CCCABECB5C1B1B265432A6039121E1"
164             );
165         }
166
167         [TestMethod]
168         public void TestVector_12()
169         {
170             RunTest
171             (
172                 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.",
173                 "5DCB7C4B27BA841688FB6A860E97A45256917533F63448CF666781EA4B5FAB20E1C746C8821099A915222E0D77624098"
174             );
175         }
176
177         //=======================================================================
178         // HELPER FUNCTIONS
179         //=======================================================================
180
181         private static void RunTest(uint repetitions, string test, string expected)
182         {
183             string digest = GetDigest(repetitions, test);
184             bool matches = digest.Equals(expected, StringComparison.OrdinalIgnoreCase);
185             m_success &= matches;
186             Trace.WriteLine(String.Format("{0} - {1}", digest, matches ? "OK" : "Error!"));
187             Assert.IsTrue(matches);
188         }
189
190         private static string GetDigest(uint repetitions, string test)
191         {
192             MHash384 hash = new MHash384();
193             for(uint i = 0; i < repetitions; ++i)
194             {
195                 hash.Update(test);
196             }
197             return ToHexString(hash.GetDigest());
198         }
199
200         private static string ToHexString(byte[] bytes)
201         {
202             StringBuilder sb = new StringBuilder(bytes.Length * 2);
203             foreach (byte b in bytes)
204             {
205                 sb.AppendFormat("{0:X2}", b);
206             }
207             return sb.ToString();
208         }
209     }
210 }