Ejemplo n.º 1
0
def main():
    """The main function reads the path to the xml file
    and optionally the hash algorithm from the command
    line arguments and calls the actual algorithm.
    """

    args = command_line_parsing()

    logging.debug("Running cli tool with arguments %s", args)

    for filename in args.file:
        # ACTUAL ALGORITHM CALL:
        (hashes,
         prehashes) = hash_generator.epcis_hash(filename, args.algorithm,
                                                args.join)

        # Output:
        if args.batch:
            with open(os.path.splitext(filename)[0] + '.hashes',
                      'w') as outfile:
                outfile.write("\n".join(hashes) + "\n")
            if args.prehash:
                with open(os.path.splitext(filename)[0] + '.prehashes',
                          'w') as outfile:
                    outfile.write("\n".join(prehashes) + "\n")
        else:
            print("\n\nHashes of the events contained in '{}':\n".format(
                filename) + "\n".join(hashes))
            if args.prehash:
                print("\nPre-hash strings:\n" + "\n---\n".join(prehashes))
Ejemplo n.º 2
0
def test_equal():
    """
    Assert that different representations of the same events have the same hash.
    """
    for (_, _, filenames) in walk(TEST_FILE_PATH_SAME_EVENT):
        for filename in filenames:
            if filename.endswith("xml") or filename.endswith("json"):
                assert len(
                    set(
                        epcis_hash(TEST_FILE_PATH_SAME_EVENT + filename,
                                   "sha256")[0])
                ) == 1, "The events in {} have different hashes!".format(
                    TEST_FILE_PATH_SAME_EVENT + filename)
        break
Ejemplo n.º 3
0
def test_explicit_hash_values():
    num_tested = 0
    for (_, _, filenames) in walk(TEST_FILE_PATH):
        for filename in filenames:
            if filename.endswith("xml") or filename.endswith("json"):
                print("Testing file " + filename)
                actualHashes = epcis_hash(TEST_FILE_PATH + filename,
                                          "sha256")[0]
                assert len(actualHashes) > 0
                with open(
                        TEST_FILE_PATH + path.splitext(filename)[0] +
                        '.hashes', 'r') as expectedfile:
                    expectedHashes = expectedfile.read().splitlines()
                assert actualHashes == expectedHashes, "Hash for {} is not as expected!".format(
                    filename)
                num_tested += 1
        break
    assert num_tested > 0
def _check_values(filename):
    if filename.endswith("xml"):
        print("Testing XML file " + filename)
        events = event_list_from_epcis_document_xml(filename)
    elif filename.endswith("json"):
        print("Testing JSON file " + filename)
        events = event_list_from_epcis_document_json(filename)
    else:
        return False

    prehash_string_list = epcis_hash(filename, "sha256")[1]
    assert len(prehash_string_list) > 0

    for (py_obj, prehash_string) in zip(events[2], prehash_string_list):
        key_values = _py_to_value_list(py_obj)
        for key, value in key_values:
            msg = "Value '{}' for key '{}' not contained in prehash string\n{}\n for file {}".format(
                value, key, prehash_string, filename)
            assert prehash_string.find(value) >= 0, msg

    return True
Ejemplo n.º 5
0
def test_distinct():
    """
    Assert that there are no collisions, i.e. different events must have different hashes.
    """
    all_hashes = []

    for (_, _, filenames) in walk(TEST_FILE_PATH):
        for filename in filenames:
            if filename.endswith("xml"):
                all_hashes += epcis_hash(TEST_FILE_PATH + filename,
                                         "sha256")[0]
        break

    assert all_hashes
    duplicates = set()
    for hash in all_hashes:
        if all_hashes.count(hash) > 1:
            duplicates.add(hash)

    assert len(
        duplicates
    ) == 0, "The following hashes appeared multiple times: " + str(duplicates)