def testUploadArtifactYamlFileAndDumpToYaml(self): artifact_registry.REGISTRY.ClearRegistry() artifact_registry.REGISTRY.ClearSources() artifact_registry.REGISTRY._CheckDirty() try: test_artifacts_file = os.path.join(config.CONFIG["Test.data_dir"], "artifacts", "test_artifacts.json") filecontent = open(test_artifacts_file, "rb").read() artifact.UploadArtifactYamlFile(filecontent, token=self.token) loaded_artifacts = artifact_registry.REGISTRY.GetArtifacts() self.assertEqual(len(loaded_artifacts), 18) self.assertIn("DepsWindirRegex", [a.name for a in loaded_artifacts]) # Now dump back to YAML. yaml_data = artifact_registry.REGISTRY.DumpArtifactsToYaml() for snippet in [ "name: TestFilesArtifact", "urls: ['https://msdn.microsoft.com/en-us/library/aa384749%28v=vs.85", "returned_types: [SoftwarePackage]", "args: [--list]", "cmd: /usr/bin/dpkg", ]: self.assertIn(snippet, yaml_data) finally: artifact.ArtifactLoader().RunOnce()
def testGetKBDependencies(self): """Test that KB dependencies are calculated correctly.""" self.SetupWindowsMocks() artifact_registry.REGISTRY.ClearSources() try: test_artifacts_file = os.path.join( config_lib.CONFIG["Test.data_dir"], "artifacts", "test_artifacts.json") artifact_registry.REGISTRY.AddFileSource(test_artifacts_file) with test_lib.ConfigOverrider({ "Artifacts.knowledge_base": [ "DepsParent", "DepsDesktop", "DepsHomedir", "DepsWindir", "DepsWindirRegex", "DepsControlSet", "FakeArtifact" ], "Artifacts.knowledge_base_additions": ["DepsHomedir2"], "Artifacts.knowledge_base_skip": ["DepsWindir"], "Artifacts.knowledge_base_heavyweight": ["FakeArtifact"] }): args = artifact.KnowledgeBaseInitializationArgs( lightweight=True) kb_init = artifact.KnowledgeBaseInitializationFlow( None, token=self.token) kb_init.args = args kb_init.state.Register("all_deps", set()) kb_init.state.Register("awaiting_deps_artifacts", []) kb_init.state.Register("knowledge_base", rdf_client.KnowledgeBase(os="Windows")) no_deps = kb_init.GetFirstFlowsForCollection() self.assertItemsEqual(no_deps, ["DepsControlSet", "DepsHomedir2"]) self.assertItemsEqual(kb_init.state.all_deps, [ "users.homedir", "users.desktop", "users.username", "environ_windir", "current_control_set" ]) self.assertItemsEqual(kb_init.state.awaiting_deps_artifacts, [ "DepsParent", "DepsDesktop", "DepsHomedir", "DepsWindirRegex" ]) finally: artifact.ArtifactLoader().RunOnce()
def testGetDependencies(self): """Test that dependencies are calculated correctly.""" self.SetupWindowsMocks() artifact_registry.REGISTRY.ClearSources() try: test_artifacts_file = os.path.join( config_lib.CONFIG["Test.data_dir"], "artifacts", "test_artifacts.json") artifact_registry.REGISTRY.AddFileSource(test_artifacts_file) # No dependencies args = artifact.CollectArtifactDependenciesArgs( artifact_list=["DepsHomedir2"]) collect_obj = artifact.CollectArtifactDependencies( None, token=self.token) collect_obj.args = args collect_obj.knowledge_base = None collect_obj.state.Register("all_deps", set()) collect_obj.state.Register("awaiting_deps_artifacts", []) collect_obj.state.Register("knowledge_base", rdf_client.KnowledgeBase(os="Windows")) no_deps = collect_obj.GetFirstFlowsForCollection() self.assertItemsEqual(no_deps, []) self.assertItemsEqual(collect_obj.state.all_deps, []) self.assertItemsEqual(collect_obj.state.awaiting_deps_artifacts, []) # Dependency tree with a single starting point args = artifact.CollectArtifactDependenciesArgs( artifact_list=["DepsHomedir"]) collect_obj.args = args no_deps = collect_obj.GetFirstFlowsForCollection() self.assertItemsEqual(no_deps, ["DepsControlSet"]) self.assertItemsEqual( collect_obj.state.all_deps, ["environ_windir", "users.username", "current_control_set"]) self.assertItemsEqual(collect_obj.state.awaiting_deps_artifacts, ["DepsWindir", "DepsWindirRegex"]) finally: artifact.ArtifactLoader().RunOnce()
def tearDown(self): super(TestArtifactRender, self).tearDown() artifact.ArtifactLoader().RunOnce()