FtpWebRequestTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //
  2. // FtpWebRequestTest.cs - NUnit Test Cases for System.Net.FtpWebRequest
  3. //
  4. // Authors:
  5. // Carlos Alberto Cortez <[email protected]>
  6. // Gonzalo Paniagua Javier <[email protected]>
  7. //
  8. // Copyright (c) 2006,2007,2008 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. namespace MonoTests.System.Net
  19. {
  20. [TestFixture]
  21. public class FtpWebRequestTest
  22. {
  23. FtpWebRequest defaultRequest;
  24. [TestFixtureSetUp]
  25. public void Init ()
  26. {
  27. defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
  28. }
  29. [Test]
  30. public void ContentLength ()
  31. {
  32. try {
  33. long l = defaultRequest.ContentLength;
  34. } catch (NotSupportedException) {
  35. Assert.Fail ("#1"); // Not overriden
  36. }
  37. try {
  38. defaultRequest.ContentLength = 2;
  39. } catch (NotSupportedException) {
  40. Assert.Fail ("#2"); // Not overriden
  41. }
  42. }
  43. [Test]
  44. public void ContentType ()
  45. {
  46. try {
  47. string t = defaultRequest.ContentType;
  48. Assert.Fail ("#1");
  49. } catch (NotSupportedException) {
  50. }
  51. try {
  52. defaultRequest.ContentType = String.Empty;
  53. Assert.Fail ("#2");
  54. } catch (NotSupportedException) {
  55. }
  56. }
  57. [Test]
  58. public void ContentOffset ()
  59. {
  60. try {
  61. defaultRequest.ContentOffset = -2;
  62. Assert.Fail ("#1");
  63. } catch (ArgumentOutOfRangeException) {
  64. }
  65. }
  66. [Test]
  67. public void Credentials ()
  68. {
  69. try {
  70. defaultRequest.Credentials = null;
  71. Assert.Fail ("#1");
  72. } catch (ArgumentNullException) {
  73. }
  74. }
  75. [Test]
  76. public void Method ()
  77. {
  78. try {
  79. defaultRequest.Method = null;
  80. Assert.Fail ("#1");
  81. } catch (ArgumentNullException) {
  82. }
  83. try {
  84. defaultRequest.Method = String.Empty;
  85. Assert.Fail ("#2");
  86. } catch (ArgumentException) {
  87. }
  88. try {
  89. defaultRequest.Method = "WrongValue";
  90. Assert.Fail ("#3");
  91. } catch (ArgumentException) {
  92. }
  93. }
  94. [Test]
  95. public void PreAuthenticate ()
  96. {
  97. try {
  98. bool p = defaultRequest.PreAuthenticate;
  99. Assert.Fail ("#1");
  100. } catch (NotSupportedException) {
  101. }
  102. try {
  103. defaultRequest.PreAuthenticate = true;
  104. } catch (NotSupportedException) {
  105. }
  106. }
  107. [Test]
  108. public void ReadWriteTimeout ()
  109. {
  110. try {
  111. defaultRequest.ReadWriteTimeout = -2;
  112. Assert.Fail ("#2");
  113. } catch (ArgumentOutOfRangeException) {
  114. }
  115. }
  116. [Test]
  117. public void Timeout ()
  118. {
  119. try {
  120. defaultRequest.Timeout = -2;
  121. Assert.Fail ("#2");
  122. } catch (ArgumentOutOfRangeException) {
  123. }
  124. }
  125. [Test]
  126. public void DefaultValues ()
  127. {
  128. FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
  129. Assert.AreEqual (0, request.ContentOffset, "ContentOffset");
  130. Assert.AreEqual (false, request.EnableSsl, "EnableSsl");
  131. // FIXME: Disabled this one by now. KeepAlive is not well supported.
  132. // Assert.AreEqual (true, request.KeepAlive, "KeepAlive");
  133. Assert.AreEqual (WebRequestMethods.Ftp.DownloadFile, request.Method, "#1");
  134. Assert.AreEqual (300000, request.ReadWriteTimeout, "ReadWriteTimeout");
  135. Assert.IsNull (request.RenameTo, "RenameTo");
  136. Assert.AreEqual (true, request.UseBinary, "UseBinary");
  137. Assert.AreEqual (100000, request.Timeout, "Timeout");
  138. Assert.AreEqual (true, request.UsePassive, "UsePassive");
  139. }
  140. [Test]
  141. public void RenameTo ()
  142. {
  143. try {
  144. defaultRequest.RenameTo = null;
  145. Assert.Fail ("#1");
  146. } catch (ArgumentException) {
  147. }
  148. try {
  149. defaultRequest.RenameTo = String.Empty;
  150. Assert.Fail ("#2");
  151. } catch (ArgumentException) {
  152. }
  153. }
  154. [Test]
  155. public void UploadFile1 ()
  156. {
  157. ServerPut sp = new ServerPut ();
  158. sp.Start ();
  159. string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
  160. try {
  161. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  162. ftp.KeepAlive = false;
  163. ftp.Timeout = 5000;
  164. ftp.Method = WebRequestMethods.Ftp.UploadFile;
  165. ftp.ContentLength = 1;
  166. ftp.UseBinary = true;
  167. Stream stream = ftp.GetRequestStream ();
  168. stream.WriteByte (0);
  169. stream.Close ();
  170. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  171. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
  172. response.Close ();
  173. } catch (Exception) {
  174. if (!String.IsNullOrEmpty (sp.Where))
  175. throw new Exception (sp.Where);
  176. throw;
  177. } finally {
  178. sp.Stop ();
  179. }
  180. }
  181. [Test]
  182. public void DownloadFile1 ()
  183. {
  184. ServerDownload sp = new ServerDownload ();
  185. sp.Start ();
  186. string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
  187. try {
  188. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  189. ftp.KeepAlive = false;
  190. ftp.Timeout = 5000;
  191. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  192. ftp.UseBinary = true;
  193. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  194. Assert.IsTrue ((int) response.StatusCode >= 100 && (int) response.StatusCode < 200, "DL#01");
  195. using (Stream st = response.GetResponseStream ()) {
  196. }
  197. // This should be "220 Bye" or similar (no KeepAlive)
  198. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DL#02");
  199. response.Close ();
  200. } catch (Exception) {
  201. if (!String.IsNullOrEmpty (sp.Where))
  202. throw new Exception (sp.Where);
  203. throw;
  204. } finally {
  205. sp.Stop ();
  206. }
  207. }
  208. [Test]
  209. public void DeleteFile1 ()
  210. {
  211. ServerDeleteFile sp = new ServerDeleteFile ();
  212. sp.Start ();
  213. string uri = String.Format ("ftp://{0}:{1}/file.txt", sp.IPAddress, sp.Port);
  214. try {
  215. FtpWebRequest ftp = (FtpWebRequest) WebRequest.Create (uri);
  216. Console.WriteLine (ftp.RequestUri);
  217. ftp.KeepAlive = false;
  218. ftp.Timeout = 5000;
  219. ftp.Method = WebRequestMethods.Ftp.DeleteFile;
  220. ftp.UseBinary = true;
  221. FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
  222. Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "DF#01");
  223. response.Close ();
  224. } catch (Exception e) {
  225. Console.WriteLine (e);
  226. if (!String.IsNullOrEmpty (sp.Where))
  227. throw new Exception (sp.Where);
  228. throw;
  229. } finally {
  230. sp.Stop ();
  231. }
  232. }
  233. class ServerDeleteFile : FtpServer {
  234. protected override void Run ()
  235. {
  236. Socket client = control.Accept ();
  237. NetworkStream ns = new NetworkStream (client, false);
  238. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  239. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  240. if (!DoAnonymousLogin (writer, reader)) {
  241. client.Close ();
  242. return;
  243. }
  244. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
  245. client.Close ();
  246. return;
  247. }
  248. string str = reader.ReadLine ();
  249. if (str.Trim () != "DELE file.txt") {
  250. Where = "DELE - " + str;
  251. client.Close ();
  252. return;
  253. }
  254. writer.WriteLine ("250 Delete operation successful");
  255. writer.Flush ();
  256. if (!EndConversation (writer, reader)) {
  257. client.Close ();
  258. return;
  259. }
  260. client.Close ();
  261. }
  262. }
  263. class ServerDownload : FtpServer {
  264. protected override void Run ()
  265. {
  266. Socket client = control.Accept ();
  267. NetworkStream ns = new NetworkStream (client, false);
  268. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  269. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  270. if (!DoAnonymousLogin (writer, reader)) {
  271. client.Close ();
  272. return;
  273. }
  274. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/")) {
  275. client.Close ();
  276. return;
  277. }
  278. string str = reader.ReadLine ();
  279. if (str != "PASV") {
  280. Where = "PASV";
  281. client.Close ();
  282. return;
  283. }
  284. IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
  285. byte [] addr_bytes = end_data.Address.GetAddressBytes ();
  286. byte [] port = new byte [2];
  287. port[0] = (byte) ((end_data.Port >> 8) & 255);
  288. port[1] = (byte) (end_data.Port & 255);
  289. StringBuilder sb = new StringBuilder ("227 Passive (");
  290. foreach (byte b in addr_bytes) {
  291. sb.AppendFormat ("{0},", b);
  292. }
  293. sb.AppendFormat ("{0},", port [0]);
  294. sb.AppendFormat ("{0})", port [1]);
  295. writer.WriteLine (sb.ToString ());
  296. writer.Flush ();
  297. str = reader.ReadLine ();
  298. if (str != "RETR file.txt") {
  299. Where = "RETR - " + str;
  300. client.Close ();
  301. return;
  302. }
  303. writer.WriteLine ("150 Opening BINARY mode data connection for blah (n bytes)");
  304. writer.Flush ();
  305. Socket data_cnc = data.Accept ();
  306. byte [] dontcare = new byte [1];
  307. data_cnc.Receive (dontcare, 1, SocketFlags.None);
  308. data_cnc.Close ();
  309. writer.WriteLine ("226 File send Ok");
  310. writer.Flush ();
  311. if (!EndConversation (writer, reader)) {
  312. client.Close ();
  313. return;
  314. }
  315. client.Close ();
  316. }
  317. }
  318. class ServerPut : FtpServer {
  319. protected override void Run ()
  320. {
  321. Socket client = control.Accept ();
  322. NetworkStream ns = new NetworkStream (client, false);
  323. StreamWriter writer = new StreamWriter (ns, Encoding.ASCII);
  324. StreamReader reader = new StreamReader (ns, Encoding.UTF8);
  325. if (!DoAnonymousLogin (writer, reader)) {
  326. client.Close ();
  327. return;
  328. }
  329. if (!DoInitialDialog (writer, reader, "/home/someuser", "/home/someuser/uploads/")) {
  330. client.Close ();
  331. return;
  332. }
  333. string str = reader.ReadLine ();
  334. if (str != "PASV") {
  335. Where = "PASV";
  336. client.Close ();
  337. return;
  338. }
  339. IPEndPoint end_data = (IPEndPoint) data.LocalEndPoint;
  340. byte [] addr_bytes = end_data.Address.GetAddressBytes ();
  341. byte [] port = new byte [2];
  342. port[0] = (byte) ((end_data.Port >> 8) & 255);
  343. port[1] = (byte) (end_data.Port & 255);
  344. StringBuilder sb = new StringBuilder ("227 Passive (");
  345. foreach (byte b in addr_bytes) {
  346. sb.AppendFormat ("{0},", b);
  347. }
  348. sb.AppendFormat ("{0},", port [0]);
  349. sb.AppendFormat ("{0})", port [1]);
  350. writer.WriteLine (sb.ToString ());
  351. writer.Flush ();
  352. str = reader.ReadLine ();
  353. if (str != "STOR file.txt") {
  354. Where = "STOR - " + str;
  355. client.Close ();
  356. return;
  357. }
  358. writer.WriteLine ("150 Ok to send data");
  359. writer.Flush ();
  360. Socket data_cnc = data.Accept ();
  361. byte [] dontcare = new byte [1];
  362. data_cnc.Receive (dontcare, 1, SocketFlags.None);
  363. data_cnc.Close ();
  364. writer.WriteLine ("226 File received Ok");
  365. writer.Flush ();
  366. if (!EndConversation (writer, reader)) {
  367. client.Close ();
  368. return;
  369. }
  370. client.Close ();
  371. }
  372. }
  373. abstract class FtpServer {
  374. protected Socket control;
  375. protected Socket data;
  376. protected ManualResetEvent evt;
  377. public string Where = "";
  378. public FtpServer ()
  379. {
  380. control = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  381. control.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  382. control.Listen (1);
  383. data = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  384. data.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  385. data.Listen (1);
  386. }
  387. public void Start ()
  388. {
  389. evt = new ManualResetEvent (false);
  390. Thread th = new Thread (new ThreadStart (Run));
  391. th.Start ();
  392. }
  393. public void Stop ()
  394. {
  395. evt.Set ();
  396. data.Close ();
  397. control.Close ();
  398. }
  399. // PWD, CWD and TYPE I (type could be moved out of here)
  400. protected bool DoInitialDialog (StreamWriter writer, StreamReader reader, string pwd, string cwd)
  401. {
  402. string str = reader.ReadLine ();
  403. if (!str.StartsWith ("OPTS utf8 on")) {
  404. Where = "OPTS utf8 - " + str;
  405. return false;
  406. }
  407. writer.WriteLine ("200 Always in UTF8 mode"); // vsftpd
  408. writer.Flush ();
  409. str = reader.ReadLine ();
  410. if (!str.StartsWith ("PWD")) {
  411. Where = "PWD - " + str;
  412. return false;
  413. }
  414. writer.WriteLine ("257 \"{0}\"", pwd);
  415. writer.Flush ();
  416. str = reader.ReadLine ();
  417. if (str != ("CWD " + cwd)) {
  418. Where = "CWD - " + str;
  419. return false;
  420. }
  421. writer.WriteLine ("250 Directory changed");
  422. writer.Flush ();
  423. str = reader.ReadLine ();
  424. if (str != ("TYPE I")) {
  425. Where = "TYPE - " + str;
  426. return false;
  427. }
  428. writer.WriteLine ("200 Switching to binary mode");
  429. writer.Flush ();
  430. return true;
  431. }
  432. protected bool EndConversation (StreamWriter writer, StreamReader reader)
  433. {
  434. string str = reader.ReadLine ();
  435. if (str != "QUIT") {
  436. Where = "QUIT";
  437. return false;
  438. }
  439. writer.WriteLine ("220 Bye");
  440. writer.Flush ();
  441. Thread.Sleep (250);
  442. return true;
  443. }
  444. protected bool DoAnonymousLogin (StreamWriter writer, StreamReader reader)
  445. {
  446. writer.WriteLine ("220 Welcome to the jungle");
  447. writer.Flush ();
  448. string str = reader.ReadLine ();
  449. if (!str.StartsWith ("USER ")) {
  450. Where = "USER";
  451. return false;
  452. }
  453. writer.WriteLine ("331 Say 'Mellon'");
  454. writer.Flush ();
  455. str = reader.ReadLine ();
  456. if (!str.StartsWith ("PASS ")) {
  457. Where = "PASS";
  458. return false;
  459. }
  460. writer.WriteLine ("230 Logged in");
  461. writer.Flush ();
  462. return true;
  463. }
  464. public IPAddress IPAddress {
  465. get { return ((IPEndPoint) control.LocalEndPoint).Address; }
  466. }
  467. public int Port {
  468. get { return ((IPEndPoint) control.LocalEndPoint).Port; }
  469. }
  470. protected abstract void Run ();
  471. }
  472. }
  473. }
  474. #endif