Skip to content

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

tests: update test_spiderfoottarget

Brendan Colesc6b7d6787bb8
diff --git a/spiderfoot/target.py b/spiderfoot/target.py
index a3aebd3a..d9f4e17d 100644
--- a/spiderfoot/target.py
+++ b/spiderfoot/target.py
@@ -214,6 +214,6 @@ class SpiderFootTarget():
if includeChildren and value.endswith("." + name):
return True
- return None
+ return False
# end of SpiderFootTarget class
diff --git a/test/unit/test_spiderfoottarget.py b/test/unit/test_spiderfoottarget.py
index 50cf2384..d8d34582 100644
--- a/test/unit/test_spiderfoottarget.py
+++ b/test/unit/test_spiderfoottarget.py
@@ -14,19 +14,37 @@ class TestSpiderFootTarget(unittest.TestCase):
'EMAILADDR', 'HUMAN_NAME', 'BGP_AS_OWNER', 'PHONE_NUMBER', "USERNAME"
]
- def test_init_invalid_target_value_should_raise(self):
+ def test_init_argument_targetValue_invalid_type_should_raise_TypeError(self):
"""
Test __init__(self, targetValue, typeName)
"""
- with self.assertRaises(TypeError):
- SpiderFootTarget(None, 'IP_ADDRESS')
+ target_type = 'IP_ADDRESS'
+
+ invalid_types = [None, list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError):
+ target = SpiderFootTarget(invalid_type, target_type)
- def test_init_unsupported_target_type_should_raise(self):
+ def test_init_argument_targetType_invalid_type_should_raise_TypeError(self):
"""
Test __init__(self, targetValue, typeName)
"""
+ target_value = 'example target value'
+
+ invalid_types = [None, list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError):
+ target = SpiderFootTarget(target_value, invalid_type)
+
+ def test_init_argument_targetType_invalid_should_raise_ValueError(self):
+ """
+ Test __init__(self, targetValue, typeName)
+ """
+ target_value = 'example target value'
with self.assertRaises(ValueError):
- SpiderFootTarget('example target value', 'example target type')
+ SpiderFootTarget(target_value, 'invalid target type')
def test_init_supported_target_types(self):
"""
@@ -37,11 +55,11 @@ class TestSpiderFootTarget(unittest.TestCase):
for target_type in self.valid_target_types:
with self.subTest(target_type=target_type):
target = SpiderFootTarget(target_value, target_type)
- self.assertEqual(SpiderFootTarget, type(target))
+ self.assertIsInstance(target, SpiderFootTarget)
self.assertEqual(target.targetType, target_type)
self.assertEqual(target.targetValue, target_value)
- def test_set_alias(self):
+ def test_setAlias(self):
"""
Test setAlias(self, value, typeName)
"""
@@ -52,7 +70,7 @@ class TestSpiderFootTarget(unittest.TestCase):
target.setAlias(None, None)
self.assertEqual('TBD', 'TBD')
- def test_target_type_attribute_should_return_a_string(self):
+ def test_targetType_attribute_should_return_a_string(self):
"""
Test targetType attribute
"""
@@ -63,7 +81,21 @@ class TestSpiderFootTarget(unittest.TestCase):
self.assertIsInstance(target.targetType, str)
self.assertEqual(target_type, target.targetType)
- def test_target_value_attribute_should_return_a_string(self):
+ def test_targetType_attribute_setter_invalid_type_should_raise_TypeError(self):
+ """
+ Test targetType attribute
+ """
+ target_value = 'example target value'
+ target_type = 'IP_ADDRESS'
+
+ invalid_types = [None, list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError):
+ target = SpiderFootTarget(target_value, target_type)
+ target.targetType = invalid_type
+
+ def test_targetValue_attribute_should_return_a_string(self):
"""
Test targetValue attribute
"""
@@ -74,7 +106,21 @@ class TestSpiderFootTarget(unittest.TestCase):
self.assertIsInstance(target.targetValue, str)
self.assertEqual(target_value, target.targetValue)
- def test_target_aliases_attribute_should_return_a_list(self):
+ def test_targetValue_attribute_setter_invalid_type_should_raise_TypeError(self):
+ """
+ Test targetValue attribute
+ """
+ target_value = 'example target value'
+ target_type = 'IP_ADDRESS'
+
+ invalid_types = [None, list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError):
+ target = SpiderFootTarget(target_value, target_type)
+ target.targetValue = invalid_type
+
+ def test_targetAliases_attribute_should_return_a_list(self):
"""
Test targetAliases attribute
"""
@@ -84,7 +130,7 @@ class TestSpiderFootTarget(unittest.TestCase):
self.assertIsInstance(target.targetAliases, list)
- def test_get_equivalents_should_return_a_list(self):
+ def test_getEquivalents_should_return_a_list(self):
"""
Test _getEquivalents(self, typeName)
"""
@@ -95,7 +141,7 @@ class TestSpiderFootTarget(unittest.TestCase):
equivalents = target._getEquivalents(target_type)
self.assertEqual(list, type(equivalents))
- def test_get_names_should_return_a_list(self):
+ def test_getNames_should_return_a_list(self):
"""
Test getNames(self)
"""
@@ -106,7 +152,7 @@ class TestSpiderFootTarget(unittest.TestCase):
names = target.getNames()
self.assertEqual(list, type(names))
- def test_get_addresses_should_return_a_list(self):
+ def test_getAddresses_should_return_a_list(self):
"""
Test getAddresses(self)
"""
@@ -117,6 +163,13 @@ class TestSpiderFootTarget(unittest.TestCase):
addresses = target.getAddresses()
self.assertEqual(list, type(addresses))
+ target_value = 'example target value'
+ target_type = 'IPV6_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
+ addresses = target.getAddresses()
+ self.assertEqual(list, type(addresses))
+
def test_matches_should_return_a_boolean(self):
"""
Test matches(self, value, includeParents=False, includeChildren=True)
@@ -125,8 +178,97 @@ class TestSpiderFootTarget(unittest.TestCase):
target_type = 'IP_ADDRESS'
target = SpiderFootTarget(target_value, target_type)
+ matches = target.matches(target_value)
+ self.assertIsInstance(matches, bool)
+
+ def test_matches_argument_value_matching_ipv4_address_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = '1.1.1.1'
+ target_type = 'IP_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(target_value)
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_unmatching_ipv4_address_should_return_False(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = '1.1.1.1'
+ target_type = 'IP_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('1.1.1.2')
+ self.assertFalse(matches)
+
+ @unittest.skip("fixme - this test fails due to a bug in the IPv6 matching logic")
+ def test_matches_argument_value_matching_ipv6_address_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = '::1'
+ target_type = 'IPV6_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(target_value)
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_unmatching_ipv6_address_should_return_False(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = '::1'
+ target_type = 'IPV6_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('::2')
+ self.assertFalse(matches)
+
+ def test_matches_argument_value_any_human_name_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'Spider Foot'
+ target_type = 'HUMAN_NAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('something else entirely')
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_any_phone_number_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'SpiderFoot'
+ target_type = 'PHONE_NUMBER'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('something else entirely')
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_any_username_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'SpiderFoot'
+ target_type = 'USERNAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('something else entirely')
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_with_empty_value_should_return_False(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'example target value'
+ target_type = 'IP_ADDRESS'
+ target = SpiderFootTarget(target_value, target_type)
+
matches = target.matches(None)
- self.assertEqual(bool, type(matches))
+ self.assertFalse(matches)
if __name__ == '__main__':