Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpClientCertificateTest.cs
1 //
2 // HttpClientCertificateTest.cs
3 //      - Unit tests for System.Web.HttpClientCertificate
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 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 System;
31 using System.Threading;
32 using System.Web;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web {
37
38         // copied from Miguel's FakeHttpWorkerRequest in HttpRequestTest
39         class MyHttpWorkerRequest : HttpWorkerRequest {
40
41                 private bool mirror;
42                 private bool alternate;
43                 private bool override_cc;
44
45                 public MyHttpWorkerRequest ()
46                 {
47                 }
48                 
49                 public override string GetUriPath()
50                 {
51                         return "/uri.aspx";
52                 }
53
54                 public override string GetQueryString()
55                 {
56                         return "GetQueryString";
57                 }
58
59                 public override string GetRawUrl()
60                 {
61                         return "/bb.aspx";
62                 }
63
64                 public override string GetHttpVerbName()
65                 {
66                         return "GET";
67                 }
68
69                 public override string GetHttpVersion()
70                 {
71                         return "HTTP/1.1";
72                 }
73
74                 public override int GetRemotePort()
75                 {
76                         return 1010;
77                 }
78
79                 public override string GetLocalAddress()
80                 {
81                         return "localhost";
82                 }
83
84                 public override string GetRemoteName ()
85                 {
86                         return "RemoteName";
87                 }
88
89                 public override string GetRemoteAddress ()
90                 {
91                         return "RemoteAddress";
92                 }
93
94                 public override int GetLocalPort()
95                 {
96                         return 2020;
97                 }
98
99                 public override void SendStatus(int s, string x)
100                 {
101                 }
102
103                 public override void SendKnownResponseHeader(int x, string j)
104                 {
105                 }
106
107                 public override void SendUnknownResponseHeader(string a, string b)
108                 {
109                 }
110         
111                 public override void SendResponseFromMemory(byte[] arr, int x)
112                 {
113                 }
114
115                 public override void SendResponseFromFile(string a, long b , long c)
116                 {
117                 }
118
119                 public override void SendResponseFromFile (IntPtr a, long b, long c)
120                 {
121                 }
122
123                 public override void FlushResponse (bool x)
124                 {
125                 }
126
127                 public override void EndOfRequest ()
128                 {
129                 }
130
131                 public bool MirrorVariableName {
132                         get { return mirror; }
133                         set { mirror = value; }
134                 }
135
136                 public bool AlternateChoice {
137                         get { return alternate; }
138                         set { alternate = value; }
139                 }
140
141                 public override string GetServerVariable (string name)
142                 {
143                         if (mirror) {
144                                 switch (name) {
145                                 case "CERT_FLAGS":
146                                         return "11";
147                                 case "CERT_KEYSIZE":
148                                         return (alternate) ? base.GetServerVariable (name) : "12";
149                                 case "CERT_SECRETKEYSIZE":
150                                         return (alternate) ? base.GetServerVariable (name) : "13";
151                                 case "HTTPS_KEYSIZE":
152                                         return "22";
153                                 case "HTTPS_SECRETKEYSIZE":
154                                         return "23";
155                                 default:
156                                         return name;
157                                 }
158                         }
159                         return base.GetServerVariable (name);
160                 }
161
162                 public bool Override {
163                         get { return override_cc; }
164                         set { override_cc = value; }
165                 }
166
167                 public override byte[] GetClientCertificate ()
168                 {
169                         if (override_cc) {
170                                 return new byte[1];
171                         } else {
172                                 return base.GetClientCertificate ();
173                         }
174                 }
175
176                 public override byte[] GetClientCertificateBinaryIssuer ()
177                 {
178                         if (override_cc) {
179                                 return new byte[2];
180                         } else {
181                                 return base.GetClientCertificateBinaryIssuer ();
182                         }
183                 }
184
185                 public override int GetClientCertificateEncoding ()
186                 {
187                         if (override_cc) {
188                                 return Int32.MinValue;
189                         } else {
190                                 return base.GetClientCertificateEncoding ();
191                         }
192                 }
193
194                 public override byte[] GetClientCertificatePublicKey ()
195                 {
196                         if (override_cc) {
197                                 return new byte[3];
198                         } else {
199                                 return base.GetClientCertificatePublicKey ();
200                         }
201                 }
202
203                 public override DateTime GetClientCertificateValidFrom ()
204                 {
205                         if (override_cc) {
206                                 return DateTime.MaxValue;
207                         } else {
208                                 return base.GetClientCertificateValidFrom ();
209                         }
210                 }
211
212                 public override DateTime GetClientCertificateValidUntil ()
213                 {
214                         if (override_cc) {
215                                 return DateTime.MinValue;
216                         } else {
217                                 return base.GetClientCertificateValidUntil ();
218                         }
219                 }
220         }
221
222         [TestFixture]
223         public class HttpClientCertificateTest {
224
225                 private MyHttpWorkerRequest hwr;
226
227
228                 [TestFixtureSetUp]
229                 public void FixtureSetUp ()
230                 {
231                         hwr = new MyHttpWorkerRequest ();
232                 }
233
234                 [SetUp]
235                 public void SetUp ()
236                 {
237                         hwr.Override = false;
238                         hwr.MirrorVariableName = false;
239                         hwr.AlternateChoice = false;
240
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (NullReferenceException))]
245                 public void HttpRequestPublicCtor ()
246                 {
247                         HttpRequest hr = new HttpRequest ("file", "http://www.mono-project.com/", "");
248                         // always throw a NullReferenceException if the public ctor is used
249                         hr.ClientCertificate.ToString ();
250                 }
251
252                 private HttpClientCertificate GetHttpClientCertificate ()
253                 {
254                         return new HttpContext (hwr).Request.ClientCertificate;
255                 }
256
257                 [Test]
258                 public void DefaultValues ()
259                 {
260                         HttpClientCertificate hcc = GetHttpClientCertificate ();
261                         Assert.AreEqual (0, hcc.BinaryIssuer.Length, "BinaryIssuer");
262                         Assert.AreEqual (0, hcc.CertEncoding, "CertEncoding");
263                         Assert.AreEqual (0, hcc.Certificate.Length, "Certificate");
264                         Assert.AreEqual (String.Empty, hcc.Cookie, "Cookie");
265                         Assert.AreEqual (0, hcc.Flags, "Flags");
266                         Assert.IsFalse (hcc.IsPresent, "IsPresent");
267                         Assert.AreEqual (String.Empty, hcc.Issuer, "Issuer");
268                         Assert.IsTrue (hcc.IsValid, "IsValid");
269                         Assert.AreEqual (0, hcc.KeySize, "KeySize");
270                         Assert.AreEqual (0, hcc.PublicKey.Length, "PublicKey");
271                         Assert.AreEqual (0, hcc.SecretKeySize, "SecretKeySize");
272                         Assert.AreEqual (String.Empty, hcc.SerialNumber, "SerialNumber");
273                         Assert.AreEqual (String.Empty, hcc.ServerIssuer, "ServerIssuer");
274                         Assert.AreEqual (String.Empty, hcc.ServerSubject, "ServerSubject");
275                         Assert.AreEqual (String.Empty, hcc.Subject, "Subject");
276                         DateTime start = DateTime.Now.AddMinutes (1);
277                         DateTime end = start.AddMinutes (-2);
278                         // creation time - doesn't update (at least after first call)
279                         Assert.IsTrue (hcc.ValidFrom < start, "ValidFrom <");
280                         Assert.IsTrue (hcc.ValidFrom > end, "ValidFrom >");
281                         Assert.IsTrue (hcc.ValidUntil < start, "ValidUntil <");
282                         Assert.IsTrue (hcc.ValidUntil > end, "ValidUntil >");
283
284                         // NameValueCollection stuff
285                         Assert.AreEqual (0, hcc.Count, "Count");
286                 }
287
288                 [Test]
289                 public void MirrorValues ()
290                 {
291                         hwr.MirrorVariableName = true;
292                         HttpClientCertificate hcc = GetHttpClientCertificate ();
293
294                         // not default (because we now have some data)
295                         Assert.IsFalse (hcc.IsValid, "IsValid");
296                         Assert.IsTrue (hcc.IsPresent, "IsPresent");
297
298                         Assert.AreEqual ("CERT_COOKIE", hcc.Cookie, "Cookie");
299                         Assert.AreEqual (11, hcc.Flags, "Flags");
300                         Assert.AreEqual ("CERT_ISSUER", hcc.Issuer, "Issuer");
301                         Assert.AreEqual (12, hcc.KeySize, "KeySize");
302                         Assert.AreEqual (13, hcc.SecretKeySize, "SecretKeySize");
303                         Assert.AreEqual ("CERT_SERIALNUMBER", hcc.SerialNumber, "SerialNumber");
304                         Assert.AreEqual ("CERT_SERVER_ISSUER", hcc.ServerIssuer, "ServerIssuer");
305                         Assert.AreEqual ("CERT_SERVER_SUBJECT", hcc.ServerSubject, "ServerSubject");
306                         Assert.AreEqual ("CERT_SUBJECT", hcc.Subject, "Subject");
307                 }
308
309                 [Test]
310                 public void MirrorValues_Alternate ()
311                 {
312                         hwr.MirrorVariableName = true;
313                         hwr.AlternateChoice = true;
314                         HttpClientCertificate hcc = GetHttpClientCertificate ();
315                         // if CERT_KEYSIZE is missing then HTTPS_KEYSIZE isn't checked
316                         Assert.AreEqual (0, hcc.KeySize, "Alternate-KeySize");
317                         // if CERT_SECRETKEYSIZE is missing then HTTPS_SECRETKEYSIZE isn't looked
318                         Assert.AreEqual (0, hcc.SecretKeySize, "Alternate-SecretKeySize");
319                 }
320
321                 [Test]
322                 public void HttpWorkerRequest ()
323                 {
324                         // required to "activate" later call as IsPresent will return true
325                         hwr.MirrorVariableName = true; 
326                         hwr.Override = true;
327                         HttpClientCertificate hcc = GetHttpClientCertificate ();
328
329                         // not affected by server variables (but by HttpWorkerRequest)
330                         Assert.AreEqual (2, hcc.BinaryIssuer.Length, "BinaryIssuer");
331                         Assert.AreEqual (Int32.MinValue, hcc.CertEncoding, "CertEncoding");
332                         Assert.AreEqual (1, hcc.Certificate.Length, "Certificate");
333                         Assert.AreEqual (3, hcc.PublicKey.Length, "PublicKey");
334                         Assert.AreEqual (DateTime.MaxValue, hcc.ValidFrom, "ValidFrom");
335                         Assert.AreEqual (DateTime.MinValue, hcc.ValidUntil, "ValidUntil");
336                 }
337
338                 [Test]
339                 public void Valid ()
340                 {
341                         HttpClientCertificate hcc = GetHttpClientCertificate ();
342                         // just to see if it always returns DateTime.Now or if it cache the value
343                         long from1 = hcc.ValidFrom.Ticks;
344                         Thread.Sleep (100); // don't go too fast
345                         long until1 = hcc.ValidUntil.Ticks;
346                         Thread.Sleep (100); // don't go too fast
347                         long from2 = hcc.ValidFrom.Ticks;
348                         Thread.Sleep (100); // don't go too fast
349                         long until2 = hcc.ValidUntil.Ticks;
350                         Assert.AreEqual (from1, from2, "from-from");
351                         Assert.AreEqual (until1, until2, "until-until");
352                         Assert.AreEqual (from1, until2, "from-until");
353                 }
354
355                 [Test]
356                 public void Add ()
357                 {
358                         HttpClientCertificate hcc = GetHttpClientCertificate ();
359                         Assert.AreEqual (0, hcc.Count, "0");
360                         hcc.Add ("a", "b");
361                         Assert.AreEqual (1, hcc.Count, "1");
362                         // it's not read-only (at least not in this case)
363                 }
364
365                 [Test]
366                 public void Get ()
367                 {
368                         HttpClientCertificate hcc = GetHttpClientCertificate ();
369                         Assert.AreEqual (String.Empty, hcc.Get (null), "null");
370                         hcc.Add ("a", "b");
371                         Assert.AreEqual (String.Empty, hcc.Get ("a"), "Get(string)");
372                         Assert.AreEqual ("b", hcc.Get (0), "Get(int)");
373                 }
374         }
375 }