Exemplo n.º 1
0
def test_anonymize_ip_order_independent():
    """Test to make sure order does not affect anonymization of addresses."""
    anonymizer_v4_forward = IpAnonymizer(SALT)
    ip_lookup_forward = {}
    for ip_addr in ip_v4_list:
        ip_int = int(anonymizer_v4_forward.make_addr(ip_addr))
        ip_int_anon = anonymizer_v4_forward.anonymize(ip_int)
        ip_lookup_forward[ip_int] = ip_int_anon

    anonymizer_v4_reverse = IpAnonymizer(SALT)
    for ip_addr in reversed(ip_v4_list):
        ip_int_reverse = int(anonymizer_v4_reverse.make_addr(ip_addr))
        ip_int_anon_reverse = anonymizer_v4_reverse.anonymize(ip_int_reverse)
        # Confirm anonymizing in reverse order does not affect
        # anonymization results
        assert ip_int_anon_reverse == ip_lookup_forward[ip_int_reverse]

    anonymizer_v4_extras = IpAnonymizer(SALT)
    for ip_addr in ip_v4_list:
        ip_int_extras = int(anonymizer_v4_extras.make_addr(ip_addr))
        ip_int_anon_extras = anonymizer_v4_extras.anonymize(ip_int_extras)
        ip_int_inverted = ip_int_extras ^ 0xFFFFFFFF
        anonymizer_v4_extras.anonymize(ip_int_inverted)
        # Confirm anonymizing with extra addresses in-between does not
        # affect anonymization results
        assert ip_int_anon_extras == ip_lookup_forward[ip_int_extras]
Exemplo n.º 2
0
def test_preserve_custom_addresses():
    """Test that addresses within a preserved block are flagged correctly as NOT needing anonymization."""
    addresses = [
        "170.0.0.0/8",
        "11.11.11.11",
    ]
    anonymizer = IpAnonymizer(SALT, preserve_addresses=addresses)

    ip_start = int(anonymizer.make_addr("170.0.0.0"))
    ip_end = int(anonymizer.make_addr("170.255.255.255"))
    ip_other = int(anonymizer.make_addr("11.11.11.11"))
    ip_outside = int(anonymizer.make_addr("10.11.12.13"))

    # Make sure anonymizer indicates addresses within preserved network blocks should not be anonymized
    assert not anonymizer.should_anonymize(ip_start)
    assert not anonymizer.should_anonymize(ip_end)
    assert not anonymizer.should_anonymize(ip_other)

    # Make sure the address outside the preserved block should be anonymized
    assert anonymizer.should_anonymize(ip_outside)
Exemplo n.º 3
0
def test_preserve_custom_prefixes():
    """Test that a custom prefix is preserved correctly."""
    subnet = "170.0.0.0/8"
    anonymizer = IpAnonymizer(SALT, [subnet])

    ip_start = int(anonymizer.make_addr("170.0.0.0"))
    ip_start_anon = anonymizer.anonymize(ip_start)

    ip_end = int(anonymizer.make_addr("170.255.255.255"))
    ip_end_anon = anonymizer.anonymize(ip_end)

    network = ipaddress.ip_network(subnet)

    # Make sure the anonymized addresses are different from the originals
    assert ip_start_anon != ip_start
    assert ip_end_anon != ip_end

    # Make sure the anonymized addresses have the same prefix as the originals
    assert ipaddress.ip_address(ip_start_anon) in network
    assert ipaddress.ip_address(ip_end_anon) in network