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 ServerDownload sp = new ServerDownload ();
208 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
210 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
211 ftp.KeepAlive = false;
213 ftp.Method = WebRequestMethods.Ftp.DownloadFile;
214 ftp.UseBinary = true;
215 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
216 Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
217 using (Stream st = response.GetResponseStream ()) {
219 // This should be "220 Bye" or similar (no KeepAlive)
220 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
222 } catch (Exception) {
223 if (!String.IsNullOrEmpty (sp.Where))
224 throw new Exception (sp.Where);
232 public void DeleteFile1 ()
234 ServerDeleteFile sp = new ServerDeleteFile ();
236 string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
238 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
239 Console.WriteLine (ftp.RequestUri);
240 ftp.KeepAlive = false;
242 ftp.Method = WebRequestMethods.Ftp.DeleteFile;
243 ftp.UseBinary = true;
244 FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
245 Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
247 } catch (Exception e) {
248 Console.WriteLine (e);
249 if (!String.IsNullOrEmpty (sp.Where))
250 throw new Exception (sp.Where);
257 class ServerDeleteFile : FtpServer {
258 protected override void Run ()
260 Socket client = control.Accept ();
261 NetworkStream ns = new NetworkStream (client, false);
262 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
263 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
264 if (!DoAnonymousLogin (writer, reader)) {
269 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
274 string str = reader.ReadLine ();
275 if (str.Trim () != "DELE file.txt") {
276 Where = "DELE - " + str;
280 writer.WriteLine ("250 Delete operation successful");
282 if (!EndConversation (writer, reader)) {
290 class ServerDownload : FtpServer {
291 protected override void Run ()
293 Socket client = control.Accept ();
294 NetworkStream ns = new NetworkStream (client, false);
295 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
296 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
297 if (!DoAnonymousLogin (writer, reader)) {
302 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
307 string str = reader.ReadLine ();
314 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
315 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
316 byte [] port = new byte [2];
317 port[0] = (byte) ((end_data.Port >> 8) & 255);
318 port[1] = (byte) (end_data.Port & 255);
319 StringBuilder sb = new StringBuilder ("227 Passive (");
320 foreach (byte b in addr_bytes) {
321 sb.AppendFormat ("{0},", b);
323 sb.AppendFormat ("{0},", port [0]);
324 sb.AppendFormat ("{0})", port [1]);
325 writer.WriteLine (sb.ToString ());
328 str = reader.ReadLine ();
329 if (str != "RETR file.txt") {
330 Where = "RETR - " + str;
334 writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
337 Socket data_cnc = data.Accept ();
338 byte [] dontcare = new byte [1];
339 data_cnc.Receive (dontcare, 1, SocketFlags.None);
341 writer.WriteLine ("226 File send Ok");
343 if (!EndConversation (writer, reader)) {
351 class ServerPut : FtpServer {
352 protected override void Run ()
354 Socket client = control.Accept ();
355 NetworkStream ns = new NetworkStream (client, false);
356 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
357 StreamReader reader = new StreamReader (ns, Encoding.UTF8);
358 if (!DoAnonymousLogin (writer, reader)) {
363 if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
368 string str = reader.ReadLine ();
375 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
376 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
377 byte [] port = new byte [2];
378 port[0] = (byte) ((end_data.Port >> 8) & 255);
379 port[1] = (byte) (end_data.Port & 255);
380 StringBuilder sb = new StringBuilder ("227 Passive (");
381 foreach (byte b in addr_bytes) {
382 sb.AppendFormat ("{0},", b);
384 sb.AppendFormat ("{0},", port [0]);
385 sb.AppendFormat ("{0})", port [1]);
386 writer.WriteLine (sb.ToString ());
389 str = reader.ReadLine ();
390 if (str != "STOR file.txt") {
391 Where = "STOR - " + str;
395 writer.WriteLine ("150 Ok to send data");
398 Socket data_cnc = data.Accept ();
399 byte [] dontcare = new byte [1];
400 data_cnc.Receive (dontcare, 1, SocketFlags.None);
402 writer.WriteLine ("226 File received Ok");
404 if (!EndConversation (writer, reader)) {
412 abstract class FtpServer {
413 protected Socket control;
414 protected Socket data;
415 protected ManualResetEvent evt;
416 public string Where = "";
420 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
421 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
423 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
424 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
430 evt = new ManualResetEvent (false);
431 Thread th = new Thread (new ThreadStart (Run));
442 // PWD, CWD and TYPE I (type could be moved out of here)
443 protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
445 string str = reader.ReadLine ();
446 if (!str.StartsWith ("OPTS utf8 on")) {
447 Where = "OPTS utf8 - " + str;
450 writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
452 str = reader.ReadLine ();
453 if (!str.StartsWith ("PWD")) {
454 Where = "PWD - " + str;
457 writer.WriteLine ("257 \"{0}\"", pwd);
459 str = reader.ReadLine ();
460 if (str != ("CWD " + cwd)) {
461 Where = "CWD - " + str;
464 writer.WriteLine ("250 Directory changed");
466 str = reader.ReadLine ();
467 if (str != ("TYPE I")) {
468 Where = "TYPE - " + str;
471 writer.WriteLine ("200 Switching to binary mode");
476 protected bool EndConversation (StreamWriter writer, StreamReader reader)
478 string str = reader.ReadLine ();
483 writer.WriteLine ("220 Bye");
489 protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
491 writer.WriteLine ("220 Welcome to the jungle");
493 string str = reader.ReadLine ();
494 if (!str.StartsWith ("USER ")) {
498 writer.WriteLine ("331 Say 'Mellon'");
500 str = reader.ReadLine ();
501 if (!str.StartsWith ("PASS ")) {
505 writer.WriteLine ("230 Logged in");
510 public IPAddress IPAddress {
511 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
515 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
518 protected abstract void Run ();