JJ / 2020-WCCI-variable-attack-surface

Paper on optimal nginx configuration for generating variable attack surfaces
GNU General Public License v3.0
3 stars 1 forks source link

Fix tests #7

Closed JJ closed 4 years ago

JJ commented 4 years ago
==================================================== test session starts =====================================================
platform linux -- Python 3.6.4, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /home/jmerelo/txt/investigacion/papers/2020/2020-WCCI-variable-attack-surface/code, inifile:
plugins: celery-4.2.1
collected 8 items                                                                                                            

test/test_genetic.py ..FF....                                                                                          [100%]

========================================================== FAILURES ==========================================================
______________________________________________ test_selection_and_reproduction _______________________________________________

function = <MagicMock name='calculate_fitness' id='140453655532544'>

    @mock.patch('genetic.calculate_fitness', return_value=1)
    def test_selection_and_reproduction(function):
        """Test selection and reproduction"""
        population = [
            [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0],
            [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0],
            [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0],
            [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0],
        ]

        expected_result = [
            [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0],
            [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0],
            [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0],
            [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0]
        ]

>       result = genetic.selection_and_reproduction(population)

test/test_genetic.py:41: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
genetic.py:122: in selection_and_reproduction
    parent = random.sample(selected, 2)  # Two parents are selected
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <random.Random object at 0x19276b8>, population = [[882, 101, 1, 0, 0, 986, ...]], k = 2

    def sample(self, population, k):
        """Chooses k unique random elements from a population sequence or set.

        Returns a new list containing elements from the population while
        leaving the original population unchanged.  The resulting list is
        in selection order so that all sub-slices will also be valid random
        samples.  This allows raffle winners (the sample) to be partitioned
        into grand prize and second place winners (the subslices).

        Members of the population need not be hashable or unique.  If the
        population contains repeats, then each occurrence is a possible
        selection in the sample.

        To choose a sample in a range of integers, use range as an argument.
        This is especially fast and space efficient for sampling from a
        large population:   sample(range(10000000), 60)
        """

        # Sampling without replacement entails tracking either potential
        # selections (the pool) in a list or previous selections in a set.

        # When the number of selections is small compared to the
        # population, then tracking selections is efficient, requiring
        # only a small set and an occasional reselection.  For
        # a larger number of selections, the pool tracking method is
        # preferred since the list takes less space than the
        # set and it doesn't suffer from frequent reselections.

        if isinstance(population, _Set):
            population = tuple(population)
        if not isinstance(population, _Sequence):
            raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
        randbelow = self._randbelow
        n = len(population)
        if not 0 <= k <= n:
>           raise ValueError("Sample larger than population or is negative")
E           ValueError: Sample larger than population or is negative

/home/jmerelo/.pyenv/versions/3.6.4/lib/python3.6/random.py:318: ValueError
---------------------------------------------------- Captured stdout call ----------------------------------------------------
Selected
[[882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0]]
_______________________________________________________ test_mutation ________________________________________________________

function1 = <MagicMock name='generate_random_config' id='140453655392440'>
function2 = <MagicMock name='random' id='140453655594488'>

    @mock.patch('random.random', return_value=0.1)
    @mock.patch('fitness.generate_random_config', return_value=[200, 39, 0, 1, 0, 1033, 1933, 1, 0, 2, 3, 0, 1])
    def test_mutation(function1, function2):
        """Test mutation function"""
        population = [
            [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0],
            [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0],
            [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0],
            [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0],
        ]
        expected_result = [
            [1523, 42, 0, 1, 1, 2045, 537, 1, 1, 2, 2, 0, 0],
            [1971, 117, 0, 1, 0, 1140, 1737, 1, 1, 2, 2, 0, 0],
            [882, 101, 1, 0, 0, 986, 644, 0, 1, 1, 5, 0, 0],
            [1529, 70, 1, 1, 1, 1462, 1834, 0, 0, 1, 1, 1, 0]
        ]
>       result = genetic.mutate(population)

test/test_genetic.py:62: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

crossed_population = [[1523, 42, 0, 1, 1, 2045, ...], [1971, 117, 0, 1, 0, 1140, ...], [882, 101, 1, 0, 0, 986, ...], [1529, 70, 1, 1, 1, 1462, ...]]

    def mutate(crossed_population):
        """
            Crossed individuals mutate randomly. Without the mutation of new genes the
            solution could never be reached.
        """
        for i in range(len(crossed_population)):
            if random.random() <= mutation_chance:  # Every individual in the population (except the parents) has a chance to mutate.
                gen = random.randint(0, genes - 1)  # A random gen is chosen

                new_value = None

                if mutation_type_random:
                    new_value = generate_random_config()[gen]  # and a new value for this gen

                    # It is important to see that the new value is not equal to the old one.
>                   while new_value == crossed_population[i][1][gen]:
E                   TypeError: 'int' object is not subscriptable

genetic.py:163: TypeError
============================================= 2 failed, 6 passed in 0.16 seconds =============================================
JJ commented 4 years ago

population has a single individual.