public void AclsAreAppliedToTemp()
        {
            DirectoryInfo localAppData         = this.CreateTestDirectory(@"AppData\Local", FileSystemRights.CreateDirectories, AccessControlType.Deny);
            DirectoryInfo temp                 = this.CreateTestDirectory("Temp");
            var           environmentVariables = new Hashtable
            {
                { "LOCALAPPDATA", localAppData.FullName },
                { "TEMP", temp.FullName },
            };

            var provider = new ApplicationFolderProvider(environmentVariables);

            PlatformFolder applicationFolder = provider.GetApplicationFolder() as PlatformFolder;

            var accessControl   = applicationFolder.Folder.GetAccessControl();
            var rulesCollection = accessControl.GetAccessRules(true, true, typeof(SecurityIdentifier));

            Assert.AreEqual(2, rulesCollection.Count);

            Assert.IsFalse(rulesCollection[0].IsInherited);
            Assert.AreEqual(new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).Value, rulesCollection[0].IdentityReference.Value);

            Assert.IsFalse(rulesCollection[1].IsInherited);
            Assert.AreEqual(WindowsIdentity.GetCurrent().User.Value, rulesCollection[1].IdentityReference.Value);

            localAppData.Delete(true);
        }
 public void ReturnsEmptyEnumerableWhenStorageFolderIsEmpty()
 {
     var folder = new PlatformFolder(this.storageFolder);
     IEnumerable<IPlatformFile> files = folder.GetFiles();
     Assert.NotNull(files);
     Assert.Empty(files);
 }
Ejemplo n.º 3
0
            public void DeletesFolderFromFileSystem()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                folder.Delete();

                Assert.IsFalse(folder.Exists());
            }
Ejemplo n.º 4
0
            public void ThrowsIOExceptionWhenFileIsAlreadyDeleted()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                FileSystemTest.DeletePlatformItem(this.storageFolder);

                AssertEx.Throws <DirectoryNotFoundException>(() => folder.Delete());
            }
Ejemplo n.º 5
0
            public void ReturnsTrueWhenFolderExists()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                bool folderExists = folder.Exists();

                Assert.IsTrue(folderExists);
            }
 public void ReturnsEmptyEnumerableWhenStorageFolderWasDeleted()
 {
     var folder = new PlatformFolder(this.storageFolder);
     FileSystemTest.DeletePlatformItem(this.storageFolder);
     IEnumerable<IPlatformFile> files = folder.GetFiles();
     Assert.NotNull(files);
     Assert.Empty(files);
 }
Ejemplo n.º 7
0
            public void ReturnsEmptyEnumerableWhenStorageFolderIsEmpty()
            {
                var folder = new PlatformFolder(this.storageFolder);
                IEnumerable <IPlatformFile> files = folder.GetFiles();

                Assert.IsNotNull(files);
                AssertEx.IsEmpty(files);
            }
Ejemplo n.º 8
0
 public void ThrowsUnauthorizedAccessExceptionWhenProcessDoesNotHaveRightToListDirectory()
 {
     using (new DirectoryAccessDenier(this.storageFolder, FileSystemRights.ListDirectory))
     {
         var folder = new PlatformFolder(this.storageFolder);
         AssertEx.Throws <UnauthorizedAccessException>(() => folder.GetFiles());
     }
 }
Ejemplo n.º 9
0
 public void ThrowsUnauthorizedAccessExceptionWhenProcessDoesNotHaveRightToCreateFile()
 {
     using (new DirectoryAccessDenier(this.storageFolder, FileSystemRights.CreateFiles))
     {
         var folder = new PlatformFolder(this.storageFolder);
         AssertEx.Throws <UnauthorizedAccessException>(() => folder.CreateFile(FileSystemTest.GetUniqueFileName()));
     }
 }
        private static IPlatformFolder CreateAndValidateApplicationFolder(string rootPath, bool createSubFolder, IList <string> errors)
        {
            string          errorMessage = null;
            IPlatformFolder result       = null;

            try
            {
                if (!string.IsNullOrEmpty(rootPath))
                {
                    var telemetryDirectory = new DirectoryInfo(rootPath);
                    if (createSubFolder)
                    {
                        telemetryDirectory = CreateTelemetrySubdirectory(telemetryDirectory);
                    }

                    CheckAccessPermissions(telemetryDirectory);
                    TelemetryChannelEventSource.Log.StorageFolder(telemetryDirectory.FullName);

                    result = new PlatformFolder(telemetryDirectory);
                }
            }
            catch (UnauthorizedAccessException exp)
            {
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (ArgumentException exp)
            {
                // Path does not specify a valid file path or contains invalid DirectoryInfo characters.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (DirectoryNotFoundException exp)
            {
                // The specified path is invalid, such as being on an unmapped drive.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (IOException exp)
            {
                // The subdirectory cannot be created. -or- A file or directory already has the name specified by path. -or-  The specified path, file name, or both exceed the system-defined maximum length. .
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (SecurityException exp)
            {
                // The caller does not have code access permission to create the directory.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                errors.Add(errorMessage);
            }

            return(result);
        }
        private static IPlatformFolder CreateAndValidateApplicationFolder(string rootPath, bool createSubFolder, IList<string> errors)
        {
            string errorMessage = null;
            IPlatformFolder result = null;

            try
            {
                if (!string.IsNullOrEmpty(rootPath))
                {
                    var telemetryDirectory = new DirectoryInfo(rootPath);
                    if (createSubFolder)
                    {
                        telemetryDirectory = CreateTelemetrySubdirectory(telemetryDirectory);
                    }

                    CheckAccessPermissions(telemetryDirectory);
                    TelemetryChannelEventSource.Log.StorageFolder(telemetryDirectory.FullName);

                    result = new PlatformFolder(telemetryDirectory);
                }
            }
            catch (UnauthorizedAccessException exp)
            {
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (ArgumentException exp)
            {
                // Path does not specify a valid file path or contains invalid DirectoryInfo characters.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (DirectoryNotFoundException exp)
            {
                // The specified path is invalid, such as being on an unmapped drive.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (IOException exp)
            {
                // The subdirectory cannot be created. -or- A file or directory already has the name specified by path. -or-  The specified path, file name, or both exceed the system-defined maximum length. .
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }
            catch (SecurityException exp)
            {
                // The caller does not have code access permission to create the directory.
                errorMessage = GetPathAccessFailureErrorMessage(exp, rootPath);
                TelemetryChannelEventSource.Log.TransmissionStorageAccessDeniedWarning(errorMessage);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                errors.Add(errorMessage);
            }

            return result;
        }
Ejemplo n.º 12
0
            public void ReturnsFalseWhenFolderDoesNotExist()
            {
                FileSystemTest.DeletePlatformItem(this.storageFolder);
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                bool folderExists = folder.Exists();

                Assert.IsFalse(folderExists);
            }
Ejemplo n.º 13
0
            public void CreatesFileWithSpecifiedName()
            {
                var folder = new PlatformFolder(this.storageFolder);

                string        fileName = GetUniqueFileName();
                IPlatformFile file     = folder.CreateFile(fileName);

                Assert.AreEqual(fileName, file.Name);
            }
Ejemplo n.º 14
0
            public void ReturnsEmptyEnumerableWhenStorageFolderWasDeleted()
            {
                var folder = new PlatformFolder(this.storageFolder);

                FileSystemTest.DeletePlatformItem(this.storageFolder);
                IEnumerable <IPlatformFile> files = folder.GetFiles();

                Assert.IsNotNull(files);
                AssertEx.IsEmpty(files);
            }
Ejemplo n.º 15
0
            public void RecreatesFolderIfItWasDeleted()
            {
                var folder = new PlatformFolder(this.storageFolder);

                FileSystemTest.DeletePlatformItem(this.storageFolder);
                string fileName = GetUniqueFileName();

                folder.CreateFile(fileName);
                Assert.IsNotNull(FileSystemTest.GetPlatformFile(fileName, this.storageFolder));
            }
Ejemplo n.º 16
0
            public void ThrowsIOExceptionWhenFileWithThatNameAlreadyExists()
            {
                string fileName = GetUniqueFileName();

                FileSystemTest.CreatePlatformFile(fileName, this.storageFolder);

                var folder = new PlatformFolder(this.storageFolder);

                AssertEx.Throws <IOException>(() => folder.CreateFile(fileName));
            }
Ejemplo n.º 17
0
 public void ThrowsUnauthorizedAccessExceptionWhenProcessDoesNotHaveRightToCreateFile()
 {
     Trace.WriteLine(string.Format("{0} Blocking Listing Permission on: {1} ", DateTime.Now.ToLongTimeString(), this.storageFolder.FullName));
     // Only on Windows as the APIs are not available in Linux.
     // The product also does not this this.
     using (new DirectoryAccessDenier(this.storageFolder, FileSystemRights.CreateFiles))
     {
         var folder = new PlatformFolder(this.storageFolder);
         AssertEx.Throws <UnauthorizedAccessException>(() => folder.CreateFile(FileSystemTest.GetUniqueFileName()));
     }
 }
Ejemplo n.º 18
0
            public void CreatesPhysicalFileInFileSystem()
            {
                var folder = new PlatformFolder(this.storageFolder);

                string        fileName = GetUniqueFileName();
                IPlatformFile file     = folder.CreateFile(fileName);

                var storageFile = FileSystemTest.GetPlatformFile(fileName, this.storageFolder);

                Assert.IsNotNull(storageFile);
            }
Ejemplo n.º 19
0
            public void ReturnsEnumerableOfObjectsRepresentingExistingFiles()
            {
                string[] expectedFileNames = new string[] { "file.1", "blah.txt", "foo.bar" };
                foreach (string fileName in expectedFileNames)
                {
                    FileSystemTest.CreatePlatformFile(fileName, this.storageFolder);
                }

                var folder = new PlatformFolder(this.storageFolder);

                IEnumerable <IPlatformFile> files = folder.GetFiles();

                AssertEx.AreEqual(expectedFileNames.OrderBy(name => name), files.Select(f => f.Name).OrderBy(name => name));
            }
            public void ThrowsIOExceptionWhenDesiredFileNameIsTooLong()
            {
                bool expectedException = false;

                try
                {
                    var folder = new PlatformFolder(this.storageFolder);
                    folder.CreateFile(new string('F', 1024));
                }
                catch (PathTooLongException)
                {
                    expectedException = true;
                }
                catch (IOException)
                {
                    // NET CORE 3.0 changed the exception that can be thrown.
                    expectedException = true;
                }

                Assert.IsTrue(expectedException);
            }
            public void ThrowsIOExceptionWhenFileWithThatNameAlreadyExists()
            {
                string fileName = GetUniqueFileName();
                FileSystemTest.CreatePlatformFile(fileName, this.storageFolder);

                var folder = new PlatformFolder(this.storageFolder);
                Assert.Throws<IOException>(() => folder.CreateFile(fileName));
            }
 public void ThrowsArgumentNullExceptionWhenGivenFileNameIsNull()
 {
     var folder = new PlatformFolder(this.storageFolder);
     Assert.Throws<ArgumentNullException>(() => folder.CreateFile(null));
 }
 public void ThrowsArgumentExceptionWhenDesiredFileNameIsEmpty()
 {
     var folder = new PlatformFolder(this.storageFolder);
     Assert.Throws<ArgumentException>(() => folder.CreateFile(string.Empty));
 }
 public void ThrowsIOExceptionWhenDesiredFileNameIsTooLong()
 {
     var folder = new PlatformFolder(this.storageFolder);
     Assert.Throws<PathTooLongException>(() => folder.CreateFile(new string('F', 1024)));
 }
 public void ThrowsUnauthorizedAccessExceptionWhenProcessDoesNotHaveRightToCreateFile()
 {
     using (new DirectoryAccessDenier(this.storageFolder, FileSystemRights.CreateFiles))
     { 
         var folder = new PlatformFolder(this.storageFolder);
         Assert.Throws<UnauthorizedAccessException>(() => folder.CreateFile(FileSystemTest.GetUniqueFileName()));
     }
 }
 public void RecreatesFolderIfItWasDeleted()
 {
     var folder = new PlatformFolder(this.storageFolder);
     FileSystemTest.DeletePlatformItem(this.storageFolder);
     string fileName = GetUniqueFileName();
     folder.CreateFile(fileName);
     Assert.NotNull(FileSystemTest.GetPlatformFile(fileName, this.storageFolder));
 }
            public void CreatesFileWithSpecifiedName()
            {
                var folder = new PlatformFolder(this.storageFolder);

                string fileName = GetUniqueFileName();
                IPlatformFile file = folder.CreateFile(fileName);

                Assert.Equal(fileName, file.Name);
            }
Ejemplo n.º 28
0
            public void ThrowsIOExceptionWhenDesiredFileNameIsTooLong()
            {
                var folder = new PlatformFolder(this.storageFolder);

                AssertEx.Throws <PathTooLongException>(() => folder.CreateFile(new string('F', 1024)));
            }
Ejemplo n.º 29
0
            public void ThrowsArgumentExceptionWhenDesiredFileNameIsEmpty()
            {
                var folder = new PlatformFolder(this.storageFolder);

                AssertEx.Throws <ArgumentException>(() => folder.CreateFile(string.Empty));
            }
Ejemplo n.º 30
0
            public void ThrowsArgumentNullExceptionWhenGivenFileNameIsNull()
            {
                var folder = new PlatformFolder(this.storageFolder);

                AssertEx.Throws <ArgumentNullException>(() => folder.CreateFile(null));
            }
            public void ThrowsIOExceptionWhenFileIsAlreadyDeleted()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);
                FileSystemTest.DeletePlatformItem(this.storageFolder);

                Assert.Throws<DirectoryNotFoundException>(() => folder.Delete());
            }
            public void ReturnsTrueWhenFolderExists()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                bool folderExists = folder.Exists();

                Assert.True(folderExists);
            }
            public void ReturnsFalseWhenFolderDoesNotExist()
            {
                FileSystemTest.DeletePlatformItem(this.storageFolder);
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                bool folderExists = folder.Exists();

                Assert.False(folderExists);
            }
            public void ReturnsEnumerableOfObjectsRepresentingExistingFiles()
            {
                string[] expectedFileNames = new string[] { "file.1", "blah.txt", "foo.bar" };
                foreach (string fileName in expectedFileNames)
                {
                    FileSystemTest.CreatePlatformFile(fileName, this.storageFolder);
                }

                var folder = new PlatformFolder(this.storageFolder);

                IEnumerable<IPlatformFile> files = folder.GetFiles();
                Assert.Equal(expectedFileNames.OrderBy(name => name), files.Select(f => f.Name).OrderBy(name => name));
            }
            public void CreatesPhysicalFileInFileSystem()
            {
                var folder = new PlatformFolder(this.storageFolder);

                string fileName = GetUniqueFileName();
                IPlatformFile file = folder.CreateFile(fileName);

                var storageFile = FileSystemTest.GetPlatformFile(fileName, this.storageFolder);
                Assert.NotNull(storageFile);
            }
 public void ThrowsUnauthorizedAccessExceptionWhenProcessDoesNotHaveRightToListDirectory()
 {
     using (new DirectoryAccessDenier(this.storageFolder, FileSystemRights.ListDirectory))
     {
         var folder = new PlatformFolder(this.storageFolder);
         Assert.Throws<UnauthorizedAccessException>(() => folder.GetFiles());
     }
 }
            public void DeletesFolderFromFileSystem()
            {
                IPlatformFolder folder = new PlatformFolder(this.storageFolder);

                folder.Delete();

                Assert.False(folder.Exists());
            }