示例#1
0
    public static void runTest()
    {
        try
        {
            String        dirName;
            String[]      expectedFiles;
            String[]      files;
            List <String> list;
            // part I - SearchOption.TopDirectoryOnly
            //Scenario 1:Vanilla - Create a directory, add a few files and call with searchPattern *.* and verify
            //that all files are returned.
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 1, 10))
                {
                    expectedFiles = fileManager.GetFiles(dirName, 0);
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, "*.*", SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_3947g! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_582bmw! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_891vut! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_349t7g! Exception caught in scenario: {0}", ex);
            }

            //Scenario 2: Add some directories to the vanilla scenario and ensure that these are not returned
            //Scenario 3: Ensure that the path contains subdirectories and call this API and ensure that only the top directory files are returned
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    expectedFiles = fileManager.GetFiles(dirName, 0);
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, "*.*", SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_763pjg! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_512kvk! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_839rbd! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_997pvx! Exception caught in scenario: {0}", ex);
            }

            // Path is not in the current directory (same drive)
            //Scenario 4: Ensure that the path contains subdirectories and call this API and ensure that only the top directory files are returned

            /* Scenario disabled when porting because it modifies the filesystem outside of the test's working directory
             * try
             * {
             *  dirName = ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), ManageFileSystem.DirPrefixName);
             *  using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
             *  {
             *      expectedFiles = fileManager.GetFiles(dirName, 0);
             *      list = new List<String>(expectedFiles);
             *      files = Directory.GetFiles(dirName, "*.*", SearchOption.TopDirectoryOnly);
             *      Eval(files.Length, list.Count, "Err_386gef! wrong file count.");
             *      for (int i = 0; i < files.Length; i++)
             *      {
             *          //This will return as \<dirName>\<fileName> whereas our utility will return as <drive>:\<dirName>\<fileName>
             *          String fileFullName = Path.GetFullPath(files[i]);
             *          if (Eval(list.Contains(fileFullName), "Err_932izm! unexpected file found: {0}", fileFullName))
             *              list.Remove(fileFullName);
             *      }
             *      if (!Eval(list.Count == 0, "Err_915sae! {0} expected files not found.", list.Count))
             *      {
             *          foreach (String fileName in list)
             *              Console.WriteLine(fileName);
             *      }
             *  }
             *  //only 1 level
             *  dirName = ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), ManageFileSystem.DirPrefixName);
             *  dirName = Path.GetFullPath(dirName);
             *  using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 1, 10))
             *  {
             *      expectedFiles = fileManager.GetFiles(dirName, 0);
             *      list = new List<String>(expectedFiles);
             *      files = Directory.GetFiles(dirName, "*.*", SearchOption.TopDirectoryOnly);
             *      Eval(files.Length, list.Count, "Err_792ifb! wrong file count.");
             *      for (int i = 0; i < files.Length; i++)
             *      {
             *          if (Eval(list.Contains(files[i]), "Err_281tff! unexpected file found: {0}", files[i]))
             *              list.Remove(files[i]);
             *      }
             *      if (!Eval(list.Count == 0, "Err_792qdn! {0} expected files not found.", list.Count))
             *      {
             *          foreach (String fileName in list)
             *              Console.WriteLine(fileName);
             *      }
             *  }
             * }
             * catch (Exception ex)
             * {
             *  s_pass = false;
             *  Console.WriteLine("Err_992hic! Exception caught in scenario: {0}", ex);
             * }
             */

            //Scenario 5: searchPattern variations - valid search characters, file match exactly the searchPattern, searchPattern is a subset of existing files, superset, no match,
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                String searchPattern;
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    expectedFiles = fileManager.GetFiles(dirName, 0);
                    //?
                    int maxLen = 0;
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileNameWithoutExtension(file);
                        if (realFile.Length > maxLen)
                        {
                            maxLen = realFile.Length;
                        }
                    }
                    searchPattern = String.Format("{0}.???", new String('?', maxLen));
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_488sjb! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_750dop! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_629dvi! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //file match exactly
                    searchPattern = Path.GetFileName(expectedFiles[0]);
                    list          = new List <String>(new String[] { expectedFiles[0] });
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_841dnz! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_796xxd! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_552puh! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //subset
                    String        tempSearchPattern = Path.GetFileName(expectedFiles[0]).Substring(2);
                    List <String> newFiles          = new List <String>();
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileName(file);
                        if (realFile.Substring(2).Equals(tempSearchPattern))
                        {
                            newFiles.Add(file);
                        }
                    }
                    searchPattern = String.Format("??{0}", tempSearchPattern);

                    list  = newFiles;
                    files = Directory.GetFiles(dirName, searchPattern, SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_847vxz! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_736kfh! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_576atr! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //there shouldn't be any with just the suffix
                    searchPattern = tempSearchPattern;
                    list          = new List <String>();
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_624jmn! wrong file count.");

                    //superset
                    searchPattern = String.Format("blah{0}", Path.GetFileName(expectedFiles[0]));
                    newFiles      = new List <String>();
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileName(file);
                        if (realFile.Equals(searchPattern))
                        {
                            newFiles.Add(file);
                        }
                    }

                    list  = newFiles;
                    files = Directory.GetFiles(dirName, searchPattern, SearchOption.TopDirectoryOnly);
                    Eval(files.Length, list.Count, "Err_026zqz! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_832yyg! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_605dke! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_728ono! Exception caught in scenario: {0}", ex);
            }

            //Scenario 6: Different local drives, Network drives (UNC and drive letters)

            /* Scenario disabled when porting because it modifies the filesystem outside of the test's working directory
             * try
             * {
             *  string otherDriveInMachine = IOServices.GetNonNtfsDriveOtherThanCurrent();
             *  if (otherDriveInMachine != null)
             *  {
             *      dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName);
             *      using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
             *      {
             *          Console.WriteLine("otherDriveInMachine: {0}", dirName);
             *          expectedFiles = fileManager.GetFiles(dirName, 0);
             *          list = new List<String>(expectedFiles);
             *          files = Directory.GetFiles(dirName, "*.*", SearchOption.TopDirectoryOnly);
             *          Eval(files.Length, list.Count, "Err_337kkf! wrong file count.");
             *          for (int i = 0; i < files.Length; i++)
             *          {
             *              if (Eval(list.Contains(files[i]), "Err_448nzn! unexpected file found: {0}", files[i]))
             *                  list.Remove(files[i]);
             *          }
             *          if (!Eval(list.Count == 0, "Err_849fvp! {0} expected files not found.", list.Count))
             *          {
             *              foreach (String fileName in list)
             *                  Console.WriteLine(fileName);
             *          }
             *      }
             *  }
             *
             *  // network path scenario moved to RemoteIOTests.cs
             * }
             * catch (Exception ex)
             * {
             *  s_pass = false;
             *  Console.WriteLine("Err_768lme! Exception caught in scenario: {0}", ex);
             * }
             */


            //Scenario 7: Arguments validation:
            // - nulls for the first 2,
            // - outside range for the enum value.
            // - Path contains empty, space and invalid filename, long, readonly invalid characters. The same for searchPattern parm as well
            try
            {
                String[] invalidValuesForPath   = Interop.IsWindows ? new[] { "", " ", ">" } : new[] { "", "\0" };
                String[] invalidValuesForSearch = { "..", @".." + Path.DirectorySeparatorChar };
                CheckException <ArgumentNullException>(delegate { files = Directory.GetFiles(null, "*.*", SearchOption.TopDirectoryOnly); }, "Err_347g! worng exception thrown");
                CheckException <ArgumentNullException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), null, SearchOption.TopDirectoryOnly); }, "Err_326pgt! worng exception thrown");
                CheckException <ArgumentOutOfRangeException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*", (SearchOption)100); }, "Err_589kvu! worng exception thrown - see bug #386545");
                CheckException <ArgumentOutOfRangeException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*", (SearchOption)(-1)); }, "Err_359vcj! worng exception thrown - see bug #386545");
                for (int i = 0; i < invalidValuesForPath.Length; i++)
                {
                    CheckException <ArgumentException>(delegate { files = Directory.GetFiles(invalidValuesForPath[i], "*.*", SearchOption.TopDirectoryOnly); }, String.Format("Err_347sd_{0}! worng exception thrown: {1}", i, invalidValuesForPath[i]));
                }
                for (int i = 0; i < invalidValuesForSearch.Length; i++)
                {
                    CheckException <ArgumentException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), invalidValuesForSearch[i], SearchOption.TopDirectoryOnly); }, String.Format("Err_074ts! worng exception thrown: {1}", i, invalidValuesForSearch[i]));
                }
                Char[] invalidPaths = Path.GetInvalidPathChars();
                for (int i = 0; i < invalidPaths.Length; i++)
                {
                    CheckException <ArgumentException>(delegate { files = Directory.GetFiles(invalidPaths[i].ToString(), "*.*", SearchOption.TopDirectoryOnly); }, String.Format("Err_538wyc! worng exception thrown: {1}", i, invalidPaths[i]));
                }
                Char[] invalidFileNames = Interop.IsWindows ? Path.GetInvalidFileNameChars() : new[] { '\0' };
                for (int i = 0; i < invalidFileNames.Length; i++)
                {
                    switch (invalidFileNames[i])
                    {
                    case '\\':
                    case '/':
                        CheckException <DirectoryNotFoundException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_631bwy_{0}! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                        break;

                    //We dont throw in V1 too
                    case ':':
                        //History:
                        // 1) we assumed that this will work in all non-9x machine
                        // 2) Then only in XP
                        // 3) NTFS?
                        if (Interop.IsWindows && FileSystemDebugInfo.IsCurrentDriveNTFS())     // testing NTFS
                        {
                            CheckException <IOException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_997gqs_{0}! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                        }
                        else
                        {
                            try
                            {
                                files = Directory.GetFiles(Directory.GetCurrentDirectory(), String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly);
                            }
                            catch (IOException)
                            {
                                Console.WriteLine(FileSystemDebugInfo.MachineInfo());
                                Eval(false, "Err_961lcx! Another OS throwing for DI.GetFiles(). modify the above check after confirming the v1.x behavior in that machine");
                            }
                        }

                        break;

                    case '*':
                    case '?':
                        files = Directory.GetFiles(Directory.GetCurrentDirectory(), String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly);
                        break;

                    default:
                        CheckException <ArgumentException>(delegate { files = Directory.GetFiles(Directory.GetCurrentDirectory(), String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_036gza! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                        break;
                    }
                }
                //path too long
                CheckException <PathTooLongException>(delegate { files = Directory.GetFiles(Path.Combine(new String('a', IOInputs.MaxPath), new String('b', IOInputs.MaxPath)), "*.*", SearchOption.TopDirectoryOnly); }, String.Format("Err_927gs! wrong exception thrown"));
                CheckException <PathTooLongException>(delegate { files = Directory.GetFiles(new String('a', IOInputs.MaxPath), new String('b', IOInputs.MaxPath), SearchOption.TopDirectoryOnly); }, String.Format("Err_213aka! wrong exception thrown"));
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_995bae! Exception caught in scenario: {0}", ex);
            }

            // part II - SearchOption.AllDirectories
            //Scenario 1: Vanilla - create a directory with some files and then add a couple of directories with some files and check with searchPattern *.*
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    expectedFiles = fileManager.GetAllFiles();
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_415mbz! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_287kkm! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_921mhs! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_415nwr! Exception caught in scenario: {0}", ex);
            }

            //Scenario 2: create a directory only top level files and directories and check
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 1, 10))
                {
                    expectedFiles = fileManager.GetAllFiles();
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_202wur! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_611lgv! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_648ibm! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_391mwx! Exception caught in scenario: {0}", ex);
            }

            // Path is not in the current directory (same drive)
            //Scenario 3: Ensure that the path contains subdirectories and call this API and ensure that only the top directory files are returned

            /* Scenario disabled when porting because it modifies the filesystem outside of the test's working directory
             * try
             * {
             *  dirName = ManageFileSystem.GetNonExistingDir(Path.DirectorySeparatorChar.ToString(), ManageFileSystem.DirPrefixName);
             *  using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
             *  {
             *      expectedFiles = fileManager.GetAllFiles();
             *      list = new List<String>(expectedFiles);
             *      files = Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories);
             *      Eval(files.Length, list.Count, "Err_123rcm! wrong file count.");
             *      for (int i = 0; i < files.Length; i++)
             *      {
             *          //This will return as \<dirName>\<fileName> whereas our utility will return as <drive>:\<dirName>\<fileName>
             *          String fileFullName = Path.GetFullPath(files[i]);
             *          if (Eval(list.Contains(fileFullName), "Err_242yur! unexpected file found: {0}", fileFullName))
             *              list.Remove(fileFullName);
             *      }
             *      if (!Eval(list.Count == 0, "Err_477xiv! {0} expected files not found.", list.Count))
             *      {
             *          foreach (String fileName in list)
             *              Console.WriteLine(fileName);
             *      }
             *  }
             * }
             * catch (Exception ex)
             * {
             *  s_pass = false;
             *  Console.WriteLine("Err_401dkm! Exception caught in scenario: {0}", ex);
             * }
             */

            //Scenario 4: searchPattern variations - valid search characters, file match exactly the searchPattern, searchPattern is a subset of existing files, superset, no match,
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                String searchPattern;
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    expectedFiles = fileManager.GetAllFiles();
                    //?
                    int maxLen = 0;
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileNameWithoutExtension(file);
                        if (realFile.Length > maxLen)
                        {
                            maxLen = realFile.Length;
                        }
                    }
                    searchPattern = String.Format("{0}.???", new String('?', maxLen));
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_654wlf! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_792olh! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_434gew! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //file match exactly
                    searchPattern = Path.GetFileName(expectedFiles[0]);
                    list          = new List <String>(new String[] { expectedFiles[0] });
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_427fug! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_382bzl! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_008xan! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //subset
                    String        tempSearchPattern = Path.GetFileName(expectedFiles[0]).Substring(2);
                    List <String> newFiles          = new List <String>();
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileName(file);
                        if (realFile.Substring(2).Equals(tempSearchPattern))
                        {
                            newFiles.Add(file);
                        }
                    }
                    searchPattern = String.Format("??{0}", tempSearchPattern);

                    list  = newFiles;
                    files = Directory.GetFiles(dirName, searchPattern, SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_030bfw! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_393mly! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_328gse! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }

                    //there shouldn't be any with just the suffix
                    searchPattern = tempSearchPattern;
                    list          = new List <String>();
                    files         = Directory.GetFiles(dirName, searchPattern, SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_747fnq! wrong file count.");

                    //superset
                    searchPattern = String.Format("blah{0}", Path.GetFileName(expectedFiles[0]));
                    newFiles      = new List <String>();
                    foreach (String file in expectedFiles)
                    {
                        String realFile = Path.GetFileName(file);
                        if (realFile.Equals(searchPattern))
                        {
                            newFiles.Add(file);
                        }
                    }

                    list  = newFiles;
                    files = Directory.GetFiles(dirName, searchPattern, SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_969vnk! wrong file count.");
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_353ygu! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_830vvw! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_983ist! Exception caught in scenario: {0}", ex);
            }

            //Scenario 6: Different local drives, Network drives (UNC and drive letters)

            /* Scenario disabled when porting because it modifies the filesystem outside of the test's working directory
             * try
             * {
             *  string otherDriveInMachine = IOServices.GetNonNtfsDriveOtherThanCurrent();
             *  if (otherDriveInMachine != null)
             *  {
             *      dirName = ManageFileSystem.GetNonExistingDir(otherDriveInMachine, ManageFileSystem.DirPrefixName);
             *      using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
             *      {
             *          expectedFiles = fileManager.GetAllFiles();
             *          list = new List<String>(expectedFiles);
             *          files = Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories);
             *          Eval(files.Length, list.Count, "Err_573sdo! wrong file count.");
             *          for (int i = 0; i < files.Length; i++)
             *          {
             *              if (Eval(list.Contains(files[i]), "Err_778kdo! unexpected file found: {0}", files[i]))
             *                  list.Remove(files[i]);
             *          }
             *          if (!Eval(list.Count == 0, "Err_954xcx! {0} expected files not found.", list.Count))
             *          {
             *              foreach (String fileName in list)
             *                  Console.WriteLine(fileName);
             *          }
             *      }
             *  }
             *
             *  // network path scenario moved to RemoteIOTests.cs
             * }
             * catch (Exception ex)
             * {
             *  s_pass = false;
             *  Console.WriteLine("Err_064vel! Exception caught in scenario: {0}", ex);
             * }
             */

            //Scenario 7: dir is readonly
            try
            {
                //Readonly
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    //now lets make this directory readonly?
                    new DirectoryInfo(dirName).Attributes = new DirectoryInfo(dirName).Attributes | FileAttributes.ReadOnly;

                    expectedFiles = fileManager.GetAllFiles();
                    list          = new List <String>(expectedFiles);
                    files         = Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories);
                    Eval(files.Length, list.Count, "Err_862vhr! wrong file count.");

                    for (int i = 0; i < files.Length; i++)
                    {
                        if (Eval(list.Contains(files[i]), "Err_556ioj! unexpected file found: {0}", files[i]))
                        {
                            list.Remove(files[i]);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_562xwh! {0} expected files not found.", list.Count))
                    {
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                    if (Directory.Exists(dirName) && (new DirectoryInfo(dirName).Attributes & FileAttributes.ReadOnly) != 0)
                    {
                        new DirectoryInfo(dirName).Attributes = new DirectoryInfo(dirName).Attributes ^ FileAttributes.ReadOnly;
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_418qfv! Exception caught in scenario: {0}", ex);
            }
        }
        catch (Exception ex)
        {
            s_pass = false;
            Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex);
        }

        Assert.True(s_pass);
    }
示例#2
0
    public static void RunTest()
    {
        try
        {
            String        dirName;
            DirectoryInfo dirInfo;
            String[]      expectedFiles;
            FileInfo[]    files;
            List <String> list;

            //Scenario 1: Vanilla
            try
            {
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);

                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    dirInfo       = new DirectoryInfo(dirName);
                    expectedFiles = fileManager.GetAllFiles();
                    list          = new List <String>(expectedFiles);
                    files         = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
                    Eval(files.Length == list.Count, "Err_415mbz! wrong count");
                    for (int i = 0; i < expectedFiles.Length; i++)
                    {
                        if (Eval(list.Contains(files[i].FullName), "Err_287kkm! No file found: {0}", files[i].FullName))
                        {
                            list.Remove(files[i].FullName);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_921mhs! wrong count: {0}", list.Count))
                    {
                        Console.WriteLine();
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_349t7g! Exception caught in scenario: {0}", ex);
            }

            //Scenario 3: Parm Validation
            try
            {
                // dir not present and then after creating
                dirName = ManageFileSystem.GetNonExistingDir(Directory.GetCurrentDirectory(), ManageFileSystem.DirPrefixName);
                dirInfo = new DirectoryInfo(dirName);
                CheckException <DirectoryNotFoundException>(delegate { files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories); }, "Err_326pgt! worng exception thrown");

                // create the dir and then check that we dont cache this info
                using (ManageFileSystem fileManager = new ManageFileSystem(dirName, 3, 100))
                {
                    dirInfo       = new DirectoryInfo(dirName);
                    expectedFiles = fileManager.GetAllFiles();
                    list          = new List <String>(expectedFiles);
                    files         = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
                    Eval(files.Length == list.Count, "Err_948kxt! wrong count");
                    for (int i = 0; i < expectedFiles.Length; i++)
                    {
                        if (Eval(list.Contains(files[i].FullName), "Err_535xaj! No file found: {0}", files[i].FullName))
                        {
                            list.Remove(files[i].FullName);
                        }
                    }
                    if (!Eval(list.Count == 0, "Err_370pjl! wrong count: {0}", list.Count))
                    {
                        Console.WriteLine();
                        foreach (String fileName in list)
                        {
                            Console.WriteLine(fileName);
                        }
                    }
                    CheckException <ArgumentNullException>(delegate { files = dirInfo.GetFiles(null, SearchOption.TopDirectoryOnly); }, "Err_751mwu! worng exception thrown");
                    CheckException <ArgumentOutOfRangeException>(delegate { files = dirInfo.GetFiles("*.*", (SearchOption)100); }, "Err_589kvu! worng exception thrown - see bug #386545");
                    CheckException <ArgumentOutOfRangeException>(delegate { files = dirInfo.GetFiles("*.*", (SearchOption)(-1)); }, "Err_359vcj! worng exception thrown - see bug #386545");
                    String[] invalidValuesForSearch = { "..", @"..\" };
                    for (int i = 0; i < invalidValuesForSearch.Length; i++)
                    {
                        CheckException <ArgumentException>(delegate { files = dirInfo.GetFiles(invalidValuesForSearch[i], SearchOption.TopDirectoryOnly); }, String.Format("Err_631bwy! worng exception thrown: {1}", i, invalidValuesForSearch[i]));
                    }
                    Char[] invalidFileNames = Path.GetInvalidFileNameChars();
                    for (int i = 0; i < invalidFileNames.Length; i++)
                    {
                        switch (invalidFileNames[i])
                        {
                        case '\\':
                        case '/':
                            CheckException <DirectoryNotFoundException>(delegate { files = dirInfo.GetFiles(String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_631bwy_{0}! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                            break;

                        case ':':
                            //History:
                            // 1) we assumed that this will work in all non-9x machine
                            // 2) Then only in XP
                            // 3) NTFS?
                            if (FileSystemDebugInfo.IsCurrentDriveNTFS())
                            {
                                CheckException <IOException>(delegate { files = dirInfo.GetFiles(String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_997gqs! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                            }
                            else
                            {
                                try
                                {
                                    files = dirInfo.GetFiles(String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly);
                                }
                                catch (IOException)
                                {
                                    Console.WriteLine(FileSystemDebugInfo.MachineInfo());
                                    Eval(false, "Err_3947g! Another OS throwing for DI.GetFiles(). modify the above check after confirming the v1.x behavior in that machine");

                                    /**
                                     *      try
                                     *      {
                                     *            DirectoryInfo dirInfo = new DirectoryInfo(".");
                                     *          FileInfo[] files = dirInfo.GetFiles("te:st");
                                     *      pass=false;
                                     *      Console.WriteLine("No exception thrown");
                                     *      }
                                     *  catch(IOException){}
                                     *      catch (Exception ex)
                                     *      {
                                     *      pass=false;
                                     *      Console.WriteLine("Err_723jvl! Different Exception caught in scenario: {0}", ex);
                                     *      }
                                     *
                                     **/
                                }
                            }
                            break;

                        case '*':
                        case '?':
                            files = dirInfo.GetFiles(String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly);
                            break;

                        default:
                            CheckException <ArgumentException>(delegate { files = dirInfo.GetFiles(String.Format("te{0}st", invalidFileNames[i].ToString()), SearchOption.TopDirectoryOnly); }, String.Format("Err_036gza! worng exception thrown: {1} - bug#387196", i, (int)invalidFileNames[i]));
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_pass = false;
                Console.WriteLine("Err_006dwq! Exception caught in scenario: {0}", ex);
            }

            //Scenario for bug #461014 - Getting files/directories of CurrentDirectory of drives are broken

            /* Test disabled while porting because it relies on state outside of its working directory not changing over time
             * try
             * {
             *  string anotherDrive = IOServices.GetNtfsDriveOtherThanCurrent();
             *  String[] paths = null == anotherDrive ? new String[] { Directory.GetCurrentDirectory() } : new String[] { anotherDrive, Directory.GetCurrentDirectory() };
             *  String path;
             *
             *  for (int i = 0; i < paths.Length; i++)
             *  {
             *      path = paths[i];
             *      if (path.Length > 1)
             *      {
             *          path = path.Substring(0, 2);
             *
             *          FileInfo[] f1 = new DirectoryInfo(Path.GetFullPath(path)).GetFiles();
             *          FileInfo[] f2 = new DirectoryInfo(path).GetFiles();
             *          Eval<int>(f1.Length, f2.Length, "Err_2497gds! wrong value");
             *          for (int j = 0; j < f1.Length; j++)
             *          {
             *              Eval<String>(f1[j].FullName, f2[j].FullName, "Err_03284t! wrong value");
             *              Eval<String>(f1[j].Name, f2[j].Name, "Err_03284t! wrong value");
             *          }
             *      }
             *  }
             * }
             * catch (Exception ex)
             * {
             *  s_pass = false;
             *  Console.WriteLine("Err_349t7g! Exception caught in scenario: {0}", ex);
             * }
             */
        }
        catch (Exception ex)
        {
            s_pass = false;
            Console.WriteLine("Err_234rsgf! Uncaught exception in RunTest: {0}", ex);
        }


        Assert.True(s_pass);
    }