Merge pull request #3328 from BrzVlad/finalizer-thread-exited2
[mono.git] / mcs / class / System / Test / System.Net / FileWebRequestTest.cs
1 //
2 // FileWebRequestTest.cs - NUnit Test Cases for System.Net.FileWebRequest
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 //
11
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Net;
16 using System.Runtime.Serialization;
17 using System.Runtime.Serialization.Formatters;
18 using System.Runtime.Serialization.Formatters.Binary;
19 using System.Security;
20 using System.Security.Permissions;
21
22 using NUnit.Framework;
23
24
25 namespace MonoTests.System.Net
26 {
27         [TestFixture]
28         [Category ("RequiresBSDSockets")]
29         public class FileWebRequestTest
30         {
31                 private string _tempDirectory;
32                 private string _tempFile;
33                 private Uri _tempFileUri;
34
35                 [SetUp]
36                 public void SetUp ()
37                 {
38                         _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
39                         _tempFile = Path.Combine (_tempDirectory, "FileWebRequestTest.tmp");
40                         if (!Directory.Exists (_tempDirectory)) {
41                                 Directory.CreateDirectory (_tempDirectory);
42                         } else {
43                                 // ensure no files are left over from previous runs
44                                 string [] files = Directory.GetFiles (_tempDirectory, "*");
45                                 foreach (string file in files)
46                                         File.Delete (file);
47                         }
48                         _tempFileUri = GetTempFileUri ();
49                 }
50
51                 [TearDown]
52                 public void TearDown ()
53                 {
54                         if (Directory.Exists (_tempDirectory))
55                                 Directory.Delete (_tempDirectory, true);
56                 }
57
58                 [Test]
59                 public void Async ()
60                 {
61                         WebResponse res = null;
62
63                         try {
64                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
65                                 req.Method = "PUT";
66                                 req.ContentLength = 1;
67                                 req.ContentType = "image/png";
68
69                                 req.Timeout = 2 * 1000;
70                                 IAsyncResult async = req.BeginGetRequestStream (null, null);
71                                 try {
72                                         req.BeginGetRequestStream (null, null);
73                                         Assert.Fail ("#1 should've failed");
74                                 } catch (InvalidOperationException) {
75                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
76                                         // a previous call is still in progress
77                                 }
78
79                                 try {
80                                         req.GetRequestStream ();
81                                         Assert.Fail ("#3 should've failed");
82                                 } catch (InvalidOperationException) {
83                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
84                                         // a previous call is still in progress
85                                 }
86
87                                 using (Stream wstream = req.EndGetRequestStream (async)) {
88                                         Assert.IsFalse (wstream.CanRead, "#1r");
89                                         Assert.IsTrue (wstream.CanWrite, "#1w");
90                                         Assert.IsTrue (wstream.CanSeek, "#1s");
91
92                                         wstream.WriteByte (72);
93                                         wstream.WriteByte (101);
94                                         wstream.WriteByte (108);
95                                         wstream.WriteByte (108);
96                                         wstream.WriteByte (111);
97                                         wstream.Close ();
98                                 }
99
100                                 Assert.AreEqual (1, req.ContentLength, "#1cl");
101                                 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
102
103                                 // stream written
104
105                                 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
106                                 res = req.GetResponse ();
107
108                                 try {
109                                         req.BeginGetRequestStream (null, null);
110                                         Assert.Fail ("#20: should've failed");
111                                 } catch (InvalidOperationException) {
112                                         // Cannot send a content-body with this verb-type
113                                 }
114
115                                 try {
116                                         req.Method = "PUT";
117                                         req.BeginGetRequestStream (null, null);
118                                         Assert.Fail ("#21: should've failed");
119                                 } catch (InvalidOperationException) {
120                                         // This operation cannot be perfomed after the request has been submitted.
121                                 }
122
123                                 req.GetResponse ();
124
125                                 IAsyncResult async2 = req.BeginGetResponse (null, null);
126
127                                 // this succeeds !!
128                                 WebResponse res2 = req.EndGetResponse (async2);
129                                 Assert.AreSame (res, res2, "#23");
130
131                                 Assert.AreEqual (5, res.ContentLength, "#2 len");
132                                 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
133                                 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
134
135                                 Stream rstream = res.GetResponseStream ();
136                                 Assert.IsTrue (rstream.CanRead, "#3r");
137                                 Assert.IsFalse (rstream.CanWrite, "#3w");
138                                 Assert.IsTrue (rstream.CanSeek, "#3s");
139
140                                 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
141                                 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
142                                 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
143                                 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
144                                 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
145
146                                 rstream.Close ();
147
148                                 try {
149                                         long len = res.ContentLength;
150                                         Assert.AreEqual ((long) 5, len, "#5");
151                                 } catch (ObjectDisposedException) {
152                                         Assert.Fail ("#disposed contentlength");
153                                 }
154                                 try {
155                                         WebHeaderCollection w = res.Headers;
156                                 } catch (ObjectDisposedException) {
157                                         Assert.Fail ("#disposed headers");
158                                 }
159                                 try {
160                                         res.Close ();
161                                 } catch (ObjectDisposedException) {
162                                         Assert.Fail ("#disposed close");
163                                 }
164                         } finally {
165                                 if (res != null)
166                                         res.Close ();
167                         }
168                 }
169
170                 [Test]
171                 [Category ("NotWorking")] // bug #323388
172                 public void Async_GetResponse_Failure ()
173                 {
174                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
175                         req.Method = "PUT";
176                         req.ContentLength = 1;
177                         req.ContentType = "image/png";
178                         req.Timeout = 500;
179
180                         IAsyncResult async = req.BeginGetRequestStream (null, null);
181                         try {
182                                 req.GetResponse ();
183                                 Assert.Fail ("#1");
184                         } catch (WebException) {
185                                 // The operation has timed out
186                         }
187
188                         try {
189                                 req.BeginGetResponse (null, null);
190                                 Assert.Fail ("#2");
191                         } catch (InvalidOperationException) {
192                                 // Cannot re-call BeginGetRequestStream/BeginGetResponse while
193                                 // a previous call is still in progress
194                         }
195
196                         using (Stream wstream = req.EndGetRequestStream (async)) {
197                                 wstream.WriteByte (72);
198                         }
199
200                         // the temp file should not be in use
201                         Directory.Delete (_tempDirectory, true);
202                 }
203
204                 [Test]
205                 public void Sync ()
206                 {
207                         WebResponse res = null;
208
209                         try {
210                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
211                                 req.ContentLength = 1;
212                                 req.ContentType = "image/png";
213
214                                 try {
215                                         Stream stream = req.GetRequestStream ();
216                                         Assert.Fail ("should throw exception");
217                                 } catch (ProtocolViolationException) {
218                                 }
219
220                                 req.Method = "PUT";
221
222                                 Stream wstream = req.GetRequestStream ();
223                                 Assert.IsFalse (wstream.CanRead, "#1r");
224                                 Assert.IsTrue (wstream.CanWrite, "#1w");
225                                 Assert.IsTrue (wstream.CanSeek, "#1s");
226
227                                 wstream.WriteByte (72);
228                                 wstream.WriteByte (101);
229                                 wstream.WriteByte (108);
230                                 wstream.WriteByte (108);
231                                 wstream.WriteByte (111);
232                                 wstream.Close ();
233
234                                 Assert.AreEqual (1, req.ContentLength, "#1cl");
235                                 Assert.AreEqual ("image/png", req.ContentType, "#1ct");
236
237                                 // stream written
238
239                                 req = (FileWebRequest) WebRequest.Create (_tempFileUri);
240                                 res = req.GetResponse ();
241                                 Assert.AreEqual ((long) 5, res.ContentLength, "#2 len");
242                                 Assert.AreEqual ("application/octet-stream", res.ContentType, "#2 type");
243                                 Assert.AreEqual ("file", res.ResponseUri.Scheme, "#2 scheme");
244
245                                 Stream rstream = res.GetResponseStream ();
246                                 Assert.IsTrue (rstream.CanRead, "#3r");
247                                 Assert.IsFalse (rstream.CanWrite, "#3w");
248                                 Assert.IsTrue (rstream.CanSeek, "#3s");
249
250                                 Assert.AreEqual (72, rstream.ReadByte (), "#4a");
251                                 Assert.AreEqual (101, rstream.ReadByte (), "#4b");
252                                 Assert.AreEqual (108, rstream.ReadByte (), "#4c");
253                                 Assert.AreEqual (108, rstream.ReadByte (), "#4d");
254                                 Assert.AreEqual (111, rstream.ReadByte (), "#4e");
255
256                                 rstream.Close ();
257
258                                 try {
259                                         long len = res.ContentLength;
260                                         Assert.AreEqual ((long) 5, len, "#5");
261                                 } catch (ObjectDisposedException) {
262                                         Assert.Fail ("#disposed contentlength");
263                                 }
264                                 try {
265                                         WebHeaderCollection w = res.Headers;
266                                 } catch (ObjectDisposedException) {
267                                         Assert.Fail ("#disposed headers");
268                                 }
269                                 try {
270                                         res.Close ();
271                                 } catch (ObjectDisposedException) {
272                                         Assert.Fail ("#disposed close");
273                                 }
274                         } finally {
275                                 if (res != null)
276                                         res.Close ();
277                         }
278                 }
279
280                 [Test]
281                 [Category ("NotWorking")] // bug #323388
282                 public void Sync_GetResponse_Failure ()
283                 {
284                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
285                         req.Method = "PUT";
286                         req.ContentLength = 1;
287                         req.ContentType = "image/png";
288                         req.Timeout = 500;
289
290                         using (Stream rs = req.GetRequestStream ()) {
291                                 try {
292                                         req.GetResponse ();
293                                         Assert.Fail ("#1");
294                                 } catch (WebException) {
295                                         // The operation has timed out
296                                 }
297
298                                 try {
299                                         req.BeginGetResponse (null, null);
300                                         Assert.Fail ("#2");
301                                 } catch (InvalidOperationException) {
302                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse while
303                                         // a previous call is still in progress
304                                 }
305                         }
306
307                         // the temp file should not be in use
308                         Directory.Delete (_tempDirectory, true);
309                 }
310
311                 [Test]
312                 public void ConnectionGroupName ()
313                 {
314                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
315                         Assert.IsNull (req.ConnectionGroupName, "#A");
316                         req.ConnectionGroupName = "whatever";
317                         Assert.IsNotNull (req.ConnectionGroupName, "#B1");
318                         Assert.AreEqual ("whatever", req.ConnectionGroupName, "#B2");
319                         req.ConnectionGroupName = string.Empty;
320                         Assert.IsNotNull (req.ConnectionGroupName, "#C1");
321                         Assert.AreEqual (string.Empty, req.ConnectionGroupName, "#C2");
322                         req.ConnectionGroupName = null;
323                         Assert.IsNull (req.ConnectionGroupName, "#D");
324                 }
325
326                 [Test]
327                 public void ContentLength ()
328                 {
329                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
330                         Assert.AreEqual (0, req.Headers.Count, "#A1");
331                         Assert.AreEqual (0, req.ContentLength, "#A2");
332                         req.ContentLength = 5;
333                         Assert.AreEqual (5, req.ContentLength, "#A3");
334                         Assert.AreEqual (0, req.Headers.Count, "#A4");
335
336                         req.Method = "PUT";
337                         using (Stream s = req.GetRequestStream ()) {
338                                 s.WriteByte (5);
339                                 Assert.AreEqual (5, req.ContentLength, "#B1");
340                                 s.WriteByte (4);
341                                 Assert.AreEqual (5, req.ContentLength, "#B2");
342                                 s.Flush ();
343                                 Assert.AreEqual (5, req.ContentLength, "#B3");
344                         }
345                         Assert.AreEqual (5, req.ContentLength, "#B4");
346                 }
347
348                 [Test]
349                 public void ContentLength_Negative ()
350                 {
351                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
352                         try {
353                                 req.ContentLength = -1;
354                                 Assert.Fail ("#1");
355                         } catch (ArgumentException ex) {
356                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
357                                 Assert.IsNotNull (ex.Message, "#3");
358                                 Assert.IsFalse (ex.Message == "value", "#4");
359                                 Assert.IsNotNull (ex.ParamName, "#5");
360                                 Assert.AreEqual ("value", ex.ParamName, "#6");
361                                 Assert.IsNull (ex.InnerException, "#7");
362                         }
363                 }
364
365                 [Test]
366                 public void ContentType ()
367                 {
368                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
369                         Assert.AreEqual (0, req.Headers.Count, "#A1");
370                         Assert.IsNull (req.ContentType, "#A2");
371
372                         req.ContentType = "application/x-gzip";
373                         Assert.AreEqual (1, req.Headers.Count, "#B1");
374                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#B2");
375                         Assert.AreEqual ("application/x-gzip", req.Headers.Get (0), "#B3");
376                         Assert.AreEqual ("application/x-gzip", req.ContentType, "#B4");
377
378                         req.Headers.Set ("Content-Type", "image/png");
379                         Assert.AreEqual ("image/png", req.ContentType, "#C1");
380
381                         req.ContentType = null;
382                         Assert.AreEqual (1, req.Headers.Count, "#D1");
383                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#D2");
384                         Assert.AreEqual (string.Empty, req.Headers.Get (0), "#D3");
385                         Assert.AreEqual (string.Empty, req.ContentType, "#D4");
386
387                         req.Headers.Remove ("Content-Type");
388                         Assert.AreEqual (0, req.Headers.Count, "#E1");
389                         Assert.IsNull (req.ContentType, "#E2");
390                 }
391
392                 [Test]
393                 public void Credentials ()
394                 {
395                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
396                         Assert.IsNull (req.Credentials, "#1");
397                         req.Credentials = new NetworkCredential ();
398                         Assert.IsNotNull (req.Credentials, "#2");
399                         req.Credentials = null;
400                         Assert.IsNull (req.Credentials, "#3");
401                 }
402
403                 [Test]
404                 [Category ("NotWorking")]
405                 public void GetRequestStream ()
406                 {
407                         FileWebRequest req = (FileWebRequest) WebRequest.Create (
408                                 _tempFileUri);
409                         req.Timeout = 1000;
410                         req.Method = "POST";
411                         FileStream fsA = null;
412                         FileStream fsB = null;
413                         try {
414                                 fsA = req.GetRequestStream () as FileStream;
415                                 Assert.IsNotNull (fsA, "#A1");
416                                 try {
417                                         req.GetRequestStream ();
418                                         Assert.Fail ("#A2");
419                                 } catch (WebException) {
420                                         // The operation has timed out
421                                 }
422                                 fsA.Close ();
423                                 try {
424                                         req.GetRequestStream ();
425                                         Assert.Fail ("#A3");
426                                 } catch (InvalidOperationException) {
427                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse 
428                                         // while a previous call is still in progress.
429                                 }
430                         } finally {
431                                 if (fsA != null)
432                                         fsA.Close ();
433                                 if (fsB != null)
434                                         fsB.Close ();
435                         }
436
437                         req = (FileWebRequest) WebRequest.Create (_tempFileUri);
438                         req.Timeout = 1000;
439                         req.Method = "POST";
440                         try {
441                                 fsA = req.GetRequestStream () as FileStream;
442                                 Assert.IsNotNull (fsA, "#B1");
443                                 fsA.Close ();
444                                 try {
445                                         req.GetRequestStream ();
446                                         Assert.Fail ("#B2");
447                                 } catch (WebException) {
448                                         // The operation has timed out
449                                 }
450                                 fsA.Close ();
451                                 try {
452                                         req.GetRequestStream ();
453                                         Assert.Fail ("#B3");
454                                 } catch (InvalidOperationException) {
455                                         // Cannot re-call BeginGetRequestStream/BeginGetResponse 
456                                         // while a previous call is still in progress.
457                                 }
458                         } finally {
459                                 if (fsA != null)
460                                         fsA.Close ();
461                                 if (fsB != null)
462                                         fsB.Close ();
463                         }
464                 }
465
466                 [Test]
467                 public void GetRequestStream_File_Exists ()
468                 {
469                         Stream s = File.Create (_tempFile);
470                         s.Close ();
471                         FileWebRequest req = (FileWebRequest) WebRequest.Create (
472                                 _tempFileUri);
473                         req.Method = "POST";
474                         s = req.GetRequestStream ();
475                         s.Close ();
476                 }
477
478                 [Test]
479                 public void GetRequestStream_Method_Valid ()
480                 {
481                         string [] methods = new string [] { "PUT", "POST", "CHECKOUT",
482                                 "DELETE", "OPTIONS", "TRACE", "GET ", "DUNNO" };
483
484                         foreach (string method in methods) {
485                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (
486                                         _tempFileUri);
487                                 req.Method = method;
488                                 using (Stream s = req.GetRequestStream ()) {
489                                         Assert.IsNotNull (s, "#1:" + method);
490                                         Assert.IsFalse (s.CanRead, "#2:" + method);
491                                         Assert.IsTrue (s.CanSeek, "#3:" + method);
492                                         Assert.IsFalse (s.CanTimeout, "#4:" + method);
493                                         Assert.IsTrue (s.CanWrite, "#5:" + method);
494                                         Assert.AreEqual (0, s.Length, "#6:" + method);
495                                         Assert.AreEqual (0, s.Position, "#7:" + method);
496                                         try {
497                                                 int i = s.ReadTimeout;
498                                                 Assert.Fail ("#8:" + method + "=>" + i);
499                                         } catch (InvalidOperationException) {
500                                         }
501                                         try {
502                                                 int i = s.WriteTimeout;
503                                                 Assert.Fail ("#9:" + method + "=>" + i);
504                                         } catch (InvalidOperationException) {
505                                         }
506                                 }
507                         }
508                 }
509
510                 [Test]
511                 public void GetRequestStream_Method_Invalid ()
512                 {
513                         string [] methods = new string [] { "GET", "get", "HEAD", "head",
514                                 "CONNECT", "connect"};
515                         foreach (string method in methods) {
516                                 FileWebRequest req = (FileWebRequest) WebRequest.Create (
517                                         _tempFileUri);
518                                 req.Method = method;
519                                 try {
520                                         req.GetRequestStream ();
521                                         Assert.Fail ("#1:" + method);
522                                 } catch (ProtocolViolationException ex) {
523                                         Assert.AreEqual (typeof (ProtocolViolationException), ex.GetType (), "#2:" + method);
524                                         Assert.IsNotNull (ex.Message, "#3:" + method);
525                                         Assert.IsNull (ex.InnerException, "#4:" + method);
526                                 }
527                         }
528                 }
529
530                 [Test]
531                 public void GetResponse_File_Exists ()
532                 {
533                         Stream s = File.Create (_tempFile);
534                         s.Close ();
535                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
536                         FileWebResponse respA = null;
537                         FileWebResponse respB = null;
538                         try {
539                                 respA = req.GetResponse () as FileWebResponse;
540                                 Assert.IsNotNull (respA, "#1");
541                                 respB = req.GetResponse () as FileWebResponse;
542                                 Assert.IsNotNull (respB, "#2");
543                                 Assert.AreSame (respA, respB, "#3");
544                         } finally {
545                                 if (respA != null)
546                                         respA.Close ();
547                                 if (respB != null)
548                                         respB.Close ();
549                         }
550                 }
551
552                 [Test]
553                 public void GetResponse_File_DoesNotExist ()
554                 {
555                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
556                         try {
557                                 req.GetResponse ();
558                                 Assert.Fail ("#1");
559                         } catch (WebException ex) {
560                                 Assert.AreEqual (typeof (WebException), ex.GetType (), "#1");
561                                 Assert.IsNotNull (ex.Message, "#2");
562                                 Assert.IsTrue (ex.Message.IndexOf ("FileWebRequestTest.tmp") != -1, "#3");                              
563                                 Assert.IsNull (ex.Response, "#4");
564                                 Assert.IsNotNull (ex.InnerException, "#5");
565
566                         }
567                 }
568
569                 [Test]
570                 public void Method ()
571                 {
572                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
573                         Assert.IsNotNull (req.Method, "#A1");
574                         Assert.AreEqual ("GET", req.Method, "#A2");
575                         req.Method = "whatever";
576                         Assert.IsNotNull (req.Method, "#B1");
577                         Assert.AreEqual ("whatever", req.Method, "#B2");
578                         req.Method = "get ";
579                         Assert.IsNotNull (req.Method, "#C1");
580                         Assert.AreEqual ("get ", req.Method, "#C2");
581                 }
582
583                 [Test]
584                 public void Method_Empty ()
585                 {
586                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
587                         try {
588                                 req.Method = string.Empty;
589                                 Assert.Fail ("#1");
590                         } catch (ArgumentException ex) {
591                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
592                                 Assert.IsNotNull (ex.Message, "#3");
593                                 Assert.AreEqual ("value", ex.ParamName, "#4");
594                                 Assert.IsNull (ex.InnerException, "#5");
595                         }
596                 }
597
598                 [Test]
599                 public void Method_Null ()
600                 {
601                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
602                         try {
603                                 req.Method = null;
604                                 Assert.Fail ("#1");
605                         } catch (ArgumentException ex) {
606                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
607                                 Assert.IsNotNull (ex.Message, "#3");
608                                 Assert.AreEqual ("value", ex.ParamName, "#4");
609                                 Assert.IsNull (ex.InnerException, "#5");
610                         }
611                 }
612
613                 [Test]
614                 public void PreAuthenticate ()
615                 {
616                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
617                         Assert.IsFalse (req.PreAuthenticate, "#1");
618                         req.PreAuthenticate = true;
619                         Assert.IsTrue (req.PreAuthenticate, "#2");
620                 }
621
622                 [Test]
623                 public void Proxy ()
624                 {
625                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
626                         Assert.IsNull (req.Proxy, "#1");
627                         req.Proxy = new WebProxy ();
628                         Assert.IsNotNull (req.Proxy, "#2");
629                         req.Proxy = null;
630                         Assert.IsNull (req.Proxy, "#3");
631                 }
632
633                 [Test]
634                 public void RequestUri ()
635                 {
636                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
637                         Assert.AreSame (_tempFileUri, req.RequestUri);
638                 }
639
640                 [Test]
641                 public void Timeout ()
642                 {
643                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
644                         Assert.AreEqual (100000, req.Timeout, "#1");
645                         req.Timeout = int.MaxValue;
646                         Assert.AreEqual (int.MaxValue, req.Timeout, "#2");
647                         req.Timeout = 0;
648                         Assert.AreEqual (0, req.Timeout, "#3");
649                 }
650
651                 [Test]
652                 public void Timeout_Negative ()
653                 {
654                         FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
655                         req.Timeout = -1;
656                         Assert.AreEqual (-1, req.Timeout, "#1");
657                         try {
658                                 req.Timeout = -2;
659                                 Assert.Fail ("#2");
660                         } catch (ArgumentOutOfRangeException ex) {
661                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#3");
662                                 Assert.IsNotNull (ex.Message, "#4");
663                                 Assert.IsNotNull (ex.ParamName, "#5");
664                                 Assert.AreEqual ("value", ex.ParamName, "#6");
665                                 Assert.IsNull (ex.InnerException, "#7");
666                         }
667                 }
668
669                 [Test]
670                 public void GetObjectData ()
671                 {
672                         FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file:///test.txt");
673                         fwr.ConnectionGroupName = "CGN";
674                         fwr.ContentLength = 10;
675                         fwr.ContentType = "image/png";
676                         fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
677                         fwr.Headers.Add ("Disposition", "attach");
678                         fwr.Method = "PUT";
679                         fwr.PreAuthenticate = true;
680                         fwr.Proxy = new WebProxy ("proxy.ximian.com");
681                         fwr.Timeout = 20;
682
683                         SerializationInfo si = new SerializationInfo (typeof (FileWebRequest),
684                                 new FormatterConverter ());
685                         ((ISerializable) fwr).GetObjectData (si, new StreamingContext ());
686                         Assert.AreEqual (9, si.MemberCount, "#A1");
687                         int i = 0;
688                         foreach (SerializationEntry entry in si) {
689                                 Assert.IsNotNull (entry.Name, "#B1:" + i);
690                                 Assert.IsNotNull (entry.ObjectType, "#B2:" + i);
691                                 Assert.IsNotNull (entry.Value, "#B3:" + i);
692
693                                 switch (i) {
694                                 case 0:
695                                         Assert.AreEqual ("headers", entry.Name, "#B4:" + i);
696                                         Assert.AreEqual (typeof (WebHeaderCollection), entry.ObjectType, "#B5:" + i);
697                                         break;
698                                 case 1:
699                                         Assert.AreEqual ("proxy", entry.Name, "#B4:" + i);
700                                         Assert.AreEqual (typeof (IWebProxy), entry.ObjectType, "#B5:" + i);
701                                         break;
702                                 case 2:
703                                         Assert.AreEqual ("uri", entry.Name, "#B4:" + i);
704                                         Assert.AreEqual (typeof (Uri), entry.ObjectType, "#B5:" + i);
705                                         break;
706                                 case 3:
707                                         Assert.AreEqual ("connectionGroupName", entry.Name, "#B4:" + i);
708                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
709                                         Assert.AreEqual ("CGN", entry.Value, "#B6:" + i);
710                                         break;
711                                 case 4:
712                                         Assert.AreEqual ("method", entry.Name, "#B4:" + i);
713                                         Assert.AreEqual (typeof (string), entry.ObjectType, "#B5:" + i);
714                                         Assert.AreEqual ("PUT", entry.Value, "#B6:" + i);
715                                         break;
716                                 case 5:
717                                         Assert.AreEqual ("contentLength", entry.Name, "#B4:" + i);
718                                         Assert.AreEqual (typeof (long), entry.ObjectType, "#B5:" + i);
719                                         Assert.AreEqual (10, entry.Value, "#B6:" + i);
720                                         break;
721                                 case 6:
722                                         Assert.AreEqual ("timeout", entry.Name, "#B4:" + i);
723                                         Assert.AreEqual (typeof (int), entry.ObjectType, "#B5:" + i);
724                                         Assert.AreEqual (20, entry.Value, "#B6:" + i);
725                                         break;
726                                 case 7:
727                                         Assert.AreEqual ("fileAccess", entry.Name, "#B4:" + i);
728                                         Assert.AreEqual (typeof (FileAccess), entry.ObjectType, "#B5:" + i);
729                                         Assert.AreEqual (FileAccess.Read, entry.Value, "#B6:" + i);
730                                         break;
731                                 case 8:
732                                         Assert.AreEqual ("preauthenticate", entry.Name, "#B4:" + i);
733                                         Assert.AreEqual (typeof (bool), entry.ObjectType, "#B5:" + i);
734                                         Assert.AreEqual (false, entry.Value, "#B6:" + i);
735                                         break;
736                                 }
737                                 i++;
738                         }
739                 }
740
741                 [Test]
742                 [Category ("NotWorking")] // Difference at index 272: 20 instead of 19
743                 public void Serialize ()
744                 {
745                         FileWebRequest fwr = (FileWebRequest) WebRequest.Create ("file://test.txt/");
746                         fwr.ConnectionGroupName = "CGN";
747                         fwr.ContentLength = 10;
748                         fwr.ContentType = "image/png";
749                         fwr.Credentials = new NetworkCredential ("Miguel", "de Icaza", "Novell");
750                         fwr.Headers.Add ("Disposition", "attach");
751                         fwr.Method = "PUT";
752                         fwr.PreAuthenticate = true;
753                         fwr.Proxy = new WebProxy ("proxy.ximian.com");
754                         fwr.Timeout = 20;
755
756                         BinaryFormatter bf = new BinaryFormatter ();
757                         bf.AssemblyFormat = FormatterAssemblyStyle.Full;
758
759                         MemoryStream ms = new MemoryStream ();
760                         bf.Serialize (ms, fwr);
761                         ms.Position = 0;
762
763                         byte [] buffer = new byte [ms.Length];
764                         ms.Read (buffer, 0, buffer.Length);
765                         Assert.AreEqual (_serialized, buffer);
766                 }
767
768                 [Test]
769                 public void Deserialize ()
770                 {
771                         MemoryStream ms = new MemoryStream ();
772                         ms.Write (_serialized, 0, _serialized.Length);
773                         ms.Position = 0;
774
775                         BinaryFormatter bf = new BinaryFormatter ();
776                         FileWebRequest req = (FileWebRequest) bf.Deserialize (ms);
777                         Assert.AreEqual ("CGN", req.ConnectionGroupName, "#A1");
778                         Assert.AreEqual (10, req.ContentLength, "#A2");
779                         Assert.AreEqual ("image/png", req.ContentType, "#A3");
780                         Assert.IsNull (req.Credentials, "#A4");
781                         Assert.AreEqual ("PUT", req.Method, "#A5");
782                         Assert.IsFalse (req.PreAuthenticate, "#A6");
783                         Assert.AreEqual ("file://test.txt/", req.RequestUri.AbsoluteUri, "#A7");
784                         Assert.AreEqual (20, req.Timeout, "#A8");
785
786                         WebHeaderCollection headers = req.Headers;
787                         Assert.IsNotNull (headers, "#C1");
788                         Assert.AreEqual (2, headers.Count, "#C2");
789                         Assert.AreEqual ("Content-Type", req.Headers.GetKey (0), "#C3");
790                         Assert.AreEqual ("image/png", req.Headers.Get (0), "#C4");
791                         Assert.AreEqual ("Disposition", req.Headers.GetKey (1), "#C5");
792                         Assert.AreEqual ("attach", req.Headers.Get (1), "#C6");
793
794                         WebProxy proxy = req.Proxy as WebProxy;
795                         Assert.IsNotNull (proxy, "#D1");
796                         Assert.AreEqual ("http://proxy.ximian.com/", proxy.Address.AbsoluteUri, "#D2");
797                         Assert.IsNotNull (proxy.BypassArrayList, "#D3");
798                         Assert.AreEqual (0, proxy.BypassArrayList.Count, "#D4");
799                         Assert.IsNotNull (proxy.BypassList, "#D5");
800                         Assert.AreEqual (0, proxy.BypassList.Length, "#D6");
801                         Assert.IsFalse (proxy.BypassProxyOnLocal, "#D7");
802                         Assert.IsNull (proxy.Credentials, "#D8");
803                 }
804
805                 private Uri GetTempFileUri ()
806                 {
807                         string tempFile = _tempFile;
808                         if (RunningOnUnix) {
809                                 // remove leading slash for absolute paths
810                                 tempFile = tempFile.TrimStart ('/');
811                         } else {
812                                 tempFile = tempFile.Replace ('\\', '/');
813                         }
814                         return new Uri ("file:///" + tempFile);
815                 }
816
817                 private bool RunningOnUnix {
818                         get {
819                                 // check for Unix platforms - see FAQ for more details
820                                 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
821                                 int platform = (int) Environment.OSVersion.Platform;
822                                 return ((platform == 4) || (platform == 128) || (platform == 6));
823                         }
824                 }
825
826                 private static readonly byte [] _serialized = new byte [] {
827                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
828                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
829                         0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
830                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
831                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
832                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
833                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
834                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
835                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
836                         0x00, 0x00, 0x19, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
837                         0x65, 0x74, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x52,
838                         0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x09, 0x00, 0x00, 0x00, 0x07,
839                         0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x05, 0x70, 0x72, 0x6f,
840                         0x78, 0x79, 0x03, 0x75, 0x72, 0x69, 0x13, 0x63, 0x6f, 0x6e, 0x6e,
841                         0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x47, 0x72, 0x6f, 0x75, 0x70,
842                         0x4e, 0x61, 0x6d, 0x65, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
843                         0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e,
844                         0x67, 0x74, 0x68, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74,
845                         0x0a, 0x66, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
846                         0x0f, 0x70, 0x72, 0x65, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
847                         0x69, 0x63, 0x61, 0x74, 0x65, 0x04, 0x04, 0x04, 0x01, 0x01, 0x00,
848                         0x00, 0x03, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
849                         0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64,
850                         0x65, 0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
851                         0x6e, 0x02, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79, 0x73, 0x74, 0x65,
852                         0x6d, 0x2e, 0x4e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x50, 0x72,
853                         0x6f, 0x78, 0x79, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73,
854                         0x74, 0x65, 0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00,
855                         0x09, 0x08, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
856                         0x4f, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73,
857                         0x73, 0x01, 0x02, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00,
858                         0x09, 0x04, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00, 0x00, 0x06,
859                         0x06, 0x00, 0x00, 0x00, 0x03, 0x43, 0x47, 0x4e, 0x06, 0x07, 0x00,
860                         0x00, 0x00, 0x03, 0x50, 0x55, 0x54, 0x0a, 0x00, 0x00, 0x00, 0x00,
861                         0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, 0xf8, 0xff, 0xff,
862                         0xff, 0x14, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49, 0x4f,
863                         0x2e, 0x46, 0x69, 0x6c, 0x65, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73,
864                         0x01, 0x00, 0x00, 0x00, 0x07, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5f,
865                         0x5f, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x00,
866                         0x00, 0x00, 0x1e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e,
867                         0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x48, 0x65, 0x61, 0x64, 0x65,
868                         0x72, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
869                         0x05, 0x00, 0x00, 0x00, 0x05, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x01,
870                         0x30, 0x01, 0x32, 0x01, 0x31, 0x01, 0x33, 0x00, 0x01, 0x01, 0x01,
871                         0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x06,
872                         0x09, 0x00, 0x00, 0x00, 0x0c, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
873                         0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x06, 0x0a, 0x00, 0x00, 0x00,
874                         0x09, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x06,
875                         0x0b, 0x00, 0x00, 0x00, 0x0b, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73,
876                         0x69, 0x74, 0x69, 0x6f, 0x6e, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x06,
877                         0x61, 0x74, 0x74, 0x61, 0x63, 0x68, 0x05, 0x04, 0x00, 0x00, 0x00,
878                         0x13, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4e, 0x65, 0x74,
879                         0x2e, 0x57, 0x65, 0x62, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x04, 0x00,
880                         0x00, 0x00, 0x0e, 0x5f, 0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4f,
881                         0x6e, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x0d, 0x5f, 0x50, 0x72, 0x6f,
882                         0x78, 0x79, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x0b, 0x5f,
883                         0x42, 0x79, 0x70, 0x61, 0x73, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x16,
884                         0x5f, 0x55, 0x73, 0x65, 0x44, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74,
885                         0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x73,
886                         0x00, 0x04, 0x02, 0x00, 0x01, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
887                         0x6d, 0x2e, 0x55, 0x72, 0x69, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02,
888                         0x00, 0x00, 0x00, 0x00, 0x09, 0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00,
889                         0x05, 0x05, 0x00, 0x00, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65,
890                         0x6d, 0x2e, 0x55, 0x72, 0x69, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x41,
891                         0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x55, 0x72, 0x69, 0x01,
892                         0x02, 0x00, 0x00, 0x00, 0x06, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x66,
893                         0x69, 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e,
894                         0x74, 0x78, 0x74, 0x2f, 0x01, 0x0d, 0x00, 0x00, 0x00, 0x05, 0x00,
895                         0x00, 0x00, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x18, 0x68, 0x74, 0x74,
896                         0x70, 0x3a, 0x2f, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x78,
897                         0x69, 0x6d, 0x69, 0x61, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x0b
898                 };
899         }
900 }