示例#1
0
        private IEnumerable <ItemType> ProcessOperationFilePath(OVAL.Definitions.ObjectType objectType)
        {
            var itemTypes = new List <ItemType>();

            EntityObjectStringType filePath = null;
            String path     = null;
            String fileName = null;

            if (objectType is OVAL.Definitions.Windows.file_object)
            {
                filePath = (EntityObjectStringType)((OVAL.Definitions.Windows.file_object)objectType).GetItemValue(file_object_ItemsChoices.filepath);
                path     = Path.GetDirectoryName(filePath.Value);
                fileName = Path.GetFileName(filePath.Value);
            }
            else if (objectType is OVAL.Definitions.Unix.file_object)
            {
                filePath = (EntityObjectStringType)((OVAL.Definitions.Unix.file_object)objectType).GetItemValue(OVAL.Definitions.Unix.ItemsChoiceType3.filepath);
                path     = GetUnixPath(filePath.Value);
                fileName = GetUnixFilename(filePath.Value);
            }

            if (filePath == null || filePath.Value == null)
            {
                return(itemTypes);
            }

            var filePathInformation = new FilePathRegexInformation(path);
            var operationForPath    = filePath.operation;

            if (this.operatorHelper.IsRegularExpression(operationForPath))
            {
                if (filePathInformation.GetPathWithFirstRegex() == null)
                {
                    operationForPath = OperationEnumeration.equals;
                }
            }
            else if (this.operatorHelper.IsNotEqualsOperation(operationForPath))
            {
                operationForPath = OperationEnumeration.equals;
            }

            var pathEntity = new EntityObjectStringType()
            {
                Value = path, operation = operationForPath
            };
            var fileNameEntity = new EntityObjectStringType()
            {
                Value = fileName, operation = filePath.operation
            };
            var fileEntities = new Dictionary <string, EntityObjectStringType>();

            fileEntities.Add("path", pathEntity);
            fileEntities.Add("filename", fileNameEntity);
            var paths     = this.ProcessOperationsPaths(fileEntities);
            var fileNames = this.ProcessOperationFileName(fileEntities, paths, true);

            var family = GetFamilyEnumerationForObjectType(objectType);

            return(new FileItemTypeFactory(family).CreateFileItemTypesWithFilePathsByCombinationFrom(paths, fileNames));
        }
        public void Should_be_possible_to_get_patternMatch_levels()
        {
            {
                var fileLevelsWithRegexWin = new FilePathRegexInformation(@"c:\.*").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegexWin, 1, new string[] { ".*" }, new int[] { 1 }); 
            }

            {
                var fileLevelsWithRegexUnix = new FilePathRegexInformation("/.*").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegexUnix, 1, new string[] { ".*" }, new int[] { 0 }); 
            }
        }
        public void Should_be_possible_to_concatenate_the_windows_path_with_next_regex_level()
        {
            {
                var pathWithNextRegex =
                    new FilePathRegexInformation(@"c:\.*\oval.*")
                        .ConcatPathWithNextLeveRegex(@"c:\temp\", 1);

                Assert.AreEqual(@"c:\temp\oval.*", pathWithNextRegex, "the path is not expected");
            }

            {
                var pathWithNextRegex =
                    new FilePathRegexInformation(@"c:\temp\file\.*\teste\^.txt")
                        .ConcatPathWithNextLeveRegex(@"c:\temp\file\temp\teste\", 3);

                Assert.AreEqual(@"c:\temp\file\temp\teste\^.txt", pathWithNextRegex, "the path is not expected");
            }
        }
        public void Should_be_possible_to_concatenate_the_unix_path_with_next_regex_level()
        {
            {
                var pathWithNextRegex =
                    new FilePathRegexInformation("/.*/oval.*")
                        .ConcatPathWithNextLeveRegex("/temp/", 0);

                Assert.AreEqual("/temp/oval.*", pathWithNextRegex, "the path is not expected");
            }

            {
                var pathWithNextRegex =
                    new FilePathRegexInformation("/temp/file/.*/teste/^.txt")
                        .ConcatPathWithNextLeveRegex("/temp/file/temp/teste/", 2);

                Assert.AreEqual("/temp/file/temp/teste/^.txt", pathWithNextRegex, "the path is not expected");
            }
        }
        public void Should_be_possible_to_get_patternMatch_levels_in_multiples_levels()
        {
            //  Windows
            {
                var fileLevelsWithRegex = new FilePathRegexInformation(@"c:\.*\oval.*").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegex, 2, new string[] { ".*", "oval.*" }, new int[] { 1, 2 } );
            }
            {
                var fileLevelsWithRegex = new FilePathRegexInformation(@"c:\temp\oval.*\support\^[win]").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegex, 2, new string[] { "oval.*", "^[win]" }, new int[] { 2, 4 });
                
            }

            //  Unix
            {
                var fileLevelsWithRegex = new FilePathRegexInformation("/.*/oval.*").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegex, 2, new string[] { ".*", "oval.*" }, new int[] { 0, 1 });
            }
            {
                var fileLevelsWithRegex = new FilePathRegexInformation("/temp/oval.*/support/^[win]").GetPathLevelsWithRegex();
                AssertFilePathRegexInformation(fileLevelsWithRegex, 2, new string[] { "oval.*", "^[win]" }, new int[] { 1, 3 });

            }
        }
        public void Should_return_the_empty_list_when_there_is_not_a_regex_pattern_in_the_path()
        {
            var fileLevelsWithRegexWin = new FilePathRegexInformation(@"c:\windows\temp").GetPathLevelsWithRegex();
            var fileLevelsWithRegexUnix = new FilePathRegexInformation("/lib/modules").GetPathLevelsWithRegex();

            Assert.AreEqual(0, fileLevelsWithRegexWin.Count());
            Assert.AreEqual(0, fileLevelsWithRegexUnix.Count());
        }
        public void Should_be_possible_to_get_the_unit_of_a_path()
        {
            var pathUnitWin = new FilePathRegexInformation(@"c:\windows\temp").GetUnitOfPath();
            var pathUnitUnix = new FilePathRegexInformation("/lib/modules").GetUnitOfPath();

            Assert.AreEqual(@"c:\", pathUnitWin, "the path unit is not expected");
            Assert.IsNull(pathUnitUnix, "the path unit for unix system should always be null.");
        }
        public void Should_be_possible_to_get_the_fixed_path_with_first_regex_pattern()
        {
            {
                var pathWithFirstRegexWin = new FilePathRegexInformation(@"c:\.*\oval.*").GetPathWithFirstRegex();
                var pathWithFirstRegexUnix = new FilePathRegexInformation("/.*/oval.*").GetPathWithFirstRegex();
                AssertPathWithPattern(pathWithFirstRegexWin, @"c:\.*", 1);
                AssertPathWithPattern(pathWithFirstRegexUnix, "/.*", 0);
            }

            {
                var pathWithFirstRegexWin = new FilePathRegexInformation(@"c:\temp\oval.*").GetPathWithFirstRegex();
                var pathWithFirstRegexUnix = new FilePathRegexInformation("/temp/oval.*").GetPathWithFirstRegex();
                AssertPathWithPattern(pathWithFirstRegexWin, @"c:\temp\oval.*", 2);
                AssertPathWithPattern(pathWithFirstRegexUnix, "/temp/oval.*", 1);
            }

            {
                var pathWithFirstRegexWin = new FilePathRegexInformation(@"c:\temp\oval.*\xml").GetPathWithFirstRegex();
                var pathWithFirstRegexUnix = new FilePathRegexInformation("/temp/oval.*/xml").GetPathWithFirstRegex();
                AssertPathWithPattern(pathWithFirstRegexWin, @"c:\temp\oval.*", 2);
                AssertPathWithPattern(pathWithFirstRegexUnix, "/temp/oval.*", 1);
            }
        }
示例#9
0
        public IEnumerable<string> SubstitutionValueInPattern(List<string> values, PathLevelWithRegex basePath, FilePathRegexInformation regexInformation)
        {
            var paths = new List<string>();
            foreach (string path in values)
            {
                var level  = basePath.Level;
                if (!regexInformation.IsWindowsFilepath())
                    level -= 1;

                var concatenatedString = regexInformation.ConcatPathWithNextLeveRegex(path, level);
                if (!string.IsNullOrEmpty(concatenatedString))
                    paths.Add(concatenatedString);
            }

            return paths;
        }        
示例#10
0
        private List<string> EvaluateOperationPatternMatchOfPath(IEnumerable<string> paths)
        {
            var values = new List<string>();
            foreach (string path in paths)
            {
                var regexInformation = new FilePathRegexInformation(path);
                var basePath = regexInformation.GetPathWithFirstRegex();
                if (basePath != null)
                {
                    var result = this.searchFileChildren(path, true);
                    if (regexInformation.IsWindowsFilepath())
                        result = this.removeFilesFromList(result);

                    var multiLevelPatternMatchOperator = new MultiLevelPatternMatchOperation(this.Platform);
                    var valuesWithMatch = multiLevelPatternMatchOperator.applyPatternMatch(path, result).ToList();
                    //var newPaths = this.SubstitutionValueInPattern(valuesWithMatch, basePath, regexInformation);
                    //if (newPaths.Count() > 0)
                    //    values.AddRange(this.EvaluateOperationPatternMatchOfPath(newPaths));
                    //else
                        values.AddRange(valuesWithMatch);
                }
            }
            return values;
        }
示例#11
0
 private List<string> EvaluateOperationPatternMatchOfFileName(string path)
 {
     List<string> values = new List<string>();
     string[] result;
     FilePathRegexInformation regexInformation = new FilePathRegexInformation(path);
     PathLevelWithRegex pathWithRegex = regexInformation.GetPathWithFirstRegex();
     result = this.searchFileChildren(path, true);
     result = this.removeDirectoriesFromList(result);
     foreach (string pathForMatch in result)
     {
         string fileName = Path.GetFileName(pathForMatch);
         values.AddRange(this.EvaluateOperation(fileName, new List<string>() { pathWithRegex.Pattern }, OperationEnumeration.patternmatch));
     }
     return values;
 }
示例#12
0
        public virtual FilePathOperationResult ProcessOperationFilePath(Dictionary<string, EntityObjectStringType> fileEntities)
        {
            FilePathOperationResult filePathOperationResult = new FilePathOperationResult();
            if (!this.IsThereFilePathEntity(fileEntities))
                return filePathOperationResult;

            EntityObjectStringType filePath;
            if (!fileEntities.TryGetValue("filepath", out filePath))
                return filePathOperationResult;

            string path = Path.GetDirectoryName(filePath.Value);
            string fileName = Path.GetFileName(filePath.Value);
            FilePathRegexInformation filePathInformation = new FilePathRegexInformation(path);
            OperationEnumeration operationForPath = filePath.operation;
            OperationEnumeration operationForFileName = operationForPath;
            if (this.operatorHelper.IsRegularExpression(operationForPath))
            {
                PathLevelWithRegex pathWithRegex = filePathInformation.GetPathWithFirstRegex();
                if (pathWithRegex == null)
                    operationForPath = OperationEnumeration.equals;
                
                PathLevelWithRegex filenameWithRegex = new FilePathRegexInformation(fileName.Replace(".", "")).GetPathWithFirstRegex();
                if (filenameWithRegex == null)
                    operationForFileName = OperationEnumeration.equals;

            }
            else if (this.operatorHelper.IsNotEqualsOperation(operationForPath))
            {
                operationForPath = OperationEnumeration.equals;
                operationForFileName = operationForPath;
            }

                
                //RegexHelper.IsPathLevelARegexPattern(fileName) ? OperationEnumeration.patternmatch : OperationEnumeration.equals;
            Dictionary<string, EntityObjectStringType> pathAndFileNameEntities = new Dictionary<string, EntityObjectStringType>();
            EntityObjectStringType pathEntity = new EntityObjectStringType() { Value = path, operation = operationForPath };
            EntityObjectStringType fileNameEntity = new EntityObjectStringType() { Value = fileName, operation = operationForFileName };
            pathAndFileNameEntities.Add("path", pathEntity);
            pathAndFileNameEntities.Add("filename", fileNameEntity);
            IEnumerable<string> paths = this.ProcessOperationsPaths(pathAndFileNameEntities);
            IEnumerable<string> fileNames = this.ProcessOperationFileName(pathAndFileNameEntities, paths, true);

            filePathOperationResult.FileNames.AddRange(fileNames);
            filePathOperationResult.Paths.AddRange(paths);

            return filePathOperationResult;
        }
 private IEnumerable<string> SubstitutionValueInPattern(List<string> values, PathLevelWithRegex basePath, FilePathRegexInformation regexInformation)
 {
     List<string> paths = new List<string>();
     foreach (string path in values)
     {
         string concatenatedString = regexInformation.ConcatPathWithNextLeveRegex(path, basePath.Level);
         if (!string.IsNullOrEmpty(concatenatedString))
             paths.Add(concatenatedString);
     }
     return paths;
 }
        private IEnumerable<ItemType> ProcessOperationFilePath(OVAL.Definitions.ObjectType objectType)
        {
            var itemTypes = new List<ItemType>();

            EntityObjectStringType filePath = null;
            String path = null;
            String fileName = null;

            if (objectType is OVAL.Definitions.Windows.file_object)
            {
                filePath = (EntityObjectStringType)((OVAL.Definitions.Windows.file_object)objectType).GetItemValue(file_object_ItemsChoices.filepath);
                path = Path.GetDirectoryName(filePath.Value);
                fileName = Path.GetFileName(filePath.Value);
            }
            else if (objectType is OVAL.Definitions.Unix.file_object)
            {
                filePath = (EntityObjectStringType)((OVAL.Definitions.Unix.file_object)objectType).GetItemValue(OVAL.Definitions.Unix.ItemsChoiceType3.filepath);
                path = GetUnixPath(filePath.Value);
                fileName = GetUnixFilename(filePath.Value);
            }

            if (filePath == null || filePath.Value == null)
                return itemTypes;
            
            var filePathInformation = new FilePathRegexInformation(path);
            var operationForPath = filePath.operation;
            if (this.operatorHelper.IsRegularExpression(operationForPath))
            {
                if (filePathInformation.GetPathWithFirstRegex() == null)
                    operationForPath = OperationEnumeration.equals;
            }
            else if (this.operatorHelper.IsNotEqualsOperation(operationForPath))
                operationForPath = OperationEnumeration.equals;
            
            var pathEntity = new EntityObjectStringType() { Value = path, operation = operationForPath };
            var fileNameEntity = new EntityObjectStringType() { Value = fileName, operation = filePath.operation };
            var fileEntities = new Dictionary<string, EntityObjectStringType>();
            fileEntities.Add("path", pathEntity);
            fileEntities.Add("filename", fileNameEntity);
            var paths = this.ProcessOperationsPaths(fileEntities);
            var fileNames = this.ProcessOperationFileName(fileEntities, paths, true);

            var family = GetFamilyEnumerationForObjectType(objectType);
            return new FileItemTypeFactory(family).CreateFileItemTypesWithFilePathsByCombinationFrom(paths, fileNames);
        }
示例#15
0
        private IEnumerable <string> SubstitutionValueInPattern(List <string> values, PathLevelWithRegex basePath, FilePathRegexInformation regexInformation)
        {
            List <string> paths = new List <string>();

            foreach (string path in values)
            {
                string concatenatedString = regexInformation.ConcatPathWithNextLeveRegex(path, basePath.Level);
                if (!string.IsNullOrEmpty(concatenatedString))
                {
                    paths.Add(concatenatedString);
                }
            }
            return(paths);
        }