Merge pull request #3749 from BrzVlad/fix-mips-fix
[mono.git] / mcs / class / System / Test / System.Net / FtpWebRequestTest.cs
index 8fd5f4b9c1e3b7a6defc33c6eb319182d752e111..8143edd7571492b7566dc025ffe0a8e076be7c69 100644 (file)
@@ -7,9 +7,9 @@
 //
 // 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,6 +228,9 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void UploadFile1 ()
                {
                        ServerPut sp = new ServerPut ();
@@ -183,13 +241,15 @@ namespace MonoTests.System.Net
                                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, "UP#01");
+                               Assert.AreEqual (10, sp.result.Count, "UP#02");
                                response.Close ();
                        } catch (Exception) {
                                if (!String.IsNullOrEmpty (sp.Where))
@@ -201,9 +261,37 @@ namespace MonoTests.System.Net
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void UploadFile_WebClient ()
+               {
+                       ServerPut sp = new ServerPut ();
+                       File.WriteAllText (_tempFile, "0123456789");
+                       sp.Start ();
+
+                       using (WebClient m_WebClient = new WebClient())
+                       {
+                               string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", 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 ()
                {
-                       ServerDownload sp = new ServerDownload ();
+                       DownloadFile (new ServerDownload ());
+               }
+
+               void DownloadFile (ServerDownload sp)
+               {
                        sp.Start ();
                        string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
                        try {
@@ -228,7 +316,139 @@ namespace MonoTests.System.Net
                        }
                }
 
-               class ServerDownload : FtpServer {
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DownloadFile2 ()
+               {
+                       // 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/"));
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void DeleteFile1 ()
+               {
+                       ServerDeleteFile sp = new ServerDeleteFile ();
+                       sp.Start ();
+                       string uri = String.Format ("ftp://{0}:{1}/file.txt", 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) {
+                               Console.WriteLine (e);
+                               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 ListDirectory1 ()
+               {
+                       ServerListDirectory sp = new ServerListDirectory ();
+                       sp.Start ();
+                       string uri = String.Format ("ftp://{0}:{1}/somedir/", 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 ();
+                       }
+               }
+
+               class ServerListDirectory : FtpServer {
+                       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/somedir/")) {
+                                       client.Close ();
+                                       return;
+                               }
+
+                               string str = reader.ReadLine ();
+                               if (str != "PASV") {
+                                       Where = "PASV";
+                                       client.Close ();
+                                       return;
+                               }
+
+                               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]);     
+                               writer.WriteLine (sb.ToString ());
+                               writer.Flush ();
+
+                               str = reader.ReadLine ();
+                               if (str != "LIST") {
+                                       Where = "LIST - '" + str + "'";
+                                       client.Close ();
+                                       return;
+                               }
+                               writer.WriteLine ("150 Here comes the directory listing");
+                               writer.Flush ();
+
+                               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;
+                               }
+                               client.Close ();
+                       }
+               }
+
+               class ServerDeleteFile : FtpServer {
                        protected override void Run ()
                        {
                                Socket client = control.Accept ();
@@ -245,6 +465,53 @@ namespace MonoTests.System.Net
                                        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 ();
+                               if (!EndConversation (writer, reader)) {
+                                       client.Close ();
+                                       return;
+                               }
+                               client.Close ();
+                       }
+               }
+
+               class ServerDownload : FtpServer {
+
+                       string Pwd, Cwd;
+
+                       public ServerDownload ()
+                               : this (null, null)
+                       {
+                       }
+
+                       public ServerDownload (string pwd, string cwd)
+                       {
+                               Pwd = pwd ?? "/home/someuser";
+                               Cwd = cwd ?? "/home/someuser/";
+                       }
+
+                       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 ();
                                if (str != "PASV") {
                                        Where = "PASV";
@@ -290,6 +557,8 @@ namespace MonoTests.System.Net
                }
 
                class ServerPut : FtpServer {
+                       public List<byte> result = new List<byte> ();
+                       
                        protected override void Run ()
                        {
                                Socket client = control.Accept ();
@@ -337,8 +606,12 @@ 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 ();
@@ -461,5 +734,4 @@ namespace MonoTests.System.Net
        }
 }
 
-#endif