### Setup Configuration (setup.cfg) Source: https://github.com/codykochmann/battle_tested/blob/master/battle_tested.egg-info/SOURCES.txt Configuration file for setuptools, defining package metadata and options. ```cfg [metadata] name = battle-tested version = 0.0.1 author = Cody Kochmann author_email = cody.kochmann@gmail.com description = A python package to help you test your code. long_description = file: README.md url = https://github.com/codykochmann/battle_tested classifiers = Programming Language :: Python :: 3 License :: OSI Approved :: MIT License Operating System :: OS Independent [options] packages = find: python_requires = >=3.6 install_requires = requests pytest colorama pygments typing-extensions [options.packages.find] where = src [options.extras_require] dev = pytest flake8 black mypy [options.package_data] battle_tested = py.typed ``` -------------------------------- ### Install Battle Tested Python Package Source: https://github.com/codykochmann/battle_tested/blob/master/README.md This command installs the 'battle_tested' Python package using pip. It is recommended to install it for user-specific use. ```shell pip install --user battle_tested ``` -------------------------------- ### Fuzz Function with Battle Tested in Python Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/quick_fuzzing.ipynb This snippet demonstrates how to use the `fuzz` function from the `battle_tested` library in Python. It takes the function to be tested as an argument, initiating the fuzzing process. ```python from battle_tested import fuzz fuzz(function_I_want_to_test) ``` -------------------------------- ### Define Function for Fuzzing in Python Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/quick_fuzzing.ipynb This snippet defines a Python function `function_I_want_to_test` that takes one argument. This function is intended to be tested or 'hardened' using fuzzing techniques. ```python def function_I_want_to_test(arg1): """this is a function we want to harden""" i=arg1 ``` -------------------------------- ### Import battle_tested library Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/breaking_simple_things.ipynb Imports the necessary fuzz and battle_tested components from the battle_tested library for fuzz testing. ```python from battle_tested import fuzz, battle_tested ``` -------------------------------- ### Fuzz test add_to_hello_with_format function Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/breaking_simple_things.ipynb Applies fuzz testing to the `add_to_hello_with_format` function using the battle_tested library. The test runs for 1 second and continues testing as long as possible. ```python def add_to_hello_with_format(a): return 'hello {}'.format(a) add_to_hello_with_format('world') ``` ```python fuzz(add_to_hello_with_format, seconds=1, keep_testing=True) ``` -------------------------------- ### Fuzz test add_to_hello function Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/breaking_simple_things.ipynb Applies fuzz testing to the `add_to_hello` function using the battle_tested library. The test runs for 1 second and continues testing as long as possible. ```python def add_to_hello(a): return 'hello '+a add_to_hello('world') ``` ```python fuzz(add_to_hello, seconds=1, keep_testing=True) ``` -------------------------------- ### Fuzz SqliteDict with Battle_tested Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_a_feeler.ipynb Uses the `battle_tested.fuzz` function to test the `harness` function with various inputs. Setting `keep_testing=True` ensures that all crashes are collected for analysis. ```python from battle_tested import fuzz, success_map, crash_map fuzz(harness, keep_testing=True) # keep testing allows us to collect "all" crashes ``` -------------------------------- ### Harness SqliteDict Key-Value Assignment Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_a_feeler.ipynb Defines a harness function to test what types of keys and values can be assigned to a SqliteDict instance. It creates an in-memory SqliteDict and attempts to assign a given key-value pair. ```python from sqlitedict import SqliteDict def harness(key, value): """ this tests what can be assigned in SqliteDict's keys and values """ mydict = SqliteDict(":memory:") mydict[key] = value ``` -------------------------------- ### Analyze SqliteDict Successes Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_a_feeler.ipynb Calls the `success_map()` function from `battle_tested` to display a map of inputs that were successfully processed without causing crashes. ```python success_map() ``` -------------------------------- ### Harden list_of_strings_v1 with battle_tested fuzz Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/hardening_filters.ipynb This snippet demonstrates how to use the `fuzz` function from the `battle_tested` library to test the `list_of_strings_v1` function. It highlights how fuzz testing can reveal unexpected issues in seemingly robust code. ```python from battle_tested import fuzz fuzz(list_of_strings_v1) ``` -------------------------------- ### Fuzz test count function Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/breaking_simple_things.ipynb Applies fuzz testing to the `count` function using the battle_tested library. The test runs for 1 second and continues testing as long as possible. ```python def count(a): return len(a) count('hello') ``` ```python fuzz(count, seconds=1, keep_testing=True) ``` -------------------------------- ### Define and harden list_of_strings_v2 with battle_tested fuzz Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/hardening_filters.ipynb This snippet defines an improved version of `list_of_strings_v2` which includes error handling for non-iterable inputs. It then uses `battle_tested.fuzz` to validate the robustness of this enhanced function. ```python from battle_tested import fuzz def list_of_strings_v2(iterable): """ converts the iterable input into a list of strings """ try: iter(iterable) # build the output out = [str(i) for i in iterable] except TypeError: # raised when input was not iterable out = [str(iterable)] # validate the output for i in out: assert type(i) == str # return return out fuzz(list_of_strings_v2) ``` -------------------------------- ### Fuzzing API Login Endpoint with Python Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/api_gatling_gun.ipynb This Python function `api_gatling_gun` uses the `requests` library to send POST requests to a login endpoint. It takes user and password as parameters and includes basic error handling for the request. The function is intended to be used with a fuzzing tool like `battle_tested`. ```python import requests def api_gatling_gun(a, b): try: requests.post('https://mysite.com/login', params={ 'user':a, 'password':b }) except Exception: # all exceptions here will be issues form requests.post # look at your server logs to see what breaks pass fuzz(api_gatling_gun) ``` -------------------------------- ### Analyze SqliteDict Crashes Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_a_feeler.ipynb Calls the `crash_map()` function from `battle_tested` to display a map of inputs that caused crashes during the fuzzing process. ```python crash_map() ``` -------------------------------- ### Fuzzing with Battle Tested Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/interpreting_the_results.ipynb Demonstrates how to use the fuzz function from the battle_tested library to perform fuzz testing on an integer type. The 'quiet=True' argument suppresses verbose output during the fuzzing process. ```Python from battle_tested import fuzz fuzz(int, quiet=True) ``` -------------------------------- ### Silence IPython Debugger Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_a_feeler.ipynb This snippet silences the IPython debugger by setting the mode to plain. ```python # this is just to silence %xmode plain ``` -------------------------------- ### Fuzz Testing a Python Function with battle_tested Source: https://github.com/codykochmann/battle_tested/blob/master/README.md This snippet demonstrates how to use the 'fuzz' function from the 'battle_tested' library to test a custom Python function. The 'test' function is designed to convert its input to an integer. The 'fuzz' function then generates various inputs to test the robustness of 'test', reporting on successful inputs, exceptions, and crashes. ```Python from battle_tested import fuzz def test(a): return int(a) fuzz(test) ``` -------------------------------- ### Decorate Function for Fuzz Testing Source: https://github.com/codykochmann/battle_tested/blob/master/README.md Applies the `@battle_tested` decorator to a Python function to enable fuzz testing. This decorator automatically generates various inputs for the decorated function, helping to discover potential issues. It supports configuration options like `default_output`, `verbose`, `seconds`, and `max_tests`. ```Python from battle_tested import battle_tested, fuzz @battle_tested(default_output=[], verbose=True, seconds=1, max_tests=5) def sample(i): return [] @battle_tested() def sample2(a,b,c,d=''): t = a, b, c, d # proof that they only get tested once print(sample(4)) print(sample2(1,2,3,4)) print(sample('i')) print(sample2('a','b',2,4)) ``` -------------------------------- ### Fuzz Test a Function Directly Source: https://github.com/codykochmann/battle_tested/blob/master/README.md Uses the `fuzz` function from the `battle_tested` library to perform fuzz testing on a given Python function. This approach is useful for testing functions that are not decorated or when a more direct control over the fuzzing process is needed. It allows specifying parameters like `seconds` and `verbose`. ```Python def sample3(input_arg): return True fuzz(sample3, seconds=5, verbose=True) ``` -------------------------------- ### Battle Tested Decorator for Function Testing Source: https://github.com/codykochmann/battle_tested/blob/master/tutorials/as_decorators.ipynb This snippet demonstrates the usage of the `@battle_tested()` decorator on a Python function. The decorator aims to automatically generate test code for the decorated function, simplifying the testing process. The `hardened_int` function is designed to convert its input to an integer, returning 0 if the conversion fails. ```python %xmode plain ``` ```python from battle_tested import battle_tested @battle_tested() def hardened_int(a): """ makes an int no matter what """ try: return int(a) except (ValueError, TypeError): return 0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.