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