public void TestSymlinksAreNotRemoved()
        {
            var logger = new TestLogsLogger(this, nameof(this.TestSymlinksAreNotRemoved));

            string testPath = UtilsSystem.GetTempPath("symlink_test" + Guid.NewGuid());

            var pathWebsite            = Path.Combine(testPath, "website");
            var pathContentsPersistent = Path.Combine(testPath, "content_store_persistent");

            Directory.CreateDirectory(pathWebsite);
            Directory.CreateDirectory(pathContentsPersistent);
            Directory.CreateDirectory(Path.Combine(pathContentsPersistent, "empty_directory"));

            string linkedDir  = Path.Combine(pathWebsite, "contents");
            string linkedDir2 = Path.Combine(pathWebsite, "contents2", "contents");

            UtilsSystem.EnsureDirectoryExists(linkedDir);
            UtilsSystem.EnsureDirectoryExists(linkedDir2);

            UtilsJunction.EnsureLink(linkedDir, pathContentsPersistent, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);
            UtilsJunction.EnsureLink(linkedDir2, pathContentsPersistent, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);

            Assert.True(UtilsJunction.IsJunctionOrSymlink(linkedDir));
            Assert.True(UtilsJunction.IsJunctionOrSymlink(linkedDir2));

            string fileInContentsPeristent = Path.Combine(pathContentsPersistent, "test.txt");
            string fileInSymlinkDir        = Path.Combine(linkedDir, "test.txt");

            Assert.Equal(fileInContentsPeristent, UtilsJunction.ResolvePath(fileInSymlinkDir));

            string fileInContentsPeristent2 = Path.Combine(pathContentsPersistent, "test2.txt");
            string fileInSymlinkDir2        = Path.Combine(linkedDir2, "test2.txt");

            Assert.Equal(fileInContentsPeristent2, UtilsJunction.ResolvePath(fileInSymlinkDir2));

            File.WriteAllText(fileInSymlinkDir, "testfile");
            File.WriteAllText(fileInSymlinkDir2, "testfile");

            Assert.True(File.Exists(fileInSymlinkDir), $"File exists {fileInSymlinkDir}");
            Assert.True(File.Exists(fileInSymlinkDir2), $"File exists {fileInSymlinkDir2}");
            Assert.True(File.Exists(fileInContentsPeristent), $"File exists {fileInContentsPeristent}");
            Assert.True(File.Exists(fileInContentsPeristent2), $"File exists {fileInContentsPeristent2}");

            // If we delete the directory containing the symlink, the file still exists
            UtilsSystem.DeleteDirectory(pathWebsite, logger);
            Assert.False(Directory.Exists(pathWebsite), "Directory exists " + pathWebsite);

            Assert.False(File.Exists(fileInSymlinkDir), $"File exists {fileInSymlinkDir}");
            Assert.True(File.Exists(fileInContentsPeristent), $"File exists {fileInContentsPeristent}");
            Assert.False(File.Exists(fileInSymlinkDir2), $"File exists {fileInSymlinkDir2}");
            Assert.True(File.Exists(fileInContentsPeristent2), $"File exists {fileInContentsPeristent2}");

            Directory.Delete(testPath, true);
        }
        public void TestWindowsRight()
        {
            ILoggerInterface logger = new TestLogsLogger(this, nameof(this.TestWindowsRight));

            UtilsWindowsAccounts.EnsureUserExists("chf_testaccount", "81Dentiaaeh#" + Guid.NewGuid(), "The display name", logger, null);
            UtilsWindowsAccounts.EnsureGroupExists("chef_testgroup", null);
            UtilsWindowsAccounts.EnsureUserInGroup("chf_testaccount", "chef_testgroup", logger, null);
            Assert.Equal(0, UtilsWindowsAccounts.SetRight("chf_testaccount", "SeCreateSymbolicLinkPrivilege", logger));
            Assert.Equal(0, UtilsWindowsAccounts.SetRight("chf_testaccount", "SeBatchLogonRight", logger));
            UtilsWindowsAccounts.DeleteUser("chf_testaccount", null);
        }
        public void TestResolveJunctionPath()
        {
            var logger = new TestLogsLogger(this, nameof(this.TestResolveJunctionPath));

            string testPath = UtilsSystem.GetTempPath("symlink_test" + Guid.NewGuid());

            // Probar resolución de nivel 1

            string test1OriginalPath = UtilsSystem.EnsureDirectoryExists(Path.Combine(testPath, "test1"), true);
            string test1LinkPath     = Path.Combine(testPath, "test1_link");
            string test1JunctionPath = Path.Combine(testPath, "test1_junction");

            UtilsJunction.EnsureLink(test1LinkPath, test1OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);
            UtilsJunction.EnsureLink(test1JunctionPath, test1OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);

            Assert.Equal(test1OriginalPath, UtilsJunction.ResolvePath(test1LinkPath));
            Assert.Equal(test1OriginalPath, UtilsJunction.ResolvePath(test1JunctionPath));

            // Probar resolución de subdirectorio existente y no existente

            string test2OriginalPath = UtilsSystem.EnsureDirectoryExists(Path.Combine(testPath, "test2"), true);
            string test2LinkPath     = Path.Combine(testPath, "test2_link");
            string test2JunctionPath = Path.Combine(testPath, "test2_junction");

            UtilsJunction.EnsureLink(test2LinkPath, test2OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);
            UtilsJunction.EnsureLink(test2JunctionPath, test2OriginalPath, logger, true, linkType: UtilsJunction.LinkTypeRequest.Junction);

            string test2LinkSubDir     = UtilsSystem.EnsureDirectoryExists(Path.Combine(test2LinkPath, "sub1", "sub2"), true);
            string test2JunctionSubDir = UtilsSystem.EnsureDirectoryExists(Path.Combine(test2JunctionPath, "sub3", "sub4"), true);

            Assert.Equal(Path.Combine(test2OriginalPath, "sub1", "sub2"), UtilsJunction.ResolvePath(test2LinkSubDir));
            Assert.Equal(Path.Combine(test2OriginalPath, "sub3", "sub4"), UtilsJunction.ResolvePath(test2JunctionSubDir));

            // Ahora subdirectorios que no existen
            Assert.Equal(Path.Combine(test2OriginalPath, "sub4", "sub5"), UtilsJunction.ResolvePath(Path.Combine(test2LinkPath, "sub4", "sub5")));
            Assert.Equal(Path.Combine(test2OriginalPath, "sub6", "sub7"), UtilsJunction.ResolvePath(Path.Combine(test2JunctionPath, "sub6", "sub7")));

            // Ahora una cadena de enlaces dentro de otro enlace...
            string test3LinkSubDir = Path.Combine(test2LinkPath, "sub8");

            UtilsSystem.EnsureDirectoryExists(Path.Combine(test2LinkPath, "test3"), true);
            UtilsJunction.EnsureLink(test3LinkSubDir, Path.Combine(test2LinkPath, "test3"), logger, true, linkType: UtilsJunction.LinkTypeRequest.Symlink);

            Assert.Equal(Path.Combine(test2OriginalPath, "test3"), UtilsJunction.ResolvePath(test3LinkSubDir));

            UtilsSystem.DeleteDirectory(testPath, logger, 2);

            // Non existent and malformed network uri get reconstructed as-is
            string testNetworkUri = "\\\\147.83.73.25\\a\\b\\c\\\\d";

            Assert.Equal(testNetworkUri, UtilsJunction.ResolvePath(testNetworkUri));
        }
        public void TestCannotDeleteDirectoriesWithSmallDepth()
        {
            var directory = Directory.CreateDirectory("c:\\testdirectory");

            var logger = new TestLogsLogger(this, nameof(this.TestCannotDeleteDirectoriesWithSmallDepth));

            Assert.Throws <InvalidOperationException>(() =>
            {
                UtilsSystem.DeleteDirectory(directory.FullName, logger);
            });

            Assert.Throws <InvalidOperationException>(() =>
            {
                UtilsSystem.DeleteDirectory(directory.FullName, logger, 5);
            });

            Assert.Throws <InvalidOperationException>(() =>
            {
                UtilsSystem.DeleteDirectory(directory.FullName, logger);
            });

            directory.Delete();
        }
        public void DeleteHttpsBindings()
        {
            var logger = new TestLogsLogger(this, nameof(this.DeleteHttpsBindings));

            using (ServerManager sm = new ServerManager())
            {
                var site = sm.Sites.Add("site0", Path.GetTempPath(), 443);
                site.Bindings.Add("127.0.0.1:5879:site0", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                site.Bindings.Add("127.0.0.1:5879:site1", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            var certificates = UtilsIis.GetBoundCertificates();

            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSiteBindings(site, sm, (i) => i.Host == "site1", logger);
                sm.CommitChanges();
            }

            certificates = UtilsIis.GetBoundCertificates();
            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSiteBindings(site, sm, (i) => i.Host == "site0", logger);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.False(certificates.Any((i) =>
                                          i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                site.Bindings.Add("127.0.0.1:5879:site0", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                site.Bindings.Add("127.0.0.1:5879:site1", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSite(site, sm, logger);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.False(certificates.Any((i) =>
                                          i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));
        }