New test.
[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 #if NET_2_0
245                 [ExpectedException (typeof (NullReferenceException))]
246 #else
247                 [ExpectedException (typeof (ArgumentNullException))]
248 #endif
249                 public void HttpRequestPublicCtor ()
250                 {
251                         HttpRequest hr = new HttpRequest ("file", "http://www.mono-project.com/", "");
252                         // always throw a NullReferenceException if the public ctor is used
253                         hr.ClientCertificate.ToString ();
254                 }
255
256                 private HttpClientCertificate GetHttpClientCertificate ()
257                 {
258                         return new HttpContext (hwr).Request.ClientCertificate;
259                 }
260
261                 [Test]
262 #if ONLY_1_1
263                 [Category ("NotDotNet")] // we don't want to duplicate this
264 #endif
265                 public void DefaultValues ()
266                 {
267                         HttpClientCertificate hcc = GetHttpClientCertificate ();
268                         Assert.AreEqual (0, hcc.BinaryIssuer.Length, "BinaryIssuer");
269                         Assert.AreEqual (0, hcc.CertEncoding, "CertEncoding");
270                         Assert.AreEqual (0, hcc.Certificate.Length, "Certificate");
271                         Assert.AreEqual (String.Empty, hcc.Cookie, "Cookie");
272                         Assert.AreEqual (0, hcc.Flags, "Flags");
273                         Assert.IsFalse (hcc.IsPresent, "IsPresent");
274                         Assert.AreEqual (String.Empty, hcc.Issuer, "Issuer");
275                         Assert.IsTrue (hcc.IsValid, "IsValid");
276                         Assert.AreEqual (0, hcc.KeySize, "KeySize");
277                         Assert.AreEqual (0, hcc.PublicKey.Length, "PublicKey");
278                         Assert.AreEqual (0, hcc.SecretKeySize, "SecretKeySize");
279                         Assert.AreEqual (String.Empty, hcc.SerialNumber, "SerialNumber");
280                         Assert.AreEqual (String.Empty, hcc.ServerIssuer, "ServerIssuer");
281                         Assert.AreEqual (String.Empty, hcc.ServerSubject, "ServerSubject");
282                         Assert.AreEqual (String.Empty, hcc.Subject, "Subject");
283                         DateTime start = DateTime.Now.AddMinutes (1);
284                         DateTime end = start.AddMinutes (-2);
285                         // creation time - doesn't update (at least after first call)
286                         Assert.IsTrue (hcc.ValidFrom < start, "ValidFrom <");
287                         Assert.IsTrue (hcc.ValidFrom > end, "ValidFrom >");
288                         Assert.IsTrue (hcc.ValidUntil < start, "ValidUntil <");
289                         Assert.IsTrue (hcc.ValidUntil > end, "ValidUntil >");
290
291                         // NameValueCollection stuff
292                         Assert.AreEqual (0, hcc.Count, "Count");
293                 }
294
295                 [Test]
296 #if ONLY_1_1
297                 [Category ("NotDotNet")] // we don't want to duplicate this
298 #endif
299                 public void MirrorValues ()
300                 {
301                         hwr.MirrorVariableName = true;
302                         HttpClientCertificate hcc = GetHttpClientCertificate ();
303
304                         // not default (because we now have some data)
305                         Assert.IsFalse (hcc.IsValid, "IsValid");
306                         Assert.IsTrue (hcc.IsPresent, "IsPresent");
307
308                         Assert.AreEqual ("CERT_COOKIE", hcc.Cookie, "Cookie");
309                         Assert.AreEqual (11, hcc.Flags, "Flags");
310                         Assert.AreEqual ("CERT_ISSUER", hcc.Issuer, "Issuer");
311                         Assert.AreEqual (12, hcc.KeySize, "KeySize");
312                         Assert.AreEqual (13, hcc.SecretKeySize, "SecretKeySize");
313                         Assert.AreEqual ("CERT_SERIALNUMBER", hcc.SerialNumber, "SerialNumber");
314                         Assert.AreEqual ("CERT_SERVER_ISSUER", hcc.ServerIssuer, "ServerIssuer");
315                         Assert.AreEqual ("CERT_SERVER_SUBJECT", hcc.ServerSubject, "ServerSubject");
316                         Assert.AreEqual ("CERT_SUBJECT", hcc.Subject, "Subject");
317                 }
318
319                 [Test]
320 #if ONLY_1_1
321                 [Category ("NotDotNet")] // we don't want to duplicate this
322 #endif
323                 public void MirrorValues_Alternate ()
324                 {
325                         hwr.MirrorVariableName = true;
326                         hwr.AlternateChoice = true;
327                         HttpClientCertificate hcc = GetHttpClientCertificate ();
328                         // if CERT_KEYSIZE is missing then HTTPS_KEYSIZE isn't checked
329                         Assert.AreEqual (0, hcc.KeySize, "Alternate-KeySize");
330                         // if CERT_SECRETKEYSIZE is missing then HTTPS_SECRETKEYSIZE isn't looked
331                         Assert.AreEqual (0, hcc.SecretKeySize, "Alternate-SecretKeySize");
332                 }
333
334                 [Test]
335 #if ONLY_1_1
336                 [Category ("NotDotNet")] // we don't want to duplicate this
337 #endif
338                 public void HttpWorkerRequest ()
339                 {
340                         // required to "activate" later call as IsPresent will return true
341                         hwr.MirrorVariableName = true; 
342                         hwr.Override = true;
343                         HttpClientCertificate hcc = GetHttpClientCertificate ();
344
345                         // not affected by server variables (but by HttpWorkerRequest)
346                         Assert.AreEqual (2, hcc.BinaryIssuer.Length, "BinaryIssuer");
347                         Assert.AreEqual (Int32.MinValue, hcc.CertEncoding, "CertEncoding");
348                         Assert.AreEqual (1, hcc.Certificate.Length, "Certificate");
349                         Assert.AreEqual (3, hcc.PublicKey.Length, "PublicKey");
350                         Assert.AreEqual (DateTime.MaxValue, hcc.ValidFrom, "ValidFrom");
351                         Assert.AreEqual (DateTime.MinValue, hcc.ValidUntil, "ValidUntil");
352                 }
353
354                 [Test]
355 #if ONLY_1_1
356                 [Category ("NotDotNet")] // we don't want to duplicate this
357 #endif
358                 public void Valid ()
359                 {
360                         HttpClientCertificate hcc = GetHttpClientCertificate ();
361                         // just to see if it always returns DateTime.Now or if it cache the value
362                         long from1 = hcc.ValidFrom.Ticks;
363                         Thread.Sleep (100); // don't go too fast
364                         long until1 = hcc.ValidUntil.Ticks;
365                         Thread.Sleep (100); // don't go too fast
366                         long from2 = hcc.ValidFrom.Ticks;
367                         Thread.Sleep (100); // don't go too fast
368                         long until2 = hcc.ValidUntil.Ticks;
369                         Assert.AreEqual (from1, from2, "from-from");
370                         Assert.AreEqual (until1, until2, "until-until");
371                         Assert.AreEqual (from1, until2, "from-until");
372                 }
373
374                 [Test]
375 #if ONLY_1_1
376                 [Category ("NotDotNet")] // we don't want to duplicate this
377 #endif
378                 public void Add ()
379                 {
380                         HttpClientCertificate hcc = GetHttpClientCertificate ();
381                         Assert.AreEqual (0, hcc.Count, "0");
382                         hcc.Add ("a", "b");
383                         Assert.AreEqual (1, hcc.Count, "1");
384                         // it's not read-only (at least not in this case)
385                 }
386
387                 [Test]
388 #if ONLY_1_1
389                 [Category ("NotDotNet")] // we don't want to duplicate this
390 #endif
391                 public void Get ()
392                 {
393                         HttpClientCertificate hcc = GetHttpClientCertificate ();
394                         Assert.AreEqual (String.Empty, hcc.Get (null), "null");
395                         hcc.Add ("a", "b");
396                         Assert.AreEqual (String.Empty, hcc.Get ("a"), "Get(string)");
397                         Assert.AreEqual ("b", hcc.Get (0), "Get(int)");
398                 }
399         }
400 }