[System] Ignore FtpWebRequestTest.DownloadFile2_v6 test when IPv6 isn't available...
[mono.git] / mcs / class / System / Test / System.Net / FtpWebRequestTest.cs
1 //
2 // FtpWebRequestTest.cs - NUnit Test Cases for System.Net.FtpWebRequest
3 //
4 // Authors:
5 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
6 //      Gonzalo Paniagua Javier <gonzalo@novell.com>
7 //
8 // Copyright (c) 2006,2007,2008 Novell, Inc. (http://www.novell.com)
9 //
10 using NUnit.Framework;
11 using System;
12 using System.Collections.Generic;
13 using System.IO;
14 using System.Net;
15 using System.Net.Sockets;
16 using System.Text;
17 using System.Threading;
18
19 namespace MonoTests.System.Net 
20 {
21         [TestFixture]
22         public class FtpWebRequestTest
23         {
24                 FtpWebRequest _defaultRequest;
25                 FtpWebRequest defaultRequest {
26                         get { return _defaultRequest ?? (_defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com")); }
27                 }
28                 
29                 private string _tempDirectory;
30                 private string _tempFile;
31
32                 [SetUp]
33                 public void SetUp ()
34                 {
35                         _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
36                         _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp");
37                         if (!Directory.Exists (_tempDirectory)) {
38                                 Directory.CreateDirectory (_tempDirectory);
39                         } else {
40                                 // ensure no files are left over from previous runs
41                                 string [] files = Directory.GetFiles (_tempDirectory, "*");
42                                 foreach (string file in files)
43                                         File.Delete (file);
44                         }
45                 }
46
47                 [TearDown]
48                 public void TearDown ()
49                 {
50                         if (Directory.Exists (_tempDirectory))
51                                 Directory.Delete (_tempDirectory, true);
52                 }
53
54                 [Test]
55                 public void ContentLength ()
56                 {
57                         try {
58                                 long l = defaultRequest.ContentLength;
59 #if FEATURE_NO_BSD_SOCKETS
60                                 Assert.Fail ("#1a");
61                         } catch (PlatformNotSupportedException) {
62                                 // OK.
63 #else
64                         } catch (NotSupportedException) {
65                                 Assert.Fail ("#1"); // Not overriden
66 #endif
67                         }
68
69                         try {
70                                 defaultRequest.ContentLength = 2;
71 #if FEATURE_NO_BSD_SOCKETS
72                                 Assert.Fail ("#2a");
73                         } catch (PlatformNotSupportedException) {
74                                 // OK.
75 #else
76                         } catch (NotSupportedException) {
77                                 Assert.Fail ("#2"); // Not overriden
78 #endif
79                         }
80                 }
81
82                 [Test]
83                 public void ContentType ()
84                 {
85                         try {
86                                 string t = defaultRequest.ContentType;
87                                 Assert.Fail ("#1");
88                         } catch (NotSupportedException) {
89                         }
90
91                         try {
92                                 defaultRequest.ContentType = String.Empty;
93                                 Assert.Fail ("#2");
94                         } catch (NotSupportedException) {
95                         }
96                 }
97
98                 [Test]
99 #if FEATURE_NO_BSD_SOCKETS
100                 [ExpectedException (typeof (PlatformNotSupportedException))]
101 #endif
102                 public void ContentOffset ()
103                 {
104                         try {
105                                 defaultRequest.ContentOffset = -2;
106                                 Assert.Fail ("#1");
107                         } catch (ArgumentOutOfRangeException) {
108                         }
109                 }
110
111                 [Test]
112 #if FEATURE_NO_BSD_SOCKETS
113                 [ExpectedException (typeof (PlatformNotSupportedException))]
114 #endif
115                 public void Credentials ()
116                 {
117                         try {
118                                 defaultRequest.Credentials = null;
119                                 Assert.Fail ("#1");
120                         } catch (ArgumentNullException) {
121                         }
122
123                 }
124
125                 [Test]
126 #if FEATURE_NO_BSD_SOCKETS
127                 [ExpectedException (typeof (PlatformNotSupportedException))]
128 #endif
129                 public void Method ()
130                 {
131                         try {
132                                 defaultRequest.Method = null;
133                                 Assert.Fail ("#1");
134                         } catch (ArgumentNullException) {
135                         }
136
137                         try {
138                                 defaultRequest.Method = String.Empty;
139                                 Assert.Fail ("#2");
140                         } catch (ArgumentException) {
141                         }
142
143                         try {
144                                 defaultRequest.Method = "WrongValue";
145                                 Assert.Fail ("#3");
146                         } catch (ArgumentException) {
147                         }
148                 }
149
150                 [Test]
151                 public void PreAuthenticate ()
152                 {
153                         try {
154                                 bool p = defaultRequest.PreAuthenticate;
155                                 Assert.Fail ("#1");
156                         } catch (NotSupportedException) {
157                         }
158
159                         try {
160                                 defaultRequest.PreAuthenticate = true;
161                         } catch (NotSupportedException) {
162                         }
163                 }
164
165                 [Test]
166 #if FEATURE_NO_BSD_SOCKETS
167                 [ExpectedException (typeof (PlatformNotSupportedException))]
168 #endif
169                 public void ReadWriteTimeout ()
170                 {
171                         try {
172                                 defaultRequest.ReadWriteTimeout = -2;
173                                 Assert.Fail ("#2");
174                         } catch (ArgumentOutOfRangeException) {
175                         }
176                 }
177
178                 [Test]
179 #if FEATURE_NO_BSD_SOCKETS
180                 [ExpectedException (typeof (PlatformNotSupportedException))]
181 #endif
182                 public void Timeout ()
183                 {
184                         try {
185                                 defaultRequest.Timeout = -2;
186                                 Assert.Fail ("#2");
187                         } catch (ArgumentOutOfRangeException) {
188                         }
189                 }
190                 
191                 [Test]
192 #if FEATURE_NO_BSD_SOCKETS
193                 [ExpectedException (typeof (PlatformNotSupportedException))]
194 #endif
195                 public void DefaultValues ()
196                 {
197                         FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
198                         
199                         Assert.AreEqual (0, request.ContentOffset, "ContentOffset");
200                         Assert.AreEqual (false, request.EnableSsl, "EnableSsl");
201                         // FIXME: Disabled this one by now. KeepAlive is not well supported.
202                         // Assert.AreEqual (true, request.KeepAlive, "KeepAlive");
203                         Assert.AreEqual (WebRequestMethods.Ftp.DownloadFile, request.Method, "#1");
204                         Assert.AreEqual (300000, request.ReadWriteTimeout, "ReadWriteTimeout");
205                         Assert.IsNull (request.RenameTo, "RenameTo");
206                         Assert.AreEqual (true, request.UseBinary, "UseBinary");
207                         Assert.AreEqual (100000, request.Timeout, "Timeout");
208                         Assert.AreEqual (true, request.UsePassive, "UsePassive");
209                 }
210
211                 [Test]
212 #if FEATURE_NO_BSD_SOCKETS
213                 [ExpectedException (typeof (PlatformNotSupportedException))]
214 #endif
215                 public void RenameTo ()
216                 {
217                         try {
218                                 defaultRequest.RenameTo = null;
219                                 Assert.Fail ("#1");
220                         } catch (ArgumentException) {
221                         }
222
223                         try {
224                                 defaultRequest.RenameTo = String.Empty;
225                                 Assert.Fail ("#2");
226                         } catch (ArgumentException) {
227                         }
228                 }
229
230                 [Test]
231 #if FEATURE_NO_BSD_SOCKETS
232                 [ExpectedException (typeof (PlatformNotSupportedException))]
233 #endif
234                 public void UploadFile1_v4 ()
235                 {
236                         UploadFile1 (false);
237                 }
238
239                 [Test]
240 #if FEATURE_NO_BSD_SOCKETS
241                 [ExpectedException (typeof (PlatformNotSupportedException))]
242 #endif
243                 public void UploadFile1_v6 ()
244                 {
245                         if (!Socket.OSSupportsIPv6)
246                                 Assert.Ignore ("IPv6 not supported.");
247
248                         UploadFile1 (true);
249                 }
250
251                 void UploadFile1 (bool ipv6)
252                 {
253                         ServerPut sp = new ServerPut (ipv6);
254                         sp.Start ();
255                         string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
256                         try {
257                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
258                                 ftp.KeepAlive = false;
259                                 ftp.Timeout = 5000;
260                                 ftp.Method = WebRequestMethods.Ftp.UploadFile;
261                                 ftp.ContentLength = 10;
262                                 ftp.UseBinary = true;
263                                 Stream stream = ftp.GetRequestStream ();
264                                 for (int i = 0; i < 10; i++)
265                                         stream.WriteByte ((byte)i);
266                                 stream.Close ();
267                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
268                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
269                                 Assert.AreEqual (10, sp.result.Count, "UP#02");
270                                 response.Close ();
271                         } catch (Exception) {
272                                 if (!String.IsNullOrEmpty (sp.Where))
273                                         throw new Exception (sp.Where);
274                                 throw;
275                         } finally {
276                                 sp.Stop ();
277                         }
278                 }
279
280                 [Test]
281 #if FEATURE_NO_BSD_SOCKETS
282                 [ExpectedException (typeof (PlatformNotSupportedException))]
283 #endif
284                 public void UploadFile_WebClient_v4 ()
285                 {
286                         UploadFile_WebClient (false);
287                 }
288
289                 [Test]
290 #if FEATURE_NO_BSD_SOCKETS
291                 [ExpectedException (typeof (PlatformNotSupportedException))]
292 #endif
293                 public void UploadFile_WebClient_v6 ()
294                 {
295                         if (!Socket.OSSupportsIPv6)
296                                 Assert.Ignore ("IPv6 not supported.");
297
298                         UploadFile_WebClient (true);
299                 }
300
301                 public void UploadFile_WebClient (bool ipv6)
302                 {
303                         ServerPut sp = new ServerPut (ipv6);
304                         File.WriteAllText (_tempFile, "0123456789");
305                         sp.Start ();
306
307                         using (WebClient m_WebClient = new WebClient())
308                         {
309                                 string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
310                                 
311                                 m_WebClient.UploadFile(uri, _tempFile);
312                         }
313                         Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
314             
315                         sp.Stop ();
316                 }
317
318                 [Test]
319 #if FEATURE_NO_BSD_SOCKETS
320                 [ExpectedException (typeof (PlatformNotSupportedException))]
321 #endif
322                 public void DownloadFile1_v4 ()
323                 {
324                         DownloadFile (new ServerDownload (false));
325                 }
326
327                 [Test]
328 #if FEATURE_NO_BSD_SOCKETS
329                 [ExpectedException (typeof (PlatformNotSupportedException))]
330 #endif
331                 public void DownloadFile1_v6 ()
332                 {
333                         if (!Socket.OSSupportsIPv6)
334                                 Assert.Ignore ("IPv6 not supported.");
335
336                         DownloadFile (new ServerDownload (true));
337                 }
338
339                 [Test]
340 #if FEATURE_NO_BSD_SOCKETS
341                 [ExpectedException (typeof (PlatformNotSupportedException))]
342 #endif
343                 public void DownloadFileNonLatinChars ()
344                 {
345                         string filename = "\u0411\u0430\u0448\u043DRowan-\u041F\u0435\u0441\u043D\u043F\u0440\u043E\u043C\u043E\u043D\u0430\u0445\u0430\u0422\u0435\u043E\u0434\u043E\u0440\u0443\u0441\u0430\u0438\u0437\u0413\u0430\u043C\u043C\u0435\u043B\u044C\u043D\u0430.mp3";
346                         DownloadFile (new ServerDownload (null, null, filename, false), "ftp://{0}:{1}/" + filename);
347                 }
348
349                 void DownloadFile (ServerDownload sp, string uriTemplate = "ftp://{0}:{1}/file.txt")
350                 {
351                         sp.Start ();
352                         string uri = String.Format (uriTemplate, EncloseIPv6 (sp.IPAddress), sp.Port);
353                         try {
354                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
355                                 ftp.KeepAlive = false;
356                                 ftp.Timeout = 5000;
357                                 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
358                                 ftp.UseBinary = true;
359                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
360                                 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
361                                 using (Stream st = response.GetResponseStream ()) {
362                                 }
363                                 // This should be "220 Bye" or similar (no KeepAlive)
364                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
365                                 response.Close ();
366                         } catch (Exception) {
367                                 if (!String.IsNullOrEmpty (sp.Where))
368                                         throw new Exception (sp.Where);
369                                 throw;
370                         } finally {
371                                 sp.Stop ();
372                         }
373                 }
374
375                 [Test]
376 #if FEATURE_NO_BSD_SOCKETS
377                 [ExpectedException (typeof (PlatformNotSupportedException))]
378 #endif
379                 public void DownloadFile2_v4 ()
380                 {
381                         // Some embedded FTP servers in Industrial Automation Hardware report
382                         // the PWD using backslashes, but allow forward slashes for CWD.
383                         DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, false));
384                 }
385
386                 [Test]
387 #if FEATURE_NO_BSD_SOCKETS
388                 [ExpectedException (typeof (PlatformNotSupportedException))]
389 #endif
390                 public void DownloadFile2_v6 ()
391                 {
392                         if (!Socket.OSSupportsIPv6)
393                                 Assert.Ignore ("IPv6 not supported.");
394
395                         // Some embedded FTP servers in Industrial Automation Hardware report
396                         // the PWD using backslashes, but allow forward slashes for CWD.
397                         DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, true));
398                 }
399
400                 [Test]
401 #if FEATURE_NO_BSD_SOCKETS
402                 [ExpectedException (typeof (PlatformNotSupportedException))]
403 #endif
404                 public void DeleteFile1_v4 ()
405                 {
406                         DeleteFile1 (false);
407                 }
408
409                 [Test]
410 #if FEATURE_NO_BSD_SOCKETS
411                 [ExpectedException (typeof (PlatformNotSupportedException))]
412 #endif
413                 public void DeleteFile1_v6 ()
414                 {
415                         if (!Socket.OSSupportsIPv6)
416                                 Assert.Ignore ("IPv6 not supported.");
417
418                         DeleteFile1 (true);
419                 }
420
421                 void DeleteFile1 (bool ipv6)
422                 {
423                         ServerDeleteFile sp = new ServerDeleteFile (ipv6);
424                         sp.Start ();
425                         string uri = String.Format ("ftp://{0}:{1}/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
426                         try {
427                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
428                                 Console.WriteLine (ftp.RequestUri);
429                                 ftp.KeepAlive = false;
430                                 ftp.Timeout = 5000;
431                                 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
432                                 ftp.UseBinary = true;
433                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
434                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
435                                 response.Close ();
436                         } catch (Exception e) {
437                                 Console.WriteLine (e);
438                                 if (!String.IsNullOrEmpty (sp.Where))
439                                         throw new Exception (sp.Where);
440                                 throw;
441                         } finally {
442                                 sp.Stop ();
443                         }
444                 }
445
446                 [Test]
447 #if FEATURE_NO_BSD_SOCKETS
448                 [ExpectedException (typeof (PlatformNotSupportedException))]
449 #endif
450                 public void ListDirectory1_v4 ()
451                 {
452                         ListDirectory1 (false);
453                 }
454
455                 [Test]
456 #if FEATURE_NO_BSD_SOCKETS
457                 [ExpectedException (typeof (PlatformNotSupportedException))]
458 #endif
459                 public void ListDirectory1_v6 ()
460                 {
461                         if (!Socket.OSSupportsIPv6)
462                                 Assert.Ignore ("IPv6 not supported.");
463
464                         ListDirectory1 (true);
465                 }
466
467                 void ListDirectory1 (bool ipv6)
468                 {
469                         ServerListDirectory sp = new ServerListDirectory (ipv6);
470                         sp.Start ();
471                         string uri = String.Format ("ftp://{0}:{1}/somedir/", EncloseIPv6 (sp.IPAddress), sp.Port);
472                         try {
473                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
474                                 Console.WriteLine (ftp.RequestUri);
475                                 ftp.KeepAlive = false;
476                                 ftp.Timeout = 5000;
477                                 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
478                                 ftp.UseBinary = true;
479                                 using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
480                                         StreamReader reader = new StreamReader (response.GetResponseStream ());
481                                         string result = reader.ReadToEnd ();
482                                         Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
483                                 }
484                         } catch (Exception e) {
485                                 Console.WriteLine (e);
486                                 if (!String.IsNullOrEmpty (sp.Where))
487                                         throw new Exception (sp.Where);
488                                 throw;
489                         } finally {
490                                 sp.Stop ();
491                         }
492                 }
493
494                 string EncloseIPv6 (IPAddress address)
495                 {
496                         if (address.AddressFamily == AddressFamily.InterNetwork)
497                                 return address.ToString ();
498                         
499                         return String.Format ("[{0}]", address.ToString ());
500                 }
501
502                 class ServerListDirectory : FtpServer {
503                         public ServerListDirectory (bool ipv6)
504                                 : base (ipv6)
505                         {
506                         }
507
508                         protected override void Run ()
509                         {
510                                 Socket client = control.Accept ();
511                                 NetworkStream ns = new NetworkStream (client, false);
512                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
513                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
514                                 if (!DoAnonymousLogin (writer, reader)) {
515                                         client.Close ();
516                                         return;
517                                 }
518
519                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
520                                         client.Close ();
521                                         return;
522                                 }
523
524                                 string str = reader.ReadLine ();
525                                 string resp = FormatPassiveResponse (str);
526                                 if (resp == null) {
527                                         client.Close ();
528                                         return;
529                                 }
530                                 writer.WriteLine (resp);
531                                 writer.Flush ();
532
533                                 str = reader.ReadLine ();
534                                 if (str != "LIST") {
535                                         Where = "LIST - '" + str + "'";
536                                         client.Close ();
537                                         return;
538                                 }
539                                 writer.WriteLine ("150 Here comes the directory listing");
540                                 writer.Flush ();
541
542                                 Socket data_cnc = data.Accept ();
543                                 byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x    2 ftp      ftp          4096 Oct 27 20:17 tests");
544                                 data_cnc.Send (dontcare, 1, SocketFlags.None);
545                                 data_cnc.Close ();
546                                 writer.WriteLine ("226 Directory send Ok");
547                                 writer.Flush ();
548                                 if (!EndConversation (writer, reader)) {
549                                         client.Close ();
550                                         return;
551                                 }
552                                 client.Close ();
553                         }
554                 }
555
556                 class ServerDeleteFile : FtpServer {
557                         public ServerDeleteFile (bool ipv6)
558                                 : base (ipv6)
559                         {
560                         }
561
562                         protected override void Run ()
563                         {
564                                 Socket client = control.Accept ();
565                                 NetworkStream ns = new NetworkStream (client, false);
566                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
567                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
568                                 if (!DoAnonymousLogin (writer, reader)) {
569                                         client.Close ();
570                                         return;
571                                 }
572
573                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
574                                         client.Close ();
575                                         return;
576                                 }
577
578                                 string str = reader.ReadLine ();
579                                 if (str.Trim () != "DELE file.txt") {
580                                         Where = "DELE - " + str;
581                                         client.Close ();
582                                         return;
583                                 }
584                                 writer.WriteLine ("250 Delete operation successful");
585                                 writer.Flush ();
586                                 if (!EndConversation (writer, reader)) {
587                                         client.Close ();
588                                         return;
589                                 }
590                                 client.Close ();
591                         }
592                 }
593
594                 class ServerDownload : FtpServer {
595
596                         string Pwd, Cwd, Filename;
597
598                         public ServerDownload (bool ipv6)
599                                 : this (null, null, null, ipv6)
600                         {
601                         }
602
603                         public ServerDownload (string pwd, string cwd, string filename, bool ipv6)
604                                 : base (ipv6)
605                         {
606                                 Pwd = pwd ?? "/home/someuser";
607                                 Cwd = cwd ?? "/home/someuser/";
608                                 Filename = filename ?? "file.txt";
609                         }
610
611                         protected override void Run ()
612                         {
613                                 Socket client = control.Accept ();
614                                 NetworkStream ns = new NetworkStream (client, false);
615                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
616                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
617                                 if (!DoAnonymousLogin (writer, reader)) {
618                                         client.Close ();
619                                         return;
620                                 }
621
622                                 if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
623                                         client.Close ();
624                                         return;
625                                 }
626
627                                 string str = reader.ReadLine ();
628                                 string resp = FormatPassiveResponse (str);
629                                 if (resp == null) {
630                                         client.Close ();
631                                         return;
632                                 }
633                                 writer.WriteLine (resp);
634                                 writer.Flush ();
635
636                                 str = reader.ReadLine ();
637                                 if (str != $"RETR {Filename}") {
638                                         Where = $"RETR - got: {str}, expected: RETR {Filename}";
639                                         client.Close ();
640                                         return;
641                                 }
642                                 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
643                                 writer.Flush ();
644
645                                 Socket data_cnc = data.Accept ();
646                                 byte [] dontcare = new byte [1];
647                                 data_cnc.Receive (dontcare, 1, SocketFlags.None);
648                                 data_cnc.Close ();
649                                 writer.WriteLine ("226 File send Ok");
650                                 writer.Flush ();
651                                 if (!EndConversation (writer, reader)) {
652                                         client.Close ();
653                                         return;
654                                 }
655                                 client.Close ();
656                         }
657                 }
658
659                 class ServerPut : FtpServer {
660                         public List<byte> result = new List<byte> ();
661                         
662                         public ServerPut (bool ipv6)
663                                 : base (ipv6)
664                         {
665                         }
666
667                         protected override void Run ()
668                         {
669                                 Socket client = control.Accept ();
670                                 NetworkStream ns = new NetworkStream (client, false);
671                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
672                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
673                                 if (!DoAnonymousLogin (writer, reader)) {
674                                         client.Close ();
675                                         return;
676                                 }
677
678                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
679                                         client.Close ();
680                                         return;
681                                 }
682
683                                 string str = reader.ReadLine ();
684                                 string resp = FormatPassiveResponse (str);
685                                 if (resp == null) {
686                                         client.Close ();
687                                         return;
688                                 }
689                                 writer.WriteLine (resp);
690                                 writer.Flush ();
691
692                                 str = reader.ReadLine ();
693                                 if (str != "STOR file.txt") {
694                                         Where = "STOR - " + str;
695                                         client.Close ();
696                                         return;
697                                 }
698                                 writer.WriteLine ("150 Ok to send data");
699                                 writer.Flush ();
700
701                                 Socket data_cnc = data.Accept ();
702                                 var datastr = new NetworkStream (data_cnc, false);
703                                 int ch;
704                                 while ((ch = datastr.ReadByte ()) != -1){
705                                         result.Add ((byte)ch);
706
707                                 }
708                                 data_cnc.Close ();
709                                 writer.WriteLine ("226 File received Ok");
710                                 writer.Flush ();
711                                 if (!EndConversation (writer, reader)) {
712                                         client.Close ();
713                                         return;
714                                 }
715                                 client.Close ();
716                         }
717                 }
718
719                 abstract class FtpServer {
720                         protected Socket control;
721                         protected Socket data;
722                         protected ManualResetEvent evt;
723                         protected bool ipv6;
724                         public string Where = "";
725
726                         public FtpServer (bool ipv6)
727                         {
728                                 control = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
729                                 control.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
730                                 control.Listen (1);
731                                 data = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
732                                 data.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
733                                 data.Listen (1);
734                                 this.ipv6 = ipv6;
735                         }
736
737                         public void Start ()
738                         {
739                                 evt = new ManualResetEvent (false);
740                                 Thread th = new Thread (new ThreadStart (Run));
741                                 th.Start ();
742                         }
743
744                         public void Stop ()
745                         {
746                                 evt.Set ();
747                                 data.Close ();
748                                 control.Close ();
749                         }
750
751                         // PWD, CWD and TYPE I (type could be moved out of here)
752                         protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
753                         {
754                                 string str = reader.ReadLine ();
755                                 if (!str.StartsWith ("OPTS utf8 on")) {
756                                         Where = "OPTS utf8 - " + str;
757                                         return false;
758                                 }
759                                 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
760                                 writer.Flush ();
761                                 str = reader.ReadLine ();
762                                 if (!str.StartsWith ("PWD")) {
763                                         Where = "PWD - " + str;
764                                         return false;
765                                 }
766                                 writer.WriteLine ("257 \"{0}\"", pwd);
767                                 writer.Flush ();
768                                 str = reader.ReadLine ();
769                                 if (str != ("CWD " + cwd)) {
770                                         Where = "CWD - " + str;
771                                         return false;
772                                 }
773                                 writer.WriteLine ("250 Directory changed");
774                                 writer.Flush ();
775                                 str = reader.ReadLine ();
776                                 if (str != ("TYPE I")) {
777                                         Where = "TYPE - " + str;
778                                         return false;
779                                 }
780                                 writer.WriteLine ("200 Switching to binary mode");
781                                 writer.Flush ();
782                                 return true;
783                         }
784
785                         protected bool EndConversation (StreamWriter writer, StreamReader reader)
786                         {
787                                 string str = reader.ReadLine ();
788                                 if (str != "QUIT") {
789                                         Where = "QUIT";
790                                         return false;
791                                 }
792                                 writer.WriteLine ("220 Bye");
793                                 writer.Flush ();
794                                 Thread.Sleep (250);
795                                 return true;
796                         }
797
798                         protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
799                         {
800                                 writer.WriteLine ("220 Welcome to the jungle");
801                                 writer.Flush ();
802                                 string str = reader.ReadLine ();
803                                 if (!str.StartsWith ("USER ")) {
804                                         Where = "USER";
805                                         return false;
806                                 }
807                                 writer.WriteLine ("331 Say 'Mellon'");
808                                 writer.Flush ();
809                                 str = reader.ReadLine ();
810                                 if (!str.StartsWith ("PASS ")) {
811                                         Where = "PASS";
812                                         return false;
813                                 }
814                                 writer.WriteLine ("230 Logged in");
815                                 writer.Flush ();
816                                 return true;
817                         }
818
819                         protected string FormatPassiveResponse (string request)
820                         {
821                                 if (ipv6) {
822                                         if (request != "EPSV") {
823                                                 Where = "EPSV";
824                                                 return null;
825                                         }
826
827                                         IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
828                                         return String.Format ("229 Extended Passive (|||{0}|)", end_data.Port);
829                                 }
830                                 else {
831                                         if (request != "PASV") {
832                                                 Where = "PASV";
833                                                 return null;
834                                         }
835
836                                         IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
837                                         byte [] addr_bytes = end_data.Address.GetAddressBytes ();
838                                         byte [] port = new byte [2];
839                                         port[0] = (byte) ((end_data.Port >> 8) & 255);
840                                         port[1] = (byte) (end_data.Port & 255);
841                                         StringBuilder sb = new StringBuilder ("227 Passive (");
842                                         foreach (byte b in addr_bytes) {
843                                                 sb.AppendFormat ("{0},", b);    
844                                         }
845                                         sb.AppendFormat ("{0},", port [0]);     
846                                         sb.AppendFormat ("{0})", port [1]);     
847                                         return sb.ToString ();
848                                 }
849                         }
850
851                         public IPAddress IPAddress {
852                                 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
853                         }
854                         
855                         public int Port {
856                                 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
857                         }
858
859                         protected abstract void Run ();
860                 }
861         }
862 }
863
864