Unverified Commit e58af70e by Steven Committed by GitHub

Add namespace, rule, and string to warning callback test (#182)

parent 17979fe3
#!/usr/local/bin/python #!/usr/local/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (c) 2007-2014. The YARA Authors. All Rights Reserved. # Copyright (c) 2007-2021. The YARA Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -75,7 +75,6 @@ ab000000000000001a0000000000000000000000000000000100000000000000\ ...@@ -75,7 +75,6 @@ ab000000000000001a0000000000000000000000000000000100000000000000\
# The 3 possible outcomes for each pattern # The 3 possible outcomes for each pattern
[SUCCEED, FAIL, SYNTAX_ERROR] = range(3) [SUCCEED, FAIL, SYNTAX_ERROR] = range(3)
RE_TESTS = [ RE_TESTS = [
# RE, string, expected result, expected matching # RE, string, expected result, expected matching
...@@ -268,8 +267,9 @@ RE_TESTS = [ ...@@ -268,8 +267,9 @@ RE_TESTS = [
def warnings_callback(warning_type, message): def warnings_callback(warning_type, message):
global warnings_callback_called global warnings_callback_called, warnings_callback_message
warnings_callback_called = warning_type warnings_callback_called = warning_type
warnings_callback_message = message
class TestYara(unittest.TestCase): class TestYara(unittest.TestCase):
...@@ -876,11 +876,11 @@ class TestYara(unittest.TestCase): ...@@ -876,11 +876,11 @@ class TestYara(unittest.TestCase):
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
self.assertTrue(yara.compile( self.assertTrue(yara.compile(
source="rule test { condition: true}", source="rule test { condition: true}",
externals={'foo': u'\u6765\u6613\u7f51\u7edc\u79d1' })) externals={'foo': u'\u6765\u6613\u7f51\u7edc\u79d1'}))
else: else:
self.assertRaises(UnicodeEncodeError, yara.compile, self.assertRaises(UnicodeEncodeError, yara.compile,
source="rule test { condition: true}", source="rule test { condition: true}",
externals={'foo': u'\u6765\u6613\u7f51\u7edc\u79d1' }) externals={'foo': u'\u6765\u6613\u7f51\u7edc\u79d1'})
def testCallbackAll(self): def testCallbackAll(self):
global rule_data global rule_data
...@@ -891,7 +891,6 @@ class TestYara(unittest.TestCase): ...@@ -891,7 +891,6 @@ class TestYara(unittest.TestCase):
rule_data.append(data) rule_data.append(data)
return yara.CALLBACK_CONTINUE return yara.CALLBACK_CONTINUE
r = yara.compile(source='rule t { condition: true } rule f { condition: false }') r = yara.compile(source='rule t { condition: true } rule f { condition: false }')
r.match(data='dummy', callback=callback, which_callbacks=yara.CALLBACK_ALL) r.match(data='dummy', callback=callback, which_callbacks=yara.CALLBACK_ALL)
...@@ -1099,14 +1098,23 @@ class TestYara(unittest.TestCase): ...@@ -1099,14 +1098,23 @@ class TestYara(unittest.TestCase):
self.assertTrue(r.match(data=data)) self.assertTrue(r.match(data=data))
def testWarningCallback(self): def testWarningCallback(self):
global warnings_callback_called global warnings_callback_called, warnings_callback_message
warnings_callback_called = False warnings_callback_called = False
r = yara.compile(source='rule x { strings: $x = "X" condition: $x }') warnings_callback_message = None
r = yara.compile(sources={'ns1': 'rule x { strings: $x = "X" condition: $x }'})
data = memoryview(b"X" * 1000099) data = memoryview(b"X" * 1000099)
r.match(data=data, warnings_callback=warnings_callback) r.match(data=data, warnings_callback=warnings_callback)
self.assertTrue(warnings_callback_called, yara.CALLBACK_TOO_MANY_MATCHES) self.assertTrue(warnings_callback_called == yara.CALLBACK_TOO_MANY_MATCHES)
self.assertTrue(warnings_callback_message == ("ns1", "x", "$x"))
self.assertTrue(warnings_callback_message.namespace == "ns1")
self.assertTrue(warnings_callback_message.rule == "x")
self.assertTrue(warnings_callback_message.string == "$x")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment