Beispiel #1
0
def test_id_probs(tmp_path):
    frames = dp.parse_csv(
        SAMPLE_PATH /
        'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv')
    probs_file = str(tmp_path / 'idprobs.json')
    dp.write_id_probs(frames, probs_file)
    idprobs = dp.load_id_probs(probs_file)
    idprobs_check = {
        1072: 0.07521674607513865,
        336: 0.15019917206904632,
        352: 0.15019917206904632,
        560: 0.15019917206904632,
        71: 0.015621338748730767,
        357: 0.0937280324923846,
        368: 0.0937280324923846,
        512: 0.0937280324923846,
        513: 0.0937280324923846,
        1056: 0.015621338748730767,
        1058: 0.015621338748730767,
        65: 0.014996485198781535,
        832: 0.014996485198781535,
        848: 0.014996485198781535,
        613: 0.0014840271811294228,
        1279: 0.0014840271811294228,
        1108: 0.0014840271811294228,
        1059: 0.0014840271811294228,
        1107: 0.0014840271811294228
    }
    assert idprobs == idprobs_check
Beispiel #2
0
def test_canlist(tmp_path):
    frames = dp.parse_csv(
        str(SAMPLE_PATH /
            'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv'))
    dp.write_canlist(frames, tmp_path / 'canlist_validate.json')
    frames_check = dp.load_canlist(tmp_path / 'canlist_validate.json')
    assert frames == frames_check
    assert dp.validate_can_data(frames)
Beispiel #3
0
def test_inject_malicious_packets():
    frames = dp.parse_csv(
        SAMPLE_PATH /
        'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv')
    prev_len = len(frames)
    mg = MaliciousGenerator()
    frames, labels = dp.inject_malicious_packets(frames, mg)
    assert len(frames) == len(labels)
    assert not all(x is None for x in labels)
    assert len(frames) > prev_len
    assert dp.validate_can_data(frames)
Beispiel #4
0
def test_feature_lists(tmp_path):
    frames = dp.parse_csv(
        SAMPLE_PATH /
        'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv')
    idprobs = dp.write_id_probs(frames, tmp_path / 'idprobs')

    feature_lists = dp.generate_feature_lists(frames, idprobs)
    feat_file = tmp_path / 'feature_lists'
    dp.write_feature_lists(feature_lists, [], feat_file)
    feature_lists, _ = dp.load_feature_lists(feat_file)
    with open(SAMPLE_PATH / 'test_preprocessor/feature_lists_validate.txt') \
            as validation_file:
        feature_lists_check = literal_eval(validation_file.read())
    assert feature_lists == feature_lists_check
Beispiel #5
0
def test_parse_csv():
    # Check parse_csv
    frames = dp.parse_csv(
        str(SAMPLE_PATH /
            'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv'))
    with open(SAMPLE_PATH / 'test_preprocessor/csv_validate.txt') \
            as verification_file:
        frames_check = literal_eval(verification_file.read())
    assert frames == frames_check

    with pytest.raises(FileNotFoundError):
        dp.parse_csv('asdf')

    with pytest.raises(FileNotFoundError):
        dp.parse_csv('.')

    with pytest.raises(ValueError):
        dp.parse_csv(str(SAMPLE_PATH.parent / 'test_preprocessor.py'))
Beispiel #6
0
def test_validate_can_data():
    old_stdout = sys.stdout
    sys.stdout = buffer = io.StringIO(
    )  # Write anything being printed to stdout to buffer instead

    frames = dp.parse_csv(
        str(SAMPLE_PATH /
            'csv/2006 Ford Fusion/Test Data/2006 Ford Fusion Test.csv'))
    assert dp.validate_can_data(frames)
    frames[163].pop('id')
    frames[124]['id'] = 3999
    frames[777]['timestamp'] = 'asdf'
    frames[32]['data'] = b'aaaaaaaaaaaa'
    frames[32]['timestamp'] = -1
    frames[777]['data'] = 3.1
    assert not dp.validate_can_data(frames)

    # Check that what was being printed is the correct output
    sys.stdout = old_stdout
    assert buffer.getvalue() == """This dataset is valid!
    def start_simulation(self, can_file_url, idprobs_name):
        if platform.system() == 'Windows':
            file_path = can_file_url.toString()[8:]
        else:
            file_path = can_file_url.toString()[7:]
        if file_path.endswith('.traffic'):
            canlist = dp.parse_traffic(file_path)
        elif file_path.endswith('.csv'):
            canlist = dp.parse_csv(file_path)
        elif file_path.endswith('.json'):
            canlist = dp.load_canlist(file_path)
        else:
            raise ValueError(
                f'Unknown type of CAN frame file provided: {file_path}.')
        self._current_canlist, self._current_labels = dp.inject_malicious_packets(
            canlist, self._malgen)

        self._ids_manager._ids.change_ids_parameters(
            'idprobs_path', idprobs_dir + '/' + idprobs_name + '.json')
        self._ids_manager.start_simulation()
        self.sim_thread = SimulationThread(self)
        self.sim_thread.start()
Beispiel #8
0
    def create_dataset(self, can_file_url, idprobs_name, malgen_probs, dataset_name):
        # Convert the JS object to a dictionary.
        malgen_probs = malgen_probs.toVariant()
        if platform.system() == 'Windows':
            file_path = can_file_url.toString()[8:]
        else:
            file_path = can_file_url.toString()[7:]
        if file_path.endswith('.traffic'):
            canlist = dp.parse_traffic(file_path)
        elif file_path.endswith('.csv'):
            canlist = dp.parse_csv(file_path)
        elif file_path.endswith('.json'):
            canlist = dp.load_canlist(file_path)
        else:
            raise ValueError(f'Unknown type of CAN frame file provided: {file_path}.')

        idprobs = dp.load_id_probs(idprobs_dir + '/' + idprobs_name + '.json')

        # Create a folder under the 'datasets' folder with the name being the
        # name of the dataset. This is where all the relevant files for the
        # dataset go.
        dataset_folder = datasets_dir + '/' + dataset_name
        os.mkdir(dataset_folder)
        # Write the good CAN frame file.
        dp.write_canlist(canlist, dataset_folder + '/good_canlist.json')

        # Create the CAN frame file with malicious frames injected.
        malgen = MaliciousGenerator()
        malgen.adjust(malgen_probs)
        bad_canlist, labels = dp.inject_malicious_packets(canlist, malgen)
        dp.write_canlist(bad_canlist, dataset_folder + '/bad_canlist.json')

        # Create the feature lists/labels file.
        features = dp.generate_feature_lists(bad_canlist, idprobs)
        dp.write_feature_lists(features, labels, dataset_folder + '/features_labels.json')
        # Update the available datasets.
        self._available_datasets.append(dataset_name)
        self.get_availableDatasets.emit()
Beispiel #9
0
 def create_idprobs_file(self, list_of_can_files, idprobs_name):
     canlist = []
     # Here, each entry in `list_of_can_files` is a QUrl.
     for file_url in list_of_can_files:
         # toString() returns 'file://<actual file path>', so strip the
         # beginning and open the rest.
         if platform.system() == 'Windows':
             file_path = file_url.toString()[8:]
         else:
             file_path = file_url.toString()[7:]
         if file_path.endswith('.traffic'):
             canlist += dp.parse_traffic(file_path)
         elif file_path.endswith('.csv'):
             canlist += dp.parse_csv(file_path)
         elif file_path.endswith('.json'):
             canlist += dp.load_canlist(file_path)
         else:
             raise ValueError(f'Unknown type of CAN frame file provided: {file_path}.')
     dp.write_id_probs(canlist, idprobs_dir + '/' + idprobs_name + '.json')
     # After writing the file, update the available ID probs files by
     # appending to the list and then emitting the "notify" signal.
     self._available_idprobs.append(idprobs_name)
     self.get_availableIdprobs.emit()