def test_update_known_technologies(technology, content, create): #create stub model-technologies.yml file tech_filepath = os.path.join(MODEL_DIR, "model-technologies"+ str(random.randint(1,1001)) +".yml") if not create: try: with open(tech_filepath, 'w') as configfile: configfile.write("technologies:\n") configfile.writelines(content) except IOError as e: print("Could not create technologies file. "+ e.strerror) assert False technology.tech_filepath = tech_filepath #get prev technologies prev_known_technologies = Technology.fetch_known_technologies(tech_filepath) #update technologies file technology.update_known_technologies() #get current technologies current_known_technologies = Technology.fetch_known_technologies(tech_filepath) #delete that file os.remove(tech_filepath) current_tech = { technology.name:{ "name": technology.name, "description": technology.description, "attributes": technology.attributes } } #check that prev_technologies + added technology == current_technologies assert { **prev_known_technologies, **current_tech } == current_known_technologies
def add_technology(self, name, description, attributes, used_in, cache=True): '''Creates and adds new Technology object Args: name (String): tech name description (String): tech desc attributes (dict(String:String)): tech attributes used_in (dict(String:Asset)): Assets this Technology is used in cache (bool, optional): Defaults to True. [description] Returns: Technology: object created ''' #if name already present - override (update that item) self.technologies[name] = Technology(name, description, attributes, used_in, self.technologies_file) if cache: self.technologies[name].update_known_technologies() self.saved = False return self.technologies[name]
def test_fetch_known_technologies(tech_file, outcome, delete): tech_filepath = os.path.join(MODEL_DIR, tech_file) known_technologies = Technology.fetch_known_technologies(tech_filepath) assert known_technologies == outcome if delete: os.remove(tech_filepath)
def technologies(): techs = {} for i in range(0, 3): tech_name = "stub" + str(random.randint(1, 1001)) tech = Technology(tech_name, "stub" + str(random.randint(1, 1001)), { "attr1": "value1", "stub2": "stub_value2" }, {}, "stub") techs[tech_name] = tech return techs
def test_init(name, description, attributes, used_in, tech_filepath): '''create new Asset ''' technology = Technology(name, description, attributes, used_in, tech_filepath) assert technology assert technology.name == name assert technology.description == description assert technology.attributes == attributes assert technology.tech_filepath == tech_filepath assert isinstance(technology.used_in, dict)
def test_fetch_known_technologies_exception(): #create a corrupted file tech_filepath = os.path.join(MODEL_DIR, "model-technologies"+ str(random.randint(1,1001)) +".yml") try: with open(tech_filepath, 'w') as tech_file: tech_file.write("\n") except IOError as e: print("Could not create technologies file. "+ e.strerror) assert False with pytest.raises(ModellingException): known_technologies = Technology.fetch_known_technologies(tech_filepath) #cleanup os.remove(tech_filepath)
def get_history(self): '''Gets known Technologies from the cache file Returns: dict(String:dict(String)): tech_name : tech_data ''' file = self.threat_model_controller.threat_model.technologies_file technologies = {} try: technologies = Technology.fetch_known_technologies(file) except ModellingException as e: Message.show_message_box(self.technologies_gui, MsgType.ERROR, str(e)) Message.print_message(MsgType.ERROR, str(e)) return technologies
def technology(): return Technology("stub","stub", {"attr1":"value1","stub2":"stub_value2"}, {},"stub")