def test_synchronizer_raises_exception_with_output_of_wrong_type():
    def sync_function(trace_object):
        return trace_object.samples.array

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    with pytest.raises(TypeError):
        scared.Synchronizer(ths, 3, sync_function)
def test_synchronizer_raises_exception_with_function_of_wrong_type():
    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    with pytest.raises(TypeError):
        scared.Synchronizer(ths, output_filename, "foo")
    _remove_result_file(output_filename)
Beispiel #3
0
def test_ets_writer_works_whatever_is_the_index_order(ets_filenames):
    base_trace = np.random.randint(0,
                                   256, (nb_trace, nb_points),
                                   dtype='uint8')

    ets1 = ETSWriter(filename_1, overwrite=True)
    indexes = np.arange(nb_trace)
    for ind in indexes:
        ets1.write_samples(base_trace[ind], index=ind)
    ets1.close()

    ets2 = ETSWriter(filename_2, overwrite=True)

    for ind in reversed(indexes):
        ets2.write_samples(base_trace[ind], index=ind)
    ets2.close()

    ets3 = ETSWriter(filename_3, overwrite=True)
    indexes_3 = [nb_trace - 1, 2, 1]
    for ind in indexes_3:
        ets3.write_samples(base_trace[ind], index=ind)
    ets3.close()

    d1 = read_ths_from_ets_file(filename_1).samples[:]
    d2 = read_ths_from_ets_file(filename_2).samples[:]
    d3 = read_ths_from_ets_file(filename_3).samples[:]
    assert np.array_equal(d1[nb_trace - 1], d3[nb_trace - 1])
    assert np.array_equal(d1[2], d3[2])
    assert np.array_equal(d1[1], d3[1])

    assert d1.shape == d2.shape
    assert d1.shape == d3.shape

    s1 = os.path.getsize(filename_1)
    s2 = os.path.getsize(filename_2)

    assert s1 == s2
def test_synchronizer_run_raises_exception_with_run_called_twice():
    def sync_function(trace_object):
        return trace_object.samples.array

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    synchronizer.run()
    with pytest.raises(scared.SynchronizerError):
        synchronizer.run()
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_check_raises_exception_with_function_raising_exception_and_catch_exceptions_to_false(
        capsys):
    def sync_function(trace_object):
        raise scared.ResynchroError('Error.')

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    with pytest.raises(scared.ResynchroError):
        synchronizer.check(nb_traces=1, catch_exceptions=False)
    captured = capsys.readouterr()
    assert captured.out == ""
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_returns_only_values_that_are_returned_by_the_given_function_without_errors(
):
    def sync_function(trace_object):
        if trace_object.name == 'Trace n°0' or trace_object.name == 'Trace n°1' or trace_object.name == 'Trace n°3':
            return trace_object.samples.array
        if trace_object.name == 'Trace n°2':
            raise scared.ResynchroError('Error.')

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    out_ths = synchronizer.run()
    assert len(out_ths) == 3
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_with_ets_file_report_prints_correct_string(capsys):
    def sync_function(trace_object):
        return trace_object.samples.array + 0.5

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    synchronizer.run()
    synchronizer.report()
    captured = capsys.readouterr()
    assert captured.out == str("Processed traces....: 10\n"
                               "Synchronized traces.: 10\n"
                               "Success rate........: 100.0%\n")
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_check_prints_exception_with_function_raising_exception_and_catch_exceptions_to_true(
        capsys):
    def sync_function(trace_object):
        raise scared.ResynchroError('Error.')

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    synchronizer.check(nb_traces=1, catch_exceptions=True)
    captured = capsys.readouterr()
    assert captured.out.startswith(
        "Raised scared.synchronization.ResynchroError: Error. in sync_function line"
    )
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_returns_traces_preserving_its_added_attributes():
    def sync_function(trace_object):
        trace_object.name2 = trace_object.name
        return trace_object.samples.array

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    out_ths = synchronizer.run()
    for i in range(len(ths)):
        trace = ths[i]
        output_trace = out_ths[i]
        assert trace.name == output_trace.name2
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_does_not_raise_exception_with_already_existing_result_file_and_overwrite_to_true(
):
    def sync_function(trace_object):
        return trace_object.samples.array

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    synchronizer.run()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        synchronizer = scared.Synchronizer(ths,
                                           output_filename,
                                           sync_function,
                                           overwrite=True)
    synchronizer.run()
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_with_ets_file_applies_given_function_and_returns_correct_ths(
):
    def sync_function(trace_object):
        return trace_object.samples.array + 0.5

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths, output_filename, sync_function)
    out_ths = synchronizer.run()
    assert len(out_ths) == len(ths)
    for i in range(len(ths)):
        trace = ths[i]
        output_trace = out_ths[i]
        assert np.array_equal(trace.samples, output_trace.samples.array - 0.5)
        assert np.array_equal(trace.plaintext, output_trace.plaintext)
        assert np.array_equal(trace.ciphertext, output_trace.ciphertext)
        assert np.array_equal(trace.foo_bar, output_trace.foo_bar)
    ths.close()
    _remove_result_file(output_filename)
def test_synchronizer_run_passes_kwargs_to_the_synchronization_function():
    def sync_function(trace_object, foo, bar):
        trace_object.foo = foo
        trace_object.bar = bar
        return trace_object.samples.array

    output_filename = "tests/samples/synchronization/synced.ets"
    _remove_result_file(output_filename)

    input_filename = "tests/samples/synchronization/ets_file.ets"
    ths = estraces.read_ths_from_ets_file(input_filename)[:10]

    synchronizer = scared.Synchronizer(ths,
                                       output_filename,
                                       sync_function,
                                       foo=10,
                                       bar=20)
    out_ths = synchronizer.run()
    for i in range(len(ths)):
        output_trace = out_ths[i]
        assert output_trace.foo == 10
        assert output_trace.bar == 20
    ths.close()
    _remove_result_file(output_filename)