2 // FtpWebRequestTest.cs - NUnit Test Cases for System.Net.FtpWebRequest
5 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
6 // Gonzalo Paniagua Javier <gonzalo@novell.com>
8 // Copyright (c) 2006,2007,2008 Novell, Inc. (http://www.novell.com)
10 using NUnit.Framework;
14 using System.Net.Sockets;
16 using System.Threading;
18 namespace MonoTests.System.Net
21 public class FtpWebRequestTest
23 FtpWebRequest defaultRequest;
28 defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
32 public void ContentLength ()
35 long l = defaultRequest.ContentLength;
36 } catch (NotSupportedException) {
37 Assert.Fail ("#1"); // Not overriden
41 defaultRequest.ContentLength = 2;
42 } catch (NotSupportedException) {
43 Assert.Fail ("#2"); // Not overriden
48 public void ContentType ()
51 string t = defaultRequest.ContentType;
53 } catch (NotSupportedException) {
57 defaultRequest.ContentType = String.Empty;
59 } catch (NotSupportedException) {
64 public void ContentOffset ()
67 defaultRequest.ContentOffset = -2;
69 } catch (ArgumentOutOfRangeException) {
74 public void Credentials ()
77 defaultRequest.Credentials = null;
79 } catch (ArgumentNullException) {
88 defaultRequest.Method = null;
90 } catch (ArgumentNullException) {
94 defaultRequest.Method = String.Empty;
96 } catch (ArgumentException) {
100 defaultRequest.Method = "WrongValue";
102 } catch (ArgumentException) {
107 public void PreAuthenticate ()
110 bool p = defaultRequest.PreAuthenticate;
112 } catch (NotSupportedException) {
116 defaultRequest.PreAuthenticate = true;
117 } catch (NotSupportedException) {
122 public void ReadWriteTimeout ()
125 defaultRequest.ReadWriteTimeout = -2;
127 } catch (ArgumentOutOfRangeException) {
132 public void Timeout ()
135 defaultRequest.Timeout = -2;
137 } catch (ArgumentOutOfRangeException) {
142 public void DefaultValues ()
144 FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
146 Assert.AreEqual (0, request.ContentOffset, "ContentOffset");
147 Assert.AreEqual (false, request.EnableSsl, "EnableSsl");
148 // FIXME: Disabled this one by now. KeepAlive is not well supported.
149 // Assert.AreEqual (true, request.KeepAlive, "KeepAlive");
150 Assert.AreEqual (WebRequestMethods.Ftp.DownloadFile, request.Method, "#1");
151 Assert.AreEqual (300000, request.ReadWriteTimeout, "ReadWriteTimeout");
152 Assert.IsNull (request.RenameTo, "RenameTo");
153 Assert.AreEqual (true, request.UseBinary, "UseBinary");
154 Assert.AreEqual (100000, request.Timeout, "Timeout");
155 Assert.AreEqual (true, request.UsePassive, "UsePassive");
159 public void RenameTo ()
162 defaultRequest.RenameTo = null;
164 } catch (ArgumentException) {
168 defaultRequest.RenameTo = String.Empty;
170 } catch (ArgumentException) {
175 public void UploadFile1 ()
177 ServerPut sp = new ServerPut ();
179 string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
181 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
182 ftp.KeepAlive = false;
184 ftp.Method = WebRequestMethods.Ftp.UploadFile;
185 ftp.ContentLength = 1;
186 ftp.UseBinary = true;
187 Stream stream = ftp.GetRequestStream ();
188 stream.WriteByte (0);
190 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
191 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
193 } catch (Exception) {
194 if (!String.IsNullOrEmpty (sp.Where))
195 throw new Exception (sp.Where);
203 public void DownloadFile1 ()
205 DownloadFile (new ServerDownload ());
208 void DownloadFile (ServerDownload sp)
211 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
213 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
214 ftp.KeepAlive = false;
216 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
217 ftp.UseBinary = true;
218 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
219 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
220 using (Stream st = response.GetResponseStream ()) {
222 // This should be "220 Bye" or similar (no KeepAlive)
223 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
225 } catch (Exception) {
226 if (!String.IsNullOrEmpty (sp.Where))
227 throw new Exception (sp.Where);
235 public void DownloadFile2 ()
237 // Some embedded FTP servers in Industrial Automation Hardware report
238 // the PWD using backslashes, but allow forward slashes for CWD.
239 DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/"));
243 public void DeleteFile1 ()
245 ServerDeleteFile sp = new ServerDeleteFile ();
247 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
249 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
250 Console.WriteLine (ftp.RequestUri);
251 ftp.KeepAlive = false;
253 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
254 ftp.UseBinary = true;
255 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
256 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
258 } catch (Exception e) {
259 Console.WriteLine (e);
260 if (!String.IsNullOrEmpty (sp.Where))
261 throw new Exception (sp.Where);
269 public void ListDirectory1 ()
271 ServerListDirectory sp = new ServerListDirectory ();
273 string uri = String.Format ("ftp://{0}:{1}/somedir/", sp.IPAddress, sp.Port);
275 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
276 Console.WriteLine (ftp.RequestUri);
277 ftp.KeepAlive = false;
279 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
280 ftp.UseBinary = true;
281 using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
282 StreamReader reader = new StreamReader (response.GetResponseStream ());
283 string result = reader.ReadToEnd ();
284 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
286 } catch (Exception e) {
287 Console.WriteLine (e);
288 if (!String.IsNullOrEmpty (sp.Where))
289 throw new Exception (sp.Where);
296 class ServerListDirectory : FtpServer {
297 protected override void Run ()
299 Socket client = control.Accept ();
300 NetworkStream ns = new NetworkStream (client, false);
301 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
302 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
303 if (!DoAnonymousLogin (writer, reader)) {
308 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
313 string str = reader.ReadLine ();
320 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
321 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
322 byte [] port = new byte [2];
323 port[0] = (byte) ((end_data.Port >> 8) & 255);
324 port[1] = (byte) (end_data.Port & 255);
325 StringBuilder sb = new StringBuilder ("227 Passive (");
326 foreach (byte b in addr_bytes) {
327 sb.AppendFormat ("{0},", b);
329 sb.AppendFormat ("{0},", port [0]);
330 sb.AppendFormat ("{0})", port [1]);
331 writer.WriteLine (sb.ToString ());
334 str = reader.ReadLine ();
336 Where = "LIST - '" + str + "'";
340 writer.WriteLine ("150 Here comes the directory listing");
343 Socket data_cnc = data.Accept ();
344 byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x 2 ftp ftp 4096 Oct 27 20:17 tests");
345 data_cnc.Send (dontcare, 1, SocketFlags.None);
347 writer.WriteLine ("226 Directory send Ok");
349 if (!EndConversation (writer, reader)) {
357 class ServerDeleteFile : FtpServer {
358 protected override void Run ()
360 Socket client = control.Accept ();
361 NetworkStream ns = new NetworkStream (client, false);
362 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
363 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
364 if (!DoAnonymousLogin (writer, reader)) {
369 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
374 string str = reader.ReadLine ();
375 if (str.Trim () != "DELE file.txt") {
376 Where = "DELE - " + str;
380 writer.WriteLine ("250 Delete operation successful");
382 if (!EndConversation (writer, reader)) {
390 class ServerDownload : FtpServer {
394 public ServerDownload ()
399 public ServerDownload (string pwd, string cwd)
401 Pwd = pwd ?? "/home/someuser";
402 Cwd = cwd ?? "/home/someuser/";
405 protected override void Run ()
407 Socket client = control.Accept ();
408 NetworkStream ns = new NetworkStream (client, false);
409 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
410 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
411 if (!DoAnonymousLogin (writer, reader)) {
416 if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
421 string str = reader.ReadLine ();
428 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
429 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
430 byte [] port = new byte [2];
431 port[0] = (byte) ((end_data.Port >> 8) & 255);
432 port[1] = (byte) (end_data.Port & 255);
433 StringBuilder sb = new StringBuilder ("227 Passive (");
434 foreach (byte b in addr_bytes) {
435 sb.AppendFormat ("{0},", b);
437 sb.AppendFormat ("{0},", port [0]);
438 sb.AppendFormat ("{0})", port [1]);
439 writer.WriteLine (sb.ToString ());
442 str = reader.ReadLine ();
443 if (str != "RETR file.txt") {
444 Where = "RETR - " + str;
448 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
451 Socket data_cnc = data.Accept ();
452 byte [] dontcare = new byte [1];
453 data_cnc.Receive (dontcare, 1, SocketFlags.None);
455 writer.WriteLine ("226 File send Ok");
457 if (!EndConversation (writer, reader)) {
465 class ServerPut : FtpServer {
466 protected override void Run ()
468 Socket client = control.Accept ();
469 NetworkStream ns = new NetworkStream (client, false);
470 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
471 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
472 if (!DoAnonymousLogin (writer, reader)) {
477 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
482 string str = reader.ReadLine ();
489 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
490 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
491 byte [] port = new byte [2];
492 port[0] = (byte) ((end_data.Port >> 8) & 255);
493 port[1] = (byte) (end_data.Port & 255);
494 StringBuilder sb = new StringBuilder ("227 Passive (");
495 foreach (byte b in addr_bytes) {
496 sb.AppendFormat ("{0},", b);
498 sb.AppendFormat ("{0},", port [0]);
499 sb.AppendFormat ("{0})", port [1]);
500 writer.WriteLine (sb.ToString ());
503 str = reader.ReadLine ();
504 if (str != "STOR file.txt") {
505 Where = "STOR - " + str;
509 writer.WriteLine ("150 Ok to send data");
512 Socket data_cnc = data.Accept ();
513 byte [] dontcare = new byte [1];
514 data_cnc.Receive (dontcare, 1, SocketFlags.None);
516 writer.WriteLine ("226 File received Ok");
518 if (!EndConversation (writer, reader)) {
526 abstract class FtpServer {
527 protected Socket control;
528 protected Socket data;
529 protected ManualResetEvent evt;
530 public string Where = "";
534 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
535 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
537 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
538 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
544 evt = new ManualResetEvent (false);
545 Thread th = new Thread (new ThreadStart (Run));
556 // PWD, CWD and TYPE I (type could be moved out of here)
557 protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
559 string str = reader.ReadLine ();
560 if (!str.StartsWith ("OPTS utf8 on")) {
561 Where = "OPTS utf8 - " + str;
564 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
566 str = reader.ReadLine ();
567 if (!str.StartsWith ("PWD")) {
568 Where = "PWD - " + str;
571 writer.WriteLine ("257 \"{0}\"", pwd);
573 str = reader.ReadLine ();
574 if (str != ("CWD " + cwd)) {
575 Where = "CWD - " + str;
578 writer.WriteLine ("250 Directory changed");
580 str = reader.ReadLine ();
581 if (str != ("TYPE I")) {
582 Where = "TYPE - " + str;
585 writer.WriteLine ("200 Switching to binary mode");
590 protected bool EndConversation (StreamWriter writer, StreamReader reader)
592 string str = reader.ReadLine ();
597 writer.WriteLine ("220 Bye");
603 protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
605 writer.WriteLine ("220 Welcome to the jungle");
607 string str = reader.ReadLine ();
608 if (!str.StartsWith ("USER ")) {
612 writer.WriteLine ("331 Say 'Mellon'");
614 str = reader.ReadLine ();
615 if (!str.StartsWith ("PASS ")) {
619 writer.WriteLine ("230 Logged in");
624 public IPAddress IPAddress {
625 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
629 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
632 protected abstract void Run ();