//
// Copyright (c) 2006,2007,2008 Novell, Inc. (http://www.novell.com)
//
-#if NET_2_0
using NUnit.Framework;
using System;
+using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
[TestFixture]
public class FtpWebRequestTest
{
- FtpWebRequest defaultRequest;
+ FtpWebRequest _defaultRequest;
+ FtpWebRequest defaultRequest {
+ get { return _defaultRequest ?? (_defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com")); }
+ }
- [TestFixtureSetUp]
- public void Init ()
+ private string _tempDirectory;
+ private string _tempFile;
+
+ [SetUp]
+ public void SetUp ()
{
- defaultRequest = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
+ _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest");
+ _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp");
+ if (!Directory.Exists (_tempDirectory)) {
+ Directory.CreateDirectory (_tempDirectory);
+ } else {
+ // ensure no files are left over from previous runs
+ string [] files = Directory.GetFiles (_tempDirectory, "*");
+ foreach (string file in files)
+ File.Delete (file);
+ }
}
-
+
+ [TearDown]
+ public void TearDown ()
+ {
+ if (Directory.Exists (_tempDirectory))
+ Directory.Delete (_tempDirectory, true);
+ }
+
[Test]
public void ContentLength ()
{
try {
long l = defaultRequest.ContentLength;
+#if FEATURE_NO_BSD_SOCKETS
+ Assert.Fail ("#1a");
+ } catch (PlatformNotSupportedException) {
+ // OK.
+#else
} catch (NotSupportedException) {
Assert.Fail ("#1"); // Not overriden
+#endif
}
try {
defaultRequest.ContentLength = 2;
+#if FEATURE_NO_BSD_SOCKETS
+ Assert.Fail ("#2a");
+ } catch (PlatformNotSupportedException) {
+ // OK.
+#else
} catch (NotSupportedException) {
Assert.Fail ("#2"); // Not overriden
+#endif
}
}
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void ContentOffset ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Credentials ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Method ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void ReadWriteTimeout ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void Timeout ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DefaultValues ()
{
FtpWebRequest request = (FtpWebRequest) WebRequest.Create ("ftp://www.contoso.com");
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void RenameTo ()
{
try {
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void UploadFile1 ()
{
ServerPut sp = new ServerPut ();
ftp.KeepAlive = false;
ftp.Timeout = 5000;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
- ftp.ContentLength = 1;
+ ftp.ContentLength = 10;
ftp.UseBinary = true;
Stream stream = ftp.GetRequestStream ();
- stream.WriteByte (0);
+ for (int i = 0; i < 10; i++)
+ stream.WriteByte ((byte)i);
stream.Close ();
FtpWebResponse response = (FtpWebResponse) ftp.GetResponse ();
Assert.IsTrue ((int) response.StatusCode >= 200 && (int) response.StatusCode < 300, "UP#01");
+ Assert.AreEqual (10, sp.result.Count, "UP#02");
response.Close ();
} catch (Exception) {
if (!String.IsNullOrEmpty (sp.Where))
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+ public void UploadFile_WebClient ()
+ {
+ ServerPut sp = new ServerPut ();
+ File.WriteAllText (_tempFile, "0123456789");
+ sp.Start ();
+
+ using (WebClient m_WebClient = new WebClient())
+ {
+ string uri = String.Format ("ftp://{0}:{1}/uploads/file.txt", sp.IPAddress, sp.Port);
+
+ m_WebClient.UploadFile(uri, _tempFile);
+ }
+ Assert.AreEqual (10, sp.result.Count, "WebClient/Ftp#01");
+
+ sp.Stop ();
+ }
+
+ [Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DownloadFile1 ()
{
DownloadFile (new ServerDownload ());
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DownloadFile2 ()
{
// Some embedded FTP servers in Industrial Automation Hardware report
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void DeleteFile1 ()
{
ServerDeleteFile sp = new ServerDeleteFile ();
}
[Test]
+#if FEATURE_NO_BSD_SOCKETS
+ [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
public void ListDirectory1 ()
{
ServerListDirectory sp = new ServerListDirectory ();
}
class ServerPut : FtpServer {
+ public List<byte> result = new List<byte> ();
+
protected override void Run ()
{
Socket client = control.Accept ();
writer.Flush ();
Socket data_cnc = data.Accept ();
- byte [] dontcare = new byte [1];
- data_cnc.Receive (dontcare, 1, SocketFlags.None);
+ var datastr = new NetworkStream (data_cnc, false);
+ int ch;
+ while ((ch = datastr.ReadByte ()) != -1){
+ result.Add ((byte)ch);
+
+ }
data_cnc.Close ();
writer.WriteLine ("226 File received Ok");
writer.Flush ();
}
}
-#endif