def YamlLoader(string): """Load an AFF4 object from a serialized YAML representation.""" representation = yaml.Parse(string) result_cls = aff4.FACTORY.AFF4Object(representation["aff4_class"]) aff4_attributes = {} for predicate, values in iteritems(representation["attributes"]): attribute = aff4.Attribute.PREDICATES[predicate] tmp = aff4_attributes[attribute] = [] for rdfvalue_cls_name, value, age in values: rdfvalue_cls = aff4.FACTORY.RDFValue(rdfvalue_cls_name) value = rdfvalue_cls(value, age=rdfvalue.RDFDatetime(age)) tmp.append(value) # Ensure the object is dirty so when we save it, it can be written to the data # store. result = result_cls(urn=representation["_urn"], clone=aff4_attributes, mode="rw", age=representation["age_policy"]) result.new_attributes, result.synced_attributes = result.synced_attributes, {} result._dirty = True # pylint: disable=protected-access return result
def testUnicodeTags(self): parsed = yaml.Parse(""" !!python/unicode żółć: !!python/unicode jaźń !!python/unicode kość: !!python/unicode łoś """) expected = { "żółć": "jaźń", "kość": "łoś", } self.assertEqual(parsed, expected)
def testRepackAll(self): """Test repacking all binaries.""" self.executables_dir = package.ResourcePath("grr-response-core", "executables") with utils.TempDirectory() as tmp_dir: new_dir = os.path.join(tmp_dir, "grr", "executables") os.makedirs(new_dir) # Copy unzipsfx so it can be used in repacking/ shutil.copy( os.path.join(self.executables_dir, "windows/templates/unzipsfx/unzipsfx-i386.exe"), new_dir) shutil.copy( os.path.join(self.executables_dir, "windows/templates/unzipsfx/unzipsfx-amd64.exe"), new_dir) with test_lib.ConfigOverrider({ "ClientBuilder.executables_dir": new_dir, "ClientBuilder.unzipsfx_stub_dir": new_dir }): repacking.TemplateRepacker().RepackAllTemplates() self.assertEqual( len(glob.glob(os.path.join(new_dir, "installers/*.deb"))), 2) self.assertEqual( len(glob.glob(os.path.join(new_dir, "installers/*.rpm"))), 2) self.assertEqual( len(glob.glob(os.path.join(new_dir, "installers/*.exe"))), 4) self.assertEqual( len(glob.glob(os.path.join(new_dir, "installers/*.pkg"))), 1) # Validate the config appended to the OS X package. zf = zipfile.ZipFile(glob.glob( os.path.join(new_dir, "installers/*.pkg")).pop(), mode="r") fd = zf.open("config.yaml") # We can't load the included build.yaml because the package hasn't been # installed. loaded = yaml.Parse(fd.read().decode("utf-8")) loaded.pop("Config.includes") packaged_config = config.CONFIG.MakeNewConfig() data = yaml.Dump(loaded) packaged_config.Initialize(parser=config_lib.YamlParser, data=data) packaged_config.Validate( sections=build.ClientRepacker.CONFIG_SECTIONS) repacker = build.ClientRepacker() repacker.ValidateEndConfig(packaged_config)
def testUnicode(self): parsed = yaml.Parse(""" gęsi: - zbożowa - krótkodzioba - białoczelna grzebiące: - jarząbek - głuszec - bażant """) expected = { "gęsi": ["zbożowa", "krótkodzioba", "białoczelna"], "grzebiące": ["jarząbek", "głuszec", "bażant"], } self.assertEqual(parsed, expected)
def testComplexDict(self): parsed = yaml.Parse(""" foo.bar: quux: [4, 8, 15, 16, 23, 42] thud: - blargh - norf foo.baz: - 3.14 - 1.62 """) expected = { "foo.bar": { "quux": [4, 8, 15, 16, 23, 42], "thud": ["blargh", "norf"], }, "foo.baz": [3.14, 1.62], } self.assertEqual(parsed, expected)
def testWriteBuildYaml(self): """Test build.yaml is output correctly.""" context = [ "Target:LinuxDeb", "Platform:Linux", "Target:Linux", "Arch:amd64" ] expected = { "Client.build_environment": "cp27-cp27mu-linux_x86_64", "Client.build_time": "2016-05-24 20:04:25", "Template.build_type": "Release", "Template.build_context": ["ClientBuilder Context"] + context, "Template.version_major": str(config.CONFIG.Get("Source.version_major")), "Template.version_minor": str(config.CONFIG.Get("Source.version_minor")), "Template.version_revision": str(config.CONFIG.Get("Source.version_revision")), "Template.version_release": str(config.CONFIG.Get("Source.version_release")), "Template.arch": u"amd64" } # TODO(hanuszczak): YAML, consider using `StringIO` instead. fd = io.BytesIO() builder = build.ClientBuilder(context=context) with mock.patch.object(rdf_client.Uname, "FromCurrentSystem") as fcs: fcs.return_value.signature.return_value = "cp27-cp27mu-linux_x86_64" with test_lib.FakeTime(1464120265): builder.WriteBuildYaml(fd) fd.seek(0) self.assertEqual(yaml.Parse(fd.getvalue().decode("utf-8")), expected)
def testStringsAreUnicodeObjects(self): self.assertIsInstance(yaml.Parse("\"foo\""), Text)
def testSimpleDict(self): parsed = yaml.Parse("{ 'foo': 'bar', 'quux': 42 }") expected = {"foo": "bar", "quux": 42} self.assertEqual(parsed, expected)