forked from p34709852/monkey
Fake IP's don't get out of range, fixed typo in requirements and added logging to telem parsing
This commit is contained in:
parent
1a784791fd
commit
9aff40d974
|
@ -1,11 +1,25 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
class FakeIpGenerator:
|
class FakeIpGenerator:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.fake_ip_parts = [1, 1, 1, 1]
|
self.fake_ip_parts = [1, 1, 1, 1]
|
||||||
|
|
||||||
def generate_fake_ips_for_real_ips(self, real_ips):
|
def generate_fake_ips_for_real_ips(self, real_ips: List[str]) -> List[str]:
|
||||||
self.fake_ip_parts[2] += 1
|
|
||||||
fake_ips = []
|
fake_ips = []
|
||||||
for i in range(len(real_ips)):
|
for i in range(len(real_ips)):
|
||||||
fake_ips.append('.'.join(str(part) for part in self.fake_ip_parts))
|
fake_ips.append('.'.join(str(part) for part in self.fake_ip_parts))
|
||||||
self.fake_ip_parts[3] += 1
|
self.increment_ip()
|
||||||
return fake_ips
|
return fake_ips
|
||||||
|
|
||||||
|
def increment_ip(self):
|
||||||
|
self.fake_ip_parts[3] += 1
|
||||||
|
self.try_fix_ip_range()
|
||||||
|
|
||||||
|
def try_fix_ip_range(self):
|
||||||
|
for i in range(len(self.fake_ip_parts)):
|
||||||
|
if self.fake_ip_parts[i] > 256:
|
||||||
|
if i-1 < 0:
|
||||||
|
raise Exception("Fake IP's out of range.")
|
||||||
|
self.fake_ip_parts[i-1] += 1
|
||||||
|
self.fake_ip_parts[i] = 1
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from envs.monkey_zoo.blackbox.tests.performance.utils.fake_ip_generator import FakeIpGenerator
|
||||||
|
|
||||||
|
|
||||||
|
class TestFakeIpGenerator(TestCase):
|
||||||
|
|
||||||
|
def test_fake_ip_generation(self):
|
||||||
|
fake_ip_gen = FakeIpGenerator()
|
||||||
|
self.assertListEqual([1, 1, 1, 1], fake_ip_gen.fake_ip_parts)
|
||||||
|
for i in range(256):
|
||||||
|
fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1'])
|
||||||
|
self.assertListEqual(['1.1.2.1'], fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1']))
|
||||||
|
fake_ip_gen.fake_ip_parts = [256, 256, 255, 256]
|
||||||
|
self.assertListEqual(['256.256.255.256', '256.256.256.1'],
|
||||||
|
fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1', '1.1.1.2']))
|
||||||
|
fake_ip_gen.fake_ip_parts = [256, 256, 256, 256]
|
||||||
|
self.assertRaises(Exception, fake_ip_gen.generate_fake_ips_for_real_ips(['1.1.1.1']))
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from os import mkdir, path
|
from os import mkdir, path
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -12,6 +13,10 @@ TEST_TELEM_DIR = "./test_telems"
|
||||||
MAX_SAME_CATEGORY_TELEMS = 10000
|
MAX_SAME_CATEGORY_TELEMS = 10000
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TestTelemStore:
|
class TestTelemStore:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -31,14 +36,17 @@ class TestTelemStore:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def export_test_telems():
|
def export_test_telems():
|
||||||
|
logger.info(f"Exporting all telemetries to {TEST_TELEM_DIR}")
|
||||||
try:
|
try:
|
||||||
mkdir(TEST_TELEM_DIR)
|
mkdir(TEST_TELEM_DIR)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
|
logger.info("Deleting all previous telemetries.")
|
||||||
shutil.rmtree(TEST_TELEM_DIR)
|
shutil.rmtree(TEST_TELEM_DIR)
|
||||||
mkdir(TEST_TELEM_DIR)
|
mkdir(TEST_TELEM_DIR)
|
||||||
for test_telem in TestTelem.objects():
|
for test_telem in TestTelem.objects():
|
||||||
with open(TestTelemStore.get_unique_file_path_for_test_telem(TEST_TELEM_DIR, test_telem), 'w') as file:
|
with open(TestTelemStore.get_unique_file_path_for_test_telem(TEST_TELEM_DIR, test_telem), 'w') as file:
|
||||||
file.write(test_telem.to_json())
|
file.write(test_telem.to_json())
|
||||||
|
logger.info("Telemetries exported!")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_unique_file_path_for_test_telem(target_dir: str, test_telem: TestTelem):
|
def get_unique_file_path_for_test_telem(target_dir: str, test_telem: TestTelem):
|
||||||
|
|
|
@ -24,4 +24,4 @@ requests
|
||||||
dpath
|
dpath
|
||||||
ring
|
ring
|
||||||
stix2
|
stix2
|
||||||
tcdm
|
tqdm
|
||||||
|
|
Loading…
Reference in New Issue