Пример #1
0
        private FileProber GetMockedFileProber(file_item fakeItem)
        {
            IList <String> fakeValues        = new List <String>(new string[] { "FakeValue" });
            CollectedItem  fakeCollectedItem = ProbeHelper.CreateFakeCollectedItem(fakeItem);

            MockRepository            mocks                 = new MockRepository();
            IConnectionManager        fakeConnection        = mocks.DynamicMock <IConnectionManager>();
            ISystemInformationService fakeSystemInformation = mocks.DynamicMock <ISystemInformationService>();
            FileConnectionProvider    fakeProvider          = mocks.DynamicMock <FileConnectionProvider>();
            WmiDataProvider           fakeWmiProvider       = mocks.DynamicMock <WmiDataProvider>();
            FileObjectCollector       fakeDataCollector     = mocks.DynamicMock <FileObjectCollector>();

            fakeDataCollector.WmiDataProvider = fakeWmiProvider;

            //Expect.Call(fakeConnection.Connect<FileConnectionProvider>(null, null)).IgnoreArguments().Repeat.Any().Return(fakeProvider);
            Expect.Call(fakeDataCollector.CollectDataForSystemItem(fakeItem)).IgnoreArguments().Repeat.Any().Return(new List <CollectedItem>()
            {
                fakeCollectedItem
            });
            Expect.Call(fakeDataCollector.GetValues(null)).IgnoreArguments().Repeat.Any().Return(fakeValues);
            Expect.Call(fakeSystemInformation.GetSystemInformationFrom(null)).IgnoreArguments().Return(SystemInformationFactory.GetExpectedSystemInformation());
            mocks.ReplayAll();

            return(new FileProber()
            {
                ConnectionManager = fakeConnection, ObjectCollector = fakeDataCollector
            });
        }
Пример #2
0
        private string GetCompleteFilepath(file_item fileItem)
        {
            if (fileItem.filepath != null && !string.IsNullOrWhiteSpace(fileItem.filepath.Value))
            {
                return(fileItem.filepath.Value.Trim());
            }

            var path          = fileItem.path.Value.TrimStart();
            var pathSeparator = path.EndsWith("/") ? string.Empty : "/";

            var filename = string.Empty;

            if ((fileItem.filename != null) && (!string.IsNullOrWhiteSpace(fileItem.filename.Value)))
            {
                filename = fileItem.filename.Value;
            }

            var completeFilepath = String.Format("{0}{1}{2}", path, pathSeparator, filename);

            if (!completeFilepath.StartsWith("/"))
            {
                completeFilepath = "/" + completeFilepath;
            }
            return(completeFilepath);
        }
Пример #3
0
        private void AssertCollectedFileItems(CollectedObject collectedObject, WmiObject[] expectedFiles)
        {
            string UNEXPECTED_ITEM_ATTRIBUTE_VALUE = "Unexpected value for a file_item attribute was found: {0}";

            SystemCharacteristics::ReferenceType[] objectReferences = collectedObject.ObjectType.reference.ToArray();
            IList <ItemType> fileItems = (IList <ItemType>)collectedObject.SystemData;

            Assert.AreEqual(expectedFiles.Count(), objectReferences.Count(), "Unexpected number of item references was found.");
            Assert.AreEqual(objectReferences.Count(), fileItems.Count, "Unexpected number of generated items type was found.");

            int i = 0;

            foreach (var expectedFile in expectedFiles)
            {
                Dictionary <string, object> fileItemFields = expectedFile.GetValues();
                Assert.IsInstanceOfType(fileItems[i], typeof(file_item), "The generated ItemType must be a instance of file_item class.");

                file_item fileItem = (file_item)fileItems[i];
                Assert.AreEqual(objectReferences[i].item_ref, fileItem.id, "The generated ItemType ID must be equal to collected object ID.");
                Assert.AreEqual(StatusEnumeration.exists, fileItem.status, "A generated ItemType with unexpected OVAL Status was found.");
                Assert.AreEqual(fileItemFields[FILE_SIZE].ToString(), fileItem.size.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, FILE_SIZE));
                Assert.AreEqual(ConvertWmiTimeToFileTime(fileItemFields[C_DATE].ToString()), fileItem.c_time.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, C_DATE));
                Assert.AreEqual(ConvertWmiTimeToFileTime(fileItemFields[M_DATE].ToString()), fileItem.m_time.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, M_DATE));
                Assert.AreEqual(ConvertWmiTimeToFileTime(fileItemFields[A_DATE].ToString()), fileItem.a_time.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, A_DATE));
                Assert.AreEqual(fileItemFields[VERSION], fileItem.version.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, VERSION));
                Assert.AreEqual(fileItemFields[FILE_TYPE], fileItem.type.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, FILE_SIZE));
                Assert.AreEqual(fileItemFields[COMPANY], fileItem.company.Value, string.Format(UNEXPECTED_ITEM_ATTRIBUTE_VALUE, COMPANY));

                i++;
            }
        }
Пример #4
0
        private FileItemSystemData CollectFileItemSystemData(file_item fileItem)
        {
            WmiObject wmiLogicalFile = this.OpenWmiLogicalFileClass(fileItem);
            string    fullFilePath   = this.GetCompleteFilePath(fileItem);


            var fileItemSystemData = new FileItemSystemData();

            this.FillFileItemSystemData(wmiLogicalFile.GetValues(), fileItemSystemData);
            return(fileItemSystemData);
        }
Пример #5
0
 private string ConcatFilePathAndFileName(file_item fileItem)
 {
     if (fileItem.path.Value.EndsWith("\\"))
     {
         return(string.Format("{0}{1}", fileItem.path.Value, fileItem.filename.Value));
     }
     else
     {
         return(string.Format("{0}\\{1}", fileItem.path.Value, fileItem.filename.Value));
     }
 }
Пример #6
0
        private file_item CreateFakeFileItem(string filepath, string path, string filename)
        {
            string fullFilePath        = filepath;
            string collectSuccessfully = "The File, which fullPath is '{0}', was collected sucessfully.";

            file_item newFileItem = new file_item();

            newFileItem.status  = StatusEnumeration.exists;
            newFileItem.message = MessageType.FromString(string.Format(collectSuccessfully, fullFilePath));

            return(newFileItem);
        }
Пример #7
0
        private string GetCompleteFilePath(file_item fileItem)
        {
            bool   isFilePathDefined = ((fileItem.filepath != null) && (!string.IsNullOrEmpty(fileItem.filepath.Value)));
            string completeFilePath  = isFilePathDefined ? fileItem.filepath.Value : ConcatFilePathAndFileName(fileItem);

            if (string.IsNullOrEmpty(Path.GetExtension(completeFilePath)) && (!completeFilePath[completeFilePath.Length - 1].Equals(@"\")))
            {
                return(string.Format(@"{0}\", completeFilePath));
            }

            return(completeFilePath);
        }
Пример #8
0
        private void AssertGeneratedFileItem(ItemType generatedItem, string expectedFilePath, string expectedPath, string expectedFileName)
        {
            Assert.IsInstanceOfType(generatedItem, typeof(file_item), "The type of generated File Item must be 'file_item'");

            file_item fileItem = (file_item)generatedItem;

            if (string.IsNullOrEmpty(expectedFilePath))
            {
                Assert.AreEqual(expectedPath, fileItem.path.Value, "A generated File Item with an unexpected 'path' value was found.");
                Assert.AreEqual(expectedFileName, fileItem.filename.Value, "A generated File Item with an unexpected 'filename' value was found.");
                return;
            }
            Assert.AreEqual(expectedFilePath, fileItem.filepath.Value, "A generated File Item with an unexpected 'filepath' value was found.");
        }
Пример #9
0
        public void Should_be_possible_to_collect_a_fileItem_without_filename_information()
        {
            var fileItem =
                new file_item()
            {
                path     = OvalHelper.CreateItemEntityWithStringValue("c:\\windows"),
                filename = OvalHelper.CreateItemEntityWithStringValue(string.Empty)
            };
            var fileObjectCollector =
                new FileObjectCollector()
            {
                WmiDataProvider = GetMockedWmiDataProvider()
            };

            var collectedObjects = fileObjectCollector.CollectDataForSystemItem(fileItem);
        }
Пример #10
0
        protected override ItemType CreateFileItem(string fullFilePath, bool containsFilePathEntity)
        {
            var newFileItem = new file_item()
            {
                filepath = new EntityItemStringType()
                {
                    Value = fullFilePath
                }
            };

            if (!containsFilePathEntity)
            {
                ((file_item)newFileItem).filepath = null;
                ((file_item)newFileItem).path     = base.CreateEntityItemWithValue(Path.GetDirectoryName(fullFilePath));
                ((file_item)newFileItem).filename = this.CreateEntityItemWithValue(Path.GetFileName(fullFilePath));
            }

            return(newFileItem);
        }
Пример #11
0
        public void FillItemTypeWithData(file_item newFileItem, object collectedData)
        {
            FileItemSystemData fileSystemData = (FileItemSystemData)collectedData;

            newFileItem.owner       = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.Owner);
            newFileItem.size        = OvalHelper.CreateItemEntityWithIntegerValue(fileSystemData.Size.ToString());
            newFileItem.a_time      = OvalHelper.CreateItemEntityWithIntegerValue(fileSystemData.ATime.ToString());
            newFileItem.c_time      = OvalHelper.CreateItemEntityWithIntegerValue(fileSystemData.CTime.ToString());
            newFileItem.m_time      = OvalHelper.CreateItemEntityWithIntegerValue(fileSystemData.MTime.ToString());
            newFileItem.ms_checksum = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.MS_Checksum);
            newFileItem.version     = OvalHelper.CreateVersionItemTypeWithValue(fileSystemData.Version);
            newFileItem.type        = new EntityItemFileTypeType()
            {
                Value = fileSystemData.Type
            };
            newFileItem.development_class = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.DevelopmentClass);
            newFileItem.company           = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.Company);
            newFileItem.internal_name     = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.InternalName);
            newFileItem.language          = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.Language);
            newFileItem.original_filename = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.OriginalFilename);
            newFileItem.product_name      = OvalHelper.CreateItemEntityWithStringValue(fileSystemData.ProductName);
            newFileItem.product_version   = OvalHelper.CreateVersionItemTypeWithValue(fileSystemData.ProductVersion);
        }
Пример #12
0
        public void Should_be_possible_to_execute_a_simple_file_collect()
        {
            // Arrange
            file_item   fakeFileItem    = this.CreateFakeFileItem("c:\\windows\\ODBC.ini", null, null);
            CollectInfo fakeCollectInfo = this.getFakeCollectInfo("definitionsWithOnlyObjects.xml");
            FileProber  fileProber      = this.GetMockedFileProber(fakeFileItem);

            // Act
            ProbeResult collectResult = fileProber.Execute(fakeContext, fakeTargetInfo, fakeCollectInfo);

            // Assert
            Assert.IsNotNull(collectResult, "The result of probe execution cannot be null.");
            Assert.IsNotNull(collectResult.CollectedObjects, "There are no collected objects.");
            Assert.AreEqual(2, collectResult.CollectedObjects.Count(), "Unexpected quantity of collected objects");

            CollectedObject fileObjectToAssert = collectResult.CollectedObjects.ElementAt(0);

            Assert.IsNotNull(fileObjectToAssert.ObjectType, "The first collected object cannot null.");
            Assert.AreEqual("oval:modulo:obj:9000", fileObjectToAssert.ObjectType.id, "The identificator of [collected object] and [definitions object] must be equal.");
            Assert.AreEqual(FlagEnumeration.complete, fileObjectToAssert.ObjectType.flag, "A successfully gathering must generate a collected object with flag equal to 'complete'.");
            Assert.IsNotNull(fileObjectToAssert.ObjectType.reference, "A successfully gathering must generate at least one item referenced");
            Assert.AreEqual(1, fileObjectToAssert.ObjectType.reference.Count(), "Unexpected number of generated items for this object collected");
            Assert.AreEqual(fileObjectToAssert.SystemData.Count(), fileObjectToAssert.ObjectType.reference.Count(), "The number of referenced items in collected object should be equal to generated items quantity.");
        }
Пример #13
0
        private WmiObject OpenWmiLogicalFileClass(file_item fileItem)
        {
            var fullFilePath = this.GetCompleteFilePath(fileItem);
            var isDirectory  =
                ((fileItem.path != null) &&
                 ((fileItem.filename == null) ||
                  (string.IsNullOrWhiteSpace(fileItem.filename.Value))));

            IEnumerable <WmiObject> result = null;

            if (isDirectory)
            {
                fullFilePath = fullFilePath.Replace("\\\\", "\\");
                var wql = GetWqlForSearchDirectory(fullFilePath);
                result = this.WmiDataProvider.ExecuteWQL(wql);
            }
            else
            {
                var wmiInParameters = this.CreateWmiParameters(fullFilePath);
                result = this.WmiDataProvider.SearchWmiObjects(WMI_FILE_CLASS, wmiInParameters);
            }

            return(ExtractResultFromWmiReturn(result));
        }
Пример #14
0
 private void AssertPathAndFileNameEntitiesExistence(file_item fileItem)
 {
     Assert.IsFalse(IsItemEntityDefined(fileItem.filepath), string.Format(INVALID_FILEITEM_FORMAT_ERROR_MSG, "<path> and <filename>", "<filepath>"));
     Assert.IsTrue(IsItemEntityDefined(fileItem.path), string.Format(FILEITEM_ENTITY_NOT_FOUND, "<filepath>"));
     Assert.IsTrue(IsItemEntityDefined(fileItem.filename), string.Format(FILEITEM_ENTITY_NOT_FOUND, "<filename>"));
 }