Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Value Pools and Value Pool Fuzzer #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pytest.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[pytest]
; logging
log_level = ERROR
log_level = WARNING
log_format = %(asctime)s %(levelname)s %(filename)s:%(funcName)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

; log file
log_file_level = DEBUG
log_file_level = WARNING
log_file = ./custom_components/test/.test_log/fuzzing.log

; execution
; addopts = --no-summary
addopts = --no-summary
14 changes: 9 additions & 5 deletions custom_components/test/fuzzing/fuzzer_overview.puml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ package "fuzzing" as fuzzing <<Folder>> {
+ run(self, ???) : list
}
}
package "value_pools" as VPools <<Folder>> {
file "test_value_pools.py"
}
package "generators" as generators <<Folder>> {
file "test_generators.py"

folder "value_pools" as VPools {
file "test_vp_on_helpers.py"
}

folder "generators" as generators {
file "test_gen_on_xxx.py"
}

folder "grammar" as grammar {
file "test_gram_on_xxx.py"
}


fuzzer <|-- VPFuzzer : inherits from <
Expand Down
2 changes: 1 addition & 1 deletion custom_components/test/fuzzing/fuzzer_overview.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 84 additions & 3 deletions custom_components/test/fuzzing/fuzzer_utils/ValuePool.py
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanheitzmann remove TODOs after editing

ThorbenCarl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import datetime


class ValuePool:
Expand All @@ -7,14 +8,19 @@ class ValuePool:
_UINT_POOL = []
_INT_POOL = []
_FLOAT_POOL = []
_STRING_POOL = []
_BOOL_POOL = []
_BYTE_POOL = []
_LIST_POOL = []
_DICT_POOL = []
_DATE_POOL = []
_ALL_VALUES_POOL = []

def __init__(self) -> None:
"""constructor
Set values for pools.

TODO: @jonathanheitzmann add more value pool
TODO: @jonathanheitzmann add one pool with all values
TODO: @jonathanheitzmann no dublicate pool values, like in Balista. e.g. _FLOAT_POOL copyyinherits values from _INT_POOL
TODO: no dublicate pool values, like in Balista. e.g. _FLOAT_POOL copyyinherits values from _INT_POOL

sys.maxsize: An integer giving the maximum value a variable of type Py_ssize_t can take. It's usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.
"""
Expand Down Expand Up @@ -53,6 +59,60 @@ def __init__(self) -> None:
sys.maxsize * sys.maxsize * 0.5,
]

# set values for _STRING_POOL
self._STRING_POOL = [
"",
"a",
"abc",
" " * 100, # long string of spaces
"special_characters_!@#$%^&*()",
"üñîçødê",
"a" * 1000, # very long string
]

# set values for _BOOL_POOL
self._BOOL_POOL = [True, False]

# set values for _BYTE_POOL
self._BYTE_POOL = [
b"",
b"\x00",
b"abc",
bytes(range(256)), # all possible byte values
]

# set values for _LIST_POOL
self._LIST_POOL = [
[],
[1, 2, 3],
["a", "b", "c"],
[True, False, None],
list(range(100)), # long list
]

# set values for _DICT_POOL
self._DICT_POOL = [
{},
{"key": "value"},
{"int": 1, "float": 1.0, "str": "string"},
{i: i for i in range(10)}, # dictionary with multiple entries
]

# set values for _DATE_POOL
self._DATE_POOL = [
datetime.datetime.min,
ThorbenCarl marked this conversation as resolved.
Show resolved Hide resolved
datetime.datetime.max,
datetime.datetime.now(),
datetime.datetime(2000, 1, 1),
datetime.datetime(1970, 1, 1),
]


# create a pool with all unique values
self._ALL_VALUES_POOL = self._INT_POOL + self._UINT_POOL + self._FLOAT_POOL + self._STRING_POOL + self._BOOL_POOL + self._BYTE_POOL + self._LIST_POOL + self._DICT_POOL + self._DATE_POOL



def get_uint(self) -> list:
return self._UINT_POOL

Expand All @@ -61,3 +121,24 @@ def get_int(self) -> list:

def get_float(self) -> list:
return self._FLOAT_POOL

def get_string(self) -> list:
return self._STRING_POOL

def get_bool(self) -> list:
return self._BOOL_POOL

def get_byte(self) -> list:
return self._BYTE_POOL

def get_list(self) -> list:
return self._LIST_POOL

def get_dict(self) -> list:
return self._DICT_POOL

def get_date(self) -> list:
return self._DATE_POOL

def get_all_values(self) -> list:
return self._ALL_VALUES_POOL
63 changes: 53 additions & 10 deletions custom_components/test/fuzzing/fuzzer_utils/ValuePoolFuzzer.py
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanheitzmann remove TODOs after editing

Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from itertools import product
import logging


from custom_components.test.fuzzing.fuzzer_utils.Fuzzer import Fuzzer
from custom_components.test.fuzzing.fuzzer_utils.ValuePool import ValuePool


class ValuePoolFuzzer(Fuzzer):
"""Value pool fuzzer class, inherits from the abstract fuzzer class."""

value_pool = ValuePool()

def __int__(self):
"""constructor"""
pass

def fuzz(
self, param_nr: int = 1, types: list = ["INT"], param_combi: int = 1
) -> list:
"""Generates an individual value pool for fuzzing based on the parameters. A list of lists is returned.

TODO: @jonathanheitzmann implement function
TODO: @jonathanheitzmann specify valid types
TODO: @jonathanheitzmann add comments and update UML if necessary
TODO: @jonathanheitzmann implement function of param_combi

:param param_nr: Number of parameters of the function to be fuzzed. Each list in the return list contains a corresponding number of entries.
:type param_nr: int
Expand All @@ -29,9 +32,49 @@ def fuzz(
:rtype: list
"""

dummy_list = [
[-1, -1, 0, 3, 6],
[0, 0, 0, 0, 0],
[-1, -1, 0, 5, 6],
]
return dummy_list
logger = logging.getLogger(__name__)
logger.warning("The var param_combi is not in use!")

# Validate input parameters
if param_nr <= 0:
logger.error("Param Nr smaller or equal 0")
raise ValueError("param_nr must be a positive integer.")
if len(types) != param_nr:
logger.error("Length of types list must be equal to param_nr.")
raise ValueError("Length of types list must be equal to param_nr.")
if param_combi <= 0 or param_combi > param_nr:
logger.error("param_combi must be between 1 and param_nr.")
raise ValueError("param_combi must be between 1 and param_nr.")

# Get the value pools for the valid types
valid_types = {
"INT": self.value_pool.get_int(),
"UINT": self.value_pool.get_uint(),
"FLOAT": self.value_pool.get_float(),
"STRING": self.value_pool.get_string(),
"BOOL": self.value_pool.get_bool(),
"BYTE": self.value_pool.get_byte(),
"LIST": self.value_pool.get_list(),
"DICT": self.value_pool.get_dict(),
"DATE": self.value_pool.get_date(),
"ALL": self.value_pool.get_all_values(),
}

# Check whether requested types are valid.
for t in types:
if t not in valid_types:
logger.error("Invalid type " + str(t) + "specified.")
raise ValueError(f"Invalid type '{t}' specified.")

# Create a list of lists containing the valid values for each type in types_list
# e.g. [[all valid INTs], [all valid BOOLs], [all valid INTs], ...]
values_list = [valid_types[type_] for type_ in types]

# Use itertools.product to generate all possible combinations
combinations = list(product(*values_list))

# Convert tuples to lists
result = [list(combination) for combination in combinations]

# print(result)
return result
Empty file.
4 changes: 2 additions & 2 deletions custom_components/test/fuzzing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ pytest
> - not needed for grade bonus

## Value pools
- [ ] @jonathanheitzmann custom_components/test/fuzzing/fuzzer_utils/ValuePool.py
- [ ] @jonathanheitzmann custom_components/test/fuzzing/fuzzer_utils/ValuePoolFuzzer.py
- [ ] @jonathanheitzmann write function that creates 2-way, 3-way, ... pools
- [ ] @ThorbenCarl create some test cases for the value pool fuzzer

## Generators
- [ ] @dsiev custom_components/test/fuzzing/fuzzer_utils/GeneratorRunner.py
Expand All @@ -90,3 +88,5 @@ pytest
## `helpers.py`
### `map_range`
- possible zero division

### `test_get_all`
36 changes: 0 additions & 36 deletions custom_components/test/fuzzing/value_pools/test_value_pools.py

This file was deleted.

Loading