private static void TestWeirdPaths()
        {
            // null path
            String nullPath = null;

            TestWeirdPathIter(nullPath, "nullPath", new ArgumentNullException());

            // empty path
            String emptyPath = "";

            TestWeirdPathIter(emptyPath, "emptyPath", new ArgumentException());

            // whitespace-only path
            char[] whitespacePathChars = { (char)0x9, (char)0xA };
            String whitespacePath      = new String(whitespacePathChars);

            TestWeirdPathIter(whitespacePath, "whitespacePath", new ArgumentException());

            // try to test a path that doesn't exist. Skip if can't find an unused drive
            if (Interop.IsWindows)
            {
                String pathNotExists = null;
                String unusedDrive   = EnumerableUtils.GetUnusedDrive();
                if (unusedDrive != null)
                {
                    pathNotExists = Path.Combine(unusedDrive, "temp", "dir");
                }
                if (pathNotExists != null)
                {
                    TestWeirdPathIter(pathNotExists, "pathNotExists", new DirectoryNotFoundException());
                }
            }

            // file (not dir) name. If we try to do GetFiles, GetDirs, etc in a file (not dir) we get IOException
            String filePath = null;

            foreach (String s in s_utils.expected_Files_Deep)
            {
                if (s != null)
                {
                    filePath = s;
                    break;
                }
            }
            TestWeirdPathIter(filePath, "pathIsFile", new IOException());

            // PathTooLong
            String longPath = Path.Combine(new String('a', IOInputs.MaxDirectory), "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");

            TestWeirdPathIter(longPath, "pathTooLong", new PathTooLongException());

            // path invalid chars
            String invalidCharPath = "temp" + Path.DirectorySeparatorChar + "fsd\0sdsds";

            TestWeirdPathIter(invalidCharPath, "invalidCharPath", new ArgumentException());
        }
Esempio n. 2
0
        private static void TestFileExceptions()
        {
            // Create common file and dir names
            String nullFileName   = null;
            String emptyFileName1 = "";
            String emptyFileName2 = " ";
            String longPath       = Path.Combine(new String('a', IOInputs.MaxDirectory), "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "test.txt");

            String notExistsFileName = null;

            if (Interop.IsWindows) // drive labels
            {
                String unusedDrive = EnumerableUtils.GetUnusedDrive();
                if (unusedDrive != null)
                {
                    notExistsFileName = Path.Combine(unusedDrive, Path.Combine("temp", "notExists", "temp.txt")); // just skip otherwise
                }
            }
            String[]             lines    = { "test line 1", "test line 2" };
            IEnumerable <String> contents = (IEnumerable <String>)lines;

            // Read exceptions
            TestReadFileExceptions(nullFileName, "null path", Encoding.UTF8, new ArgumentNullException());
            TestReadFileExceptions(emptyFileName1, "empty path 1", Encoding.UTF8, new ArgumentException());
            if (Interop.IsWindows) // whitespace-only names are valid on Unix
            {
                TestReadFileExceptions(emptyFileName2, "empty path 2", Encoding.UTF8, new ArgumentException());
            }
            TestReadFileExceptions(longPath, "long path", Encoding.UTF8, new PathTooLongException());
            if (notExistsFileName != null)
            {
                TestReadFileExceptions(notExistsFileName, "not exists", Encoding.UTF8, new DirectoryNotFoundException());
            }
            TestReadFileExceptionsWithEncoding("temp.txt", "null encoding", null, new ArgumentNullException(), File.ReadLines, File.ReadLines, "File.ReadLines");

            // Write exceptions
            TestWriteFileExceptions(nullFileName, "null path", contents, Encoding.UTF8, new ArgumentNullException());
            TestWriteFileExceptions(emptyFileName1, "empty path 1", contents, Encoding.UTF8, new ArgumentException());
            if (Interop.IsWindows) // whitespace-only paths are valid on Unix
            {
                TestWriteFileExceptions(emptyFileName2, "empty path 2", contents, Encoding.UTF8, new ArgumentException());
            }
            TestWriteFileExceptions(longPath, "long path", contents, Encoding.UTF8, new PathTooLongException());
            if (notExistsFileName != null)
            {
                TestWriteFileExceptions(notExistsFileName, "not exists", contents, Encoding.UTF8, new DirectoryNotFoundException());
            }
            TestWriteFileExceptions("temp.txt", "null contents", null, Encoding.UTF8, new ArgumentNullException());

            TestWriteFileExceptionsWithEncoding("temp.txt", "null encoding", contents, null, new ArgumentNullException(), File.WriteAllLines, File.WriteAllLines, "File.WriteAllLines");
            TestWriteFileExceptionsWithEncoding("temp.txt", "null encoding", contents, null, new ArgumentNullException(), File.AppendAllLines, File.AppendAllLines, "File.AppendAllLines");
        }
Esempio n. 3
0
        private static void TestFileExceptions()
        {
            // Create common file and dir names
            String nullFileName   = null;
            String emptyFileName1 = "";
            String emptyFileName2 = " ";
            String longPath       = new String('a', 240) + @"\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\test.txt";

            String notExistsFileName = null;
            String unusedDrive       = EnumerableUtils.GetUnusedDrive();

            if (unusedDrive != null)
            {
                notExistsFileName = Path.Combine(unusedDrive, @"temp\notExists\temp.txt"); // just skip otherwise
            }
            String[]             lines    = { "test line 1", "test line 2" };
            IEnumerable <String> contents = (IEnumerable <String>)lines;

            // Read exceptions
            TestReadFileExceptions(nullFileName, "null path", Encoding.UTF8, new ArgumentNullException());
            TestReadFileExceptions(emptyFileName1, "empty path 1", Encoding.UTF8, new ArgumentException());
            TestReadFileExceptions(emptyFileName2, "empty path 2", Encoding.UTF8, new ArgumentException());
            TestReadFileExceptions(longPath, "long path", Encoding.UTF8, new PathTooLongException());
            if (notExistsFileName != null)
            {
                TestReadFileExceptions(notExistsFileName, "not exists", Encoding.UTF8, new DirectoryNotFoundException());
            }
            TestReadFileExceptionsWithEncoding("temp.txt", "null encoding", null, new ArgumentNullException(), File.ReadLines, File.ReadLines, "File.ReadLines");

            // Write exceptions
            TestWriteFileExceptions(nullFileName, "null path", contents, Encoding.UTF8, new ArgumentNullException());
            TestWriteFileExceptions(emptyFileName1, "empty path 1", contents, Encoding.UTF8, new ArgumentException());
            TestWriteFileExceptions(emptyFileName2, "empty path 2", contents, Encoding.UTF8, new ArgumentException());
            TestWriteFileExceptions(longPath, "long path", contents, Encoding.UTF8, new PathTooLongException());
            if (notExistsFileName != null)
            {
                TestWriteFileExceptions(notExistsFileName, "not exists", contents, Encoding.UTF8, new DirectoryNotFoundException());
            }
            TestWriteFileExceptions("temp.txt", "null contents", null, Encoding.UTF8, new ArgumentNullException());

            TestWriteFileExceptionsWithEncoding("temp.txt", "null encoding", contents, null, new ArgumentNullException(), File.WriteAllLines, File.WriteAllLines, "File.WriteAllLines");
            TestWriteFileExceptionsWithEncoding("temp.txt", "null encoding", contents, null, new ArgumentNullException(), File.AppendAllLines, File.AppendAllLines, "File.AppendAllLines");
        }