Merge pull request #3626 from lateralusX/jlorenss/win-api-family-support-eglib
[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 ()
235                 {
236                         ServerPut sp = new ServerPut ();
237                         sp.Start ();
238                         string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
239                         try {
240                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
241                                 ftp.KeepAlive = false;
242                                 ftp.Timeout = 5000;
243                                 ftp.Method = WebRequestMethods.Ftp.UploadFile;
244                                 ftp.ContentLength = 10;
245                                 ftp.UseBinary = true;
246                                 Stream stream = ftp.GetRequestStream ();
247                                 for (int i = 0; i < 10; i++)
248                                         stream.WriteByte ((byte)i);
249                                 stream.Close ();
250                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
251                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
252                                 Assert.AreEqual (10, sp.result.Count, "UP#02");
253                                 response.Close ();
254                         } catch (Exception) {
255                                 if (!String.IsNullOrEmpty (sp.Where))
256                                         throw new Exception (sp.Where);
257                                 throw;
258                         } finally {
259                                 sp.Stop ();
260                         }
261                 }
262
263                 [Test]
264 #if FEATURE_NO_BSD_SOCKETS
265                 [ExpectedException (typeof (PlatformNotSupportedException))]
266 #endif
267                 public void UploadFile_WebClient ()
268                 {
269                         ServerPut sp = new ServerPut ();
270                         File.WriteAllText (_tempFile, "0123456789");
271                         sp.Start ();
272
273                         using (WebClient m_WebClient = new WebClient())
274                         {
275                                 string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
276                                 
277                                 m_WebClient.UploadFile(uri, _tempFile);
278                         }
279                         Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
280             
281                         sp.Stop ();
282                 }
283
284                 [Test]
285 #if FEATURE_NO_BSD_SOCKETS
286                 [ExpectedException (typeof (PlatformNotSupportedException))]
287 #endif
288                 public void DownloadFile1 ()
289                 {
290                         DownloadFile (new ServerDownload ());
291                 }
292
293                 void DownloadFile (ServerDownload sp)
294                 {
295                         sp.Start ();
296                         string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
297                         try {
298                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
299                                 ftp.KeepAlive = false;
300                                 ftp.Timeout = 5000;
301                                 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
302                                 ftp.UseBinary = true;
303                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
304                                 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
305                                 using (Stream st = response.GetResponseStream ()) {
306                                 }
307                                 // This should be "220 Bye" or similar (no KeepAlive)
308                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
309                                 response.Close ();
310                         } catch (Exception) {
311                                 if (!String.IsNullOrEmpty (sp.Where))
312                                         throw new Exception (sp.Where);
313                                 throw;
314                         } finally {
315                                 sp.Stop ();
316                         }
317                 }
318
319                 [Test]
320 #if FEATURE_NO_BSD_SOCKETS
321                 [ExpectedException (typeof (PlatformNotSupportedException))]
322 #endif
323                 public void DownloadFile2 ()
324                 {
325                         // Some embedded FTP servers in Industrial Automation Hardware report
326                         // the PWD using backslashes, but allow forward slashes for CWD.
327                         DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/"));
328                 }
329
330                 [Test]
331 #if FEATURE_NO_BSD_SOCKETS
332                 [ExpectedException (typeof (PlatformNotSupportedException))]
333 #endif
334                 public void DeleteFile1 ()
335                 {
336                         ServerDeleteFile sp = new ServerDeleteFile ();
337                         sp.Start ();
338                         string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
339                         try {
340                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
341                                 Console.WriteLine (ftp.RequestUri);
342                                 ftp.KeepAlive = false;
343                                 ftp.Timeout = 5000;
344                                 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
345                                 ftp.UseBinary = true;
346                                 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
347                                 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
348                                 response.Close ();
349                         } catch (Exception e) {
350                                 Console.WriteLine (e);
351                                 if (!String.IsNullOrEmpty (sp.Where))
352                                         throw new Exception (sp.Where);
353                                 throw;
354                         } finally {
355                                 sp.Stop ();
356                         }
357                 }
358
359                 [Test]
360 #if FEATURE_NO_BSD_SOCKETS
361                 [ExpectedException (typeof (PlatformNotSupportedException))]
362 #endif
363                 public void ListDirectory1 ()
364                 {
365                         ServerListDirectory sp = new ServerListDirectory ();
366                         sp.Start ();
367                         string uri = String.Format ("ftp://{0}:{1}/somedir/", sp.IPAddress, sp.Port);
368                         try {
369                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
370                                 Console.WriteLine (ftp.RequestUri);
371                                 ftp.KeepAlive = false;
372                                 ftp.Timeout = 5000;
373                                 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
374                                 ftp.UseBinary = true;
375                                 using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
376                                         StreamReader reader = new StreamReader (response.GetResponseStream ());
377                                         string result = reader.ReadToEnd ();
378                                         Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
379                                 }
380                         } catch (Exception e) {
381                                 Console.WriteLine (e);
382                                 if (!String.IsNullOrEmpty (sp.Where))
383                                         throw new Exception (sp.Where);
384                                 throw;
385                         } finally {
386                                 sp.Stop ();
387                         }
388                 }
389
390                 class ServerListDirectory : FtpServer {
391                         protected override void Run ()
392                         {
393                                 Socket client = control.Accept ();
394                                 NetworkStream ns = new NetworkStream (client, false);
395                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
396                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
397                                 if (!DoAnonymousLogin (writer, reader)) {
398                                         client.Close ();
399                                         return;
400                                 }
401
402                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
403                                         client.Close ();
404                                         return;
405                                 }
406
407                                 string str = reader.ReadLine ();
408                                 if (str != "PASV") {
409                                         Where = "PASV";
410                                         client.Close ();
411                                         return;
412                                 }
413
414                                 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
415                                 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
416                                 byte [] port = new byte [2];
417                                 port[0] = (byte) ((end_data.Port >> 8) & 255);
418                                 port[1] = (byte) (end_data.Port & 255);
419                                 StringBuilder sb = new StringBuilder ("227 Passive (");
420                                 foreach (byte b in addr_bytes) {
421                                         sb.AppendFormat ("{0},", b);    
422                                 }
423                                 sb.AppendFormat ("{0},", port [0]);     
424                                 sb.AppendFormat ("{0})", port [1]);     
425                                 writer.WriteLine (sb.ToString ());
426                                 writer.Flush ();
427
428                                 str = reader.ReadLine ();
429                                 if (str != "LIST") {
430                                         Where = "LIST - '" + str + "'";
431                                         client.Close ();
432                                         return;
433                                 }
434                                 writer.WriteLine ("150 Here comes the directory listing");
435                                 writer.Flush ();
436
437                                 Socket data_cnc = data.Accept ();
438                                 byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x    2 ftp      ftp          4096 Oct 27 20:17 tests");
439                                 data_cnc.Send (dontcare, 1, SocketFlags.None);
440                                 data_cnc.Close ();
441                                 writer.WriteLine ("226 Directory send Ok");
442                                 writer.Flush ();
443                                 if (!EndConversation (writer, reader)) {
444                                         client.Close ();
445                                         return;
446                                 }
447                                 client.Close ();
448                         }
449                 }
450
451                 class ServerDeleteFile : FtpServer {
452                         protected override void Run ()
453                         {
454                                 Socket client = control.Accept ();
455                                 NetworkStream ns = new NetworkStream (client, false);
456                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
457                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
458                                 if (!DoAnonymousLogin (writer, reader)) {
459                                         client.Close ();
460                                         return;
461                                 }
462
463                                 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
464                                         client.Close ();
465                                         return;
466                                 }
467
468                                 string str = reader.ReadLine ();
469                                 if (str.Trim () != "DELE file.txt") {
470                                         Where = "DELE - " + str;
471                                         client.Close ();
472                                         return;
473                                 }
474                                 writer.WriteLine ("250 Delete operation successful");
475                                 writer.Flush ();
476                                 if (!EndConversation (writer, reader)) {
477                                         client.Close ();
478                                         return;
479                                 }
480                                 client.Close ();
481                         }
482                 }
483
484                 class ServerDownload : FtpServer {
485
486                         string Pwd, Cwd;
487
488                         public ServerDownload ()
489                                 : this (null, null)
490                         {
491                         }
492
493                         public ServerDownload (string pwd, string cwd)
494                         {
495                                 Pwd = pwd ?? "/home/someuser";
496                                 Cwd = cwd ?? "/home/someuser/";
497                         }
498
499                         protected override void Run ()
500                         {
501                                 Socket client = control.Accept ();
502                                 NetworkStream ns = new NetworkStream (client, false);
503                                 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
504                                 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
505                                 if (!DoAnonymousLogin (writer, reader)) {
506                                         client.Close ();
507                                         return;
508                                 }
509
510                                 if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
511                                         client.Close ();
512                                         return;
513                                 }
514
515                                 string str = reader.ReadLine ();
516                                 if (str != "PASV") {
517                                         Where = "PASV";
518                                         client.Close ();
519                                         return;
520                                 }
521
522                                 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
523                                 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
524                                 byte [] port = new byte [2];
525                                 port[0] = (byte) ((end_data.Port >> 8) & 255);
526                                 port[1] = (byte) (end_data.Port & 255);
527                                 StringBuilder sb = new StringBuilder ("227 Passive (");
528                                 foreach (byte b in addr_bytes) {
529                                         sb.AppendFormat ("{0},", b);    
530                                 }
531                                 sb.AppendFormat ("{0},", port [0]);     
532                                 sb.AppendFormat ("{0})", port [1]);     
533                                 writer.WriteLine (sb.ToString ());
534                                 writer.Flush ();
535
536                                 str = reader.ReadLine ();
537                                 if (str != "RETR file.txt") {
538                                         Where = "RETR - " + str;
539                                         client.Close ();
540                                         return;
541                                 }
542                                 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
543                                 writer.Flush ();
544
545                                 Socket data_cnc = data.Accept ();
546                                 byte [] dontcare = new byte [1];
547                                 data_cnc.Receive (dontcare, 1, SocketFlags.None);
548                                 data_cnc.Close ();
549                                 writer.WriteLine ("226 File send Ok");
550                                 writer.Flush ();
551                                 if (!EndConversation (writer, reader)) {
552                                         client.Close ();
553                                         return;
554                                 }
555                                 client.Close ();
556                         }
557                 }
558
559                 class ServerPut : FtpServer {
560                         public List<byte> result = new List<byte> ();
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/uploads/")) {
574                                         client.Close ();
575                                         return;
576                                 }
577
578                                 string str = reader.ReadLine ();
579                                 if (str != "PASV") {
580                                         Where = "PASV";
581                                         client.Close ();
582                                         return;
583                                 }
584
585                                 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
586                                 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
587                                 byte [] port = new byte [2];
588                                 port[0] = (byte) ((end_data.Port >> 8) & 255);
589                                 port[1] = (byte) (end_data.Port & 255);
590                                 StringBuilder sb = new StringBuilder ("227 Passive (");
591                                 foreach (byte b in addr_bytes) {
592                                         sb.AppendFormat ("{0},", b);    
593                                 }
594                                 sb.AppendFormat ("{0},", port [0]);     
595                                 sb.AppendFormat ("{0})", port [1]);     
596                                 writer.WriteLine (sb.ToString ());
597                                 writer.Flush ();
598
599                                 str = reader.ReadLine ();
600                                 if (str != "STOR file.txt") {
601                                         Where = "STOR - " + str;
602                                         client.Close ();
603                                         return;
604                                 }
605                                 writer.WriteLine ("150 Ok to send data");
606                                 writer.Flush ();
607
608                                 Socket data_cnc = data.Accept ();
609                                 var datastr = new NetworkStream (data_cnc, false);
610                                 int ch;
611                                 while ((ch = datastr.ReadByte ()) != -1){
612                                         result.Add ((byte)ch);
613
614                                 }
615                                 data_cnc.Close ();
616                                 writer.WriteLine ("226 File received Ok");
617                                 writer.Flush ();
618                                 if (!EndConversation (writer, reader)) {
619                                         client.Close ();
620                                         return;
621                                 }
622                                 client.Close ();
623                         }
624                 }
625
626                 abstract class FtpServer {
627                         protected Socket control;
628                         protected Socket data;
629                         protected ManualResetEvent evt;
630                         public string Where = "";
631
632                         public FtpServer ()
633                         {
634                                 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
635                                 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
636                                 control.Listen (1);
637                                 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
638                                 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
639                                 data.Listen (1);
640                         }
641
642                         public void Start ()
643                         {
644                                 evt = new ManualResetEvent (false);
645                                 Thread th = new Thread (new ThreadStart (Run));
646                                 th.Start ();
647                         }
648
649                         public void Stop ()
650                         {
651                                 evt.Set ();
652                                 data.Close ();
653                                 control.Close ();
654                         }
655
656                         // PWD, CWD and TYPE I (type could be moved out of here)
657                         protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
658                         {
659                                 string str = reader.ReadLine ();
660                                 if (!str.StartsWith ("OPTS utf8 on")) {
661                                         Where = "OPTS utf8 - " + str;
662                                         return false;
663                                 }
664                                 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
665                                 writer.Flush ();
666                                 str = reader.ReadLine ();
667                                 if (!str.StartsWith ("PWD")) {
668                                         Where = "PWD - " + str;
669                                         return false;
670                                 }
671                                 writer.WriteLine ("257 \"{0}\"", pwd);
672                                 writer.Flush ();
673                                 str = reader.ReadLine ();
674                                 if (str != ("CWD " + cwd)) {
675                                         Where = "CWD - " + str;
676                                         return false;
677                                 }
678                                 writer.WriteLine ("250 Directory changed");
679                                 writer.Flush ();
680                                 str = reader.ReadLine ();
681                                 if (str != ("TYPE I")) {
682                                         Where = "TYPE - " + str;
683                                         return false;
684                                 }
685                                 writer.WriteLine ("200 Switching to binary mode");
686                                 writer.Flush ();
687                                 return true;
688                         }
689
690                         protected bool EndConversation (StreamWriter writer, StreamReader reader)
691                         {
692                                 string str = reader.ReadLine ();
693                                 if (str != "QUIT") {
694                                         Where = "QUIT";
695                                         return false;
696                                 }
697                                 writer.WriteLine ("220 Bye");
698                                 writer.Flush ();
699                                 Thread.Sleep (250);
700                                 return true;
701                         }
702
703                         protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
704                         {
705                                 writer.WriteLine ("220 Welcome to the jungle");
706                                 writer.Flush ();
707                                 string str = reader.ReadLine ();
708                                 if (!str.StartsWith ("USER ")) {
709                                         Where = "USER";
710                                         return false;
711                                 }
712                                 writer.WriteLine ("331 Say 'Mellon'");
713                                 writer.Flush ();
714                                 str = reader.ReadLine ();
715                                 if (!str.StartsWith ("PASS ")) {
716                                         Where = "PASS";
717                                         return false;
718                                 }
719                                 writer.WriteLine ("230 Logged in");
720                                 writer.Flush ();
721                                 return true;
722                         }
723                         
724                         public IPAddress IPAddress {
725                                 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
726                         }
727                         
728                         public int Port {
729                                 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
730                         }
731
732                         protected abstract void Run ();
733                 }
734         }
735 }
736
737