Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

SpiderFootPlugin: Add get/set Target get/set ScanId error handling

Brendan Coles5027a57669ed
diff --git a/sflib.py b/sflib.py
index a39b4266..059aa11f 100644
--- a/sflib.py
+++ b/sflib.py
@@ -2424,8 +2424,11 @@ class SpiderFootPlugin(object):
"""Assigns the current target this module is acting against.
Args:
- target (str): target
+ target (SpiderFootTarget): target
"""
+ if not isinstance(target, SpiderFootTarget):
+ raise TypeError("target is %s; expected SpiderFootTarget" % type(target))
+
self._currentTarget = target
def setDbh(self, dbh):
@@ -2437,13 +2440,16 @@ class SpiderFootPlugin(object):
"""
self.__sfdb__ = dbh
- def setScanId(self, id):
+ def setScanId(self, scanId):
"""Set the scan ID.
Args:
id (str): scan ID
"""
- self.__scanId__ = id
+ if not isinstance(scanId, str):
+ raise TypeError("scanId is %s; expected str" % type(scanId))
+
+ self.__scanId__ = scanId
def getScanId(self):
"""Get the scan ID.
@@ -2451,14 +2457,16 @@ class SpiderFootPlugin(object):
Returns:
str: scan ID
"""
+ if not self.__scanId__:
+ raise TypeError("Module called getScanId() but no scanId is set.")
+
return self.__scanId__
def getTarget(self):
"""Gets the current target this module is acting against."""
+ if not self._currentTarget:
+ raise TypeError("Module called getTarget() but no target is set.")
- if self._currentTarget is None:
- print("Internal Error: Module called getTarget() but no target set.")
- sys.exit(-1)
return self._currentTarget
def registerListener(self, listener):
diff --git a/test/unit/test_spiderfootplugin.py b/test/unit/test_spiderfootplugin.py
index 564d9be0..18dd2c0b 100644
--- a/test/unit/test_spiderfootplugin.py
+++ b/test/unit/test_spiderfootplugin.py
@@ -1,6 +1,6 @@
# test_spiderfootplugin.py
import sflib
-from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
+from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent, SpiderFootTarget
from sfdb import SpiderFootDb
import unittest
@@ -87,12 +87,24 @@ class TestSpiderFootPlugin(unittest.TestCase):
"""
sfp = SpiderFootPlugin()
- target = 'spiderfoot.net'
+ target = SpiderFootTarget("spiderfoot.net", "INTERNET_NAME")
sfp.setTarget(target)
- get_target = sfp.getTarget()
+ get_target = sfp.getTarget().targetValue
self.assertIsInstance(get_target, str)
- self.assertEqual(target, get_target)
+ self.assertEqual("spiderfoot.net", get_target)
+
+ def test_set_target_invalid_target_should_raise(self):
+ """
+ Test setTarget(self, target)
+ """
+ sfp = SpiderFootPlugin()
+
+ invalid_types = [None, "", list(), dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError) as cm:
+ sfp.setTarget(invalid_type)
def test_set_dbh(self):
"""
@@ -104,14 +116,18 @@ class TestSpiderFootPlugin(unittest.TestCase):
sfp.setDbh(sfdb)
self.assertIsInstance(sfp.__sfdb__, SpiderFootDb)
- def test_set_scan_id(self):
+ def test_set_scan_id_should_set_a_scan_id(self):
"""
Test setScanId(self, id)
"""
sfp = SpiderFootPlugin()
- sfp.setScanId(None)
- self.assertEqual('TBD', 'TBD')
+ scan_id = '1234'
+ sfp.setScanId(scan_id)
+
+ get_scan_id = sfp.getScanId()
+ self.assertIsInstance(get_scan_id, str)
+ self.assertEqual(scan_id, get_scan_id)
def test_get_scan_id_should_return_a_string(self):
"""
@@ -126,29 +142,36 @@ class TestSpiderFootPlugin(unittest.TestCase):
self.assertEqual(str, type(get_scan_id))
self.assertEqual(scan_id, get_scan_id)
+ def test_get_scan_id_unitialised_scanid_should_raise(self):
+ """
+ Test getScanId(self)
+ """
+ sfp = SpiderFootPlugin()
+
+ with self.assertRaises(TypeError) as cm:
+ scan_id = sfp.getScanId()
+
def test_get_target_should_return_a_string(self):
"""
Test getTarget(self)
"""
sfp = SpiderFootPlugin()
- target = 'spiderfoot.net'
+ target = SpiderFootTarget("spiderfoot.net", "INTERNET_NAME")
sfp.setTarget(target)
- get_target = sfp.getTarget()
+ get_target = sfp.getTarget().targetValue
self.assertIsInstance(get_target, str)
- self.assertEqual(target, get_target)
+ self.assertEqual("spiderfoot.net", get_target)
- def test_get_target_invalid_target_should_exit(self):
+ def test_get_target_unitialised_target_should_raise(self):
"""
Test getTarget(self)
"""
sfp = SpiderFootPlugin()
- with self.assertRaises(SystemExit) as cm:
- sfp.getTarget()
-
- self.assertEqual(cm.exception.code, -1)
+ with self.assertRaises(TypeError) as cm:
+ get_target = sfp.getTarget()
def test_register_listener(self):
"""