using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
+using Microsoft.Win32.SafeHandles;
namespace MonoTests.System.IO
{
[Test]
public void CtorFileNotFoundException_Mode_Open ()
{
+ const string file_name = "thisfileshouldnotexist.test";
// absolute path
- string path = TempFolder + DSC + "thisfileshouldnotexists.test";
+ string path = TempFolder + DSC + file_name;
DeleteFile (path);
FileStream stream = null;
try {
- stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
+ stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
Assert.Fail ("#A1");
} catch (FileNotFoundException ex) {
Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
// relative path
string orignalCurrentDir = Directory.GetCurrentDirectory ();
Directory.SetCurrentDirectory (TempFolder);
+
+ // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+ // the real directory name, not that of the TempFolder symlink and the test will fail
+ // (happens e.g. on Android M)
+ string realTempDir = Directory.GetCurrentDirectory ();
+ path = realTempDir + DSC + file_name;
+
try {
- stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
+ stream = new FileStream (file_name, FileMode.Open);
Assert.Fail ("#B1");
} catch (FileNotFoundException ex) {
Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
- // under OSX 'var' is a symlink to 'private/var'
- if (MacOSX)
- path = "/private" + path;
Assert.AreEqual (path, ex.FileName, "#B3");
Assert.IsNull (ex.InnerException, "#B4");
Assert.IsNotNull (ex.Message, "#B5");
[Test]
public void CtorFileNotFoundException_Mode_Truncate ()
{
+ const string file_name = "thisfileshouldNOTexist.test";
// absolute path
- string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
+ string path = TempFolder + DSC + file_name;
DeleteFile (path);
FileStream stream = null;
try {
// relative path
string orignalCurrentDir = Directory.GetCurrentDirectory ();
Directory.SetCurrentDirectory (TempFolder);
+
+ // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+ // the real directory name, not that of the TempFolder symlink and the test will fail
+ // (happens e.g. on Android M)
+ string realTempDir = Directory.GetCurrentDirectory ();
+ path = realTempDir + DSC + file_name;
+
try {
- stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
+ stream = new FileStream (file_name, FileMode.Truncate);
Assert.Fail ("#B1");
} catch (FileNotFoundException ex) {
Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
- // under OSX 'var' is a symlink to 'private/var'
- if (MacOSX)
- path = "/private" + path;
Assert.AreEqual (path, ex.FileName, "#B3");
Assert.IsNull (ex.InnerException, "#B4");
Assert.IsNotNull (ex.Message, "#B5");
[Test]
public void CtorIOException1 ()
{
+ const string file_name = "thisfileshouldexists.test";
// absolute path
- string path = TempFolder + DSC + "thisfileshouldexists.test";
+ string path = TempFolder + DSC + file_name;
FileStream stream = null;
DeleteFile (path);
try {
// relative path
string orignalCurrentDir = Directory.GetCurrentDirectory ();
Directory.SetCurrentDirectory (TempFolder);
+
+ // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+ // the real directory name, not that of the TempFolder symlink and the test will fail
+ // (happens e.g. on Android M)
+ string realTempDir = Directory.GetCurrentDirectory ();
+ path = realTempDir + DSC + file_name;
+
try {
- stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+ stream = new FileStream (file_name, FileMode.CreateNew);
stream.Close ();
stream = null;
- stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+ stream = new FileStream (file_name, FileMode.CreateNew);
Assert.Fail ("#B1");
} catch (IOException ex) {
Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
{
string orignalCurrentDir = Directory.GetCurrentDirectory ();
Directory.SetCurrentDirectory (TempFolder);
+
+ // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+ // the real directory name, not that of the TempFolder symlink and the test will fail
+ // (happens e.g. on Android M)
+ string realTempDir = Directory.GetCurrentDirectory ();
+
string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
- string fullPath = Path.Combine (TempFolder, relativePath);
+ string fullPath = Path.Combine (realTempDir, relativePath);
try {
new FileStream (relativePath, FileMode.Open);
Assert.Fail ("#A1");
}
}
-#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
[Test, ExpectedException (typeof (IOException))]
public void CtorIOException2 ()
{
stream.Close ();
}
}
-#endif // TARGET_JVM
- [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
[Test, ExpectedException (typeof (IOException))]
public void CtorIOException ()
{
}
[Test]
- [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
[ExpectedException (typeof (IOException))]
public void CtorAccess1Read2Write ()
{
}
[Test]
- [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
[ExpectedException (typeof (IOException))]
public void CtorAccess1Write2Write ()
{
DeleteFile (path);
}
- [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
public void TestLock ()
{
string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
}
}
-#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
// Check that the stream is flushed even when it doesn't own the
// handle
[Test]
Assert.AreEqual ((int) '1', s.ReadByte ());
s.Close ();
}
-#endif // TARGET_JVM
private void DeleteFile (string path)
{
}
}
-#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Constructor_InvalidFileHandle ()
{
new FileStream ((IntPtr) (-1L), FileAccess.Read);
}
-#endif // TARGET_JVM
[Test]
public void PositionAfterSetLength ()
}
[Test]
- [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
[ExpectedException (typeof (ObjectDisposedException))]
public void BeginRead_Disposed ()
{
}
[Test]
- [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
[ExpectedException (typeof (ObjectDisposedException))]
public void BeginWrite_Disposed ()
{
}
[Test]
- [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
[ExpectedException (typeof (ObjectDisposedException))]
public void Lock_Disposed ()
{
}
[Test]
- [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
[ExpectedException (typeof (ObjectDisposedException))]
public void Unlock_Disposed ()
{
}
}
- [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
[Test]
public void DeleteOnClose ()
{
DeleteFile (path);
}
}
+
+ [Test]
+ public void Ctor_InvalidSafeHandle ()
+ {
+ var sf = new SafeFileHandle (IntPtr.Zero, true);
+ try {
+ new FileStream (sf, FileAccess.ReadWrite);
+ Assert.Fail ("#1");
+ } catch (ArgumentException) {
+ }
+ }
#endif
}
}