Ejemplo n.º 1
0
def test_ipv4_range_to_prefixes():
    assert [repr(x) for x in IPv4.range_to_prefixes('192.168.0.2', '192.168.0.2')] == [
        "<IPv4 192.168.0.2/32>"
    ]
    assert [repr(x) for x in IPv4.range_to_prefixes('192.168.0.2', '192.168.0.16')] == [
        "<IPv4 192.168.0.2/31>", "<IPv4 192.168.0.4/30>", "<IPv4 192.168.0.8/29>",
        "<IPv4 192.168.0.16/32>"
    ]
    assert [repr(x)
            for x in IPv4.range_to_prefixes('0.0.0.0', '255.255.255.255')] == ["<IPv4 0.0.0.0/0>"]
Ejemplo n.º 2
0
 def forwards(self):
     PrefixTable = db.mock_model(model_name="PrefixTable",
         db_table="main_prefixtable", db_tablespace="", pk_field_name="id",
         pk_field_type=models.AutoField)
     db.add_column(
         "sa_activator", "prefix_table",
         models.ForeignKey(
             PrefixTable,
             verbose_name=_("Prefix Table"), null=True, blank=True)
     )
     # Migrate data
     for id, name, ip, to_ip in db.execute(
             "SELECT id, name, ip, to_ip FROM sa_activator"):
         pt_name = "Activator::%s" % name
         db.execute(
             """
             INSERT INTO main_prefixtable(name)
             VALUES(%s)
             """, [pt_name])
         pt_id, = db.execute("SELECT id FROM main_prefixtable WHERE name = %s", [pt_name])[0]
         for p in IPv4.range_to_prefixes(ip, to_ip):
             db.execute("""
                 INSERT INTO main_prefixtableprefix(table_id, afi, prefix)
                 VALUES(%s, '4', %s)
                 """, [pt_id, p.prefix])
         db.execute("UPDATE sa_activator SET prefix_table_id=%s WHERE id=%s", [pt_id, id])
Ejemplo n.º 3
0
 def migrate(self):
     PrefixTable = self.db.mock_model(model_name="PrefixTable",
                                      db_table="main_prefixtable")
     self.db.add_column(
         "sa_activator",
         "prefix_table",
         models.ForeignKey(
             PrefixTable,
             verbose_name="Prefix Table",
             null=True,
             blank=True,
             on_delete=models.CASCADE,
         ),
     )
     # Migrate data
     for id, name, ip, to_ip in self.db.execute(
             "SELECT id, name, ip, to_ip FROM sa_activator"):
         pt_name = "Activator::%s" % name
         self.db.execute(
             """
             INSERT INTO main_prefixtable(name)
             VALUES(%s)
             """,
             [pt_name],
         )
         pt_id, = self.db.execute(
             "SELECT id FROM main_prefixtable WHERE name = %s",
             [pt_name])[0]
         for p in IPv4.range_to_prefixes(ip, to_ip):
             self.db.execute(
                 """
                 INSERT INTO main_prefixtableprefix(table_id, afi, prefix)
                 VALUES(%s, '4', %s)
                 """,
                 [pt_id, p.prefix],
             )
         self.db.execute(
             "UPDATE sa_activator SET prefix_table_id=%s WHERE id=%s",
             [pt_id, id])
Ejemplo n.º 4
0
        ("2001:db8::/32", "2001:db8::/32"),
        ("::ffff:192.168.0.1", "::ffff:192.168.0.1/128"),
        ("::", "::/128"),
    ],
)
def test_ipv6_str(p1, p2):
    assert str(IPv6(p1)) == p2


@pytest.mark.parametrize(
    "p1,p2",
    [
        (
            [
                repr(x)
                for x in IPv4.range_to_prefixes("192.168.0.2", "192.168.0.2")
            ],
            ["<IPv4 192.168.0.2/32>"],
        ),
        (
            [
                repr(x)
                for x in IPv4.range_to_prefixes("192.168.0.2", "192.168.0.16")
            ],
            [
                "<IPv4 192.168.0.2/31>",
                "<IPv4 192.168.0.4/30>",
                "<IPv4 192.168.0.8/29>",
                "<IPv4 192.168.0.16/32>",
            ],
        ),