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)
11 using NUnit.Framework;
15 using System.Net.Sockets;
17 using System.Threading;
19 namespace MonoTests.System.Net
22 public class FtpWebRequestTest
24 FtpWebRequest defaultRequest;
29 defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
33 public void ContentLength ()
36 long l = defaultRequest.ContentLength;
37 } catch (NotSupportedException) {
38 Assert.Fail ("#1"); // Not overriden
42 defaultRequest.ContentLength = 2;
43 } catch (NotSupportedException) {
44 Assert.Fail ("#2"); // Not overriden
49 public void ContentType ()
52 string t = defaultRequest.ContentType;
54 } catch (NotSupportedException) {
58 defaultRequest.ContentType = String.Empty;
60 } catch (NotSupportedException) {
65 public void ContentOffset ()
68 defaultRequest.ContentOffset = -2;
70 } catch (ArgumentOutOfRangeException) {
75 public void Credentials ()
78 defaultRequest.Credentials = null;
80 } catch (ArgumentNullException) {
89 defaultRequest.Method = null;
91 } catch (ArgumentNullException) {
95 defaultRequest.Method = String.Empty;
97 } catch (ArgumentException) {
101 defaultRequest.Method = "WrongValue";
103 } catch (ArgumentException) {
108 public void PreAuthenticate ()
111 bool p = defaultRequest.PreAuthenticate;
113 } catch (NotSupportedException) {
117 defaultRequest.PreAuthenticate = true;
118 } catch (NotSupportedException) {
123 public void ReadWriteTimeout ()
126 defaultRequest.ReadWriteTimeout = -2;
128 } catch (ArgumentOutOfRangeException) {
133 public void Timeout ()
136 defaultRequest.Timeout = -2;
138 } catch (ArgumentOutOfRangeException) {
143 public void DefaultValues ()
145 FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
147 Assert.AreEqual (0, request.ContentOffset, "ContentOffset");
148 Assert.AreEqual (false, request.EnableSsl, "EnableSsl");
149 // FIXME: Disabled this one by now. KeepAlive is not well supported.
150 // Assert.AreEqual (true, request.KeepAlive, "KeepAlive");
151 Assert.AreEqual (WebRequestMethods.Ftp.DownloadFile, request.Method, "#1");
152 Assert.AreEqual (300000, request.ReadWriteTimeout, "ReadWriteTimeout");
153 Assert.IsNull (request.RenameTo, "RenameTo");
154 Assert.AreEqual (true, request.UseBinary, "UseBinary");
155 Assert.AreEqual (100000, request.Timeout, "Timeout");
156 Assert.AreEqual (true, request.UsePassive, "UsePassive");
160 public void RenameTo ()
163 defaultRequest.RenameTo = null;
165 } catch (ArgumentException) {
169 defaultRequest.RenameTo = String.Empty;
171 } catch (ArgumentException) {
176 public void UploadFile1 ()
178 ServerPut sp = new ServerPut ();
180 string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
182 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
183 ftp.KeepAlive = false;
185 ftp.Method = WebRequestMethods.Ftp.UploadFile;
186 ftp.ContentLength = 1;
187 ftp.UseBinary = true;
188 Stream stream = ftp.GetRequestStream ();
189 stream.WriteByte (0);
191 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
192 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
194 } catch (Exception) {
195 if (!String.IsNullOrEmpty (sp.Where))
196 throw new Exception (sp.Where);
204 public void DownloadFile1 ()
206 DownloadFile (new ServerDownload ());
209 void DownloadFile (ServerDownload sp)
212 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
214 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
215 ftp.KeepAlive = false;
217 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
218 ftp.UseBinary = true;
219 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
220 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
221 using (Stream st = response.GetResponseStream ()) {
223 // This should be "220 Bye" or similar (no KeepAlive)
224 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
226 } catch (Exception) {
227 if (!String.IsNullOrEmpty (sp.Where))
228 throw new Exception (sp.Where);
236 public void DownloadFile2 ()
238 // Some embedded FTP servers in Industrial Automation Hardware report
239 // the PWD using backslashes, but allow forward slashes for CWD.
240 DownloadFile (new ServerDownload (@"\Users\someuser", "/Users/someuser/"));
244 public void DeleteFile1 ()
246 ServerDeleteFile sp = new ServerDeleteFile ();
248 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
250 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
251 Console.WriteLine (ftp.RequestUri);
252 ftp.KeepAlive = false;
254 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
255 ftp.UseBinary = true;
256 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
257 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
259 } catch (Exception e) {
260 Console.WriteLine (e);
261 if (!String.IsNullOrEmpty (sp.Where))
262 throw new Exception (sp.Where);
270 public void ListDirectory1 ()
272 ServerListDirectory sp = new ServerListDirectory ();
274 string uri = String.Format ("ftp://{0}:{1}/somedir/", sp.IPAddress, sp.Port);
276 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
277 Console.WriteLine (ftp.RequestUri);
278 ftp.KeepAlive = false;
280 ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
281 ftp.UseBinary = true;
282 using (FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ()) {
283 StreamReader reader = new StreamReader (response.GetResponseStream ());
284 string result = reader.ReadToEnd ();
285 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
287 } catch (Exception e) {
288 Console.WriteLine (e);
289 if (!String.IsNullOrEmpty (sp.Where))
290 throw new Exception (sp.Where);
297 class ServerListDirectory : FtpServer {
298 protected override void Run ()
300 Socket client = control.Accept ();
301 NetworkStream ns = new NetworkStream (client, false);
302 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
303 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
304 if (!DoAnonymousLogin (writer, reader)) {
309 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/somedir/")) {
314 string str = reader.ReadLine ();
321 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
322 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
323 byte [] port = new byte [2];
324 port[0] = (byte) ((end_data.Port >> 8) & 255);
325 port[1] = (byte) (end_data.Port & 255);
326 StringBuilder sb = new StringBuilder ("227 Passive (");
327 foreach (byte b in addr_bytes) {
328 sb.AppendFormat ("{0},", b);
330 sb.AppendFormat ("{0},", port [0]);
331 sb.AppendFormat ("{0})", port [1]);
332 writer.WriteLine (sb.ToString ());
335 str = reader.ReadLine ();
337 Where = "LIST - '" + str + "'";
341 writer.WriteLine ("150 Here comes the directory listing");
344 Socket data_cnc = data.Accept ();
345 byte [] dontcare = Encoding.ASCII.GetBytes ("drwxr-xr-x 2 ftp ftp 4096 Oct 27 20:17 tests");
346 data_cnc.Send (dontcare, 1, SocketFlags.None);
348 writer.WriteLine ("226 Directory send Ok");
350 if (!EndConversation (writer, reader)) {
358 class ServerDeleteFile : FtpServer {
359 protected override void Run ()
361 Socket client = control.Accept ();
362 NetworkStream ns = new NetworkStream (client, false);
363 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
364 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
365 if (!DoAnonymousLogin (writer, reader)) {
370 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
375 string str = reader.ReadLine ();
376 if (str.Trim () != "DELE file.txt") {
377 Where = "DELE - " + str;
381 writer.WriteLine ("250 Delete operation successful");
383 if (!EndConversation (writer, reader)) {
391 class ServerDownload : FtpServer {
395 public ServerDownload ()
400 public ServerDownload (string pwd, string cwd)
402 Pwd = pwd ?? "/home/someuser";
403 Cwd = cwd ?? "/home/someuser/";
406 protected override void Run ()
408 Socket client = control.Accept ();
409 NetworkStream ns = new NetworkStream (client, false);
410 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
411 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
412 if (!DoAnonymousLogin (writer, reader)) {
417 if (!DoInitialDialog (writer, reader, Pwd, Cwd)) {
422 string str = reader.ReadLine ();
429 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
430 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
431 byte [] port = new byte [2];
432 port[0] = (byte) ((end_data.Port >> 8) & 255);
433 port[1] = (byte) (end_data.Port & 255);
434 StringBuilder sb = new StringBuilder ("227 Passive (");
435 foreach (byte b in addr_bytes) {
436 sb.AppendFormat ("{0},", b);
438 sb.AppendFormat ("{0},", port [0]);
439 sb.AppendFormat ("{0})", port [1]);
440 writer.WriteLine (sb.ToString ());
443 str = reader.ReadLine ();
444 if (str != "RETR file.txt") {
445 Where = "RETR - " + str;
449 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
452 Socket data_cnc = data.Accept ();
453 byte [] dontcare = new byte [1];
454 data_cnc.Receive (dontcare, 1, SocketFlags.None);
456 writer.WriteLine ("226 File send Ok");
458 if (!EndConversation (writer, reader)) {
466 class ServerPut : FtpServer {
467 protected override void Run ()
469 Socket client = control.Accept ();
470 NetworkStream ns = new NetworkStream (client, false);
471 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
472 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
473 if (!DoAnonymousLogin (writer, reader)) {
478 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
483 string str = reader.ReadLine ();
490 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
491 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
492 byte [] port = new byte [2];
493 port[0] = (byte) ((end_data.Port >> 8) & 255);
494 port[1] = (byte) (end_data.Port & 255);
495 StringBuilder sb = new StringBuilder ("227 Passive (");
496 foreach (byte b in addr_bytes) {
497 sb.AppendFormat ("{0},", b);
499 sb.AppendFormat ("{0},", port [0]);
500 sb.AppendFormat ("{0})", port [1]);
501 writer.WriteLine (sb.ToString ());
504 str = reader.ReadLine ();
505 if (str != "STOR file.txt") {
506 Where = "STOR - " + str;
510 writer.WriteLine ("150 Ok to send data");
513 Socket data_cnc = data.Accept ();
514 byte [] dontcare = new byte [1];
515 data_cnc.Receive (dontcare, 1, SocketFlags.None);
517 writer.WriteLine ("226 File received Ok");
519 if (!EndConversation (writer, reader)) {
527 abstract class FtpServer {
528 protected Socket control;
529 protected Socket data;
530 protected ManualResetEvent evt;
531 public string Where = "";
535 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
536 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
538 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
539 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
545 evt = new ManualResetEvent (false);
546 Thread th = new Thread (new ThreadStart (Run));
557 // PWD, CWD and TYPE I (type could be moved out of here)
558 protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
560 string str = reader.ReadLine ();
561 if (!str.StartsWith ("OPTS utf8 on")) {
562 Where = "OPTS utf8 - " + str;
565 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
567 str = reader.ReadLine ();
568 if (!str.StartsWith ("PWD")) {
569 Where = "PWD - " + str;
572 writer.WriteLine ("257 \"{0}\"", pwd);
574 str = reader.ReadLine ();
575 if (str != ("CWD " + cwd)) {
576 Where = "CWD - " + str;
579 writer.WriteLine ("250 Directory changed");
581 str = reader.ReadLine ();
582 if (str != ("TYPE I")) {
583 Where = "TYPE - " + str;
586 writer.WriteLine ("200 Switching to binary mode");
591 protected bool EndConversation (StreamWriter writer, StreamReader reader)
593 string str = reader.ReadLine ();
598 writer.WriteLine ("220 Bye");
604 protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
606 writer.WriteLine ("220 Welcome to the jungle");
608 string str = reader.ReadLine ();
609 if (!str.StartsWith ("USER ")) {
613 writer.WriteLine ("331 Say 'Mellon'");
615 str = reader.ReadLine ();
616 if (!str.StartsWith ("PASS ")) {
620 writer.WriteLine ("230 Logged in");
625 public IPAddress IPAddress {
626 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
630 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
633 protected abstract void Run ();