示例#1
0
def test_digest_too_early():
    ion_str = "{ a: 1, b: 2 }"
    hr = hash_reader(binary_reader_over(ion_str),
                     hash_function_provider("identity"))
    hr.send(NEXT_EVENT)
    with pytest.raises(Exception):
        hr.send(HashEvent.DIGEST)
def test_hashlib_hash_function_provider():
    ion_str = '[1, 2, {a: 3, b: (4 {c: 5} 6) }, 7]'
    algorithm = "md5"

    # calculate expected digest
    hr = hash_reader(binary_reader_over(ion_str),
                     hash_function_provider(algorithm))
    consume(hr)
    expected_digest = hr.send(HashEvent.DIGEST)

    # calculate digest using hashlib_hash_function_provider
    hr = hash_reader(binary_reader_over(ion_str),
                     hashlib_hash_function_provider(algorithm))
    consume(hr)
    actual_digest = hr.send(HashEvent.DIGEST)

    assert actual_digest == expected_digest
示例#3
0
def test_digest_too_late():
    ion_str = "{ a: 1, b: 2 }"
    reader = binary_reader_over(ion_str)
    reader.send(NEXT_EVENT)

    hr = hash_reader(reader, hash_function_provider("identity"))
    hr.send(NEXT_EVENT)
    hr.send(NEXT_EVENT)
    with pytest.raises(Exception):
        hr.send(NEXT_EVENT)
示例#4
0
def _run_test(ion_str, skip_list, algorithm):
    r = binary_reader_over(ion_str)
    r_events = consume(r, skip_list)

    hr = hash_reader(binary_reader_over(ion_str), hash_function_provider(algorithm))
    hr_events = consume(hr, skip_list)

    # assert reader/hash_reader response behavior is identical
    assert hr_events == r_events

    return hr.send(HashEvent.DIGEST)
    def _f(algorithm):
        buf.seek(0)
        reader = hash_reader(
            ion_reader.blocking_reader(managed_reader(reader_provider(), None),
                                       buf),
            hash_function_provider(algorithm, _actual_updates,
                                   _actual_digests))

        _consume(reader)

        return reader.send(HashEvent.DIGEST)
    def skipping_consumer(algorithm):
        buf.seek(0)
        reader = hash_reader(
            ion_reader.blocking_reader(
                managed_reader(_reader_provider("binary")(), None), buf),
            hash_function_provider(algorithm, _actual_updates,
                                   _actual_digests))

        event = reader.send(NEXT_EVENT)
        while event.event_type != IonEventType.STREAM_END:
            if event.event_type == IonEventType.CONTAINER_START:
                event = reader.send(SKIP_EVENT)
            else:
                event = reader.send(NEXT_EVENT)

        return reader.send(HashEvent.DIGEST)
示例#7
0
def test_hash_reader():
    ion_str = '[1, 2, {a: 3, b: (4 {c: 5} 6) }, 7]'
    algorithm = "md5"

    # calculate max # of events
    max_events = len(consume(binary_reader_over(ion_str)))

    # calculate expected digest
    hr = hash_reader(binary_reader_over(ion_str), hash_function_provider(algorithm))
    consume(hr)
    expected_digest = hr.send(HashEvent.DIGEST)

    # verify events produced by reader and hash_reader are identical
    for i in range(0, max_events - 1):
        skip_list = [i] if i > 0 else []

        # verify the hash_reader's digest matches expected digest
        assert _run_test(ion_str, skip_list, algorithm) == expected_digest
def test_no_fieldname_in_hash(test_data):
    """
    This test verifies a hash_reader/writer that receives field events but did not
    receive the preceeding "begin struct" event DOES NOT include the field_name as
    part of the hash.
    """
    reader = binary_reader_over(test_data.ion_str)
    events = consume(reader)

    buf = BytesIO()
    writer = blocking_writer(raw_writer(), buf)
    writer.send(IonEvent(IonEventType.CONTAINER_START, IonType.STRUCT))

    hw = hash_writer(writer, hash_function_provider("identity"))
    for e in events[:-1]:
        field_name = e.field_name
        if e.depth == 0:
            field_name = SymbolToken("field_name", None)
        new_event = IonEvent(e.event_type, e.ion_type, e.value, field_name, e.annotations, e.depth + 1)
        hw.send(new_event)
    writer.send(IonEvent(IonEventType.CONTAINER_END, IonType.STRUCT))
    writer.send(events[-1])     # send the final event (which should be a STREAM_END event)

    output = buf.getvalue()
    hw_digest = hw.send(HashEvent.DIGEST)
    assert hw_digest == test_data.expected_digest

    reader = binary_reader_over(output)
    hr_digest = b''
    while True:
        event = reader.send(NEXT_EVENT)
        if event.event_type == IonEventType.CONTAINER_START:
            hr = hash_reader(reader, hash_function_provider("identity"))
            for i in range(0, len(events) - 1):
                e = hr.send(NEXT_EVENT)
            hr_digest = hr.send(HashEvent.DIGEST)
        if event.event_type == IonEventType.STREAM_END:
            break
    assert hr_digest == test_data.expected_digest