Skip to content

Commit

Permalink
Restructure comments and harmonize module naming
Browse files Browse the repository at this point in the history
Restructured the comments to be in line with PEP 8 style guidelines.
Renamed also `utils` to have more descriptive name and to be more in
line with the naming convention used in all the other modules.
  • Loading branch information
cobaltine committed May 13, 2023
1 parent 6891909 commit a33f435
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 57 deletions.
2 changes: 1 addition & 1 deletion json_fingerprint/_create.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ._jfpv1 import _create_jfpv1_fingerprint
from ._utils import _load_json
from ._load_json import _load_json
from ._validators import (
_validate_hash_function,
_validate_input_type,
Expand Down
5 changes: 3 additions & 2 deletions json_fingerprint/_find_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def _create_input_fingerprints(input: str, target_hashes: List[Dict]) -> List[st


def find_matches(input: str, fingerprints: List[str], deduplicate: bool = False) -> List[str]:
"""Match raw json str input to a list of fingerprints.
"""Match raw json string input to a list of fingerprints
Decodes the target fingerprints and creates a fingerprint from the input with identical parameters.
Creates a fingerprint from the input of each different JSON fingerprint type present in the fingerprint list."""
Creates a fingerprint from the input of each different JSON fingerprint type present in the fingerprint list.
"""
if deduplicate:
fingerprints = list(set(fingerprints))
target_hashes = _get_target_hashes(fingerprints=fingerprints)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion json_fingerprint/_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def match(input: str, target_fingerprint: str) -> bool:
"""Match raw json str input to target fingerprint.
"""Match raw json string input to target fingerprint
Decodes the target fingerprint and creates a fingerprint from the input with identical parameters."""
version, hash_function, _ = decode(fingerprint=target_fingerprint)
Expand Down
6 changes: 3 additions & 3 deletions json_fingerprint/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def _validate_hash_function(hash_function: str, version: int):
if version == 1 and hash_function not in JFPV1_HASH_FUNCTIONS:
err = f"Expected one of supported hash functions '{JFPV1_HASH_FUNCTIONS}', " f"instead got '{hash_function}'"
err = f"Expected one of supported hash functions '{JFPV1_HASH_FUNCTIONS}', instead got '{hash_function}'"
raise FingerprintHashFunctionError(err)


Expand All @@ -34,7 +34,7 @@ def _validate_input_type(input: str):

def _validate_version(version: int):
if version not in JSON_FINGERPRINT_VERSIONS:
err = f"Expected one of supported JSON fingerprint versions '{JSON_FINGERPRINT_VERSIONS}', " f"instead got '{version}'"
err = f"Expected one of supported JSON fingerprint versions '{JSON_FINGERPRINT_VERSIONS}', instead got '{version}'"
raise FingerprintVersionError(err)


Expand All @@ -45,5 +45,5 @@ def _validate_fingerprint_format(fingerprint: str):
is_valid = True

if not is_valid:
err = "Expected JSON fingerprint in format '{fingerprint_version}${hash_function}${hex_digest}', instead got: " f"{fingerprint}"
err = "Expected JSON fingerprint in format '{fingerprint_version}${hash_function}${hex_digest}', " f"instead got: {fingerprint}"
raise FingerprintStringFormatError(err)
43 changes: 25 additions & 18 deletions json_fingerprint/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,48 @@

class TestCreate(unittest.TestCase):
def test_jfpv1_json_load_error(self):
"""Test json fingerprint raw json string load error.
"""Test json fingerprint raw json string load error
Verify that:
- FingerprintJSONLoadError is properly raised with malformed json input string"""
- FingerprintJSONLoadError is properly raised with malformed json input string
"""
with self.assertRaises(FingerprintJSONLoadError):
create('{"foo": bar}', hash_function="sha256", version=1)

def test_jfpv1_sha256_output_format(self):
"""Test jfpv1 output format.
"""Test jfpv1 output format
Verify that:
- Complete jfpv1-sha256 output fingerprint is properly formatted"""
- Complete jfpv1-sha256 output fingerprint is properly formatted
"""
fp = create(input='{"foo": "bar"}', hash_function="sha256", version=1)
self.assertRegex(fp, "^jfpv1\\$sha256\\$[0-9a-f]{64}$")

def test_jfpv1_sha384_output_format(self):
"""Test jfpv1 output format.
"""Test jfpv1 output format
Verify that:
- Complete jfpv1-sha256 output fingerprint is properly formatted"""
- Complete jfpv1-sha256 output fingerprint is properly formatted
"""
fp = create(input='{"foo": "bar"}', hash_function="sha384", version=1)
self.assertRegex(fp, "^jfpv1\\$sha384\\$[0-9a-f]{96}$")

def test_jfpv1_sha512_output_format(self):
"""Test jfpv1 output format.
"""Test jfpv1 output format
Verify that:
- Complete jfpv1-sha256 output fingerprint is properly formatted"""
- Complete jfpv1-sha256 output fingerprint is properly formatted
"""
fp = create(input='{"foo": "bar"}', hash_function="sha512", version=1)
self.assertRegex(fp, "^jfpv1\\$sha512\\$[0-9a-f]{128}$")

def test_jfpv1_sha256_mixed_order(self):
"""Test jfpv1 sha256 mixed order fingerprint match.
"""Test jfpv1 sha256 mixed order fingerprint match
Verify that:
- The fingerprints of test objects 1 and 2 match despite same data being ordered differently
- The fingerprints also match against a known valid fingerprint"""
- The fingerprints also match against a known valid fingerprint
"""
with open(os.path.join(TESTDATA_DIR, "jfpv1_test_obj_1.json"), "r") as file:
self.test_obj_1 = file.read()
file.close()
Expand All @@ -61,11 +66,12 @@ def test_jfpv1_sha256_mixed_order(self):
self.assertEqual(fp_1, "jfpv1$sha256$b182c755347a6884fd11f1194cbe0961f548e5ac62be78a56c48c3c05eb56650")

def test_jfpv1_sha256_structural_distinction_1(self):
"""Test jfpv1 json flattener's structural value distinction.
"""Test jfpv1 json flattener's structural value distinction
Verify that:
- Identical values at identical depths, but held in different data structures,
don't produce identical outputs"""
don't produce identical outputs
"""
obj_in_1 = [
1,
[1, [2, 2]],
Expand All @@ -82,10 +88,11 @@ def test_jfpv1_sha256_structural_distinction_1(self):
self.assertNotEqual(fp_1, fp_2)

def test_jfpv1_sha256_structural_distinction_2(self):
"""Test jfpv1 json flattener's structural value distinction.
"""Test jfpv1 json flattener's structural value distinction
Verify that:
- Values in identical data structure paths, but different sibling values, don't get matched"""
- Values in identical data structure paths, but different sibling values, don't get matched
"""
obj_in_1 = [
[1, ["x", "x"]],
[2, ["y", "y"]],
Expand All @@ -101,14 +108,14 @@ def test_jfpv1_sha256_structural_distinction_2(self):
self.assertNotEqual(fp_1, fp_2)

def test_jfpv1_empty_list_as_value(self):
"""Test jfpv1 json flattener's ability to handle empty lists as values.
"""Test jfpv1 json flattener's ability to handle empty lists as values
Versions up to 0.12.2 did not acknowledge empty lists as values.
Related issue: https://github.com/cobaltine/json-fingerprint/issues/33
Verify that:
- Empty lists (and, as such, underlying data structure paths) are not ignored by the json flattener"""

- Empty lists (and, as such, underlying data structure paths) are not ignored by the json flattener
"""
obj_in_1 = {"field1": "yes"}
fp_1 = create(input=json.dumps(obj_in_1), hash_function="sha256", version=1)

Expand All @@ -118,7 +125,7 @@ def test_jfpv1_empty_list_as_value(self):
self.assertNotEqual(fp_1, fp_2)

def test_jfpv1_empty_dict_as_value(self):
"""Test jfpv1 json flattener's ability to handle empty dicts as values.
"""Test jfpv1 json flattener's ability to handle empty dicts as values
Versions up to 0.12.2 did not acknowledge empty dicts as values.
Related issue: https://github.com/cobaltine/json-fingerprint/issues/33
Expand Down
3 changes: 2 additions & 1 deletion json_fingerprint/tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def test_jfpv1_decode(self):
Verify that:
- Fingerprints of all jfpv1 SHA-2 variants are properly decoded
- Exception is properly raised with invalid fingerprint input"""
- Exception is properly raised with invalid fingerprint input
"""
input = json.dumps({"foo": "bar"})
jfpv1_sha256 = create(input=input, hash_function="sha256", version=1)
jfpv1_sha384 = create(input=input, hash_function="sha384", version=1)
Expand Down
9 changes: 6 additions & 3 deletions json_fingerprint/tests/test_find_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def test_get_target_hashes(self):
"""Test target hash list creation.
Verify that:
- The returned element list contains only unique entries"""
- The returned element list contains only unique entries
"""
# Fingerprint list with duplicate entries
fingerprints = [
self.jfpv1_sha256,
Expand All @@ -39,7 +40,8 @@ def test_create_input_fingerprints(self):
"""Test list matching's fingerprint creation function.
Verify that:
- Fingerprints are correctly parsed from the given target hash elements"""
- Fingerprints are correctly parsed from the given target hash elements
"""
target_hashes = [
{"version": 1, "hash_function": "sha256"},
{"version": 1, "hash_function": "sha384"},
Expand All @@ -58,7 +60,8 @@ def test_jfpv1_find_matches(self):
Verify that:
- Fingerprints of all jfpv1 SHA-2 variants are properly matched in a fingerprint list
- Deduplication works for duplicate entries in fingerprint list
- Exceptions are properly raised with invalid fingerprints and input types"""
- Exceptions are properly raised with invalid fingerprints and input types
"""
input = json.dumps({"bar": "foo"})
chaff_jfpv1_sha256 = create(input=input, hash_function="sha256", version=1)

Expand Down
47 changes: 28 additions & 19 deletions json_fingerprint/tests/test_jfpv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

class TestJfpv1(unittest.TestCase):
def test_create_json_hash(self):
"""Test jfpv1 json sha256 hash creation.
"""Test jfpv1 json sha256 hash creation
Verify that:
- Key-value pairs are sorted properly and the resulting SHA256 matches the expected hash"""
- Key-value pairs are sorted properly and the resulting SHA256 matches the expected hash
"""
data = {"value": "bar", "path": "{foo}"}
stringified = json.dumps(
data,
Expand All @@ -28,11 +29,12 @@ def test_create_json_hash(self):
self.assertEqual(hex_digest, expected_hex_digest)

def test_jfpv1_create_sorted_hash_list(self):
"""Test jfpv1 hash list, used for condensing unique identifiers into an easily sortable list.
"""Test jfpv1 hash list used for condensing unique identifiers into an easily sortable list
Verify that:
- The hash list produces valid SHA256 hashes from json-formatted data
- Sorts the hashes properly"""
- Sorts the hashes properly
"""
input_data = [
# SHA256 (json-formatted): ac8d8342bbb2362d13f0a559a3621bb407011368895164b628a54f7fc33fc43c
"a",
Expand All @@ -54,13 +56,14 @@ def test_jfpv1_create_sorted_hash_list(self):
self.assertEqual(input_data_hashes, output_data_hashes)

def test_jfpv1_build_path(self):
"""Test jfpv1 raw path formatting.
"""Test jfpv1 raw path formatting
Verify that:
- Path list and dict element encapsulation work as intended
- Combination paths are separated properly (pipe character)
- List and dict indicators, and path separator, display correctly even if same
characters ([]{}|) are used in dict field names"""
characters ([]{}|) are used in dict field names
"""
root = ""
dict_key = "{foo}" # Dict with a field named 'foo'
list_key = "[5]" # List with 5 elements in it
Expand Down Expand Up @@ -91,7 +94,8 @@ def test_jfpv1_flattened_json_sha256_fingerprint(self):
Verify that:
- jfpv1 json flattener produces expected raw output format (non-hashed siblings in debug mode)
- jfpv1 json flattener produces expected standard output format (hashed siblings)
- jfpv1 json flattener output will produce valid jfpv1 fingerprints"""
- jfpv1 json flattener output will produce valid jfpv1 fingerprints
"""
obj_in = [
1,
[2, 3],
Expand Down Expand Up @@ -138,62 +142,67 @@ def test_jfpv1_flattened_json_sha256_fingerprint(self):
self.assertEqual(jfpv1_out_1, jfpv1_out_2)

def test_jfpv1_json_flattener_primitive_integer_handling(self):
"""Test jfpv1 primitive integer handling.
"""Test jfpv1 primitive integer handling
Verify that:
- Integers are 'flattened' correctly (path, siblings and value)
- Fingerprint matches with pre-verified fingerprint"""
- Fingerprint matches with pre-verified fingerprint
"""
int_val = 123
int_out_raw = _jfpv1._flatten_json(data=int_val, hash_function="sha256")
expected_int_out_raw = [{"path": "", "value": int_val}]
self.assertEqual(int_out_raw, expected_int_out_raw)

def test_jfpv1_json_flattener_primitive_float_handling(self):
"""Test jfpv1 primitive float handling.
"""Test jfpv1 primitive float handling
Verify that:
- Floats are 'flattened' correctly (path, siblings and value)
- Fingerprint matches with pre-verified fingerprint"""
- Fingerprint matches with pre-verified fingerprint
"""
float_val = 123.321
float_out_raw = _jfpv1._flatten_json(data=float_val, hash_function="sha256")
expected_float_out_raw = [{"path": "", "value": float_val}]
self.assertEqual(float_out_raw, expected_float_out_raw)

def test_jfpv1_json_flattener_primitive_string_handling(self):
"""Test jfpv1 primitive string handling.
"""Test jfpv1 primitive string handling
Verify that:
- Strings are 'flattened' correctly (path, siblings and value)
- Fingerprint matches with pre-verified fingerprint"""
- Fingerprint matches with pre-verified fingerprint
"""
string_val = "alpha 123"
string_out_raw = _jfpv1._flatten_json(data=string_val, hash_function="sha256")
expected_string_out_raw = [{"path": "", "value": string_val}]
self.assertEqual(string_out_raw, expected_string_out_raw)

def test_jfpv1_json_flattener_primitive_boolean_handling(self):
"""Test jfpv1 primitive boolean handling.
"""Test jfpv1 primitive boolean handling
Verify that:
- Booleans are 'flattened' correctly (path, siblings and value)
- Fingerprint matches with pre-verified fingerprint"""
- Fingerprint matches with pre-verified fingerprint
"""
bool_val = True
bool_out_raw = _jfpv1._flatten_json(data=bool_val, hash_function="sha256")
expected_bool_out_raw = [{"path": "", "value": bool_val}]
self.assertEqual(bool_out_raw, expected_bool_out_raw)

def test_jfpv1_json_flattener_primitive_none_handling(self):
"""Test jfpv1 primitive boolean handling.
"""Test jfpv1 primitive boolean handling
Verify that:
- Booleans are 'flattened' correctly (path, siblings and value)
- Fingerprint matches with pre-verified fingerprint"""
- Fingerprint matches with pre-verified fingerprint
"""
none_val = None
none_out_raw = _jfpv1._flatten_json(data=none_val, hash_function="sha256")
expected_bool_out_raw = [{"path": "", "value": none_val}]
self.assertEqual(none_out_raw, expected_bool_out_raw)

def test_jfpv1_json_flattener_empty_list_handling(self):
"""Test jfpv1 json flattener's ability to handle empty lists as values.
"""Test jfpv1 json flattener's ability to handle empty lists as values
Versions up to v0.12.2 did not acknowledge empty lists as values.
Related issue: https://github.com/cobaltine/json-fingerprint/issues/33
Expand All @@ -207,7 +216,7 @@ def test_jfpv1_json_flattener_empty_list_handling(self):
self.assertEqual(empty_list_out_raw, expected_emtpy_list_out_raw)

def test_jfpv1_json_flattener_empty_dict_handling(self):
"""Test jfpv1 json flattener's ability to handle empty dicts as values.
"""Test jfpv1 json flattener's ability to handle empty dicts as values
Versions up to v0.12.2 did not acknowledge empty dicts as values.
Related issue: https://github.com/cobaltine/json-fingerprint/issues/33
Expand Down
5 changes: 3 additions & 2 deletions json_fingerprint/tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

class TestMatch(unittest.TestCase):
def test_jfpv1_match(self):
"""Test json fingerprint matcher.
"""Test json fingerprint matcher
Verify that:
- Fingerprints of all jfpv1 SHA-2 variants are properly matched
- Exceptions are properly raised with invalid fingerprints and input types"""
- Exceptions are properly raised with invalid fingerprints and input types
"""
input = json.dumps({"foo": "bar"})
jfpv1_sha256 = create(input=input, hash_function="sha256", version=1)
jfpv1_sha384 = create(input=input, hash_function="sha384", version=1)
Expand Down
Loading

0 comments on commit a33f435

Please sign in to comment.