Merge pull request #231 from linquize/a853199c497bb0977970974303fac7e42080809d
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / SymmetricAlgorithm2Test.cs
1 //
2 // SymmetricAlgorithm2Test.cs -
3 //      Non generated NUnit Test Cases for SymmetricAlgorithm
4 //
5 // Author:
6 //      Sebastien Pouliot  <spouliot@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.IO;
33 using System.Security.Cryptography;
34 using System.Text;
35
36 namespace MonoTests.System.Security.Cryptography {
37
38         // SymmetricAlgorithm is a abstract class - so most of it's functionality wont
39         // be tested here (but will be in its descendants).
40
41         [TestFixture]
42         public class SymmetricAlgorithm2Test {
43
44                 [Test]
45                 public void KeySize_SameSize () 
46                 {
47                         using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
48                                 // get a copy of the key
49                                 byte[] key = algo.Key;
50                                 int ks = algo.KeySize;
51                                 // set the key size
52                                 algo.KeySize = ks;
53                                 // did it change the key ? Yes!
54                                 Assert.AreNotEqual (BitConverter.ToString (key), BitConverter.ToString (algo.Key), "Key");
55                         }
56                 }
57
58                 [Test]
59                 public void BlockSize_SameSize () 
60                 {
61                         using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
62                                 // get a copy of the IV
63                                 byte[] iv = algo.IV;
64                                 int bs = algo.BlockSize;
65                                 // set the iv size
66                                 algo.BlockSize = bs;
67                                 // did it change the IV ? No!
68                                 Assert.AreEqual (BitConverter.ToString (iv), BitConverter.ToString (algo.IV), "IV");
69                         }
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (CryptographicException))]
74                 public void InvalidBlockSize () 
75                 {
76                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
77                         algo.BlockSize = 255;
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (CryptographicException))]
82                 public void InvalidFeedbackSize () 
83                 {
84                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
85                         algo.FeedbackSize = algo.BlockSize + 1;
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentNullException))]
90                 public void IV_Null () 
91                 {
92                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
93                         algo.IV = null;
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (CryptographicException))]
98                 public void IV_None () 
99                 {
100                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
101                         algo.IV = new byte [0]; // e.g. stream ciphers
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (CryptographicException))]
106                 public void IV_TooBig () 
107                 {
108                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
109                         algo.IV = new byte [algo.BlockSize + 1];
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (ArgumentNullException))]
114                 public void Key_Null () 
115                 {
116                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
117                         algo.Key = null;
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (CryptographicException))]
122                 public void Key_WrongSize () 
123                 {
124                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
125                         algo.Key = new byte [255];
126                 }
127
128                 [Test]
129                 [ExpectedException (typeof (CryptographicException))]
130                 public void KeySize_WrongSize () 
131                 {
132                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
133                         int n = 0;
134                         while (algo.ValidKeySize (++n));
135                         algo.KeySize = n;
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (CryptographicException))]
140                 public void InvalidCipherMode () 
141                 {
142                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
143                         algo.Mode = (CipherMode) 255;
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (CryptographicException))]
148                 public void InvalidPaddingMode () 
149                 {
150                         SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
151                         algo.Padding = (PaddingMode) 255;
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (CryptographicException))]
156                 public void FeedbackZero ()
157                 {
158                         // thanks to Yakk for the sample
159                         DES des = new DESCryptoServiceProvider();
160                         des.FeedbackSize = 0;
161                         des.Padding = PaddingMode.None;
162                         des.Mode = CipherMode.ECB;
163                         des.Key = new byte [8] { 8, 7, 6, 5, 4, 3, 2, 1 };
164
165                         ICryptoTransform enc = des.CreateEncryptor (); 
166                         byte[] response = new byte [16];
167                         byte[] challenge = new byte [16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
168
169                         enc.TransformBlock (challenge, 0, challenge.Length, response, 0);
170                         Assert.AreEqual ("7A-64-CD-1B-4F-EE-B5-92-54-90-53-E9-83-71-A6-0C", BitConverter.ToString (response));
171                 }
172         }
173 }