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 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);
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, "#01");
192 } catch (Exception e) {
193 throw new Exception (sp.Where);
199 class ServerPut : FtpServer {
200 public string Where = "";
202 protected override void Run ()
204 Socket client = control.Accept ();
205 NetworkStream ns = new NetworkStream (client, false);
206 StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
207 writer.WriteLine ("220 Welcome to the jungle");
209 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
210 string str = reader.ReadLine ();
211 if (!str.StartsWith ("USER ")) {
216 writer.WriteLine ("331 Say 'Mellon'");
218 str = reader.ReadLine ();
219 if (!str.StartsWith ("PASS ")) {
224 writer.WriteLine ("230 Logged in");
226 str = reader.ReadLine ();
227 if (!str.StartsWith ("PWD")) {
232 writer.WriteLine ("257 \"/home/someuser\"");
234 str = reader.ReadLine ();
235 if (str != ("CWD /home/someuser/uploads/")) {
236 Where = "CWD - " + str;
240 writer.WriteLine ("250 Directory changed");
242 str = reader.ReadLine ();
243 if (str != ("TYPE I")) {
244 Where = "TYPE - " + str;
248 writer.WriteLine ("200 Switching to binary mode");
250 str = reader.ReadLine ();
257 IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
258 byte [] addr_bytes = end_data.Address.GetAddressBytes ();
259 byte [] port = new byte [2];
260 port[0] = (byte) ((end_data.Port >> 8) & 255);
261 port[1] = (byte) (end_data.Port & 255);
262 StringBuilder sb = new StringBuilder ("227 Passive (");
263 foreach (byte b in addr_bytes) {
264 sb.AppendFormat ("{0},", b);
266 sb.AppendFormat ("{0},", port [0]);
267 sb.AppendFormat ("{0})", port [1]);
268 writer.WriteLine (sb.ToString ());
271 str = reader.ReadLine ();
272 if (str != "STOR file.txt") {
273 Where = "STOR - " + str;
277 writer.WriteLine ("150 Ok to send data");
280 Socket data_cnc = data.Accept ();
281 byte [] dontcare = new byte [1];
282 data_cnc.Receive (dontcare, 1, SocketFlags.None);
284 writer.WriteLine ("226 File received Ok");
286 str = reader.ReadLine ();
292 writer.WriteLine ("220 Bye");
299 abstract class FtpServer
301 protected Socket control;
302 protected Socket data;
303 protected Exception error;
304 protected ManualResetEvent evt;
308 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
309 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
311 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
312 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
318 evt = new ManualResetEvent (false);
319 Thread th = new Thread (new ThreadStart (Run));
330 public IPAddress IPAddress {
331 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
335 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
338 public Exception Error {
339 get { return error; }
342 protected abstract void Run ();