| 1 |
import dns.rdata |
| 2 |
import dns.rdataset |
| 3 |
import dns.rdtypes.IN.SRV |
| 4 |
|
| 5 |
|
| 6 |
def test_processing_order_shuffle(): |
| 7 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1", "10.0.0.2", "10.0.0.3") |
| 8 |
seen = set() |
| 9 |
for i in range(100): |
| 10 |
po = rds.processing_order() |
| 11 |
assert len(po) == 3 |
| 12 |
for j in range(3): |
| 13 |
assert rds[j] in po |
| 14 |
seen.add(tuple(po)) |
| 15 |
assert len(seen) == 6 |
| 16 |
|
| 17 |
|
| 18 |
def test_processing_order_priority_mx(): |
| 19 |
rds = dns.rdataset.from_text("in", "mx", 300, "10 a", "20 b", "20 c") |
| 20 |
seen = set() |
| 21 |
for i in range(100): |
| 22 |
po = rds.processing_order() |
| 23 |
assert len(po) == 3 |
| 24 |
for j in range(3): |
| 25 |
assert rds[j] in po |
| 26 |
assert rds[0] == po[0] |
| 27 |
seen.add(tuple(po)) |
| 28 |
assert len(seen) == 2 |
| 29 |
|
| 30 |
|
| 31 |
def test_processing_order_priority_weighted(): |
| 32 |
rds = dns.rdataset.from_text( |
| 33 |
"in", "srv", 300, "1 10 1234 a", "2 90 1234 b", "2 10 1234 c" |
| 34 |
) |
| 35 |
seen = set() |
| 36 |
weight_90_count = 0 |
| 37 |
weight_10_count = 0 |
| 38 |
for i in range(100): |
| 39 |
po = rds.processing_order() |
| 40 |
assert len(po) == 3 |
| 41 |
for j in range(3): |
| 42 |
assert rds[j] in po |
| 43 |
assert rds[0] == po[0] |
| 44 |
assert isinstance(po[1], dns.rdtypes.IN.SRV.SRV) |
| 45 |
if po[1].weight == 90: |
| 46 |
weight_90_count += 1 |
| 47 |
else: |
| 48 |
assert po[1].weight == 10 |
| 49 |
weight_10_count += 1 |
| 50 |
seen.add(tuple(po)) |
| 51 |
assert len(seen) == 2 |
| 52 |
# We can't assert anything with certainty given these are random |
| 53 |
# draws, but it's super likely that weight_90_count > weight_10_count, |
| 54 |
# so we just assert that. |
| 55 |
assert weight_90_count > weight_10_count |
| 56 |
|
| 57 |
|
| 58 |
def test_processing_order_priority_naptr(): |
| 59 |
rds = dns.rdataset.from_text( |
| 60 |
"in", |
| 61 |
"naptr", |
| 62 |
300, |
| 63 |
"1 10 a b c foo.", |
| 64 |
"1 20 a b c foo.", |
| 65 |
"2 10 a b c foo.", |
| 66 |
"2 10 d e f bar.", |
| 67 |
) |
| 68 |
seen = set() |
| 69 |
for i in range(100): |
| 70 |
po = rds.processing_order() |
| 71 |
assert len(po) == 4 |
| 72 |
for j in range(4): |
| 73 |
assert rds[j] in po |
| 74 |
assert rds[0] == po[0] |
| 75 |
assert rds[1] == po[1] |
| 76 |
seen.add(tuple(po)) |
| 77 |
assert len(seen) == 2 |
| 78 |
|
| 79 |
|
| 80 |
def test_processing_order_empty(): |
| 81 |
rds = dns.rdataset.from_text("in", "naptr", 300) |
| 82 |
po = rds.processing_order() |
| 83 |
assert po == [] |
| 84 |
|
| 85 |
|
| 86 |
def test_processing_singleton_priority(): |
| 87 |
rds = dns.rdataset.from_text("in", "mx", 300, "10 a") |
| 88 |
po = rds.processing_order() |
| 89 |
assert po == [rds[0]] |
| 90 |
|
| 91 |
|
| 92 |
def test_processing_singleton_weighted(): |
| 93 |
rds = dns.rdataset.from_text("in", "srv", 300, "1 10 1234 a") |
| 94 |
po = rds.processing_order() |
| 95 |
assert po == [rds[0]] |
| 96 |
|
| 97 |
|
| 98 |
def test_processing_all_zero_weight_srv(): |
| 99 |
rds = dns.rdataset.from_text( |
| 100 |
"in", "srv", 300, "1 0 1234 a", "1 0 1234 b", "1 0 1234 c" |
| 101 |
) |
| 102 |
seen = set() |
| 103 |
for i in range(100): |
| 104 |
po = rds.processing_order() |
| 105 |
assert len(po) == 3 |
| 106 |
for j in range(3): |
| 107 |
assert rds[j] in po |
| 108 |
seen.add(tuple(po)) |
| 109 |
assert len(seen) == 6 |
| 110 |
|
| 111 |
|
| 112 |
def test_processing_order_uri(): |
| 113 |
# We're testing here just to provide coverage for URI methods; the |
| 114 |
# testing of the weighting algorithm is done above in tests with |
| 115 |
# SRV. |
| 116 |
rds = dns.rdataset.from_text( |
| 117 |
"in", |
| 118 |
"uri", |
| 119 |
300, |
| 120 |
'1 1 "ftp://ftp1.example.com/public"', |
| 121 |
'2 2 "ftp://ftp2.example.com/public"', |
| 122 |
'3 3 "ftp://ftp3.example.com/public"', |
| 123 |
) |
| 124 |
po = rds.processing_order() |
| 125 |
assert len(po) == 3 |
| 126 |
for i in range(3): |
| 127 |
assert po[i] == rds[i] |
| 128 |
|
| 129 |
|
| 130 |
def test_processing_order_svcb(): |
| 131 |
# We're testing here just to provide coverage for SVCB methods; the |
| 132 |
# testing of the priority algorithm is done above in tests with |
| 133 |
# MX and NAPTR. |
| 134 |
rds = dns.rdataset.from_text( |
| 135 |
"in", |
| 136 |
"svcb", |
| 137 |
300, |
| 138 |
"1 . mandatory=alpn alpn=h2", |
| 139 |
"2 . mandatory=alpn alpn=h2", |
| 140 |
"3 . mandatory=alpn alpn=h2", |
| 141 |
) |
| 142 |
po = rds.processing_order() |
| 143 |
assert len(po) == 3 |
| 144 |
for i in range(3): |
| 145 |
assert po[i] == rds[i] |