Skip to content

Commit

Permalink
python: Expose tokens for Meta object
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejKastak committed Mar 22, 2021
1 parent 974ba1e commit 60a7827
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/python/yaramod_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,13 @@ void addBasicClasses(py::module& module)

py::class_<Meta>(module, "Meta")
.def_property("key", &Meta::getKey, &Meta::setKey)
.def_property("value", &Meta::getValue, &Meta::setValue);
.def_property("value", &Meta::getValue, &Meta::setValue)
.def_property_readonly("token_key", [](Meta& self) {
return *self.getKeyTokenIt();
})
.def_property_readonly("token_value", [](Meta& self) {
return *self.getValueTokenIt();
});

py::class_<Variable>(module, "Variable")
.def_property("key", &Variable::getKey, &Variable::setKey)
Expand Down
43 changes: 43 additions & 0 deletions tests/python/test_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,46 @@ def test_get_tokenstream_after_unknown_module_error(self):
'\n',
'rule'
])

def test_meta_values_interface(self):
input_text = """rule test {
meta:
author = "Name Surname"
description = "Test checking the meta value tokens"
condition:
false
}
"""
ymod = yaramod.Yaramod()
yfile = ymod.parse_string(input_text)
self.assertEqual(len(yfile.rules[0].metas), 2)

meta = yfile.rules[0].metas[0] # author
self.assertTrue(hasattr(meta, "token_key"))
token = meta.token_key
self.assertEqual(token.location.begin.line, 3)
self.assertEqual(token.location.begin.column, 9)
self.assertEqual(token.location.end.line, 3)
self.assertEqual(token.location.end.column, 14)

self.assertTrue(hasattr(meta, "token_value"))
token = meta.token_value
self.assertEqual(token.location.begin.line, 3)
self.assertEqual(token.location.begin.column, 18)
self.assertEqual(token.location.end.line, 3)
self.assertEqual(token.location.end.column, 31)

meta = yfile.rules[0].metas[1] # description
self.assertTrue(hasattr(meta, "token_key"))
token = meta.token_key
self.assertEqual(token.location.begin.line, 4)
self.assertEqual(token.location.begin.column, 9)
self.assertEqual(token.location.end.line, 4)
self.assertEqual(token.location.end.column, 19)

self.assertTrue(hasattr(meta, "token_value"))
token = meta.token_value
self.assertEqual(token.location.begin.line, 4)
self.assertEqual(token.location.begin.column, 23)
self.assertEqual(token.location.end.line, 4)
self.assertEqual(token.location.end.column, 59)

0 comments on commit 60a7827

Please sign in to comment.