Added tests, fixed errors

This commit is contained in:
Pawel Srokosz
2019-04-01 17:13:25 +02:00
parent 3c1e835b78
commit 2eba200738
6 changed files with 35 additions and 10 deletions
+2 -2
View File
@@ -35,7 +35,7 @@ def enumerate_plugins(dirpath, module_prefix, namespace, class_,
if os.path.isfile(dirpath):
dirpath = os.path.dirname(dirpath)
for _, module_name, _ in pkgutil.iter_modules(dirpath, module_prefix):
for _, module_name, _ in pkgutil.iter_modules([dirpath], module_prefix+"."):
try:
importlib.import_module(module_name)
except ImportError as e:
@@ -70,7 +70,7 @@ def enumerate_plugins(dirpath, module_prefix, namespace, class_,
if as_dict:
ret = {}
for plugin in plugins:
plugin_module = plugin.__module__.split(".")[len(module_prefix) + 1:]
plugin_module = plugin.__module__[len(module_prefix) + 1:]
ret[plugin_module] = plugin
return ret
+2 -1
View File
@@ -3,6 +3,7 @@
# See the file 'docs/LICENSE' for copying permission.
from . import sig1, sig2, sig3
from .sigsub import sigsub1, sigsub2
class meta:
plugins = sig1.Sig1, sig2.Sig2, sig3.Sig3
plugins = sig1.Sig1, sig2.Sig2, sig3.Sig3, sigsub1.Sigsub1, sigsub2.Sigsub2
@@ -0,0 +1,6 @@
# Copyright (C) 2019 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
from .sigsub1 import Sigsub1
from .sigsub2 import Sigsub2
@@ -0,0 +1,8 @@
# Copyright (C) 2019 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
from cuckoo.common.abstracts import Signature
class Sigsub1(Signature):
name = "sigsub1"
@@ -0,0 +1,8 @@
# Copyright (C) 2019 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
from .sigsub1 import Sigsub1
class Sigsub2(Sigsub1):
name = "sigsub2"
+9 -7
View File
@@ -24,16 +24,14 @@ def test_enumerate_plugins():
)
sys.path.pop(0)
assert len(plugins) == 3
assert plugins[0].name == "sig1"
assert plugins[1].name == "sig2"
assert plugins[2].name == "sig3"
assert plugins[0].foo == "bar"
assert plugins[1].foo == "bar"
assert plugins[2].foo == "bar"
assert len(plugins) == 5
assert [plugin.name for plugin in plugins] == ["sig1", "sig2", "sig3", "sigsub1", "sigsub2"]
assert all([plugin.foo == "bar" for plugin in plugins])
assert issubclass(sys.modules["enumplugins"].sig1.Sig1, Signature)
assert issubclass(sys.modules["enumplugins"].sig2.Sig2, Signature)
assert issubclass(sys.modules["enumplugins"].sig3.Sig3, Signature)
assert issubclass(sys.modules["enumplugins"].sigsub.Sigsub1, Signature)
assert issubclass(sys.modules["enumplugins"].sigsub.Sigsub2, Signature)
def test_load_signatures():
set_cwd(tempfile.mkdtemp())
@@ -51,6 +49,8 @@ def test_load_signatures():
assert "signatures.sig1" in names
assert "signatures.sig2" in names
assert "signatures.sig3" in names
assert "signatures.sigsub.sigsub1" in names
assert "signatures.sigsub.sigsub2" in names
# Ensure that the Signatures are loaded in the RunSignatures object.
RunSignatures.init_once()
@@ -60,6 +60,8 @@ def test_load_signatures():
assert "Sig1" in names
assert "Sig2" in names
assert "Sig3" in names
assert "Sigsub1" in names
assert "Sigsub2" in names
def test_libvirt_loaded():
"""KVM is a subclass of LibVirtMachine, which is now autoloaded as well."""