[System] Ignore FtpWebRequestTest.DownloadFile2_v6 test when IPv6 isn't available...
[mono.git] / mcs / class / System / Test / System.Net / FtpWebRequestTest.cs
index 17995f75c25d1c52cd86bd0646e39e345b404c30..dd5df82310ba05891cc02abe68a8a8513f71d9ae 100644 (file)
@@ -5,11 +5,11 @@
 //     Carlos Alberto Cortez <calberto.cortez@gmail.com>
 //     Gonzalo Paniagua Javier <gonzalo@novell.com>
 //
-// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2006,2007,2008 Novell, Inc. (http://www.novell.com)
 //
-#if NET_2_0
 using NUnit.Framework;
 using System;
+using System.Collections.Generic;
 using System.IO;
 using System.Net;
 using System.Net.Sockets;
@@ -21,27 +21,61 @@ namespace MonoTests.System.Net
        [TestFixture]
        public class FtpWebRequestTest
        {
-               FtpWebRequest defaultRequest;
+               FtpWebRequest _defaultRequest;
+               FtpWebRequest defaultRequest {
+                       get { return _defaultRequest ?? (_defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com")); }
+               }
                
-               [TestFixtureSetUp]
-               public void Init ()
+               private string _tempDirectory;
+               private string _tempFile;
+
+               [SetUp]
+               public void SetUp ()
                {
-                       defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
+                       _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
+                       _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp");
+                       if (!Directory.Exists (_tempDirectory)) {
+                               Directory.CreateDirectory (_tempDirectory);
+                       } else {
+                               // ensure no files are left over from previous runs
+                               string [] files = Directory.GetFiles (_tempDirectory, "*");
+                               foreach (string file in files)
+                                       File.Delete (file);
+                       }
                }
-               
+
+               [TearDown]
+               public void TearDown ()
+               {
+                       if (Directory.Exists (_tempDirectory))
+                               Directory.Delete (_tempDirectory, true);
+               }
+
                [Test]
                public void ContentLength ()
                {
                        try {
                                long l = defaultRequest.ContentLength;
+#if FEATURE_NO_BSD_SOCKETS
+                               Assert.Fail ("#1a");
+                       } catch (PlatformNotSupportedException) {
+                               // OK.
+#else
                        } catch (NotSupportedException) {
                                Assert.Fail ("#1"); // Not overriden
+#endif
                        }
 
                        try {
                                defaultRequest.ContentLength = 2;
+#if FEATURE_NO_BSD_SOCKETS
+                               Assert.Fail ("#2a");
+                       } catch (PlatformNotSupportedException) {
+                               // OK.
+#else
                        } catch (NotSupportedException) {
                                Assert.Fail ("#2"); // Not overriden
+#endif
                        }
                }
 
@@ -62,6 +96,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ContentOffset ()
                {
                        try {
@@ -72,6 +109,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Credentials ()
                {
                        try {
@@ -83,6 +123,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Method ()
                {
                        try {
@@ -120,6 +163,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReadWriteTimeout ()
                {
                        try {
@@ -130,6 +176,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Timeout ()
                {
                        try {
@@ -140,6 +189,9 @@ namespace MonoTests.System.Net
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void DefaultValues ()
                {
                        FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
@@ -157,6 +209,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void RenameTo ()
                {
                        try {
@@ -173,99 +228,465 @@ namespace MonoTests.System.Net
                }
 
                [Test]
-               public void UploadFile1 ()
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void UploadFile1_v4 ()
                {
-                       ServerPut sp = new ServerPut ();
+                       UploadFile1 (false);
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void UploadFile1_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       UploadFile1 (true);
+               }
+
+               void UploadFile1 (bool ipv6)
+               {
+                       ServerPut sp = new ServerPut (ipv6);
                        sp.Start ();
-                       string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
+                       string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
                        try {
                                FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
+                               ftp.KeepAlive = false;
                                ftp.Timeout = 5000;
                                ftp.Method = WebRequestMethods.Ftp.UploadFile;
-                               ftp.ContentLength = 1;
+                               ftp.ContentLength = 10;
                                ftp.UseBinary = true;
                                Stream stream = ftp.GetRequestStream ();
-                               stream.WriteByte (0);
+                               for (int i = 0; i < 10; i++)
+                                       stream.WriteByte ((byte)i);
                                stream.Close ();
                                FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
-                               Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "#01");
+                               Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
+                               Assert.AreEqual (10, sp.result.Count, "UP#02");
+                               response.Close ();
+                       } catch (Exception) {
+                               if (!String.IsNullOrEmpty (sp.Where))
+                                       throw new Exception (sp.Where);
+                               throw;
+                       } finally {
+                               sp.Stop ();
+                       }
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void UploadFile_WebClient_v4 ()
+               {
+                       UploadFile_WebClient (false);
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void UploadFile_WebClient_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       UploadFile_WebClient (true);
+               }
+
+               public void UploadFile_WebClient (bool ipv6)
+               {
+                       ServerPut sp = new ServerPut (ipv6);
+                       File.WriteAllText (_tempFile, "0123456789");
+                       sp.Start ();
+
+                       using (WebClient m_WebClient = new WebClient())
+                       {
+                               string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
+                               
+                               m_WebClient.UploadFile(uri, _tempFile);
+                       }
+                       Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
+           
+                       sp.Stop ();
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFile1_v4 ()
+               {
+                       DownloadFile (new ServerDownload (false));
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFile1_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       DownloadFile (new ServerDownload (true));
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFileNonLatinChars ()
+               {
+                       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";
+                       DownloadFile (new ServerDownload (null, null, filename, false), "ftp://{0}:{1}/" + filename);
+               }
+
+               void DownloadFile (ServerDownload sp, string uriTemplate = "ftp://{0}:{1}/file.txt")
+               {
+                       sp.Start ();
+                       string uri = String.Format (uriTemplate, EncloseIPv6 (sp.IPAddress), sp.Port);
+                       try {
+                               FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
+                               ftp.KeepAlive = false;
+                               ftp.Timeout = 5000;
+                               ftp.Method = WebRequestMethods.Ftp.DownloadFile;
+                               ftp.UseBinary = true;
+                               FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
+                               Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
+                               using (Stream st = response.GetResponseStream ()) {
+                               }
+                               // This should be "220 Bye" or similar (no KeepAlive)
+                               Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
+                               response.Close ();
+                       } catch (Exception) {
+                               if (!String.IsNullOrEmpty (sp.Where))
+                                       throw new Exception (sp.Where);
+                               throw;
+                       } finally {
+                               sp.Stop ();
+                       }
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFile2_v4 ()
+               {
+                       // Some embedded FTP servers in Industrial Automation Hardware report
+                       // the PWD using backslashes, but allow forward slashes for CWD.
+                       DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, false));
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFile2_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       // Some embedded FTP servers in Industrial Automation Hardware report
+                       // the PWD using backslashes, but allow forward slashes for CWD.
+                       DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/", null, true));
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DeleteFile1_v4 ()
+               {
+                       DeleteFile1 (false);
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DeleteFile1_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       DeleteFile1 (true);
+               }
+
+               void DeleteFile1 (bool ipv6)
+               {
+                       ServerDeleteFile sp = new ServerDeleteFile (ipv6);
+                       sp.Start ();
+                       string uri = String.Format ("ftp://{0}:{1}/file.txt", EncloseIPv6 (sp.IPAddress), sp.Port);
+                       try {
+                               FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
+                               Console.WriteLine (ftp.RequestUri);
+                               ftp.KeepAlive = false;
+                               ftp.Timeout = 5000;
+                               ftp.Method = WebRequestMethods.Ftp.DeleteFile;
+                               ftp.UseBinary = true;
+                               FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
+                               Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
+                               response.Close ();
                        } catch (Exception e) {
-                               throw new Exception (sp.Where);
+                               Console.WriteLine (e);
+                               if (!String.IsNullOrEmpty (sp.Where))
+                                       throw new Exception (sp.Where);
+                               throw;
                        } finally {
                                sp.Stop ();
                        }
                }
 
-               class ServerPut : FtpServer {
-                       public string Where = "";
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void ListDirectory1_v4 ()
+               {
+                       ListDirectory1 (false);
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void ListDirectory1_v6 ()
+               {
+                       if (!Socket.OSSupportsIPv6)
+                               Assert.Ignore ("IPv6 not supported.");
+
+                       ListDirectory1 (true);
+               }
+
+               void ListDirectory1 (bool ipv6)
+               {
+                       ServerListDirectory sp = new ServerListDirectory (ipv6);
+                       sp.Start ();
+                       string uri = String.Format ("ftp://{0}:{1}/somedir/", EncloseIPv6 (sp.IPAddress), sp.Port);
+                       try {
+                               FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
+                               Console.WriteLine (ftp.RequestUri);
+                               ftp.KeepAlive = false;
+                               ftp.Timeout = 5000;
+                               ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
+                               ftp.UseBinary = true;
+                               using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
+                                       StreamReader reader = new StreamReader (response.GetResponseStream ());
+                                       string result = reader.ReadToEnd ();
+                                       Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
+                               }
+                       } catch (Exception e) {
+                               Console.WriteLine (e);
+                               if (!String.IsNullOrEmpty (sp.Where))
+                                       throw new Exception (sp.Where);
+                               throw;
+                       } finally {
+                               sp.Stop ();
+                       }
+               }
+
+               string EncloseIPv6 (IPAddress address)
+               {
+                       if (address.AddressFamily == AddressFamily.InterNetwork)
+                               return address.ToString ();
+                       
+                       return String.Format ("[{0}]", address.ToString ());
+               }
+
+               class ServerListDirectory : FtpServer {
+                       public ServerListDirectory (bool ipv6)
+                               : base (ipv6)
+                       {
+                       }
 
                        protected override void Run ()
                        {
                                Socket client = control.Accept ();
                                NetworkStream ns = new NetworkStream (client, false);
                                StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
-                               writer.WriteLine ("220 Welcome to the jungle");
-                               writer.Flush ();
-                               StreamReader reader = new StreamReader (ns, Encoding.ASCII);
+                               StreamReader reader = new StreamReader (ns, Encoding.UTF8);
+                               if (!DoAnonymousLogin (writer, reader)) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
+                                       client.Close ();
+                                       return;
+                               }
+
                                string str = reader.ReadLine ();
-                               if (!str.StartsWith ("USER ")) {
-                                       Where = "USER";
+                               string resp = FormatPassiveResponse (str);
+                               if (resp == null) {
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("331 Say 'Mellon'");
+                               writer.WriteLine (resp);
                                writer.Flush ();
+
                                str = reader.ReadLine ();
-                               if (!str.StartsWith ("PASS ")) {
-                                       Where = "PASS";
+                               if (str != "LIST") {
+                                       Where = "LIST - '" + str + "'";
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("230 Logged in");
+                               writer.WriteLine ("150 Here comes the directory listing");
                                writer.Flush ();
-                               str = reader.ReadLine ();
-                               if (!str.StartsWith ("PWD")) {
-                                       Where = "PWD";
+
+                               Socket data_cnc = data.Accept ();
+                               byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x    2 ftp      ftp          4096 Oct 27 20:17 tests");
+                               data_cnc.Send (dontcare, 1, SocketFlags.None);
+                               data_cnc.Close ();
+                               writer.WriteLine ("226 Directory send Ok");
+                               writer.Flush ();
+                               if (!EndConversation (writer, reader)) {
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("257 \"/home/someuser\"");
+                               client.Close ();
+                       }
+               }
+
+               class ServerDeleteFile : FtpServer {
+                       public ServerDeleteFile (bool ipv6)
+                               : base (ipv6)
+                       {
+                       }
+
+                       protected override void Run ()
+                       {
+                               Socket client = control.Accept ();
+                               NetworkStream ns = new NetworkStream (client, false);
+                               StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
+                               StreamReader reader = new StreamReader (ns, Encoding.UTF8);
+                               if (!DoAnonymousLogin (writer, reader)) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               string str = reader.ReadLine ();
+                               if (str.Trim () != "DELE file.txt") {
+                                       Where = "DELE - " + str;
+                                       client.Close ();
+                                       return;
+                               }
+                               writer.WriteLine ("250 Delete operation successful");
                                writer.Flush ();
-                               str = reader.ReadLine ();
-                               if (str != ("CWD /home/someuser/uploads/")) {
-                                       Where = "CWD - " + str;
+                               if (!EndConversation (writer, reader)) {
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("250 Directory changed");
+                               client.Close ();
+                       }
+               }
+
+               class ServerDownload : FtpServer {
+
+                       string Pwd, Cwd, Filename;
+
+                       public ServerDownload (bool ipv6)
+                               : this (null, null, null, ipv6)
+                       {
+                       }
+
+                       public ServerDownload (string pwd, string cwd, string filename, bool ipv6)
+                               : base (ipv6)
+                       {
+                               Pwd = pwd ?? "/home/someuser";
+                               Cwd = cwd ?? "/home/someuser/";
+                               Filename = filename ?? "file.txt";
+                       }
+
+                       protected override void Run ()
+                       {
+                               Socket client = control.Accept ();
+                               NetworkStream ns = new NetworkStream (client, false);
+                               StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
+                               StreamReader reader = new StreamReader (ns, Encoding.UTF8);
+                               if (!DoAnonymousLogin (writer, reader)) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               string str = reader.ReadLine ();
+                               string resp = FormatPassiveResponse (str);
+                               if (resp == null) {
+                                       client.Close ();
+                                       return;
+                               }
+                               writer.WriteLine (resp);
                                writer.Flush ();
+
                                str = reader.ReadLine ();
-                               if (str != ("TYPE I")) {
-                                       Where = "TYPE - " + str;
+                               if (str != $"RETR {Filename}") {
+                                       Where = $"RETR - got: {str}, expected: RETR {Filename}";
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("200 Switching to binary mode");
+                               writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
                                writer.Flush ();
-                               str = reader.ReadLine ();
-                               if (str != "PASV") {
-                                       Where = "PASV";
+
+                               Socket data_cnc = data.Accept ();
+                               byte [] dontcare = new byte [1];
+                               data_cnc.Receive (dontcare, 1, SocketFlags.None);
+                               data_cnc.Close ();
+                               writer.WriteLine ("226 File send Ok");
+                               writer.Flush ();
+                               if (!EndConversation (writer, reader)) {
                                        client.Close ();
                                        return;
                                }
+                               client.Close ();
+                       }
+               }
+
+               class ServerPut : FtpServer {
+                       public List<byte> result = new List<byte> ();
+                       
+                       public ServerPut (bool ipv6)
+                               : base (ipv6)
+                       {
+                       }
 
-                               IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
-                               byte [] addr_bytes = end_data.Address.GetAddressBytes ();
-                               byte [] port = new byte [2];
-                               port[0] = (byte) ((end_data.Port >> 8) & 255);
-                               port[1] = (byte) (end_data.Port & 255);
-                               StringBuilder sb = new StringBuilder ("227 Passive (");
-                               foreach (byte b in addr_bytes) {
-                                       sb.AppendFormat ("{0},", b);    
+                       protected override void Run ()
+                       {
+                               Socket client = control.Accept ();
+                               NetworkStream ns = new NetworkStream (client, false);
+                               StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
+                               StreamReader reader = new StreamReader (ns, Encoding.UTF8);
+                               if (!DoAnonymousLogin (writer, reader)) {
+                                       client.Close ();
+                                       return;
                                }
-                               sb.AppendFormat ("{0},", port [0]);     
-                               sb.AppendFormat ("{0})", port [1]);     
-                               writer.WriteLine (sb.ToString ());
+
+                               if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               string str = reader.ReadLine ();
+                               string resp = FormatPassiveResponse (str);
+                               if (resp == null) {
+                                       client.Close ();
+                                       return;
+                               }
+                               writer.WriteLine (resp);
                                writer.Flush ();
 
                                str = reader.ReadLine ();
@@ -278,39 +699,39 @@ namespace MonoTests.System.Net
                                writer.Flush ();
 
                                Socket data_cnc = data.Accept ();
-                               byte [] dontcare = new byte [1];
-                               data_cnc.Receive (dontcare, 1, SocketFlags.None);
+                               var datastr = new NetworkStream (data_cnc, false);
+                               int ch;
+                               while ((ch = datastr.ReadByte ()) != -1){
+                                       result.Add ((byte)ch);
+
+                               }
                                data_cnc.Close ();
                                writer.WriteLine ("226 File received Ok");
                                writer.Flush ();
-                               str = reader.ReadLine ();
-                               if (str != "QUIT") {
-                                       Where = "QUIT";
+                               if (!EndConversation (writer, reader)) {
                                        client.Close ();
                                        return;
                                }
-                               writer.WriteLine ("220 Bye");
-                               writer.Flush ();
-                               Thread.Sleep (250);
                                client.Close ();
                        }
                }
 
-               abstract class FtpServer
-               {
+               abstract class FtpServer {
                        protected Socket control;
                        protected Socket data;
-                       protected Exception error;
                        protected ManualResetEvent evt;
+                       protected bool ipv6;
+                       public string Where = "";
 
-                       public FtpServer ()
+                       public FtpServer (bool ipv6)
                        {
-                               control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                               control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
+                               control = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                               control.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
                                control.Listen (1);
-                               data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                               data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
+                               data = new Socket (ipv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                               data.Bind (new IPEndPoint (ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
                                data.Listen (1);
+                               this.ipv6 = ipv6;
                        }
 
                        public void Start ()
@@ -326,7 +747,107 @@ namespace MonoTests.System.Net
                                data.Close ();
                                control.Close ();
                        }
-                       
+
+                       // PWD, CWD and TYPE I (type could be moved out of here)
+                       protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
+                       {
+                               string str = reader.ReadLine ();
+                               if (!str.StartsWith ("OPTS utf8 on")) {
+                                       Where = "OPTS utf8 - " + str;
+                                       return false;
+                               }
+                               writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
+                               writer.Flush ();
+                               str = reader.ReadLine ();
+                               if (!str.StartsWith ("PWD")) {
+                                       Where = "PWD - " + str;
+                                       return false;
+                               }
+                               writer.WriteLine ("257 \"{0}\"", pwd);
+                               writer.Flush ();
+                               str = reader.ReadLine ();
+                               if (str != ("CWD " + cwd)) {
+                                       Where = "CWD - " + str;
+                                       return false;
+                               }
+                               writer.WriteLine ("250 Directory changed");
+                               writer.Flush ();
+                               str = reader.ReadLine ();
+                               if (str != ("TYPE I")) {
+                                       Where = "TYPE - " + str;
+                                       return false;
+                               }
+                               writer.WriteLine ("200 Switching to binary mode");
+                               writer.Flush ();
+                               return true;
+                       }
+
+                       protected bool EndConversation (StreamWriter writer, StreamReader reader)
+                       {
+                               string str = reader.ReadLine ();
+                               if (str != "QUIT") {
+                                       Where = "QUIT";
+                                       return false;
+                               }
+                               writer.WriteLine ("220 Bye");
+                               writer.Flush ();
+                               Thread.Sleep (250);
+                               return true;
+                       }
+
+                       protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
+                       {
+                               writer.WriteLine ("220 Welcome to the jungle");
+                               writer.Flush ();
+                               string str = reader.ReadLine ();
+                               if (!str.StartsWith ("USER ")) {
+                                       Where = "USER";
+                                       return false;
+                               }
+                               writer.WriteLine ("331 Say 'Mellon'");
+                               writer.Flush ();
+                               str = reader.ReadLine ();
+                               if (!str.StartsWith ("PASS ")) {
+                                       Where = "PASS";
+                                       return false;
+                               }
+                               writer.WriteLine ("230 Logged in");
+                               writer.Flush ();
+                               return true;
+                       }
+
+                       protected string FormatPassiveResponse (string request)
+                       {
+                               if (ipv6) {
+                                       if (request != "EPSV") {
+                                               Where = "EPSV";
+                                               return null;
+                                       }
+
+                                       IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
+                                       return String.Format ("229 Extended Passive (|||{0}|)", end_data.Port);
+                               }
+                               else {
+                                       if (request != "PASV") {
+                                               Where = "PASV";
+                                               return null;
+                                       }
+
+                                       IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
+                                       byte [] addr_bytes = end_data.Address.GetAddressBytes ();
+                                       byte [] port = new byte [2];
+                                       port[0] = (byte) ((end_data.Port >> 8) & 255);
+                                       port[1] = (byte) (end_data.Port & 255);
+                                       StringBuilder sb = new StringBuilder ("227 Passive (");
+                                       foreach (byte b in addr_bytes) {
+                                               sb.AppendFormat ("{0},", b);    
+                                       }
+                                       sb.AppendFormat ("{0},", port [0]);     
+                                       sb.AppendFormat ("{0})", port [1]);     
+                                       return sb.ToString ();
+                               }
+                       }
+
                        public IPAddress IPAddress {
                                get { return ((IPEndPoint) control.LocalEndPoint).Address; }
                        }
@@ -335,15 +856,9 @@ namespace MonoTests.System.Net
                                get { return ((IPEndPoint) control.LocalEndPoint).Port; }
                        }
 
-                       public Exception Error { 
-                               get { return error; }
-                       }
-
                        protected abstract void Run ();
                }
-
        }
 }
 
-#endif