|
| 1 | +from api.views.utils import is_ip_address, is_sha256hash |
1 | 2 | from greedybear.consts import FEEDS_LICENSE
|
2 | 3 | from greedybear.models import GeneralHoneypot, Statistics, viewType
|
3 | 4 | from rest_framework.test import APIClient
|
@@ -156,6 +157,7 @@ def setUpClass(self):
|
156 | 157 |
|
157 | 158 | @classmethod
|
158 | 159 | def tearDownClass(self):
|
| 160 | + super(StatisticsViewTestCase, self).tearDownClass() |
159 | 161 | Statistics.objects.all().delete()
|
160 | 162 |
|
161 | 163 | def test_200_feeds_sources(self):
|
@@ -209,3 +211,96 @@ def test_200_active_general_honeypots(self):
|
209 | 211 | response = self.client.get("/api/general_honeypot?onlyActive=true")
|
210 | 212 | self.assertEqual(response.status_code, 200)
|
211 | 213 | self.assertEqual(response.json(), ["Heralding", "Ciscoasa"])
|
| 214 | + |
| 215 | + |
| 216 | +class CommandSequenceViewTestCase(CustomTestCase): |
| 217 | + """Test cases for the command_sequence_view.""" |
| 218 | + |
| 219 | + def setUp(self): |
| 220 | + # setup client |
| 221 | + self.client = APIClient() |
| 222 | + self.client.force_authenticate(user=self.superuser) |
| 223 | + |
| 224 | + def test_missing_query_parameter(self): |
| 225 | + """Test that view returns BadRequest when query parameter is missing.""" |
| 226 | + response = self.client.get("/api/command_sequence") |
| 227 | + self.assertEqual(response.status_code, 400) |
| 228 | + |
| 229 | + def test_invalid_query_parameter(self): |
| 230 | + """Test that view returns BadRequest when query parameter is invalid.""" |
| 231 | + response = self.client.get("/api/command_sequence?query=invalid-input}") |
| 232 | + self.assertEqual(response.status_code, 400) |
| 233 | + |
| 234 | + def test_ip_address_query(self): |
| 235 | + """Test view with a valid IP address query.""" |
| 236 | + response = self.client.get("/api/command_sequence?query=140.246.171.141") |
| 237 | + self.assertEqual(response.status_code, 200) |
| 238 | + self.assertIn("executed_commands", response.data) |
| 239 | + self.assertIn("executed_by", response.data) |
| 240 | + |
| 241 | + def test_ip_address_query_with_similar(self): |
| 242 | + """Test view with a valid IP address query including similar sequences.""" |
| 243 | + response = self.client.get("/api/command_sequence?query=140.246.171.141&include_similar") |
| 244 | + self.assertEqual(response.status_code, 200) |
| 245 | + self.assertIn("executed_commands", response.data) |
| 246 | + self.assertIn("executed_by", response.data) |
| 247 | + |
| 248 | + def test_nonexistent_ip_address(self): |
| 249 | + """Test that view returns 404 for IP with no sequences.""" |
| 250 | + response = self.client.get("/api/command_sequence?query=10.0.0.1") |
| 251 | + self.assertEqual(response.status_code, 404) |
| 252 | + |
| 253 | + def test_hash_query(self): |
| 254 | + """Test view with a valid hash query.""" |
| 255 | + response = self.client.get(f"/api/command_sequence?query={self.hash}") |
| 256 | + self.assertEqual(response.status_code, 200) |
| 257 | + self.assertIn("commands", response.data) |
| 258 | + self.assertIn("iocs", response.data) |
| 259 | + |
| 260 | + def test_hash_query_with_similar(self): |
| 261 | + """Test view with a valid hash query including similar sequences.""" |
| 262 | + response = self.client.get(f"/api/command_sequence?query={self.hash}&include_similar") |
| 263 | + self.assertEqual(response.status_code, 200) |
| 264 | + self.assertIn("commands", response.data) |
| 265 | + self.assertIn("iocs", response.data) |
| 266 | + |
| 267 | + def test_nonexistent_hash(self): |
| 268 | + """Test that view returns 404 for nonexistent hash.""" |
| 269 | + response = self.client.get(f"/api/command_sequence?query={'f' * 64}") |
| 270 | + self.assertEqual(response.status_code, 404) |
| 271 | + |
| 272 | + |
| 273 | +class ValidationHelpersTestCase(CustomTestCase): |
| 274 | + """Test cases for the validation helper functions.""" |
| 275 | + |
| 276 | + def test_is_ip_address_valid_ipv4(self): |
| 277 | + """Test that is_ip_address returns True for valid IPv4 addresses.""" |
| 278 | + self.assertTrue(is_ip_address("192.168.1.1")) |
| 279 | + self.assertTrue(is_ip_address("10.0.0.1")) |
| 280 | + self.assertTrue(is_ip_address("127.0.0.1")) |
| 281 | + |
| 282 | + def test_is_ip_address_valid_ipv6(self): |
| 283 | + """Test that is_ip_address returns True for valid IPv6 addresses.""" |
| 284 | + self.assertTrue(is_ip_address("::1")) |
| 285 | + self.assertTrue(is_ip_address("2001:db8::1")) |
| 286 | + self.assertTrue(is_ip_address("fe80::1ff:fe23:4567:890a")) |
| 287 | + |
| 288 | + def test_is_ip_address_invalid(self): |
| 289 | + """Test that is_ip_address returns False for invalid IP addresses.""" |
| 290 | + self.assertFalse(is_ip_address("not-an-ip")) |
| 291 | + self.assertFalse(is_ip_address("256.256.256.256")) |
| 292 | + self.assertFalse(is_ip_address("192.168.0")) |
| 293 | + self.assertFalse(is_ip_address("2001:xyz::1")) |
| 294 | + |
| 295 | + def test_is_sha256hash_valid(self): |
| 296 | + """Test that is_sha256hash returns True for valid SHA-256 hashes.""" |
| 297 | + self.assertTrue(is_sha256hash("a" * 64)) |
| 298 | + self.assertTrue(is_sha256hash("1234567890abcdef" * 4)) |
| 299 | + self.assertTrue(is_sha256hash("A" * 64)) |
| 300 | + |
| 301 | + def test_is_sha256hash_invalid(self): |
| 302 | + """Test that is_sha256hash returns False for invalid SHA-256 hashes.""" |
| 303 | + self.assertFalse(is_sha256hash("a" * 63)) # Too short |
| 304 | + self.assertFalse(is_sha256hash("a" * 65)) # Too long |
| 305 | + self.assertFalse(is_sha256hash("z" * 64)) # Invalid chars |
| 306 | + self.assertFalse(is_sha256hash("not-a-hash")) |
0 commit comments