17995f75c25d1c52cd86bd0646e39e345b404c30
[mono.git] / mcs / class / System / Test / System.Net / FtpWebRequestTest.cs
1 //
2 // FtpWebRequestTest.cs - NUnit Test Cases for System.Net.FtpWebRequest
3 //
4 // Authors:
5 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
6 //      Gonzalo Paniagua Javier <gonzalo@novell.com>
7 //
8 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
9 //
10 #if NET_2_0
11 using NUnit.Framework;
12 using System;
13 using System.IO;
14 using System.Net;
15 using System.Net.Sockets;
16 using System.Text;
17 using System.Threading;
18
19 namespace MonoTests.System.Net 
20 {
21         [TestFixture]
22         public class FtpWebRequestTest
23         {
24                 FtpWebRequest defaultRequest;
25                 
26                 [TestFixtureSetUp]
27                 public void Init ()
28                 {
29                         defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
30                 }
31                 
32                 [Test]
33                 public void ContentLength ()
34                 {
35                         try {
36                                 long l = defaultRequest.ContentLength;
37                         } catch (NotSupportedException) {
38                                 Assert.Fail ("#1"); // Not overriden
39                         }
40
41                         try {
42                                 defaultRequest.ContentLength = 2;
43                         } catch (NotSupportedException) {
44                                 Assert.Fail ("#2"); // Not overriden
45                         }
46                 }
47
48                 [Test]
49                 public void ContentType ()
50                 {
51                         try {
52                                 string t = defaultRequest.ContentType;
53                                 Assert.Fail ("#1");
54                         } catch (NotSupportedException) {
55                         }
56
57                         try {
58                                 defaultRequest.ContentType = String.Empty;
59                                 Assert.Fail ("#2");
60                         } catch (NotSupportedException) {
61                         }
62                 }
63
64                 [Test]
65                 public void ContentOffset ()
66                 {
67                         try {
68                                 defaultRequest.ContentOffset = -2;
69                                 Assert.Fail ("#1");
70                         } catch (ArgumentOutOfRangeException) {
71                         }
72                 }
73
74                 [Test]
75                 public void Credentials ()
76                 {
77                         try {
78                                 defaultRequest.Credentials = null;
79                                 Assert.Fail ("#1");
80                         } catch (ArgumentNullException) {
81                         }
82
83                 }
84
85                 [Test]
86                 public void Method ()
87                 {
88                         try {
89                                 defaultRequest.Method = null;
90                                 Assert.Fail ("#1");
91                         } catch (ArgumentNullException) {
92                         }
93
94                         try {
95                                 defaultRequest.Method = String.Empty;
96                                 Assert.Fail ("#2");
97                         } catch (ArgumentException) {
98                         }
99
100                         try {
101                                 defaultRequest.Method = "WrongValue";
102                                 Assert.Fail ("#3");
103                         } catch (ArgumentException) {
104                         }
105                 }
106
107                 [Test]
108                 public void PreAuthenticate ()
109                 {
110                         try {
111                                 bool p = defaultRequest.PreAuthenticate;
112                                 Assert.Fail ("#1");
113                         } catch (NotSupportedException) {
114                         }
115
116                         try {
117                                 defaultRequest.PreAuthenticate = true;
118                         } catch (NotSupportedException) {
119                         }
120                 }
121
122                 [Test]
123                 public void ReadWriteTimeout ()
124                 {
125                         try {
126                                 defaultRequest.ReadWriteTimeout = -2;
127                                 Assert.Fail ("#2");
128                         } catch (ArgumentOutOfRangeException) {
129                         }
130                 }
131
132                 [Test]
133                 public void Timeout ()
134                 {
135                         try {
136                                 defaultRequest.Timeout = -2;
137                                 Assert.Fail ("#2");
138                         } catch (ArgumentOutOfRangeException) {
139                         }
140                 }
141                 
142                 [Test]
143                 public void DefaultValues ()
144                 {
145                         FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
146                         
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");
157                 }
158
159                 [Test]
160                 public void RenameTo ()
161                 {
162                         try {
163                                 defaultRequest.RenameTo = null;
164                                 Assert.Fail ("#1");
165                         } catch (ArgumentException) {
166                         }
167
168                         try {
169                                 defaultRequest.RenameTo = String.Empty;
170                                 Assert.Fail ("#2");
171                         } catch (ArgumentException) {
172                         }
173                 }
174
175                 [Test]
176                 public void UploadFile1 ()
177                 {
178                         ServerPut sp = new ServerPut ();
179                         sp.Start ();
180                         string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
181                         try {
182                                 FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
183                                 ftp.Timeout = 5000;
184                                 ftp.Method = WebRequestMethods.Ftp.UploadFile;
185                                 ftp.ContentLength = 1;
186                                 ftp.UseBinary = true;
187                                 Stream stream = ftp.GetRequestStream ();
188                                 stream.WriteByte (0);
189                                 stream.Close ();
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);
194                         } finally {
195                                 sp.Stop ();
196                         }
197                 }
198
199                 class ServerPut : FtpServer {
200                         public string Where = "";
201
202                         protected override void Run ()
203                         {
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");
208                                 writer.Flush ();
209                                 StreamReader reader = new StreamReader (ns, Encoding.ASCII);
210                                 string str = reader.ReadLine ();
211                                 if (!str.StartsWith ("USER ")) {
212                                         Where = "USER";
213                                         client.Close ();
214                                         return;
215                                 }
216                                 writer.WriteLine ("331 Say 'Mellon'");
217                                 writer.Flush ();
218                                 str = reader.ReadLine ();
219                                 if (!str.StartsWith ("PASS ")) {
220                                         Where = "PASS";
221                                         client.Close ();
222                                         return;
223                                 }
224                                 writer.WriteLine ("230 Logged in");
225                                 writer.Flush ();
226                                 str = reader.ReadLine ();
227                                 if (!str.StartsWith ("PWD")) {
228                                         Where = "PWD";
229                                         client.Close ();
230                                         return;
231                                 }
232                                 writer.WriteLine ("257 \"/home/someuser\"");
233                                 writer.Flush ();
234                                 str = reader.ReadLine ();
235                                 if (str != ("CWD /home/someuser/uploads/")) {
236                                         Where = "CWD - " + str;
237                                         client.Close ();
238                                         return;
239                                 }
240                                 writer.WriteLine ("250 Directory changed");
241                                 writer.Flush ();
242                                 str = reader.ReadLine ();
243                                 if (str != ("TYPE I")) {
244                                         Where = "TYPE - " + str;
245                                         client.Close ();
246                                         return;
247                                 }
248                                 writer.WriteLine ("200 Switching to binary mode");
249                                 writer.Flush ();
250                                 str = reader.ReadLine ();
251                                 if (str != "PASV") {
252                                         Where = "PASV";
253                                         client.Close ();
254                                         return;
255                                 }
256
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);    
265                                 }
266                                 sb.AppendFormat ("{0},", port [0]);     
267                                 sb.AppendFormat ("{0})", port [1]);     
268                                 writer.WriteLine (sb.ToString ());
269                                 writer.Flush ();
270
271                                 str = reader.ReadLine ();
272                                 if (str != "STOR file.txt") {
273                                         Where = "STOR - " + str;
274                                         client.Close ();
275                                         return;
276                                 }
277                                 writer.WriteLine ("150 Ok to send data");
278                                 writer.Flush ();
279
280                                 Socket data_cnc = data.Accept ();
281                                 byte [] dontcare = new byte [1];
282                                 data_cnc.Receive (dontcare, 1, SocketFlags.None);
283                                 data_cnc.Close ();
284                                 writer.WriteLine ("226 File received Ok");
285                                 writer.Flush ();
286                                 str = reader.ReadLine ();
287                                 if (str != "QUIT") {
288                                         Where = "QUIT";
289                                         client.Close ();
290                                         return;
291                                 }
292                                 writer.WriteLine ("220 Bye");
293                                 writer.Flush ();
294                                 Thread.Sleep (250);
295                                 client.Close ();
296                         }
297                 }
298
299                 abstract class FtpServer
300                 {
301                         protected Socket control;
302                         protected Socket data;
303                         protected Exception error;
304                         protected ManualResetEvent evt;
305
306                         public FtpServer ()
307                         {
308                                 control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
309                                 control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
310                                 control.Listen (1);
311                                 data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
312                                 data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
313                                 data.Listen (1);
314                         }
315
316                         public void Start ()
317                         {
318                                 evt = new ManualResetEvent (false);
319                                 Thread th = new Thread (new ThreadStart (Run));
320                                 th.Start ();
321                         }
322
323                         public void Stop ()
324                         {
325                                 evt.Set ();
326                                 data.Close ();
327                                 control.Close ();
328                         }
329                         
330                         public IPAddress IPAddress {
331                                 get { return ((IPEndPoint) control.LocalEndPoint).Address; }
332                         }
333                         
334                         public int Port {
335                                 get { return ((IPEndPoint) control.LocalEndPoint).Port; }
336                         }
337
338                         public Exception Error { 
339                                 get { return error; }
340                         }
341
342                         protected abstract void Run ();
343                 }
344
345         }
346 }
347
348 #endif
349