Ejemplo n.º 1
0
        public NtStatus UnsafeCreateFile(string fileName, DokanNet.FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes, DokanFileInfo info)
        {
            string writePath    = GetWritePath(fileName);
            string readOnlyPath = GetReadOnlyPath(fileName);

            if (info.IsDirectory)
            {
                if (mode == FileMode.CreateNew)
                {
                    if (Directory.Exists(writePath))
                    {
                        return(DokanResult.FileExists);
                    }
                    else if (File.Exists(writePath))
                    {
                        return(DokanResult.AlreadyExists);
                    }
                    else if (Directory.Exists(readOnlyPath))
                    {
                        return(DokanResult.FileExists);
                    }
                    else if (File.Exists(readOnlyPath))
                    {
                        return(DokanResult.AlreadyExists);
                    }

                    info.Context = new LayeredDirectoryContext(fileName)
                    {
                        WriteDirInfo = Directory.CreateDirectory(writePath)
                    };
                    return(DokanResult.Success);
                }
                else if (mode == FileMode.Open)
                {
                    var context = new LayeredDirectoryContext(fileName);

                    if (Directory.Exists(writePath))
                    {
                        context.WriteDirInfo = new DirectoryInfo(writePath);
                    }
                    else if (File.Exists(writePath))
                    {
                        return(DokanResult.NotADirectory);
                    }

                    if (Directory.Exists(readOnlyPath))
                    {
                        context.ReadOnlyDirInfo = new DirectoryInfo(readOnlyPath);
                    }
                    else if (context.WriteDirInfo == null)
                    {
                        if (File.Exists(readOnlyPath))
                        {
                            return(DokanResult.NotADirectory);
                        }
                        else
                        {
                            return(DokanResult.PathNotFound);
                        }
                    }

                    info.Context = context;
                    return(DokanResult.Success);
                }
                else
                {
                    // unkown operation
                    return(DokanResult.Unsuccessful);
                }
            }
            else
            {
                var writable        = false;
                var pathExists      = false;
                var pathIsDirectory = Directory.Exists(writePath);

                if (pathIsDirectory)
                {
                    pathExists = true;
                }
                else if (File.Exists(writePath))
                {
                    writable   = true;
                    pathExists = true;
                }
                else
                {
                    if (pathIsDirectory = Directory.Exists(readOnlyPath))
                    {
                        pathExists      = true;
                        pathIsDirectory = true;
                    }
                    else
                    {
                        pathExists = File.Exists(readOnlyPath);
                    }
                }

                var readAttributes = (access & ReadAttributes) == 0;
                var readAccess     = (access & DataWriteAccess) == 0;

                switch (mode)
                {
                case FileMode.Open:
                    if (!pathExists)
                    {
                        return(DokanResult.FileNotFound);
                    }
                    else
                    {
                        if (pathIsDirectory)
                        {
                            info.IsDirectory = true;

                            if ((access & FileAccess.Delete) == FileAccess.Delete &&
                                (access & FileAccess.Synchronize) != FileAccess.Synchronize)
                            {
                                // it is a DeleteFile request on a directory
                                return(DokanResult.AccessDenied);
                            }

                            // call it again, with the IsDirectory set to true
                            return(UnsafeCreateFile(fileName, access, share, mode, options, attributes, info));
                        }
                        else if (readAttributes)
                        {
                            info.Context = new LayeredReadAttributesContext(fileName, writable ? writePath : readOnlyPath, pathIsDirectory);
                            return(DokanResult.Success);
                        }
                    }
                    break;

                case FileMode.CreateNew:
                    if (writable)
                    {
                        if (pathExists)
                        {
                            return(DokanResult.FileExists);
                        }
                    }
                    writable = true;

                    break;

                case FileMode.Create:
                    writable = true;
                    break;

                case FileMode.Truncate:
                    if (!pathExists)
                    {
                        return(DokanResult.FileNotFound);
                    }
                    break;

                case FileMode.OpenOrCreate:
                    if (!pathExists)
                    {
                        writable = true;
                    }
                    break;

                default:
                    throw new Exception($"Unhandled FileMode {mode.ToString("g")}");
                }

                if (writable)
                {
                    var path = Path.GetDirectoryName(writePath);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                }
                else if (!readAccess)
                {
                    var path = Path.GetDirectoryName(writePath);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    File.Copy(readOnlyPath, writePath, true);

                    FileInfo finfo = new FileInfo(readOnlyPath);
                    File.SetCreationTime(writePath, finfo.CreationTime);
                    File.SetLastAccessTime(writePath, finfo.LastAccessTime);
                    File.SetLastWriteTime(writePath, finfo.LastWriteTime);

                    writable = true;
                }

                LayeredFileContext context = new LayeredFileContext(fileName, writable ? writePath : readOnlyPath, writable);
                info.Context = context;

                try
                {
                    context.Stream = new FileStream(writable ? writePath : readOnlyPath, mode,
                                                    readAccess ? System.IO.FileAccess.Read : System.IO.FileAccess.ReadWrite, share, 4096, options);
                }
                catch (Exception ex)
                {
                    var hr = (uint)Marshal.GetHRForException(ex);
                    switch (hr)
                    {
                    case 0x80070020:     //Sharing violation
                        return(DokanResult.SharingViolation);

                    default:
                        throw;
                    }
                }

                if (mode == FileMode.CreateNew || mode == FileMode.Create) // files are always created as Archive
                {
                    attributes |= FileAttributes.Archive;
                    context.SetAttributes(attributes);
                }

                return(DokanResult.Success);
            }
        }
Ejemplo n.º 2
0
        public void DoSetup()
        {
            string vmdir    = MainDir;
            string datadir  = MainDir;
            string modeldir = MainDir;
            string vmns     = MainNs + ".ViewModels";
            string datans   = MainNs;
            string modelns  = MainNs;

            if (ProjectType == ProjectTypeEnum.Single)
            {
                Directory.CreateDirectory($"{MainDir}\\Models");
                File.WriteAllText($"{MainDir}\\Models\\ReadMe.txt", "Put your models here");
                Directory.CreateDirectory($"{MainDir}\\ViewModels\\HomeVMs");
                vmdir = MainDir + "\\ViewModels";
            }
            else
            {
                Directory.CreateDirectory($"{MainDir}.ViewModel\\HomeVMs");
                Directory.CreateDirectory($"{MainDir}.Model");
                Directory.CreateDirectory($"{MainDir}.DataAccess");
                vmdir    = MainDir + ".ViewModel";
                datadir  = MainDir + ".DataAccess";
                modeldir = MainDir + ".Model";
                vmns     = MainNs + ".ViewModel";
                datans   = MainNs + ".DataAccess";
                modelns  = MainNs + ".Model";
                File.WriteAllText($"{modeldir}\\{modelns}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
                File.WriteAllText($"{vmdir}\\{vmns}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
                File.WriteAllText($"{datadir}\\{datans}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
            }
            Directory.CreateDirectory($"{MainDir}\\Areas");
            Directory.CreateDirectory($"{MainDir}\\Controllers");
            Directory.CreateDirectory($"{MainDir}\\Views\\Home");
            Directory.CreateDirectory($"{MainDir}\\Views\\Login");
            Directory.CreateDirectory($"{MainDir}\\Views\\Shared");
            Directory.CreateDirectory($"{MainDir}\\wwwroot");

            File.WriteAllText($"{MainDir}\\appsettings.json", GetResource("Appsettings.txt")
                              .Replace("$cs$", CS ?? "")
                              .Replace("$dbtype$", DbType.ToString())
                              .Replace("$cookiepre$", CookiePre ?? "")
                              .Replace("$enablelog$", EnableLog.ToString().ToLower())
                              .Replace("$logexception$", LogExceptionOnly.ToString().ToLower())
                              .Replace("$rpp$", Rpp == null ? "" : Rpp.ToString())
                              .Replace("$filemode$", FileMode.ToString())
                              .Replace("$uploaddir$", UploadDir ?? ""), Encoding.UTF8
                              );
            File.WriteAllText($"{datadir}\\DataContext.cs", GetResource("DataContext.txt").Replace("$ns$", datans), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Controllers\\HomeController.cs", GetResource("HomeController.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Controllers\\LoginController.cs", GetResource("LoginController.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\_ViewStart.cshtml", GetResource("ViewStart.txt"), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Home\\Index.cshtml", GetResource("home.Index.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Login\\ChangePassword.cshtml", GetResource("home.ChangePassword.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Home\\Header.cshtml", GetResource("home.Header.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Login\\Login.cshtml", GetResource("home.Login.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Home\\Menu.cshtml", GetResource("home.Menu.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Home\\PIndex.cshtml", GetResource("home.PIndex.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{MainDir}\\Views\\Home\\FrontPage.cshtml", GetResource("home.FrontPage.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{vmdir}\\HomeVMs\\ChangePasswordVM.cs", GetResource("vms.ChangePasswordVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{vmdir}\\HomeVMs\\IndexVM.cs", GetResource("vms.IndexVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
            File.WriteAllText($"{vmdir}\\HomeVMs\\LoginVM.cs", GetResource("vms.LoginVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);

            if (UI == UIEnum.LayUI)
            {
                File.WriteAllText($"{MainDir}\\Views\\Shared\\_Layout.cshtml", GetResource("layui.Layout.txt").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}\\Views\\Shared\\_PLayout.cshtml", GetResource("layui.PLayout.txt").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}\\Program.cs", GetResource("layui.Program.txt").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}\\Views\\_ViewImports.cshtml", GetResource("layui.ViewImports.txt"), Encoding.UTF8);
                File.WriteAllText($"{MainDir}\\Areas\\_ViewImports.cshtml", GetResource("layui.ViewImports.txt"), Encoding.UTF8);
                Assembly assembly = Assembly.GetExecutingAssembly();
                var      sr       = assembly.GetManifestResourceStream($"WalkingTec.Mvvm.Mvc.SetupFiles.layui.layui.zip");
                System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(sr);
                foreach (var entry in zip.Entries)
                {
                    int index = entry.FullName.LastIndexOf("/");
                    if (index >= 0)
                    {
                        string dir = $"{MainDir}\\wwwroot\\{entry.FullName.Substring(0, index)}";
                        if (Directory.Exists(dir) == false)
                        {
                            Directory.CreateDirectory(dir);
                        }
                    }
                    if (entry.FullName.EndsWith("/") == false)
                    {
                        var f = File.OpenWrite($"{MainDir}\\wwwroot\\{entry.FullName}");
                        var z = entry.Open();
                        z.CopyTo(f);
                        f.Flush();
                        f.Dispose();
                        z.Dispose();
                    }
                }
                sr.Dispose();
            }
            if (ProjectType == ProjectTypeEnum.Single)
            {
                var proj = File.ReadAllText($"{MainDir}\\{MainNs}.csproj");
                if (proj.IndexOf("WalkingTec.Mvvm.TagHelpers.LayUI") < 0)
                {
                    proj = proj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.TagHelpers.LayUI"" Version=""{version}"" />
    <PackageReference Include=""WalkingTec.Mvvm.Mvc.Admin"" Version=""{version}"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{MainDir}\\{MainNs}.csproj", proj, Encoding.UTF8);
                }
            }
            if (ProjectType == ProjectTypeEnum.Multi)
            {
                var proj = File.ReadAllText($"{MainDir}\\{MainNs}.csproj");
                if (proj.IndexOf("WalkingTec.Mvvm.TagHelpers.LayUI") < 0)
                {
                    proj = proj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.TagHelpers.LayUI"" Version=""{version}"" />
    <PackageReference Include=""WalkingTec.Mvvm.Mvc.Admin"" Version=""{version}"" />
    <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
    <ProjectReference Include=""..\{datans}\{datans}.csproj"" />
    <ProjectReference Include=""..\{vmns}\{vmns}.csproj"" />
 </ItemGroup >
</Project>
");
                    File.WriteAllText($"{MainDir}\\{MainNs}.csproj", proj, Encoding.UTF8);
                }
                //修改modelproject
                var modelproj = File.ReadAllText($"{modeldir}\\{modelns}.csproj");
                if (modelproj.IndexOf("WalkingTec.Mvvm.Core") < 0)
                {
                    modelproj = modelproj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.Core"" Version=""{version}"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{modeldir}\\{modelns}.csproj", modelproj, Encoding.UTF8);
                }
                //修改dataproject
                var dataproj = File.ReadAllText($"{datadir}\\{datans}.csproj");
                if (dataproj.IndexOf($"{modelns}.csproj") < 0)
                {
                    dataproj = dataproj.Replace("</Project>", $@"
  <ItemGroup>
    <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{datadir}\\{datans}.csproj", dataproj, Encoding.UTF8);
                }
                //修改viewmodelproject
                var vmproj = File.ReadAllText($"{vmdir}\\{vmns}.csproj");
                if (vmproj.IndexOf($"{modelns}.csproj") < 0)
                {
                    vmproj = vmproj.Replace("</Project>", $@"
  <ItemGroup>
    <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{vmdir}\\{vmns}.csproj", vmproj, Encoding.UTF8);
                }
                var solution = File.ReadAllText($"{Directory.GetParent(MainDir)}\\{MainNs}.sln");
                if (solution.IndexOf($"{modelns}.csproj") < 0)
                {
                    Guid g1 = Guid.NewGuid();
                    Guid g2 = Guid.NewGuid();
                    Guid g3 = Guid.NewGuid();
                    solution = solution.Replace("EndProject", $@"EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{modelns}"", ""{modelns}\{modelns}.csproj"", ""{{{g1}}}""
EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{datans}"", ""{datans}\{datans}.csproj"", ""{{{g2}}}""
EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{vmns}"", ""{vmns}\{vmns}.csproj"", ""{{{g3}}}""
EndProject
");
                    solution = solution.Replace(".Release|Any CPU.Build.0 = Release|Any CPU", $@".Release|Any CPU.Build.0 = Release|Any CPU
		{{{g1}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g1}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g1}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g1}}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{{g2}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g2}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g2}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g2}}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{{g3}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g3}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g3}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g3}}}.Release|Any CPU.Build.0 = Release|Any CPU"        );
                    File.WriteAllText($"{Directory.GetParent(MainDir)}\\{MainNs}.sln", solution, Encoding.UTF8);
                }
            }
            if (File.Exists($"{MainDir}\\Startup.cs"))
            {
                File.Delete($"{MainDir}\\Startup.cs");
            }
        }
Ejemplo n.º 3
0
        private MFTestResults TestMethod(FileMode fm)
        {
            Log.Comment("Starting tests in FileMode: " + fm.ToString());
            FileInfo     fil2;
            StreamWriter sw2;
            Stream       fs2 = null;
            String       str2;
            int          iCountErrors = 0;


            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            Log.Comment("File does not exist");
            //------------------------------------------------------------------

            fil2 = new FileInfo(fileName);
            switch (fm)
            {
            case FileMode.CreateNew:
            case FileMode.Create:
            case FileMode.OpenOrCreate:

                Log.Comment("With a null string");
                try
                {
                    fs2 = File.Open(null, fm);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                }
                catch (ArgumentException ex)
                {
                    Log.Comment("Expected exception thrown :: " + ex.Message);
                }
                catch (Exception ex)
                {
                    iCountErrors++;
                    Log.Exception("Unexpected exception thrown :: " + ex.ToString());
                }

                Log.Comment("with an empty string");
                try
                {
                    fs2 = File.Open("", fm);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                }
                catch (ArgumentException ex)
                {
                    Log.Comment("Expected exception thrown :: " + ex.Message);
                }
                catch (Exception ex)
                {
                    iCountErrors++;
                    Log.Exception("Unexpected exception thrown :: " + ex.ToString());
                }

                fs2 = File.Open(fileName, fm);
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    Log.Exception("File not created, FileMode==" + fm.ToString());
                }
                fs2.Close();
                break;

            case FileMode.Open:
            case FileMode.Truncate:
                try
                {
                    fs2 = File.Open(fileName, fm);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (IOException fexc)
                {
                    Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            case FileMode.Append:
                try
                {
                    fs2 = File.Open(fileName, fm);
                    fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                    if (fs2.Length != 4)
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected file length .... " + fs2.Length);
                    }
                    fs2.Close();
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            default:
                iCountErrors++;
                Log.Exception("Invalid mode.");
                break;
            }
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            //------------------------------------------------------------------


            Log.Comment("File already exists");
            //------------------------------------------------------------------

            sw2  = new StreamWriter(fileName);
            str2 = "Du er en ape";
            sw2.Write(str2);
            sw2.Close();
            switch (fm)
            {
            case FileMode.CreateNew:
                try
                {
                    fs2 = File.Open(fileName, fm);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (IOException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            case FileMode.Create:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != 0)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect length of file==" + fil2.Length);
                }
                fs2.Close();
                break;

            case FileMode.OpenOrCreate:
            case FileMode.Open:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != str2.Length)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect length on file==" + fil2.Length);
                }
                fs2.Close();
                break;

            case FileMode.Truncate:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != 0)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect length on file==" + fil2.Length);
                }
                fs2.Close();
                break;

            case FileMode.Append:
                try
                {
                    fs2 = File.Open(fileName, fm);
                    fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                    if (fs2.Length != 16)
                    {      // already 12 characters are written to the file.
                        iCountErrors++;
                        Log.Exception("Unexpected file length .... " + fs2.Length);
                    }
                    fs2.Close();
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            default:
                iCountErrors++;
                Log.Exception("Invalid mode.");
                break;
            }

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            return(iCountErrors == 0 ? MFTestResults.Pass : MFTestResults.Fail);
        }
		internal static Stream DoGetOutputStream(INode node, string contentName, string encoding, FileMode mode, FileShare sharing)
		{
			if (contentName != null)
			{
				throw new NotSupportedException();
			}

			if (mode == FileMode.Append)
			{
				throw new NotSupportedException("FileMode: " + mode.ToString());
			}

			return WebRequest.Create(node.Address.Uri).GetRequestStream();
		}
Ejemplo n.º 5
0
        public NtStatus CreateFile(string fileName, FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes, DokanFileInfo info)
        {
            Debug.WriteLine(@"{0} : {1} {2}", fileName, mode.ToString(), access.ToString());

            fileName = ToUnixStylePath(fileName);

            if (fileName.EndsWith("desktop.ini", StringComparison.OrdinalIgnoreCase) ||
                fileName.EndsWith("autorun.inf", StringComparison.OrdinalIgnoreCase))
            {
                return DokanResult.FileNotFound;
            }

            // todo : add to memory cache
            bool exists = sftpClient.Exists(fileName);
            SftpFileAttributes attr = sftpClient.GetAttributes(fileName);
            bool readWriteAttributes = (access & DataAccess) == 0;
            bool readAccess = (access & DataWriteAccess) == 0;
            System.IO.FileAccess acs = readAccess ? System.IO.FileAccess.Read : System.IO.FileAccess.ReadWrite;

            switch(mode)
            {
                case FileMode.Open:

                    if (!exists)
                        return DokanResult.FileNotFound;

                    if (((uint)access & 0xe0000027) == 0 || attr.IsDirectory)
                    {
                        info.IsDirectory = attr.IsDirectory;
                        info.Context = new object();
                        return DokanResult.Success;
                    }

                    break;

                case FileMode.CreateNew:
                    if (exists)
                        return DokanResult.AlreadyExists;


                    // cache invalidate

                    break;

                case FileMode.Truncate:
                    if (!exists)
                        return DokanResult.FileNotFound;
                    // cache invalidate
                    break;

                default:
                    // cache invalidate
                    break;
            }

            try
            {
                info.Context = sftpClient.Open(fileName, mode, acs) as SftpFileStream;
            }
            catch(Renci.SshNet.Common.SshException)
            {
                return DokanResult.AccessDenied;
            }

            return DokanResult.Success;
        }
Ejemplo n.º 6
0
        private MFTestResults TestMethod(FileMode fm)
        {
            Log.Comment("Starting tests in FileMode: " + fm.ToString());
            FileInfo fil2;
            StreamWriter sw2;
            Stream fs2 = null;
            String str2;
            int iCountErrors = 0;


            if (File.Exists(fileName))
                File.Delete(fileName);

            Log.Comment("File does not exist");
            //------------------------------------------------------------------

            fil2 = new FileInfo(fileName);
            switch (fm)
            {
                case FileMode.CreateNew:
                case FileMode.Create:
                case FileMode.OpenOrCreate:

                    Log.Comment("With a null string");
                    try
                    {
                        fs2 = File.Open(null, fm);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created, FileMode==" + fm.ToString());
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        Log.Comment("Expected exception thrown :: " + ex.Message);
                    }
                    catch (Exception ex)
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception thrown :: " + ex.ToString());
                    }

                    Log.Comment("with an empty string");
                    try
                    {
                        fs2 = File.Open("", fm);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created, FileMode==" + fm.ToString());
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        Log.Comment("Expected exception thrown :: " + ex.Message);
                    }
                    catch (Exception ex)
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception thrown :: " + ex.ToString());
                    }

                    fs2 = File.Open(fileName, fm);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                    fs2.Close();
                    break;
                case FileMode.Open:
                case FileMode.Truncate:
                    try
                    {
                        fs2 = File.Open(fileName, fm);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (IOException fexc)
                    {
                        Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                case FileMode.Append:
                    try
                    {
                        fs2 = File.Open(fileName, fm);
                        fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                        if (fs2.Length != 4)
                        {
                            iCountErrors++;
                            Log.Exception("Unexpected file length .... " + fs2.Length);
                        }
                        fs2.Close();
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                default:
                    iCountErrors++;
                    Log.Exception("Invalid mode.");
                    break;
            }
            if (File.Exists(fileName))
                File.Delete(fileName);

            //------------------------------------------------------------------


            Log.Comment("File already exists");
            //------------------------------------------------------------------

            sw2 = new StreamWriter(fileName);
            str2 = "Du er en ape";
            sw2.Write(str2);
            sw2.Close();
            switch (fm)
            {
                case FileMode.CreateNew:
                    try
                    {
                        fs2 = File.Open(fileName, fm);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (IOException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                case FileMode.Create:
                    fs2 = File.Open(fileName, fm);
                    if (fs2.Length != 0)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length of file==" + fil2.Length);
                    }
                    fs2.Close();
                    break;
                case FileMode.OpenOrCreate:
                case FileMode.Open:
                    fs2 = File.Open(fileName, fm);
                    if (fs2.Length != str2.Length)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length on file==" + fil2.Length);
                    }
                    fs2.Close();
                    break;
                case FileMode.Truncate:
                    fs2 = File.Open(fileName, fm);
                    if (fs2.Length != 0)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length on file==" + fil2.Length);
                    }
                    fs2.Close();
                    break;
                case FileMode.Append:
                    try
                    {
                        fs2 = File.Open(fileName, fm);
                        fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                        if (fs2.Length != 16)
                        {  // already 12 characters are written to the file.
                            iCountErrors++;
                            Log.Exception("Unexpected file length .... " + fs2.Length);
                        }
                        fs2.Close();
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                default:
                    iCountErrors++;
                    Log.Exception("Invalid mode.");
                    break;
            }

            if (File.Exists(fileName))
                File.Delete(fileName);

            return iCountErrors == 0 ? MFTestResults.Pass : MFTestResults.Fail;
        }
Ejemplo n.º 7
0
	public void TestMethod(FileMode fm)
	{
			String fileName = s_strTFAbbrev+"TestFile";
			FileInfo fil2;
			StreamWriter sw2;
			Stream fs2 = null;
			String str2;
			if(File.Exists(fileName))
				File.Delete(fileName);
			strLoc = "Loc_0001";
			fil2 = new FileInfo(fileName);
			switch(fm) {
			case FileMode.CreateNew:
			case FileMode.Create:
			case FileMode.OpenOrCreate:
				iCountTestcases++;
				try{
					fs2 = File.Open( null , fm);
					if(!File.Exists(fileName)) {
						iCountErrors++;
						printerr( "Error_0002! File not created, FileMode=="+ fm.ToString());
					}
				} catch(ArgumentNullException ex ){
					printinfo("Expected exception thrown :: " + ex.Message );
				} catch ( Exception ex ){
					iCountErrors++ ;
					printerr("Error_0003! Unexpected exception thrown :: " + ex.ToString() );
				}
				iCountTestcases++;
				try{
					fs2 = File.Open( "", fm);
					if(!File.Exists(fileName)) {
						iCountErrors++;
						printerr( "Error_0004! File not created, FileMode=="+ fm.ToString());
					}
				} catch(ArgumentException ex ){
					printinfo("Expected exception thrown :: " + ex.Message );
				} catch ( Exception ex ){
					iCountErrors++ ;
					printerr("Error_0005! Unexpected exception thrown :: " + ex.ToString() );
				}
				fs2 = File.Open(fileName, fm);
				iCountTestcases++;
				if(!File.Exists(fileName)) {
					iCountErrors++;
					printerr( "Error_0006! File not created, FileMode=="+ fm.ToString());
				}
				fs2.Close();
				break;
			case FileMode.Open:
			case FileMode.Truncate:
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName, fm);
					iCountErrors++;
					printerr( "Error_0007! Expected exception not thrown");
					fs2.Close();
				} catch (FileNotFoundException fexc) {
					printinfo( "Info_0008! Caught expected exception, fexc=="+fexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_0009! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			case FileMode.Append:
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName , fm);
                    fs2.Write(new Byte[]{54,65,54,90}, 0 , 4 );
                    if ( fs2.Length != 4) {
                        iCountErrors++ ;
                        Console.WriteLine( "Unexpected file length .... " + fs2.Length );
                    }
					fs2.Close();
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_0012! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			default:
				iCountErrors++;
				printerr( "Error_27tbv! This should not be....");
				break;
			}
			if(File.Exists(fileName))
				File.Delete(fileName);
			strLoc = "Loc_4yg7b";
			sw2 = new StreamWriter(fileName);
			str2 = "Du er en ape";
			sw2.Write(str2);
			sw2.Close();
			switch(fm) {
			case FileMode.CreateNew:
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName , fm);
					iCountErrors++;
					printerr( "Error_27b98! Expected exception not thrown");
					fs2.Close();
				} catch (IOException aexc) {
					printinfo( "Info_2399c! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_g8782! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			case FileMode.Create:
				fs2 = File.Open(fileName , fm);
				if(fs2.Length != 0) {
					iCountErrors++;
					printerr( "Error_287vb! Incorrect length of file=="+fil2.Length);
				}
				fs2.Close();
				break;
			case FileMode.OpenOrCreate:
			case FileMode.Open:
				fs2 = File.Open(fileName , fm);
				if(fs2.Length != str2.Length ) {
					iCountErrors++;
					printerr( "Error_2gy78! Incorrect length on file=="+fil2.Length);
				}
				fs2.Close();
				break;
			case FileMode.Truncate:
				fs2 = File.Open(fileName , fm);
				if(fs2.Length != 0) {
					iCountErrors++;
					printerr( "Error_29gv9! Incorrect length on file=="+fil2.Length);
				}
				fs2.Close();
				break;
			case FileMode.Append:
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName , fm);
                    fs2.Write(new Byte[]{54,65,54,90}, 0 , 4 );
                    if ( fs2.Length != 16) {  
                        iCountErrors++ ;
                        Console.WriteLine( "Unexpected file length .... " + fs2.Length );
                    }
					fs2.Close();
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_27878! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			default:
				iCountErrors++;
				printerr( "Error_587yb! This should not be...");
				break;
			}
			if(File.Exists(fileName))
				File.Delete(fileName);
	}
Ejemplo n.º 8
0
        internal SftpFileStream(ISftpSession session, string path, FileMode mode, FileAccess access, int bufferSize)
        {
            if (session == null)
            {
                throw new SshConnectionException("Client not connected.");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize");
            }

            Timeout = TimeSpan.FromSeconds(30);
            Name    = path;

            // Initialize the object state.
            _session  = session;
            _canRead  = (access & FileAccess.Read) != 0;
            _canSeek  = true;
            _canWrite = (access & FileAccess.Write) != 0;

            var flags = Flags.None;

            switch (access)
            {
            case FileAccess.Read:
                flags |= Flags.Read;
                break;

            case FileAccess.Write:
                flags |= Flags.Write;
                break;

            case FileAccess.ReadWrite:
                flags |= Flags.Read;
                flags |= Flags.Write;
                break;

            default:
                throw new ArgumentOutOfRangeException("access");
            }

            if ((access & FileAccess.Read) != 0 && mode == FileMode.Append)
            {
                throw new ArgumentException(string.Format("{0} mode can be requested only when combined with write-only access.", mode.ToString("G")));
            }

            if ((access & FileAccess.Write) == 0)
            {
                if (mode == FileMode.Create || mode == FileMode.CreateNew || mode == FileMode.Truncate || mode == FileMode.Append)
                {
                    throw new ArgumentException(string.Format("Combining {0}: {1} with {2}: {3} is invalid.",
                                                              typeof(FileMode).Name,
                                                              mode,
                                                              typeof(FileAccess).Name,
                                                              access));
                }
            }

            switch (mode)
            {
            case FileMode.Append:
                flags |= Flags.Append | Flags.CreateNewOrOpen;
                break;

            case FileMode.Create:
                _handle = _session.RequestOpen(path, flags | Flags.Truncate, true);
                if (_handle == null)
                {
                    flags |= Flags.CreateNew;
                }
                else
                {
                    flags |= Flags.Truncate;
                }
                break;

            case FileMode.CreateNew:
                flags |= Flags.CreateNew;
                break;

            case FileMode.Open:
                break;

            case FileMode.OpenOrCreate:
                flags |= Flags.CreateNewOrOpen;
                break;

            case FileMode.Truncate:
                flags |= Flags.Truncate;
                break;

            default:
                throw new ArgumentOutOfRangeException("mode");
            }

            if (_handle == null)
            {
                _handle = _session.RequestOpen(path, flags);
            }

            // instead of using the specified buffer size as is, we use it to calculate a buffer size
            // that ensures we always receive or send the max. number of bytes in a single SSH_FXP_READ
            // or SSH_FXP_WRITE message

            _readBufferSize  = (int)session.CalculateOptimalReadLength((uint)bufferSize);
            _writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle);

            if (mode == FileMode.Append)
            {
                var attributes = _session.RequestFStat(_handle, false);
                _position = attributes.Size;
            }
        }
Ejemplo n.º 9
0
        private MFTestResults TestMethod(FileMode fm, FileAccess fa)
        {
            Log.Comment("Starting tests in FileMode: " + fm.ToString() + " with FileAccess: " + fa.ToString());
            int iCountErrors = 0;

            String       fileName = "TestFile";
            StreamWriter sw2;
            FileStream   fs2;
            String       str2;

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            Log.Comment("File does not exist");
            //------------------------------------------------------------------

            switch (fm)
            {
            case FileMode.CreateNew:
            case FileMode.Create:
            case FileMode.OpenOrCreate:
                try
                {
                    Log.Comment("null path");
                    fs2 = File.Open(null, fm, fa);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc);
                }
                try
                {
                    Log.Comment("string empty path");
                    fs2 = File.Open("", fm, fa);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc);
                }
                try
                {
                    Log.Comment("string:" + fileName);
                    fs2 = File.Open(fileName, fm, fa);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created, FileMode==" + fm.ToString());
                    }
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    if ((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read))
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    else
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc);
                }
                break;

            case FileMode.Open:
            case FileMode.Truncate:
                try
                {
                    Log.Comment("null path");
                    fs2 = File.Open(null, fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (IOException fexc)
                {
                    Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }

                try
                {
                    Log.Comment("string empty path");
                    fs2 = File.Open("", fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (IOException fexc)
                {
                    Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }

                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (IOException fexc)
                {
                    Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                }
                catch (ArgumentException aexc)
                {
                    if (fa == FileAccess.Read)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    else
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception thrown, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            case FileMode.Append:
                if (fa == FileAccess.Write)
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created");
                    }
                    fs2.Close();
                }
                else
                {
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                break;

            default:
                iCountErrors++;
                Log.Exception("Invalid FileMode.");
                break;
            }
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            //------------------------------------------------------------------


            Log.Comment("File already exists");
            //------------------------------------------------------------------

            sw2  = new StreamWriter(fileName);
            str2 = "Du er en ape";
            sw2.Write(str2);
            sw2.Close();
            switch (fm)
            {
            case FileMode.CreateNew:
                try
                {
                    fs2 = File.Open(null, fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (IOException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }

                try
                {
                    fs2 = File.Open("", fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (IOException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }

                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    iCountErrors++;
                    Log.Exception("Expected exception not thrown");
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    if (fa == FileAccess.Read)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    else
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (IOException aexc)
                {
                    Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            case FileMode.Create:
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (fs2.Length != 0)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length of file==" + fs2.Length);
                    }
                    fs2.Close();
                }
                catch (ArgumentException aexc)
                {
                    if (fa == FileAccess.Read)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    else
                    {
                        iCountErrors++;
                        Log.Exception("Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;

            case FileMode.OpenOrCreate:
            case FileMode.Open:
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != str2.Length)
                {
                    iCountErrors++;
                    Log.Exception("Incorrect length on file==" + fs2.Length);
                }
                fs2.Close();
                break;

            case FileMode.Truncate:
                if (fa == FileAccess.Read)
                {
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                    }
                    catch (ArgumentException iexc)
                    {
                        Log.Comment("Caught expected exception, iexc==" + iexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                else
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (fs2.Length != 0)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length on file==" + fs2.Length);
                    }
                    fs2.Close();
                }
                break;

            case FileMode.Append:
                if (fa == FileAccess.Write)
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (!File.Exists(fileName))
                    {
                        iCountErrors++;
                        Log.Exception("File not created");
                    }
                    fs2.Close();
                }
                else
                {
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                break;

            default:
                iCountErrors++;
                Log.Exception("Invalid file mode");
                break;
            }
            return(iCountErrors == 0 ? MFTestResults.Pass : MFTestResults.Fail);
        }
Ejemplo n.º 10
0
	public void TestMethod(FileMode fm, FileAccess fa)
	{
			String fileName = s_strTFAbbrev+"TestFile";
			StreamWriter sw2;
			FileStream fs2;
			String str2;
			if(File.Exists(fileName))
				File.Delete(fileName);
			strLoc = "Loc_234yg";
			switch(fm) {
			case FileMode.CreateNew:
			case FileMode.Create:
			case FileMode.OpenOrCreate:
			    try {
				fs2 = File.Open( null, fm, fa);
				iCountTestcases++;
				if(!File.Exists(fileName)) {
					iCountErrors++;
					printerr( "Error_0001! File not created, FileMode=="+fm.ToString("G"));
				}
				fs2.Close();
				} catch (ArgumentException aexc) {
					printinfo( "Info_0002! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_0004! Incorrect exception thrown, exc=="+exc);
				}
			    try {
				fs2 = File.Open( "", fm, fa);
				iCountTestcases++;
				if(!File.Exists(fileName)) {
					iCountErrors++;
					printerr( "Error_0005! File not created, FileMode=="+fm.ToString("G"));
				}
				fs2.Close();
				} catch (ArgumentException aexc) {
						printinfo( "Info_0006! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_0008! Incorrect exception thrown, exc=="+exc);
				}
			    try {
				fs2 = File.Open( fileName, fm, fa);
				iCountTestcases++;
				if(!File.Exists(fileName)) {
					iCountErrors++;
					printerr( "Error_48gb7! File not created, FileMode=="+fm.ToString("G"));
				}
				fs2.Close();
				} catch (ArgumentException aexc) {
					if((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read)) {
						printinfo( "Info_4987v! Caught expected exception, aexc=="+aexc.Message);
					}
					else {
						iCountErrors++;
						printerr( "Error_478v8! Unexpected exception, aexc=="+aexc);
					}
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_4879v! Incorrect exception thrown, exc=="+exc);
				}
				break;
			case FileMode.Open:
			case FileMode.Truncate:
				iCountTestcases++;
				try {
					fs2 = File.Open(null, fm, fa);
					iCountErrors++;
					printerr( "Error_1001! Expected exception not thrown");
					fs2.Close();
				} catch (IOException fexc) {
					printinfo( "Info_1002! Caught expected exception, fexc=="+fexc.Message);
				} catch (ArgumentException aexc) {
						printinfo( "Info_1003! Caught expected exception, aexc=="+aexc.Message);					
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_1005! Incorrect exception thrown, exc=="+exc.ToString());
				}
				iCountTestcases++;
				try {
					fs2 = File.Open("", fm, fa);
					iCountErrors++;
					printerr( "Error_1006! Expected exception not thrown");
					fs2.Close();
				} catch (IOException fexc) {
					printinfo( "Info_1007! Caught expected exception, fexc=="+fexc.Message);
				} catch (ArgumentException aexc) { 
						printinfo( "Info_1008! Caught expected exception, aexc=="+aexc.Message);				
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_1010! Incorrect exception thrown, exc=="+exc.ToString());
				}
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName, fm,fa);
					iCountErrors++;
					printerr( "Error_2yg8b! Expected exception not thrown");
					fs2.Close();
				} catch (IOException fexc) {
					printinfo( "Info_49y7b! Caught expected exception, fexc=="+fexc.Message);
				} catch (ArgumentException aexc) {
					if(fa == FileAccess.Read) 
						printinfo( "Info_4398v! Caught expected exception, aexc=="+aexc.Message);
					else {
						iCountErrors++;
						printerr( "Error_v48y8! Unexpected exception thrown, aexc=="+aexc);
					}						
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_2y7gf! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			case FileMode.Append:
				if(fa == FileAccess.Write) {
					fs2 = File.Open(fileName, fm,fa);
					iCountTestcases++;
					if(!File.Exists(fileName)) {
						iCountErrors++;
						printerr( "Error_2498y! File not created");
					}
					fs2.Close();
				}
				else {
					iCountTestcases++;
					try {
						fs2 = File.Open(fileName, fm,fa);
						iCountErrors++;
						printerr( "Error_2g78b! Expected exception not thrown");
						fs2.Close();
					} catch (ArgumentException aexc) {
						printinfo( "Info_2g7y7! Caught expected exception, aexc=="+aexc.Message);
					} catch (Exception exc) {
						iCountErrors++;
						printerr( "Error_g77b7! Incorrect exception thrown, exc=="+exc.ToString());
					}
				}
				break;
			default:
				iCountErrors++;
				printerr( "Error_27tbv! This should not be....");
				break;
			}
			if(File.Exists(fileName))
				File.Delete(fileName);
			strLoc = "Loc_4yg7b";
			sw2 = new StreamWriter(fileName);
			str2 = "Du er en ape";
			sw2.Write(str2);
			sw2.Close();
			switch(fm) {
			case FileMode.CreateNew:
				iCountTestcases++;
				try {
					fs2 = File.Open(null, fm,fa);
					iCountErrors++;
					printerr( "Error_2001! Expected exception not thrown");
					fs2.Close();
				} catch (ArgumentException aexc) {
						printinfo( "Info_2002! Caught expected exception, aexc=="+aexc.Message);
				} catch (IOException aexc) {
					printinfo( "Info_2004! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_2005! Incorrect exception thrown, exc=="+exc.ToString());
				}
				iCountTestcases++;
				try {
					fs2 = File.Open("", fm,fa);
					iCountErrors++;
					printerr( "Error_2006! Expected exception not thrown");
					fs2.Close();
				} catch (ArgumentException aexc) {
						printinfo( "Info_2007! Caught expected exception, aexc=="+aexc.Message);
				} catch (IOException aexc) {
					printinfo( "Info_2009! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_2010! Incorrect exception thrown, exc=="+exc.ToString());
				}
				iCountTestcases++;
				try {
					fs2 = File.Open(fileName, fm, fa);
					iCountErrors++;
					printerr( "Error_27b98! Expected exception not thrown");
					fs2.Close();
				} catch (ArgumentException aexc) {
					if(fa == FileAccess.Read) {
						printinfo( "Info_348vy! Caught expected exception, aexc=="+aexc.Message);
					}
					else {
						iCountErrors++;
						printerr( "Error_4387v! Unexpected exception, aexc=="+aexc);
					}
				} catch (IOException aexc) {
					printinfo( "Info_2399c! Caught expected exception, aexc=="+aexc.Message);
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_g8782! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			case FileMode.Create:
				try {
				fs2 = File.Open(fileName, fm,fa);
				if(fs2.Length != 0) {
					iCountErrors++;
					printerr( "Error_287vb! Incorrect length of file=="+fs2.Length);
				}
				fs2.Close();
				} catch (ArgumentException aexc) {
					if(fa == FileAccess.Read) {
						printinfo( "Info_4398v! Caught expected exception, aexc=="+aexc.Message);
					}
					else {
						iCountErrors++;
						printerr( "Error_48vy7! Unexpected exception, aexc=="+aexc);
					}
				} catch (Exception exc) {
					iCountErrors++;
					printerr( "Error_47yv3! Incorrect exception thrown, exc=="+exc.ToString());
				}
				break;
			case FileMode.OpenOrCreate:
			case FileMode.Open:
				fs2 = File.Open(fileName, fm,fa);
				if(fs2.Length != str2.Length ) {
					iCountErrors++;
					printerr( "Error_2gy78! Incorrect length on file=="+fs2.Length);
				}
				fs2.Close();
				break;
			case FileMode.Truncate:
				if(fa == FileAccess.Read) {
					iCountTestcases++;
					try {
						fs2 = File.Open(fileName,fm,fa);
						iCountErrors++;
						printerr( "Error_g95y8! Expected exception not thrown");
					} catch (ArgumentException iexc) {
						printinfo( "Info_2988f! Caught expected exception, iexc=="+iexc.Message);
					} catch (Exception exc) {
						iCountErrors++;
						printerr( "Error_98y4v! Incorrect exception thrown, exc=="+exc.ToString());
					}
				}
				else {
					fs2 = File.Open(fileName, fm,fa);
					if(fs2.Length != 0) {
						iCountErrors++;
						printerr( "Error_29gv9! Incorrect length on file=="+fs2.Length);
					}
					fs2.Close();
				}
				break;
			case FileMode.Append:
				if(fa == FileAccess.Write) {
					fs2 = File.Open(fileName, fm,fa);
					iCountTestcases++;
					if(!File.Exists(fileName)) {
						iCountErrors++;
						printerr( "Error_4089v! File not created");
					}
					fs2.Close();
				}
				else {
					iCountTestcases++;
					try {
						fs2 = File.Open(fileName,fm,fa);
						iCountErrors++;
					printerr( "Error_287yb! Expected exception not thrown");
					fs2.Close();
					} catch (ArgumentException aexc) {
						printinfo( "Info_93282! Caught expected exception, aexc=="+aexc.Message);
					} catch (Exception exc) {
						iCountErrors++;
						printerr( "Error_27878! Incorrect exception thrown, exc=="+exc.ToString());
					}
				}
				break;
			default:
				iCountErrors++;
				printerr( "Error_587yb! This should not be...");
				break;
			}
			if(File.Exists(fileName))
				File.Delete(fileName);
	}
Ejemplo n.º 11
0
 public int CreateFile(String filename, FileAccess access, FileShare share, FileMode mode, FileOptions options, DokanFileInfo info)
 {
     String userName = GetProcessOwner((int)info.ProcessId);
     string path = GetPath(filename);
     info.Context = count_++;
     logger.WriteLine("CreateFile: " + path + " //// " + mode.ToString(), Logger.MessagePriority.Debug);
     if (!(Directory.Exists(path)) && (mode == FileMode.Create || mode == FileMode.CreateNew || mode == FileMode.OpenOrCreate))
     {
         if (File.Exists(path))
         {
             return 0;
         }
         RamFile fs = null;
         logger.WriteLine("Creating " + path, Logger.MessagePriority.Debug);
         if (!userFSList.ContainsKey(userName))
         {
             //fs = new OpenedRamFile(root_ + filename);
             UserFS ufs = new UserFS();
             ufs.userName = userName;
             ufs.files = new System.Collections.Generic.LinkedList<RamFile>();
             //ufs.files.AddLast(fs);
             lock (userFSLock)
             {
                 userFSList.Add(userName, ufs);
             }
             AddUserFSRamFile(userName, filename);
         }
         else
         {
             fs = GetUserFSFile(userName, filename);
             if (fs == null)
             {
                 lock (userFSLock)
                 {
                     fs = GetUserFSFile(userName, filename);
                     if (fs == null)
                     {
                         fs = AddUserFSRamFile(userName, filename);
                     }
                 }
             }
         }
         return 0;
     }
     else if (File.Exists(path))
     {
         return 0;
     }
     else if (Directory.Exists(path))
     {
         info.IsDirectory = true;
         return 0;
     }
     else
     {
         UserFS ufs;
         if (userFSList.TryGetValue(userName, out ufs))
         {
             foreach (RamFile f in ufs.files)
             {
                 if (f.Name.ToLower().Equals(path.ToLower()))
                 {
                     return 0;
                 }
             }
         }
         return -DokanNet.ERROR_FILE_NOT_FOUND;
     }
 }
Ejemplo n.º 12
0
    public static void TestMethod(FileMode fm, FileAccess fa)
    {
        String       fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
        StreamWriter sw2;
        FileStream   fs2;
        String       str2;

        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        // File does not exist
        //------------------------------------------------------------------
        s_strLoc = "Loc_234yg";

        switch (fm)
        {
        case FileMode.CreateNew:
        case FileMode.Create:
        case FileMode.OpenOrCreate:
            try
            {
                fs2 = File.Open(null, fm, fa);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_0001! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Dispose();
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_0004! Incorrect exception thrown, exc==" + exc);
            }
            try
            {
                fs2 = File.Open("", fm, fa);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_0005! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Dispose();
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_0008! Incorrect exception thrown, exc==" + exc);
            }
            try
            {
                fs2 = File.Open(fileName, fm, fa);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_48gb7! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Dispose();
            }
            catch (ArgumentException aexc)
            {
                if (!((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read)))
                {
                    s_iCountErrors++;
                    printerr("Error_478v8! Unexpected exception, aexc==" + aexc);
                }
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_4879v! Incorrect exception thrown, exc==" + exc);
            }
            break;

        case FileMode.Open:
        case FileMode.Truncate:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(null, fm, fa);
                s_iCountErrors++;
                printerr("Error_1001! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (IOException)
            {
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_1005! Incorrect exception thrown, exc==" + exc.ToString());
            }

            s_iCountTestcases++;
            try
            {
                fs2 = File.Open("", fm, fa);
                s_iCountErrors++;
                printerr("Error_1006! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (IOException)
            {
            }
            catch (ArgumentException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_1010! Incorrect exception thrown, exc==" + exc.ToString());
            }

            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm, fa);
                s_iCountErrors++;
                printerr("Error_2yg8b! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (IOException)
            {
            }
            catch (ArgumentException aexc)
            {
                if (fa != FileAccess.Read)
                {
                    s_iCountErrors++;
                    printerr("Error_v48y8! Unexpected exception thrown, aexc==" + aexc);
                }
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_2y7gf! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Append:
            if (fa == FileAccess.Write)
            {
                fs2 = File.Open(fileName, fm, fa);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_2498y! File not created");
                }
                fs2.Dispose();
            }
            else
            {
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_2g78b! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_g77b7! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            break;

        default:
            s_iCountErrors++;
            printerr("Error_27tbv! This should not be....");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        //------------------------------------------------------------------


        //  File already exists
        //------------------------------------------------------------------
        s_strLoc = "Loc_4yg7b";

        FileStream stream = new FileStream(fileName, FileMode.Create);

        sw2  = new StreamWriter(stream);
        str2 = "Du er en ape";
        sw2.Write(str2);
        sw2.Dispose();
        stream.Dispose();
        switch (fm)
        {
        case FileMode.CreateNew:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(null, fm, fa);
                s_iCountErrors++;
                printerr("Error_2001! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (ArgumentException)
            {
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_2005! Incorrect exception thrown, exc==" + exc.ToString());
            }

            s_iCountTestcases++;
            try
            {
                fs2 = File.Open("", fm, fa);
                s_iCountErrors++;
                printerr("Error_2006! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (ArgumentException)
            {
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_2010! Incorrect exception thrown, exc==" + exc.ToString());
            }

            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm, fa);
                s_iCountErrors++;
                printerr("Error_27b98! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (ArgumentException aexc)
            {
                if (fa != FileAccess.Read)
                {
                    s_iCountErrors++;
                    printerr("Error_4387v! Unexpected exception, aexc==" + aexc);
                }
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Create:
            try
            {
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != 0)
                {
                    s_iCountErrors++;
                    printerr("Error_287vb! Incorrect length of file==" + fs2.Length);
                }
                fs2.Dispose();
            }
            catch (ArgumentException aexc)
            {
                if (fa != FileAccess.Read)
                {
                    s_iCountErrors++;
                    printerr("Error_48vy7! Unexpected exception, aexc==" + aexc);
                }
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_47yv3! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.OpenOrCreate:
        case FileMode.Open:
            fs2 = File.Open(fileName, fm, fa);
            if (fs2.Length != str2.Length)
            {
                s_iCountErrors++;
                printerr("Error_2gy78! Incorrect length on file==" + fs2.Length);
            }
            fs2.Dispose();
            break;

        case FileMode.Truncate:
            if (fa == FileAccess.Read)
            {
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_g95y8! Expected exception not thrown");
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_98y4v! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            else
            {
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != 0)
                {
                    s_iCountErrors++;
                    printerr("Error_29gv9! Incorrect length on file==" + fs2.Length);
                }
                fs2.Dispose();
            }
            break;

        case FileMode.Append:
            if (fa == FileAccess.Write)
            {
                fs2 = File.Open(fileName, fm, fa);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_4089v! File not created");
                }
                fs2.Dispose();
            }
            else
            {
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_287yb! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            break;

        default:
            s_iCountErrors++;
            printerr("Error_587yb! This should not be...");
            break;
        }


        //------------------------------------------------------------------



        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
    }
Ejemplo n.º 13
0
    public static void TestMethod(FileMode fm)
    {
        String       fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
        FileInfo     fil2;
        StreamWriter sw2;
        Stream       fs2 = null;
        String       str2;


        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        // [] File does not exist
        //------------------------------------------------------------------
        s_strLoc = "Loc_0001";

        fil2 = new FileInfo(fileName);
        switch (fm)
        {
        case FileMode.CreateNew:
        case FileMode.Create:
        case FileMode.OpenOrCreate:

            //With a null string
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(null, fm);
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_0002! File not created, FileMode==" + fm.ToString());
                }
            }
            catch (ArgumentNullException)
            {
            }
            catch (Exception ex)
            {
                s_iCountErrors++;
                printerr("Error_0003! Unexpected exception thrown :: " + ex.ToString());
            }

            //with anempty string
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open("", fm);
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_0004! File not created, FileMode==" + fm.ToString());
                }
            }
            catch (ArgumentException)
            {
            }
            catch (Exception ex)
            {
                s_iCountErrors++;
                printerr("Error_0005! Unexpected exception thrown :: " + ex.ToString());
            }

            fs2 = File.Open(fileName, fm);
            s_iCountTestcases++;
            if (!File.Exists(fileName))
            {
                s_iCountErrors++;
                printerr("Error_0006! File not created, FileMode==" + fm.ToString());
            }
            fs2.Dispose();
            break;

        case FileMode.Open:
        case FileMode.Truncate:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm);
                s_iCountErrors++;
                printerr("Error_0007! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (FileNotFoundException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_0009! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Append:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm);
                fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                if (fs2.Length != 4)
                {
                    s_iCountErrors++;
                    Console.WriteLine("Unexpected file length .... " + fs2.Length);
                }
                fs2.Dispose();
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_0012! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        default:
            s_iCountErrors++;
            printerr("Error_27tbv! This should not be....");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        //------------------------------------------------------------------


        // [] File already exists
        //------------------------------------------------------------------
        s_strLoc = "Loc_4yg7b";

        FileStream stream = new FileStream(fileName, FileMode.Create);

        sw2  = new StreamWriter(stream);
        str2 = "Du er en ape";
        sw2.Write(str2);
        sw2.Dispose();
        stream.Dispose();
        switch (fm)
        {
        case FileMode.CreateNew:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm);
                s_iCountErrors++;
                printerr("Error_27b98! Expected exception not thrown");
                fs2.Dispose();
            }
            catch (IOException)
            {
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Create:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != 0)
            {
                s_iCountErrors++;
                printerr("Error_287vb! Incorrect length of file==" + fil2.Length);
            }
            fs2.Dispose();
            break;

        case FileMode.OpenOrCreate:
        case FileMode.Open:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != str2.Length)
            {
                s_iCountErrors++;
                printerr("Error_2gy78! Incorrect length on file==" + fil2.Length);
            }
            fs2.Dispose();
            break;

        case FileMode.Truncate:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != 0)
            {
                s_iCountErrors++;
                printerr("Error_29gv9! Incorrect length on file==" + fil2.Length);
            }
            fs2.Dispose();
            break;

        case FileMode.Append:
            s_iCountTestcases++;
            try
            {
                fs2 = File.Open(fileName, fm);
                fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                if (fs2.Length != 16)
                {      // already 12 characters are written to the file.
                    s_iCountErrors++;
                    Console.WriteLine("Unexpected file length .... " + fs2.Length);
                }
                fs2.Dispose();
            }
            catch (Exception exc)
            {
                s_iCountErrors++;
                printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        default:
            s_iCountErrors++;
            printerr("Error_587yb! This should not be...");
            break;
        }

        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
    }
Ejemplo n.º 14
0
        private MFTestResults TestMethod(FileMode fm, FileAccess fa)
        {
            Log.Comment("Starting tests in FileMode: " + fm.ToString() + " with FileAccess: " + fa.ToString());
            int iCountErrors = 0;

            String fileName = "TestFile";
            StreamWriter sw2;
            FileStream fs2;
            String str2;

            if (File.Exists(fileName))
                File.Delete(fileName);

            Log.Comment("File does not exist");
            //------------------------------------------------------------------

            switch (fm)
            {
                case FileMode.CreateNew:
                case FileMode.Create:
                case FileMode.OpenOrCreate:
                    try
                    {
                        Log.Comment("null path");
                        fs2 = File.Open(null, fm, fa);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created, FileMode==" + fm.ToString());
                        }
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc);
                    }
                    try
                    {
                        Log.Comment("string empty path");
                        fs2 = File.Open("", fm, fa);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created, FileMode==" + fm.ToString());
                        }
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc);
                    }
                    try
                    {
                        Log.Comment("string:" + fileName);
                        fs2 = File.Open(fileName, fm, fa);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created, FileMode==" + fm.ToString());
                        }
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        if ((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read))
                        {
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        }
                        else
                        {
                            iCountErrors++;
                            Log.Exception("Unexpected exception, aexc==" + aexc);
                        }
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc);
                    }
                    break;
                case FileMode.Open:
                case FileMode.Truncate:
                    try
                    {
                        Log.Comment("null path");
                        fs2 = File.Open(null, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (IOException fexc)
                    {
                        Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }

                    try
                    {
                        Log.Comment("string empty path");
                        fs2 = File.Open("", fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (IOException fexc)
                    {
                        Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }

                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (IOException fexc)
                    {
                        Log.Comment("Caught expected exception, fexc==" + fexc.Message);
                    }
                    catch (ArgumentException aexc)
                    {
                        if (fa == FileAccess.Read)
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        else
                        {
                            iCountErrors++;
                            Log.Exception("Unexpected exception thrown, aexc==" + aexc);
                        }
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                case FileMode.Append:
                    if (fa == FileAccess.Write)
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created");
                        }
                        fs2.Close();
                    }
                    else
                    {
                        try
                        {
                            fs2 = File.Open(fileName, fm, fa);
                            iCountErrors++;
                            Log.Exception("Expected exception not thrown");
                            fs2.Close();
                        }
                        catch (ArgumentException aexc)
                        {
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        }
                        catch (Exception exc)
                        {
                            iCountErrors++;
                            Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                        }
                    }
                    break;
                default:
                    iCountErrors++;
                    Log.Exception("Invalid FileMode.");
                    break;
            }
            if (File.Exists(fileName))
                File.Delete(fileName);

            //------------------------------------------------------------------


            Log.Comment("File already exists");
            //------------------------------------------------------------------

            sw2 = new StreamWriter(fileName);
            str2 = "Du er en ape";
            sw2.Write(str2);
            sw2.Close();
            switch (fm)
            {
                case FileMode.CreateNew:
                    try
                    {
                        fs2 = File.Open(null, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (IOException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }

                    try
                    {
                        fs2 = File.Open("", fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (IOException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }

                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        iCountErrors++;
                        Log.Exception("Expected exception not thrown");
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        if (fa == FileAccess.Read)
                        {
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        }
                        else
                        {
                            iCountErrors++;
                            Log.Exception("Unexpected exception, aexc==" + aexc);
                        }

                    }
                    catch (IOException aexc)
                    {
                        Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                case FileMode.Create:
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        if (fs2.Length != 0)
                        {
                            iCountErrors++;
                            Log.Exception("Incorrect length of file==" + fs2.Length);
                        }
                        fs2.Close();
                    }
                    catch (ArgumentException aexc)
                    {
                        if (fa == FileAccess.Read)
                        {
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        }
                        else
                        {
                            iCountErrors++;
                            Log.Exception("Unexpected exception, aexc==" + aexc);
                        }
                    }
                    catch (Exception exc)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                    }
                    break;
                case FileMode.OpenOrCreate:
                case FileMode.Open:
                    fs2 = File.Open(fileName, fm, fa);
                    if (fs2.Length != str2.Length)
                    {
                        iCountErrors++;
                        Log.Exception("Incorrect length on file==" + fs2.Length);
                    }
                    fs2.Close();
                    break;
                case FileMode.Truncate:
                    if (fa == FileAccess.Read)
                    {
                        try
                        {
                            fs2 = File.Open(fileName, fm, fa);
                            iCountErrors++;
                            Log.Exception("Expected exception not thrown");
                        }
                        catch (ArgumentException iexc)
                        {
                            Log.Comment("Caught expected exception, iexc==" + iexc.Message);
                        }
                        catch (Exception exc)
                        {
                            iCountErrors++;
                            Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                        }
                    }
                    else
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        if (fs2.Length != 0)
                        {
                            iCountErrors++;
                            Log.Exception("Incorrect length on file==" + fs2.Length);
                        }
                        fs2.Close();
                    }
                    break;
                case FileMode.Append:
                    if (fa == FileAccess.Write)
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        if (!File.Exists(fileName))
                        {
                            iCountErrors++;
                            Log.Exception("File not created");
                        }
                        fs2.Close();
                    }
                    else
                    {
                        try
                        {
                            fs2 = File.Open(fileName, fm, fa);
                            iCountErrors++;
                            Log.Exception("Expected exception not thrown");
                            fs2.Close();
                        }
                        catch (ArgumentException aexc)
                        {
                            Log.Comment("Caught expected exception, aexc==" + aexc.Message);
                        }
                        catch (Exception exc)
                        {
                            iCountErrors++;
                            Log.Exception("Incorrect exception thrown, exc==" + exc.ToString());
                        }
                    }
                    break;
                default:
                    iCountErrors++;
                    Log.Exception("Invalid file mode");
                    break;
            }
            return iCountErrors == 0 ? MFTestResults.Pass : MFTestResults.Fail;
        }
Ejemplo n.º 15
0
    public static void TestMethod(FileMode fm, FileAccess fa)
    {
        String fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
        StreamWriter sw2;
        FileStream fs2;
        String str2;

        if (File.Exists(fileName))
            File.Delete(fileName);

        // File does not exist
        //------------------------------------------------------------------
        s_strLoc = "Loc_234yg";

        switch (fm)
        {
            case FileMode.CreateNew:
            case FileMode.Create:
            case FileMode.OpenOrCreate:
                try
                {
                    fs2 = File.Open(null, fm, fa);
                    s_iCountTestcases++;
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_0001! File not created, FileMode==" + fm.ToString("G"));
                    }
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_0004! Incorrect exception thrown, exc==" + exc);
                }
                try
                {
                    fs2 = File.Open("", fm, fa);
                    s_iCountTestcases++;
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_0005! File not created, FileMode==" + fm.ToString("G"));
                    }
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_0008! Incorrect exception thrown, exc==" + exc);
                }
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountTestcases++;
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_48gb7! File not created, FileMode==" + fm.ToString("G"));
                    }
                    fs2.Dispose();
                }
                catch (ArgumentException aexc)
                {
                    if (!((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read)))
                    {
                        s_iCountErrors++;
                        printerr("Error_478v8! Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_4879v! Incorrect exception thrown, exc==" + exc);
                }
                break;
            case FileMode.Open:
            case FileMode.Truncate:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(null, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_1001! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (IOException)
                {
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_1005! Incorrect exception thrown, exc==" + exc.ToString());
                }

                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open("", fm, fa);
                    s_iCountErrors++;
                    printerr("Error_1006! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (IOException)
                {
                }
                catch (ArgumentException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_1010! Incorrect exception thrown, exc==" + exc.ToString());
                }

                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_2yg8b! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (IOException)
                {
                }
                catch (ArgumentException aexc)
                {
                    if (fa != FileAccess.Read)
                    {
                        s_iCountErrors++;
                        printerr("Error_v48y8! Unexpected exception thrown, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_2y7gf! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            case FileMode.Append:
                if (fa == FileAccess.Write)
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountTestcases++;
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_2498y! File not created");
                    }
                    fs2.Dispose();
                }
                else
                {
                    s_iCountTestcases++;
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        s_iCountErrors++;
                        printerr("Error_2g78b! Expected exception not thrown");
                        fs2.Dispose();
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (Exception exc)
                    {
                        s_iCountErrors++;
                        printerr("Error_g77b7! Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                break;
            default:
                s_iCountErrors++;
                printerr("Error_27tbv! This should not be....");
                break;
        }
        if (File.Exists(fileName))
            File.Delete(fileName);

        //------------------------------------------------------------------


        //  File already exists
        //------------------------------------------------------------------
        s_strLoc = "Loc_4yg7b";

        FileStream stream = new FileStream(fileName, FileMode.Create);
        sw2 = new StreamWriter(stream);
        str2 = "Du er en ape";
        sw2.Write(str2);
        sw2.Dispose();
        stream.Dispose();
        switch (fm)
        {
            case FileMode.CreateNew:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(null, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_2001! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (IOException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_2005! Incorrect exception thrown, exc==" + exc.ToString());
                }

                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open("", fm, fa);
                    s_iCountErrors++;
                    printerr("Error_2006! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (ArgumentException)
                {
                }
                catch (IOException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_2010! Incorrect exception thrown, exc==" + exc.ToString());
                }

                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountErrors++;
                    printerr("Error_27b98! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (ArgumentException aexc)
                {
                    if (fa != FileAccess.Read)
                    {
                        s_iCountErrors++;
                        printerr("Error_4387v! Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (IOException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            case FileMode.Create:
                try
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (fs2.Length != 0)
                    {
                        s_iCountErrors++;
                        printerr("Error_287vb! Incorrect length of file==" + fs2.Length);
                    }
                    fs2.Dispose();
                }
                catch (ArgumentException aexc)
                {
                    if (fa != FileAccess.Read)
                    {
                        s_iCountErrors++;
                        printerr("Error_48vy7! Unexpected exception, aexc==" + aexc);
                    }
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_47yv3! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            case FileMode.OpenOrCreate:
            case FileMode.Open:
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != str2.Length)
                {
                    s_iCountErrors++;
                    printerr("Error_2gy78! Incorrect length on file==" + fs2.Length);
                }
                fs2.Dispose();
                break;
            case FileMode.Truncate:
                if (fa == FileAccess.Read)
                {
                    s_iCountTestcases++;
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        s_iCountErrors++;
                        printerr("Error_g95y8! Expected exception not thrown");
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (Exception exc)
                    {
                        s_iCountErrors++;
                        printerr("Error_98y4v! Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                else
                {
                    fs2 = File.Open(fileName, fm, fa);
                    if (fs2.Length != 0)
                    {
                        s_iCountErrors++;
                        printerr("Error_29gv9! Incorrect length on file==" + fs2.Length);
                    }
                    fs2.Dispose();
                }
                break;
            case FileMode.Append:
                if (fa == FileAccess.Write)
                {
                    fs2 = File.Open(fileName, fm, fa);
                    s_iCountTestcases++;
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_4089v! File not created");
                    }
                    fs2.Dispose();
                }
                else
                {
                    s_iCountTestcases++;
                    try
                    {
                        fs2 = File.Open(fileName, fm, fa);
                        s_iCountErrors++;
                        printerr("Error_287yb! Expected exception not thrown");
                        fs2.Dispose();
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (Exception exc)
                    {
                        s_iCountErrors++;
                        printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
                    }
                }
                break;
            default:
                s_iCountErrors++;
                printerr("Error_587yb! This should not be...");
                break;
        }


        //------------------------------------------------------------------



        if (File.Exists(fileName))
            File.Delete(fileName);
    }
    public void TestMethod(FileMode fm, FileAccess fa)
    {
        String       fileName = s_strTFAbbrev + "TestFile";
        StreamWriter sw2;
        FileStream   fs2;
        String       str2;

        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        strLoc = "Loc_234yg";
        switch (fm)
        {
        case FileMode.CreateNew:
        case FileMode.Create:
        case FileMode.OpenOrCreate:
            try {
                fs2 = File.Open(null, fm, fa);
                iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_0001! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Close();
            } catch (ArgumentException aexc) {
                printinfo("Info_0002! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_0004! Incorrect exception thrown, exc==" + exc);
            }
            try {
                fs2 = File.Open("", fm, fa);
                iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_0005! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Close();
            } catch (ArgumentException aexc) {
                printinfo("Info_0006! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_0008! Incorrect exception thrown, exc==" + exc);
            }
            try {
                fs2 = File.Open(fileName, fm, fa);
                iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_48gb7! File not created, FileMode==" + fm.ToString("G"));
                }
                fs2.Close();
            } catch (ArgumentException aexc) {
                if ((fm == FileMode.Create && fa == FileAccess.Read) || (fm == FileMode.CreateNew && fa == FileAccess.Read))
                {
                    printinfo("Info_4987v! Caught expected exception, aexc==" + aexc.Message);
                }
                else
                {
                    iCountErrors++;
                    printerr("Error_478v8! Unexpected exception, aexc==" + aexc);
                }
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_4879v! Incorrect exception thrown, exc==" + exc);
            }
            break;

        case FileMode.Open:
        case FileMode.Truncate:
            iCountTestcases++;
            try {
                fs2 = File.Open(null, fm, fa);
                iCountErrors++;
                printerr("Error_1001! Expected exception not thrown");
                fs2.Close();
            } catch (IOException fexc) {
                printinfo("Info_1002! Caught expected exception, fexc==" + fexc.Message);
            } catch (ArgumentException aexc) {
                printinfo("Info_1003! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_1005! Incorrect exception thrown, exc==" + exc.ToString());
            }
            iCountTestcases++;
            try {
                fs2 = File.Open("", fm, fa);
                iCountErrors++;
                printerr("Error_1006! Expected exception not thrown");
                fs2.Close();
            } catch (IOException fexc) {
                printinfo("Info_1007! Caught expected exception, fexc==" + fexc.Message);
            } catch (ArgumentException aexc) {
                printinfo("Info_1008! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_1010! Incorrect exception thrown, exc==" + exc.ToString());
            }
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm, fa);
                iCountErrors++;
                printerr("Error_2yg8b! Expected exception not thrown");
                fs2.Close();
            } catch (IOException fexc) {
                printinfo("Info_49y7b! Caught expected exception, fexc==" + fexc.Message);
            } catch (ArgumentException aexc) {
                if (fa == FileAccess.Read)
                {
                    printinfo("Info_4398v! Caught expected exception, aexc==" + aexc.Message);
                }
                else
                {
                    iCountErrors++;
                    printerr("Error_v48y8! Unexpected exception thrown, aexc==" + aexc);
                }
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_2y7gf! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Append:
            if (fa == FileAccess.Write)
            {
                fs2 = File.Open(fileName, fm, fa);
                iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_2498y! File not created");
                }
                fs2.Close();
            }
            else
            {
                iCountTestcases++;
                try {
                    fs2 = File.Open(fileName, fm, fa);
                    iCountErrors++;
                    printerr("Error_2g78b! Expected exception not thrown");
                    fs2.Close();
                } catch (ArgumentException aexc) {
                    printinfo("Info_2g7y7! Caught expected exception, aexc==" + aexc.Message);
                } catch (Exception exc) {
                    iCountErrors++;
                    printerr("Error_g77b7! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            break;

        default:
            iCountErrors++;
            printerr("Error_27tbv! This should not be....");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        strLoc = "Loc_4yg7b";
        sw2    = new StreamWriter(fileName);
        str2   = "Du er en ape";
        sw2.Write(str2);
        sw2.Close();
        switch (fm)
        {
        case FileMode.CreateNew:
            iCountTestcases++;
            try {
                fs2 = File.Open(null, fm, fa);
                iCountErrors++;
                printerr("Error_2001! Expected exception not thrown");
                fs2.Close();
            } catch (ArgumentException aexc) {
                printinfo("Info_2002! Caught expected exception, aexc==" + aexc.Message);
            } catch (IOException aexc) {
                printinfo("Info_2004! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_2005! Incorrect exception thrown, exc==" + exc.ToString());
            }
            iCountTestcases++;
            try {
                fs2 = File.Open("", fm, fa);
                iCountErrors++;
                printerr("Error_2006! Expected exception not thrown");
                fs2.Close();
            } catch (ArgumentException aexc) {
                printinfo("Info_2007! Caught expected exception, aexc==" + aexc.Message);
            } catch (IOException aexc) {
                printinfo("Info_2009! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_2010! Incorrect exception thrown, exc==" + exc.ToString());
            }
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm, fa);
                iCountErrors++;
                printerr("Error_27b98! Expected exception not thrown");
                fs2.Close();
            } catch (ArgumentException aexc) {
                if (fa == FileAccess.Read)
                {
                    printinfo("Info_348vy! Caught expected exception, aexc==" + aexc.Message);
                }
                else
                {
                    iCountErrors++;
                    printerr("Error_4387v! Unexpected exception, aexc==" + aexc);
                }
            } catch (IOException aexc) {
                printinfo("Info_2399c! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Create:
            try {
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != 0)
                {
                    iCountErrors++;
                    printerr("Error_287vb! Incorrect length of file==" + fs2.Length);
                }
                fs2.Close();
            } catch (ArgumentException aexc) {
                if (fa == FileAccess.Read)
                {
                    printinfo("Info_4398v! Caught expected exception, aexc==" + aexc.Message);
                }
                else
                {
                    iCountErrors++;
                    printerr("Error_48vy7! Unexpected exception, aexc==" + aexc);
                }
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_47yv3! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.OpenOrCreate:
        case FileMode.Open:
            fs2 = File.Open(fileName, fm, fa);
            if (fs2.Length != str2.Length)
            {
                iCountErrors++;
                printerr("Error_2gy78! Incorrect length on file==" + fs2.Length);
            }
            fs2.Close();
            break;

        case FileMode.Truncate:
            if (fa == FileAccess.Read)
            {
                iCountTestcases++;
                try {
                    fs2 = File.Open(fileName, fm, fa);
                    iCountErrors++;
                    printerr("Error_g95y8! Expected exception not thrown");
                } catch (ArgumentException iexc) {
                    printinfo("Info_2988f! Caught expected exception, iexc==" + iexc.Message);
                } catch (Exception exc) {
                    iCountErrors++;
                    printerr("Error_98y4v! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            else
            {
                fs2 = File.Open(fileName, fm, fa);
                if (fs2.Length != 0)
                {
                    iCountErrors++;
                    printerr("Error_29gv9! Incorrect length on file==" + fs2.Length);
                }
                fs2.Close();
            }
            break;

        case FileMode.Append:
            if (fa == FileAccess.Write)
            {
                fs2 = File.Open(fileName, fm, fa);
                iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_4089v! File not created");
                }
                fs2.Close();
            }
            else
            {
                iCountTestcases++;
                try {
                    fs2 = File.Open(fileName, fm, fa);
                    iCountErrors++;
                    printerr("Error_287yb! Expected exception not thrown");
                    fs2.Close();
                } catch (ArgumentException aexc) {
                    printinfo("Info_93282! Caught expected exception, aexc==" + aexc.Message);
                } catch (Exception exc) {
                    iCountErrors++;
                    printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
                }
            }
            break;

        default:
            iCountErrors++;
            printerr("Error_587yb! This should not be...");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
    }
Ejemplo n.º 17
0
    public void TestMethod(FileMode fm)
    {
        String       fileName = s_strTFAbbrev + "TestFile";
        FileInfo     fil2;
        StreamWriter sw2;
        Stream       fs2 = null;
        String       str2;

        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        strLoc = "Loc_0001";
        fil2   = new FileInfo(fileName);
        switch (fm)
        {
        case FileMode.CreateNew:
        case FileMode.Create:
        case FileMode.OpenOrCreate:
            iCountTestcases++;
            try{
                fs2 = File.Open(null, fm);
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_0002! File not created, FileMode==" + fm.ToString());
                }
            } catch (ArgumentNullException ex) {
                printinfo("Expected exception thrown :: " + ex.Message);
            } catch (Exception ex) {
                iCountErrors++;
                printerr("Error_0003! Unexpected exception thrown :: " + ex.ToString());
            }
            iCountTestcases++;
            try{
                fs2 = File.Open("", fm);
                if (!File.Exists(fileName))
                {
                    iCountErrors++;
                    printerr("Error_0004! File not created, FileMode==" + fm.ToString());
                }
            } catch (ArgumentException ex) {
                printinfo("Expected exception thrown :: " + ex.Message);
            } catch (Exception ex) {
                iCountErrors++;
                printerr("Error_0005! Unexpected exception thrown :: " + ex.ToString());
            }
            fs2 = File.Open(fileName, fm);
            iCountTestcases++;
            if (!File.Exists(fileName))
            {
                iCountErrors++;
                printerr("Error_0006! File not created, FileMode==" + fm.ToString());
            }
            fs2.Close();
            break;

        case FileMode.Open:
        case FileMode.Truncate:
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm);
                iCountErrors++;
                printerr("Error_0007! Expected exception not thrown");
                fs2.Close();
            } catch (FileNotFoundException fexc) {
                printinfo("Info_0008! Caught expected exception, fexc==" + fexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_0009! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Append:
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm);
                fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                if (fs2.Length != 4)
                {
                    iCountErrors++;
                    Console.WriteLine("Unexpected file length .... " + fs2.Length);
                }
                fs2.Close();
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_0012! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        default:
            iCountErrors++;
            printerr("Error_27tbv! This should not be....");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        strLoc = "Loc_4yg7b";
        sw2    = new StreamWriter(fileName);
        str2   = "Du er en ape";
        sw2.Write(str2);
        sw2.Close();
        switch (fm)
        {
        case FileMode.CreateNew:
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm);
                iCountErrors++;
                printerr("Error_27b98! Expected exception not thrown");
                fs2.Close();
            } catch (IOException aexc) {
                printinfo("Info_2399c! Caught expected exception, aexc==" + aexc.Message);
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        case FileMode.Create:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != 0)
            {
                iCountErrors++;
                printerr("Error_287vb! Incorrect length of file==" + fil2.Length);
            }
            fs2.Close();
            break;

        case FileMode.OpenOrCreate:
        case FileMode.Open:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != str2.Length)
            {
                iCountErrors++;
                printerr("Error_2gy78! Incorrect length on file==" + fil2.Length);
            }
            fs2.Close();
            break;

        case FileMode.Truncate:
            fs2 = File.Open(fileName, fm);
            if (fs2.Length != 0)
            {
                iCountErrors++;
                printerr("Error_29gv9! Incorrect length on file==" + fil2.Length);
            }
            fs2.Close();
            break;

        case FileMode.Append:
            iCountTestcases++;
            try {
                fs2 = File.Open(fileName, fm);
                fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                if (fs2.Length != 16)
                {
                    iCountErrors++;
                    Console.WriteLine("Unexpected file length .... " + fs2.Length);
                }
                fs2.Close();
            } catch (Exception exc) {
                iCountErrors++;
                printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
            }
            break;

        default:
            iCountErrors++;
            printerr("Error_587yb! This should not be...");
            break;
        }
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
    }
Ejemplo n.º 18
0
        public void DoSetup()
        {
            string vmdir    = MainDir;
            string datadir  = MainDir;
            string modeldir = MainDir;
            string vmns     = MainNs + ".ViewModels";
            string datans   = MainNs;
            string modelns  = MainNs;

            if (ProjectType == ProjectTypeEnum.Single)
            {
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Models");
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Models{Path.DirectorySeparatorChar}ReadMe.txt", "Put your models here");
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}ViewModels{Path.DirectorySeparatorChar}HomeVMs");
                vmdir = MainDir + $"{Path.DirectorySeparatorChar}ViewModels";
            }
            else
            {
                Directory.CreateDirectory($"{MainDir}.ViewModel{Path.DirectorySeparatorChar}HomeVMs");
                Directory.CreateDirectory($"{MainDir}.Model");
                Directory.CreateDirectory($"{MainDir}.DataAccess");
                vmdir    = MainDir + ".ViewModel";
                datadir  = MainDir + ".DataAccess";
                modeldir = MainDir + ".Model";
                vmns     = MainNs + ".ViewModel";
                datans   = MainNs + ".DataAccess";
                modelns  = MainNs + ".Model";
                File.WriteAllText($"{modeldir}{Path.DirectorySeparatorChar}{modelns}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
                File.WriteAllText($"{vmdir}{Path.DirectorySeparatorChar}{vmns}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
                File.WriteAllText($"{datadir}{Path.DirectorySeparatorChar}{datans}.csproj", GetResource("Proj.txt"), Encoding.UTF8);
            }
            Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Areas");
            Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Controllers");
            if (UI == UIEnum.LayUI)
            {
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home");
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Login");
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Shared");
            }
            Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}wwwroot");

            var proj = File.ReadAllText($"{MainDir}{Path.DirectorySeparatorChar}{MainNs}.csproj");

            if (UI == UIEnum.LayUI)
            {
                proj = proj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.TagHelpers.LayUI"" Version=""{version}"" />
    <PackageReference Include=""WalkingTec.Mvvm.Mvc.Admin"" Version=""{version}"" />
 </ItemGroup>
</Project>
");
                proj = proj.Replace("</PropertyGroup>", $@"
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
");
            }
            if (UI == UIEnum.React)
            {
                proj = proj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.TagHelpers.LayUI"" Version=""{version}"" />
    <PackageReference Include=""WalkingTec.Mvvm.Mvc.Admin"" Version=""{version}"" />
    <PackageReference Include=""Swashbuckle.AspNetCore"" Version=""4.0.1"" />
 </ItemGroup>
  <ItemGroup>
    <Content Remove=""$(SpaRoot)**"" />
    <None Include=""$(SpaRoot)**"" Exclude=""$(SpaRoot)node_modules\**;$(SpaRoot).awcache\**;$(SpaRoot).cache-loader\**"" />
  </ItemGroup>
  <Target Name=""DebugEnsureNodeEnv"" BeforeTargets=""Build"" Condition="" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') "">
    <Exec Command=""node --version"" ContinueOnError=""true"">
      <Output TaskParameter=""ExitCode"" PropertyName=""ErrorCode"" />
    </Exec>
    <Error Condition=""'$(ErrorCode)' != '0'"" Text=""Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE."" />
    <Message Importance=""high"" Text=""Restoring dependencies using 'npm'. This may take several minutes..."" />
    <Exec WorkingDirectory=""$(SpaRoot)"" Command=""npm install"" />
  </Target>
  <Target Name=""PublishRunWebpack"" AfterTargets=""ComputeFilesToPublish"">
    <Exec WorkingDirectory=""$(SpaRoot)"" Command=""npm install"" />
    <Exec WorkingDirectory=""$(SpaRoot)"" Command=""npm run build"" />
    <ItemGroup>
      <DistFiles Include=""$(SpaRoot)build\**"" />
      <ResolvedFileToPublish Include=""@(DistFiles->'%(FullPath)')"" Exclude=""@(ResolvedFileToPublish)"">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
</Project>
");
                proj = proj.Replace("</PropertyGroup>", $@"
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>3.2</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
");
            }

            if (ProjectType == ProjectTypeEnum.Multi)
            {
                proj = proj.Replace("</Project>", $@"
  <ItemGroup>
   <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
    <ProjectReference Include=""..\{datans}\{datans}.csproj"" />
    <ProjectReference Include=""..\{vmns}\{vmns}.csproj"" />
 </ItemGroup >
</Project>
");
            }

            File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}{MainNs}.csproj", proj, Encoding.UTF8);
            if (ProjectType == ProjectTypeEnum.Multi)
            {
                //修改modelproject
                var modelproj = File.ReadAllText($"{modeldir}{Path.DirectorySeparatorChar}{modelns}.csproj");
                if (modelproj.IndexOf("WalkingTec.Mvvm.Core") < 0)
                {
                    modelproj = modelproj.Replace("</Project>", $@"
  <ItemGroup>
    <PackageReference Include=""WalkingTec.Mvvm.Core"" Version=""{version}"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{modeldir}{Path.DirectorySeparatorChar}{modelns}.csproj", modelproj, Encoding.UTF8);
                }
                //修改dataproject
                var dataproj = File.ReadAllText($"{datadir}{Path.DirectorySeparatorChar}{datans}.csproj");
                if (dataproj.IndexOf($"{modelns}.csproj") < 0)
                {
                    dataproj = dataproj.Replace("</Project>", $@"
  <ItemGroup>
    <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{datadir}{Path.DirectorySeparatorChar}{datans}.csproj", dataproj, Encoding.UTF8);
                }
                //修改viewmodelproject
                var vmproj = File.ReadAllText($"{vmdir}{Path.DirectorySeparatorChar}{vmns}.csproj");
                if (vmproj.IndexOf($"{modelns}.csproj") < 0)
                {
                    vmproj = vmproj.Replace("</Project>", $@"
  <ItemGroup>
    <ProjectReference Include=""..\{modelns}\{modelns}.csproj"" />
  </ItemGroup >
</Project>
");
                    File.WriteAllText($"{vmdir}{Path.DirectorySeparatorChar}{vmns}.csproj", vmproj, Encoding.UTF8);
                }
                var solution = File.ReadAllText($"{Directory.GetParent(MainDir)}{Path.DirectorySeparatorChar}{MainNs}.sln");
                if (solution.IndexOf($"{modelns}.csproj") < 0)
                {
                    Guid g1 = Guid.NewGuid();
                    Guid g2 = Guid.NewGuid();
                    Guid g3 = Guid.NewGuid();
                    solution = solution.Replace("EndProject", $@"EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{modelns}"", ""{modelns}\{modelns}.csproj"", ""{{{g1}}}""
EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{datans}"", ""{datans}\{datans}.csproj"", ""{{{g2}}}""
EndProject
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{vmns}"", ""{vmns}\{vmns}.csproj"", ""{{{g3}}}""
EndProject
");
                    solution = solution.Replace(".Release|Any CPU.Build.0 = Release|Any CPU", $@".Release|Any CPU.Build.0 = Release|Any CPU
		{{{g1}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g1}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g1}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g1}}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{{g2}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g2}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g2}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g2}}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{{g3}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{g3}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{g3}}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{{g3}}}.Release|Any CPU.Build.0 = Release|Any CPU"        );
                    File.WriteAllText($"{Directory.GetParent(MainDir)}{Path.DirectorySeparatorChar}{MainNs}.sln", solution, Encoding.UTF8);
                }
            }


            File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}appsettings.json", GetResource("Appsettings.txt")
                              .Replace("$cs$", CS ?? "")
                              .Replace("$dbtype$", DbType.ToString())
                              .Replace("$pagemode$", PageMode.ToString())
                              .Replace("$cookiepre$", CookiePre ?? "")
                              .Replace("$enablelog$", EnableLog.ToString().ToLower())
                              .Replace("$logexception$", LogExceptionOnly.ToString().ToLower())
                              .Replace("$rpp$", Rpp == null ? "" : Rpp.ToString())
                              .Replace("$filemode$", FileMode.ToString())
                              .Replace("$uploaddir$", UploadDir ?? ""), Encoding.UTF8
                              );
            File.WriteAllText($"{datadir}{Path.DirectorySeparatorChar}DataContext.cs", GetResource("DataContext.txt").Replace("$ns$", datans), Encoding.UTF8);
            if (UI == UIEnum.LayUI)
            {
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Controllers{Path.DirectorySeparatorChar}HomeController.cs", GetResource("HomeController.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Controllers{Path.DirectorySeparatorChar}LoginController.cs", GetResource("LoginController.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}_ViewStart.cshtml", GetResource("ViewStart.txt", "Mvc"), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home{Path.DirectorySeparatorChar}Index.cshtml", GetResource("home.Index.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Login{Path.DirectorySeparatorChar}ChangePassword.cshtml", GetResource("home.ChangePassword.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home{Path.DirectorySeparatorChar}Header.cshtml", GetResource("home.Header.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Login{Path.DirectorySeparatorChar}Login.cshtml", GetResource("home.Login.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home{Path.DirectorySeparatorChar}Menu.cshtml", GetResource("home.Menu.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home{Path.DirectorySeparatorChar}PIndex.cshtml", GetResource("home.PIndex.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Home{Path.DirectorySeparatorChar}FrontPage.cshtml", GetResource("home.FrontPage.txt", "Mvc").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{vmdir}{Path.DirectorySeparatorChar}HomeVMs{Path.DirectorySeparatorChar}ChangePasswordVM.cs", GetResource("vms.ChangePasswordVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{vmdir}{Path.DirectorySeparatorChar}HomeVMs{Path.DirectorySeparatorChar}IndexVM.cs", GetResource("vms.IndexVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{vmdir}{Path.DirectorySeparatorChar}HomeVMs{Path.DirectorySeparatorChar}LoginVM.cs", GetResource("vms.LoginVM.txt").Replace("$ns$", MainNs).Replace("$vmns$", vmns), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Shared{Path.DirectorySeparatorChar}_Layout.cshtml", GetResource("layui.Layout.txt", "Mvc").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}Shared{Path.DirectorySeparatorChar}_PLayout.cshtml", GetResource("layui.PLayout.txt", "Mvc").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Program.cs", GetResource("layui.Program.txt", "Mvc").Replace("$ns$", MainNs), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Views{Path.DirectorySeparatorChar}_ViewImports.cshtml", GetResource("layui.ViewImports.txt", "Mvc"), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Areas{Path.DirectorySeparatorChar}_ViewImports.cshtml", GetResource("layui.ViewImports.txt", "Mvc"), Encoding.UTF8);
                UnZip("WalkingTec.Mvvm.Mvc.SetupFiles.Mvc.layui.layui.zip", $"{MainDir}{Path.DirectorySeparatorChar}wwwroot");
            }
            if (UI == UIEnum.React)
            {
                Directory.CreateDirectory($"{MainDir}{Path.DirectorySeparatorChar}ClientApp");
                UnZip("WalkingTec.Mvvm.Mvc.SetupFiles.Mvc.layui.layui.zip", $"{MainDir}{Path.DirectorySeparatorChar}wwwroot");
                UnZip("WalkingTec.Mvvm.Mvc.SetupFiles.Spa.React.ClientApp.zip", $"{MainDir}{Path.DirectorySeparatorChar}ClientApp");
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}Program.cs", GetResource("Program.txt", "Spa").Replace("$ns$", MainNs), Encoding.UTF8);
                var config = File.ReadAllText($"{MainDir}{Path.DirectorySeparatorChar}ClientApp{Path.DirectorySeparatorChar}src{Path.DirectorySeparatorChar}global.config.tsx");
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}ClientApp{Path.DirectorySeparatorChar}src{Path.DirectorySeparatorChar}global.config.tsx", config.Replace("title: \"WalkingTec MVVM\",", $"title: \"{MainNs}\","), Encoding.UTF8);
                File.WriteAllText($"{MainDir}{Path.DirectorySeparatorChar}ClientApp{Path.DirectorySeparatorChar}src{Path.DirectorySeparatorChar}setupProxy.js", $@"
const proxy = require('http-proxy-middleware');

module.exports = (app) => {{
    app.use(proxy('/api', {{
        target: 'http://localhost:{Port}/',
        changeOrigin: true,
        logLevel: ""debug""
    }}));
}};
", Encoding.UTF8);
            }

            if (File.Exists($"{MainDir}{Path.DirectorySeparatorChar}Startup.cs"))
            {
                File.Delete($"{MainDir}{Path.DirectorySeparatorChar}Startup.cs");
            }
        }
Ejemplo n.º 19
0
    public static void TestMethod(FileMode fm)
    {
        String fileName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
        FileInfo fil2;
        StreamWriter sw2;
        Stream fs2 = null;
        String str2;


        if (File.Exists(fileName))
            File.Delete(fileName);

        // [] File does not exist
        //------------------------------------------------------------------
        s_strLoc = "Loc_0001";

        fil2 = new FileInfo(fileName);
        switch (fm)
        {
            case FileMode.CreateNew:
            case FileMode.Create:
            case FileMode.OpenOrCreate:

                //With a null string
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(null, fm);
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_0002! File not created, FileMode==" + fm.ToString());
                    }
                }
                catch (ArgumentNullException)
                {
                }
                catch (Exception ex)
                {
                    s_iCountErrors++;
                    printerr("Error_0003! Unexpected exception thrown :: " + ex.ToString());
                }

                //with anempty string
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open("", fm);
                    if (!File.Exists(fileName))
                    {
                        s_iCountErrors++;
                        printerr("Error_0004! File not created, FileMode==" + fm.ToString());
                    }
                }
                catch (ArgumentException)
                {
                }
                catch (Exception ex)
                {
                    s_iCountErrors++;
                    printerr("Error_0005! Unexpected exception thrown :: " + ex.ToString());
                }

                fs2 = File.Open(fileName, fm);
                s_iCountTestcases++;
                if (!File.Exists(fileName))
                {
                    s_iCountErrors++;
                    printerr("Error_0006! File not created, FileMode==" + fm.ToString());
                }
                fs2.Dispose();
                break;
            case FileMode.Open:
            case FileMode.Truncate:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm);
                    s_iCountErrors++;
                    printerr("Error_0007! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (FileNotFoundException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_0009! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            case FileMode.Append:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm);
                    fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                    if (fs2.Length != 4)
                    {
                        s_iCountErrors++;
                        Console.WriteLine("Unexpected file length .... " + fs2.Length);
                    }
                    fs2.Dispose();
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_0012! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            default:
                s_iCountErrors++;
                printerr("Error_27tbv! This should not be....");
                break;
        }
        if (File.Exists(fileName))
            File.Delete(fileName);

        //------------------------------------------------------------------


        // [] File already exists
        //------------------------------------------------------------------
        s_strLoc = "Loc_4yg7b";

        FileStream stream = new FileStream(fileName, FileMode.Create);
        sw2 = new StreamWriter(stream);
        str2 = "Du er en ape";
        sw2.Write(str2);
        sw2.Dispose();
        stream.Dispose();
        switch (fm)
        {
            case FileMode.CreateNew:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm);
                    s_iCountErrors++;
                    printerr("Error_27b98! Expected exception not thrown");
                    fs2.Dispose();
                }
                catch (IOException)
                {
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_g8782! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            case FileMode.Create:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != 0)
                {
                    s_iCountErrors++;
                    printerr("Error_287vb! Incorrect length of file==" + fil2.Length);
                }
                fs2.Dispose();
                break;
            case FileMode.OpenOrCreate:
            case FileMode.Open:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != str2.Length)
                {
                    s_iCountErrors++;
                    printerr("Error_2gy78! Incorrect length on file==" + fil2.Length);
                }
                fs2.Dispose();
                break;
            case FileMode.Truncate:
                fs2 = File.Open(fileName, fm);
                if (fs2.Length != 0)
                {
                    s_iCountErrors++;
                    printerr("Error_29gv9! Incorrect length on file==" + fil2.Length);
                }
                fs2.Dispose();
                break;
            case FileMode.Append:
                s_iCountTestcases++;
                try
                {
                    fs2 = File.Open(fileName, fm);
                    fs2.Write(new Byte[] { 54, 65, 54, 90 }, 0, 4);
                    if (fs2.Length != 16)
                    {  // already 12 characters are written to the file.
                        s_iCountErrors++;
                        Console.WriteLine("Unexpected file length .... " + fs2.Length);
                    }
                    fs2.Dispose();
                }
                catch (Exception exc)
                {
                    s_iCountErrors++;
                    printerr("Error_27878! Incorrect exception thrown, exc==" + exc.ToString());
                }
                break;
            default:
                s_iCountErrors++;
                printerr("Error_587yb! This should not be...");
                break;
        }

        if (File.Exists(fileName))
            File.Delete(fileName);
    }