From c8e17fc02a2d3ebbee3e7c34cd7828858e13fe57 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Sat, 31 May 2014 11:07:58 -0700 Subject: overhaul test system --- dns/dnssec.py | 2 + tests/Makefile | 5 +- tests/bugs.py | 49 --- tests/dnssec.py | 219 -------------- tests/flags.py | 59 ---- tests/generate.py | 499 ------------------------------- tests/grange.py | 95 ------ tests/message.py | 179 ----------- tests/name.py | 697 ------------------------------------------- tests/namedict.py | 102 ------- tests/ntoaaton.py | 203 ------------- tests/rdtypeandclass.py | 123 -------- tests/rdtypeanyloc.py | 68 ----- tests/resolver.py | 184 ------------ tests/rrset.py | 54 ---- tests/set.py | 208 ------------- tests/test_bugs.py | 49 +++ tests/test_dnssec.py | 233 +++++++++++++++ tests/test_flags.py | 59 ++++ tests/test_generate.py | 499 +++++++++++++++++++++++++++++++ tests/test_grange.py | 95 ++++++ tests/test_message.py | 179 +++++++++++ tests/test_name.py | 697 +++++++++++++++++++++++++++++++++++++++++++ tests/test_namedict.py | 102 +++++++ tests/test_ntoaaton.py | 203 +++++++++++++ tests/test_rdtypeandclass.py | 123 ++++++++ tests/test_rdtypeanyloc.py | 68 +++++ tests/test_resolver.py | 184 ++++++++++++ tests/test_rrset.py | 54 ++++ tests/test_set.py | 208 +++++++++++++ tests/test_tokenizer.py | 190 ++++++++++++ tests/test_update.py | 114 +++++++ tests/test_zone.py | 389 ++++++++++++++++++++++++ tests/tokenizer.py | 190 ------------ tests/update.py | 114 ------- tests/utest.py | 8 + tests/zone.py | 389 ------------------------ 37 files changed, 3457 insertions(+), 3436 deletions(-) delete mode 100644 tests/bugs.py delete mode 100644 tests/dnssec.py delete mode 100644 tests/flags.py delete mode 100644 tests/generate.py delete mode 100644 tests/grange.py delete mode 100644 tests/message.py delete mode 100644 tests/name.py delete mode 100644 tests/namedict.py delete mode 100644 tests/ntoaaton.py delete mode 100644 tests/rdtypeandclass.py delete mode 100644 tests/rdtypeanyloc.py delete mode 100644 tests/resolver.py delete mode 100644 tests/rrset.py delete mode 100644 tests/set.py create mode 100644 tests/test_bugs.py create mode 100644 tests/test_dnssec.py create mode 100644 tests/test_flags.py create mode 100644 tests/test_generate.py create mode 100644 tests/test_grange.py create mode 100644 tests/test_message.py create mode 100644 tests/test_name.py create mode 100644 tests/test_namedict.py create mode 100644 tests/test_ntoaaton.py create mode 100644 tests/test_rdtypeandclass.py create mode 100644 tests/test_rdtypeanyloc.py create mode 100644 tests/test_resolver.py create mode 100644 tests/test_rrset.py create mode 100644 tests/test_set.py create mode 100644 tests/test_tokenizer.py create mode 100644 tests/test_update.py create mode 100644 tests/test_zone.py delete mode 100644 tests/tokenizer.py delete mode 100644 tests/update.py create mode 100644 tests/utest.py delete mode 100644 tests/zone.py diff --git a/dns/dnssec.py b/dns/dnssec.py index edbe479..f1d70ce 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -405,9 +405,11 @@ try: import Crypto.Util.number validate = _validate validate_rrsig = _validate_rrsig + _have_pycrypto = True except ImportError: validate = _need_pycrypto validate_rrsig = _need_pycrypto + _have_pycrypto = False try: import ecdsa diff --git a/tests/Makefile b/tests/Makefile index 6ab444f..1524552 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -20,7 +20,4 @@ PYTHON=python check: test test: - @for i in *.py; do \ - echo "Running $$i:"; \ - ${PYTHON} $$i || exit 1; \ - done + ${PYTHON} ./utest.py diff --git a/tests/bugs.py b/tests/bugs.py deleted file mode 100644 index 312ec3e..0000000 --- a/tests/bugs.py +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.rdata -import dns.rdataclass -import dns.rdatatype -import dns.ttl - -class BugsTestCase(unittest.TestCase): - - def test_float_LOC(self): - rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.LOC, - "30 30 0.000 N 100 30 0.000 W 10.00m 20m 2000m 20m") - self.failUnless(rdata.float_latitude == 30.5) - self.failUnless(rdata.float_longitude == -100.5) - - def test_SOA_BIND8_TTL(self): - rdata1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - "a b 100 1s 1m 1h 1d") - rdata2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - "a b 100 1 60 3600 86400") - self.failUnless(rdata1 == rdata2) - - def test_TTL_bounds_check(self): - def bad(): - ttl = dns.ttl.from_text("2147483648") - self.failUnlessRaises(dns.ttl.BadTTL, bad) - - def test_empty_NSEC3_window(self): - rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NSEC3, - "1 0 100 ABCD SCBCQHKU35969L2A68P3AD59LHF30715") - self.failUnless(rdata.windows == []) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/dnssec.py b/tests/dnssec.py deleted file mode 100644 index edb028b..0000000 --- a/tests/dnssec.py +++ /dev/null @@ -1,219 +0,0 @@ -# Copyright (C) 2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.dnssec -import dns.name -import dns.rdata -import dns.rdataclass -import dns.rdatatype -import dns.rrset - -abs_dnspython_org = dns.name.from_text('dnspython.org') - -abs_keys = { abs_dnspython_org : - dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'DNSKEY', - '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', - '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') - } - -abs_keys_duplicate_keytag = { abs_dnspython_org : - dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'DNSKEY', - '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', - '256 3 5 AwEAAdSSg++++THIS/IS/NOT/THE/CORRECT/KEY++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ AaOSydAF', - '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') - } - -rel_keys = { dns.name.empty : - dns.rrset.from_text('@', 3600, 'IN', 'DNSKEY', - '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', - '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') - } - -when = 1290250287 - -abs_soa = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'SOA', - 'howl.dnspython.org. hostmaster.dnspython.org. 2010020047 3600 1800 604800 3600') - -abs_other_soa = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'SOA', - 'foo.dnspython.org. hostmaster.dnspython.org. 2010020047 3600 1800 604800 3600') - -abs_soa_rrsig = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'RRSIG', - 'SOA 5 2 3600 20101127004331 20101119213831 61695 dnspython.org. sDUlltRlFTQw5ITFxOXW3TgmrHeMeNpdqcZ4EXxM9FHhIlte6V9YCnDw t6dvM9jAXdIEi03l9H/RAd9xNNW6gvGMHsBGzpvvqFQxIBR2PoiZA1mX /SWHZFdbt4xjYTtXqpyYvrMK0Dt7bUYPadyhPFCJ1B+I8Zi7B5WJEOd0 8vs=') - -rel_soa = dns.rrset.from_text('@', 3600, 'IN', 'SOA', - 'howl hostmaster 2010020047 3600 1800 604800 3600') - -rel_other_soa = dns.rrset.from_text('@', 3600, 'IN', 'SOA', - 'foo hostmaster 2010020047 3600 1800 604800 3600') - -rel_soa_rrsig = dns.rrset.from_text('@', 3600, 'IN', 'RRSIG', - 'SOA 5 2 3600 20101127004331 20101119213831 61695 @ sDUlltRlFTQw5ITFxOXW3TgmrHeMeNpdqcZ4EXxM9FHhIlte6V9YCnDw t6dvM9jAXdIEi03l9H/RAd9xNNW6gvGMHsBGzpvvqFQxIBR2PoiZA1mX /SWHZFdbt4xjYTtXqpyYvrMK0Dt7bUYPadyhPFCJ1B+I8Zi7B5WJEOd0 8vs=') - -sep_key = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DNSKEY, - '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=') - -good_ds = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, - '57349 5 2 53A79A3E7488AB44FFC56B2D1109F0699D1796DD977E72108B841F96 E47D7013') - -when2 = 1290425644 - -abs_example = dns.name.from_text('example') - -abs_dsa_keys = { abs_example : - dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', - '257 3 3 CI3nCqyJsiCJHTjrNsJOT4RaszetzcJPYuoH3F9ZTVt3KJXncCVR3bwn 1w0iavKljb9hDlAYSfHbFCp4ic/rvg4p1L8vh5s8ToMjqDNl40A0hUGQ Ybx5hsECyK+qHoajilUX1phYSAD8d9WAGO3fDWzUPBuzR7o85NiZCDxz yXuNVfni0uhj9n1KYhEO5yAbbruDGN89wIZcxMKuQsdUY2GYD93ssnBv a55W6XRABYWayKZ90WkRVODLVYLSn53Pj/wwxGH+XdhIAZJXimrZL4yl My7rtBsLMqq8Ihs4Tows7LqYwY7cp6y/50tw6pj8tFqMYcPUjKZV36l1 M/2t5BVg3i7IK61Aidt6aoC3TDJtzAxg3ZxfjZWJfhHjMJqzQIfbW5b9 q1mjFsW5EUv39RaNnX+3JWPRLyDqD4pIwDyqfutMsdk/Py3paHn82FGp CaOg+nicqZ9TiMZURN/XXy5JoXUNQ3RNvbHCUiPUe18KUkY6mTfnyHld 1l9YCWmzXQVClkx/hOYxjJ4j8Ife58+Obu5X', - '256 3 3 CJE1yb9YRQiw5d2xZrMUMR+cGCTt1bp1KDCefmYKmS+Z1+q9f42ETVhx JRiQwXclYwmxborzIkSZegTNYIV6mrYwbNB27Q44c3UGcspb3PiOw5TC jNPRYEcdwGvDZ2wWy+vkSV/S9tHXY8O6ODiE6abZJDDg/RnITyi+eoDL R3KZ5n/V1f1T1b90rrV6EewhBGQJpQGDogaXb2oHww9Tm6NfXyo7SoMM pbwbzOckXv+GxRPJIQNSF4D4A9E8XCksuzVVdE/0lr37+uoiAiPia38U 5W2QWe/FJAEPLjIp2eTzf0TrADc1pKP1wrA2ASpdzpm/aX3IB5RPp8Ew S9U72eBFZJAUwg635HxJVxH1maG6atzorR566E+e0OZSaxXS9o1o6QqN 3oPlYLGPORDiExilKfez3C/x/yioOupW9K5eKF0gmtaqrHX0oq9s67f/ RIM2xVaKHgG9Vf2cgJIZkhv7sntujr+E4htnRmy9P9BxyFxsItYxPI6Z bzygHAZpGhlI/7ltEGlIwKxyTK3ZKBm67q7B') - } - -abs_dsa_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 2 10800 3600 604800 86400') - -abs_other_dsa_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') - -abs_dsa_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', - 'SOA 3 1 86400 20101129143231 20101122112731 42088 example. CGul9SuBofsktunV8cJs4eRs6u+3NCS3yaPKvBbD+pB2C76OUXDZq9U=') - -example_sep_key = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DNSKEY, - '257 3 3 CI3nCqyJsiCJHTjrNsJOT4RaszetzcJPYuoH3F9ZTVt3KJXncCVR3bwn 1w0iavKljb9hDlAYSfHbFCp4ic/rvg4p1L8vh5s8ToMjqDNl40A0hUGQ Ybx5hsECyK+qHoajilUX1phYSAD8d9WAGO3fDWzUPBuzR7o85NiZCDxz yXuNVfni0uhj9n1KYhEO5yAbbruDGN89wIZcxMKuQsdUY2GYD93ssnBv a55W6XRABYWayKZ90WkRVODLVYLSn53Pj/wwxGH+XdhIAZJXimrZL4yl My7rtBsLMqq8Ihs4Tows7LqYwY7cp6y/50tw6pj8tFqMYcPUjKZV36l1 M/2t5BVg3i7IK61Aidt6aoC3TDJtzAxg3ZxfjZWJfhHjMJqzQIfbW5b9 q1mjFsW5EUv39RaNnX+3JWPRLyDqD4pIwDyqfutMsdk/Py3paHn82FGp CaOg+nicqZ9TiMZURN/XXy5JoXUNQ3RNvbHCUiPUe18KUkY6mTfnyHld 1l9YCWmzXQVClkx/hOYxjJ4j8Ife58+Obu5X') - -example_ds_sha1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, - '18673 3 1 71b71d4f3e11bbd71b4eff12cde69f7f9215bbe7') - -example_ds_sha256 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, - '18673 3 2 eb8344cbbf07c9d3d3d6c81d10c76653e28d8611a65e639ef8f716e4e4e5d913') - -when3 = 1379801800 - -abs_ecdsa256_keys = { abs_example : - dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', - "256 3 13 +3ss1sCpdARVA61DJigEsL/8quo2a8MszKtn2gkkfxgzFs8S2UHtpb4N fY+XFmNW+JK6MsCkI3jHYN8eEQUgMw==", - "257 3 13 eJCEVH7AS3wnoaQpaNlAXH0W8wxymtT9P6P3qjN2ZCV641ED8pF7wZ5V yWfOpgTs6oaZevbJgehl/GaRPUgVyQ==") - } - -abs_ecdsa256_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 4 10800 3600 604800 86400') - -abs_other_ecdsa256_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') - -abs_ecdsa256_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', - "SOA 13 1 86400 20130921221753 20130921221638 7460 example. Sm09SOGz1ULB5D/duwdE2Zpn8bWbVBM77H6N1wPkc42LevvVO+kZEjpq 2nq4GOMJcih52667GIAbMrwmU5P2MQ==") - -when4 = 1379804850 - -abs_ecdsa384_keys = { abs_example : - dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', - "256 3 14 1bG8qWviKNXQX3BIuG6/T5jrP1FISiLW/8qGF6BsM9DQtWYhhZUA3Owr OAEiyHAhQwjkN2kTvWiAYoPN80Ii+5ff9/atzY4F9W50P4l75Dj9PYrL HN/hLUgWMNVc9pvA", - "257 3 14 mSub2n0KRt6u2FaD5XJ3oQu0R4XvB/9vUJcyW6+oo0y+KzfQeTdkf1ro ZMVKoyWXW9zUKBYGJpMUIdbAxzrYi7f5HyZ3yDpBFz1hw9+o3CX+gtgb +RyhHfJDwwFXBid9") - } - -abs_ecdsa384_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 2 10800 3600 604800 86400') - -abs_other_ecdsa384_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', - 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') - -abs_ecdsa384_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', - "SOA 14 1 86400 20130929021229 20130921230729 63571 example. CrnCu34EeeRz0fEhL9PLlwjpBKGYW8QjBjFQTwd+ViVLRAS8tNkcDwQE NhSV89NEjj7ze1a/JcCfcJ+/mZgnvH4NHLNg3Tf6KuLZsgs2I4kKQXEk 37oIHravPEOlGYNI") - -class DNSSECValidatorTestCase(unittest.TestCase): - - def testAbsoluteRSAGood(self): - dns.dnssec.validate(abs_soa, abs_soa_rrsig, abs_keys, None, when) - - def testDuplicateKeytag(self): - dns.dnssec.validate(abs_soa, abs_soa_rrsig, abs_keys_duplicate_keytag, None, when) - - def testAbsoluteRSABad(self): - def bad(): - dns.dnssec.validate(abs_other_soa, abs_soa_rrsig, abs_keys, None, - when) - self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) - - def testRelativeRSAGood(self): - dns.dnssec.validate(rel_soa, rel_soa_rrsig, rel_keys, - abs_dnspython_org, when) - - def testRelativeRSABad(self): - def bad(): - dns.dnssec.validate(rel_other_soa, rel_soa_rrsig, rel_keys, - abs_dnspython_org, when) - self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) - - def testMakeSHA256DS(self): - ds = dns.dnssec.make_ds(abs_dnspython_org, sep_key, 'SHA256') - self.failUnless(ds == good_ds) - - def testAbsoluteDSAGood(self): - dns.dnssec.validate(abs_dsa_soa, abs_dsa_soa_rrsig, abs_dsa_keys, None, - when2) - - def testAbsoluteDSABad(self): - def bad(): - dns.dnssec.validate(abs_other_dsa_soa, abs_dsa_soa_rrsig, - abs_dsa_keys, None, when2) - self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) - - def testMakeExampleSHA1DS(self): - ds = dns.dnssec.make_ds(abs_example, example_sep_key, 'SHA1') - self.failUnless(ds == example_ds_sha1) - - def testMakeExampleSHA256DS(self): - ds = dns.dnssec.make_ds(abs_example, example_sep_key, 'SHA256') - self.failUnless(ds == example_ds_sha256) - - @unittest.skipIf(not dns.dnssec._have_ecdsa, - "python ECDSA can not be imported") - def testAbsoluteECDSA256Good(self): - dns.dnssec.validate(abs_ecdsa256_soa, abs_ecdsa256_soa_rrsig, - abs_ecdsa256_keys, None, when3) - - @unittest.skipIf(not dns.dnssec._have_ecdsa, - "python ECDSA can not be imported") - def testAbsoluteECDSA256Bad(self): - def bad(): - dns.dnssec.validate(abs_other_ecdsa256_soa, abs_ecdsa256_soa_rrsig, - abs_ecdsa256_keys, None, when3) - self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) - - @unittest.skipIf(not dns.dnssec._have_ecdsa, - "python ECDSA can not be imported") - def testAbsoluteECDSA384Good(self): - dns.dnssec.validate(abs_ecdsa384_soa, abs_ecdsa384_soa_rrsig, - abs_ecdsa384_keys, None, when4) - - @unittest.skipIf(not dns.dnssec._have_ecdsa, - "python ECDSA can not be imported") - def testAbsoluteECDSA384Bad(self): - def bad(): - dns.dnssec.validate(abs_other_ecdsa384_soa, abs_ecdsa384_soa_rrsig, - abs_ecdsa384_keys, None, when4) - self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) - - -if __name__ == '__main__': - import_ok = False - try: - import Crypto.Util.number - import_ok = True - except: - pass - if import_ok: - unittest.main() - else: - print 'skipping DNSSEC tests because pycrypto is not installed' diff --git a/tests/flags.py b/tests/flags.py deleted file mode 100644 index b3cf671..0000000 --- a/tests/flags.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.flags -import dns.rcode -import dns.opcode - -class FlagsTestCase(unittest.TestCase): - - def test_rcode1(self): - self.failUnless(dns.rcode.from_text('FORMERR') == dns.rcode.FORMERR) - - def test_rcode2(self): - self.failUnless(dns.rcode.to_text(dns.rcode.FORMERR) == "FORMERR") - - def test_rcode3(self): - self.failUnless(dns.rcode.to_flags(dns.rcode.FORMERR) == (1, 0)) - - def test_rcode4(self): - self.failUnless(dns.rcode.to_flags(dns.rcode.BADVERS) == \ - (0, 0x01000000)) - - def test_rcode6(self): - self.failUnless(dns.rcode.from_flags(0, 0x01000000) == \ - dns.rcode.BADVERS) - - def test_rcode6(self): - self.failUnless(dns.rcode.from_flags(5, 0) == dns.rcode.REFUSED) - - def test_rcode7(self): - def bad(): - dns.rcode.to_flags(4096) - self.failUnlessRaises(ValueError, bad) - - def test_flags1(self): - self.failUnless(dns.flags.from_text("RA RD AA QR") == \ - dns.flags.QR|dns.flags.AA|dns.flags.RD|dns.flags.RA) - - def test_flags2(self): - flags = dns.flags.QR|dns.flags.AA|dns.flags.RD|dns.flags.RA - self.failUnless(dns.flags.to_text(flags) == "QR AA RD RA") - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/generate.py b/tests/generate.py deleted file mode 100644 index 43c5f9a..0000000 --- a/tests/generate.py +++ /dev/null @@ -1,499 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import sys -sys.path.insert(0, '../') # Force the local project to be *the* dns - -import cStringIO -import filecmp -import os -import unittest - -import dns.exception -import dns.rdata -import dns.rdataclass -import dns.rdatatype -import dns.rrset -import dns.zone - -import pprint - -pp = pprint.PrettyPrinter(indent=2) - -import pdb -example_text = """$TTL 1h -$ORIGIN 0.0.192.IN-ADDR.ARPA. -$GENERATE 1-2 0 CNAME SERVER$.EXAMPLE. -""" - -example_text1 = """$TTL 1h -$ORIGIN 0.0.192.IN-ADDR.ARPA. -$GENERATE 1-10 fooo$ CNAME $.0 -""" - -example_text2 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 3-5 foo$ A 10.0.0.$ -""" - -example_text3 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 4-8/2 foo$ A 10.0.0.$ -""" - -example_text4 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-13 wp-db${-10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. -""" - -example_text5 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-13 wp-db${10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. -""" - -example_text6 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-13 wp-db${+10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. -""" - -example_text7 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-13 sync${-10}.db IN A 10.10.16.0 -""" - -example_text8 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-12 wp-db${-10,2,d} IN A 10.10.16.0 -""" - -example_text9 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 11-12 wp-db${-10,2,d} IN A 10.10.16.0 -$GENERATE 11-13 sync${-10}.db IN A 10.10.16.0 -""" -example_text10 = """$TTL 1h -@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -$GENERATE 27-28 $.2 PTR zlb${-26}.oob -""" - - -class GenerateTestCase(unittest.TestCase): - - def testFromText(self): - def bad(): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - self.failUnlessRaises(dns.zone.NoSOA, bad) - - def testFromText1(self): - def bad(): - z = dns.zone.from_text(example_text1, 'example.', relativize=True) - self.failUnlessRaises(dns.zone.NoSOA, bad) - - def testIterateAllRdatas2(self): - z = dns.zone.from_text(example_text2, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - (dns.name.from_text('foo3', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.3')), - (dns.name.from_text('foo4', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.4')), - (dns.name.from_text('foo5', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.5'))] - - exl.sort() - self.failUnless(l == exl) - - def testIterateAllRdatas3(self): - z = dns.zone.from_text(example_text3, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - (dns.name.from_text('foo4', None), 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.4')), - (dns.name.from_text('foo6', None), 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.6')), - (dns.name.from_text('foo8', None), 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.8'))] - exl.sort() - self.failUnless(l == exl) - def testGenerate1(self): - z = dns.zone.from_text(example_text4, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - - (dns.name.from_text('wp-db01.services.mozilla.com', None), - 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db02.services.mozilla.com', None), - 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db03.services.mozilla.com', None), - 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.'))] - exl.sort() - self.failUnless(l == exl) - - def testGenerate2(self): - z = dns.zone.from_text(example_text5, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - - (dns.name.from_text('wp-db21.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db22.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db23.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.'))] - exl.sort() - self.failUnless(l == exl) - - def testGenerate3(self): - z = dns.zone.from_text(example_text6, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - (dns.name.from_text('wp-db21.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db22.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.')), - - (dns.name.from_text('wp-db23.services.mozilla.com', None), 0L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, - 'SERVER.FOOBAR.'))] - exl.sort() - self.failUnless(l == exl) - - def testGenerate4(self): - z = dns.zone.from_text(example_text7, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - - (dns.name.from_text('sync1.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - - (dns.name.from_text('sync2.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - - (dns.name.from_text('sync3.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0'))] - exl.sort() - self.failUnless(l == exl) - - def testGenerate6(self): - z = dns.zone.from_text(example_text9, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - - (dns.name.from_text('wp-db01', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - (dns.name.from_text('wp-db02', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - - (dns.name.from_text('sync1.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - - (dns.name.from_text('sync2.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0')), - - (dns.name.from_text('sync3.db', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.10.16.0'))] - exl.sort() - self.failUnless(l == exl) - - def testGenerate7(self): - z = dns.zone.from_text(example_text10, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2')), - - (dns.name.from_text('27.2', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, - 'zlb1.oob')), - - (dns.name.from_text('28.2', None), 3600L, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, - 'zlb2.oob'))] - - exl.sort() - self.failUnless(l == exl) - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/grange.py b/tests/grange.py deleted file mode 100644 index 1bb7dbb..0000000 --- a/tests/grange.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import sys -sys.path.insert(0, '../') - -import cStringIO -import filecmp -import os -import unittest - -import dns -import dns.exception -import dns.grange - -import pdb - - - -class GRangeTestCase(unittest.TestCase): - - def testFromText1(self): - start, stop, step = dns.grange.from_text('1-1') - self.assertEqual(start, 1) - self.assertEqual(stop, 1) - self.assertEqual(step, 1) - - def testFromText2(self): - start, stop, step = dns.grange.from_text('1-4') - self.assertEqual(start, 1) - self.assertEqual(stop, 4) - self.assertEqual(step, 1) - - def testFromText3(self): - start, stop, step = dns.grange.from_text('4-255') - self.assertEqual(start, 4) - self.assertEqual(stop, 255) - self.assertEqual(step, 1) - - def testFromText4(self): - start, stop, step = dns.grange.from_text('1-1/1') - self.assertEqual(start, 1) - self.assertEqual(stop, 1) - self.assertEqual(step, 1) - - def testFromText5(self): - start, stop, step = dns.grange.from_text('1-4/2') - self.assertEqual(start, 1) - self.assertEqual(stop, 4) - self.assertEqual(step, 2) - - def testFromText6(self): - start, stop, step = dns.grange.from_text('4-255/77') - self.assertEqual(start, 4) - self.assertEqual(stop, 255) - self.assertEqual(step, 77) - - def testFailFromText1(self): - def bad(): - start = 2 - stop = 1 - step = 1 - dns.grange.from_text('%d-%d/%d' % (start, stop, step)) - self.assertRaises(AssertionError, bad) - - def testFailFromText2(self): - def bad(): - start = '-1' - stop = 3 - step = 1 - dns.grange.from_text('%s-%d/%d' % (start, stop, step)) - self.assertRaises(dns.exception.SyntaxError, bad) - - def testFailFromText2(self): - def bad(): - start = 1 - stop = 4 - step = '-2' - dns.grange.from_text('%d-%d/%s' % (start, stop, step)) - self.assertRaises(dns.exception.SyntaxError, bad) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/message.py b/tests/message.py deleted file mode 100644 index 931bb19..0000000 --- a/tests/message.py +++ /dev/null @@ -1,179 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import cStringIO -import os -import unittest - -import dns.exception -import dns.message - -query_text = """id 1234 -opcode QUERY -rcode NOERROR -flags RD -edns 0 -eflags DO -payload 4096 -;QUESTION -wwww.dnspython.org. IN A -;ANSWER -;AUTHORITY -;ADDITIONAL""" - -goodhex = '04d201000001000000000001047777777709646e73707974686f6e' \ - '036f726700000100010000291000000080000000' - -goodwire = goodhex.decode('hex_codec') - -answer_text = """id 1234 -opcode QUERY -rcode NOERROR -flags QR AA RD -;QUESTION -dnspython.org. IN SOA -;ANSWER -dnspython.org. 3600 IN SOA woof.dnspython.org. hostmaster.dnspython.org. 2003052700 3600 1800 604800 3600 -;AUTHORITY -dnspython.org. 3600 IN NS ns1.staff.nominum.org. -dnspython.org. 3600 IN NS ns2.staff.nominum.org. -dnspython.org. 3600 IN NS woof.play-bow.org. -;ADDITIONAL -woof.play-bow.org. 3600 IN A 204.152.186.150 -""" - -goodhex2 = '04d2 8500 0001 0001 0003 0001' \ - '09646e73707974686f6e036f726700 0006 0001' \ - 'c00c 0006 0001 00000e10 0028 ' \ - '04776f6f66c00c 0a686f73746d6173746572c00c' \ - '7764289c 00000e10 00000708 00093a80 00000e10' \ - 'c00c 0002 0001 00000e10 0014' \ - '036e7331057374616666076e6f6d696e756dc016' \ - 'c00c 0002 0001 00000e10 0006 036e7332c063' \ - 'c00c 0002 0001 00000e10 0010 04776f6f6608706c61792d626f77c016' \ - 'c091 0001 0001 00000e10 0004 cc98ba96' - - -goodwire2 = goodhex2.replace(' ', '').decode('hex_codec') - -query_text_2 = """id 1234 -opcode QUERY -rcode 4095 -flags RD -edns 0 -eflags DO -payload 4096 -;QUESTION -wwww.dnspython.org. IN A -;ANSWER -;AUTHORITY -;ADDITIONAL""" - -goodhex3 = '04d2010f0001000000000001047777777709646e73707974686f6e' \ - '036f726700000100010000291000ff0080000000' - -goodwire3 = goodhex3.decode('hex_codec') - -class MessageTestCase(unittest.TestCase): - - def test_comparison_eq1(self): - q1 = dns.message.from_text(query_text) - q2 = dns.message.from_text(query_text) - self.failUnless(q1 == q2) - - def test_comparison_ne1(self): - q1 = dns.message.from_text(query_text) - q2 = dns.message.from_text(query_text) - q2.id = 10 - self.failUnless(q1 != q2) - - def test_comparison_ne2(self): - q1 = dns.message.from_text(query_text) - q2 = dns.message.from_text(query_text) - q2.question = [] - self.failUnless(q1 != q2) - - def test_comparison_ne3(self): - q1 = dns.message.from_text(query_text) - self.failUnless(q1 != 1) - - def test_EDNS_to_wire1(self): - q = dns.message.from_text(query_text) - w = q.to_wire() - self.failUnless(w == goodwire) - - def test_EDNS_from_wire1(self): - m = dns.message.from_wire(goodwire) - self.failUnless(str(m) == query_text) - - def test_EDNS_to_wire2(self): - q = dns.message.from_text(query_text_2) - w = q.to_wire() - self.failUnless(w == goodwire3) - - def test_EDNS_from_wire2(self): - m = dns.message.from_wire(goodwire3) - self.failUnless(str(m) == query_text_2) - - def test_TooBig(self): - def bad(): - q = dns.message.from_text(query_text) - for i in xrange(0, 25): - rrset = dns.rrset.from_text('foo%d.' % i, 3600, - dns.rdataclass.IN, - dns.rdatatype.A, - '10.0.0.%d' % i) - q.additional.append(rrset) - w = q.to_wire(max_size=512) - self.failUnlessRaises(dns.exception.TooBig, bad) - - def test_answer1(self): - a = dns.message.from_text(answer_text) - wire = a.to_wire(want_shuffle=False) - self.failUnless(wire == goodwire2) - - def test_TrailingJunk(self): - def bad(): - badwire = goodwire + '\x00' - m = dns.message.from_wire(badwire) - self.failUnlessRaises(dns.message.TrailingJunk, bad) - - def test_ShortHeader(self): - def bad(): - badwire = '\x00' * 11 - m = dns.message.from_wire(badwire) - self.failUnlessRaises(dns.message.ShortHeader, bad) - - def test_RespondingToResponse(self): - def bad(): - q = dns.message.make_query('foo', 'A') - r1 = dns.message.make_response(q) - r2 = dns.message.make_response(r1) - self.failUnlessRaises(dns.exception.FormError, bad) - - def test_ExtendedRcodeSetting(self): - m = dns.message.make_query('foo', 'A') - m.set_rcode(4095) - self.failUnless(m.rcode() == 4095) - m.set_rcode(2) - self.failUnless(m.rcode() == 2) - - def test_EDNSVersionCoherence(self): - m = dns.message.make_query('foo', 'A') - m.use_edns(1) - self.failUnless((m.ednsflags >> 16) & 0xFF == 1) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/name.py b/tests/name.py deleted file mode 100644 index e30e43d..0000000 --- a/tests/name.py +++ /dev/null @@ -1,697 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import cStringIO -import socket - -import dns.name -import dns.reversename -import dns.e164 - -class NameTestCase(unittest.TestCase): - def setUp(self): - self.origin = dns.name.from_text('example.') - - def testFromTextRel1(self): - n = dns.name.from_text('foo.bar') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testFromTextRel2(self): - n = dns.name.from_text('foo.bar', origin=self.origin) - self.failUnless(n.labels == ('foo', 'bar', 'example', '')) - - def testFromTextRel3(self): - n = dns.name.from_text('foo.bar', origin=None) - self.failUnless(n.labels == ('foo', 'bar')) - - def testFromTextRel4(self): - n = dns.name.from_text('@', origin=None) - self.failUnless(n == dns.name.empty) - - def testFromTextRel5(self): - n = dns.name.from_text('@', origin=self.origin) - self.failUnless(n == self.origin) - - def testFromTextAbs1(self): - n = dns.name.from_text('foo.bar.') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testTortureFromText(self): - good = [ - r'.', - r'a', - r'a.', - r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - r'\000.\008.\010.\032.\046.\092.\099.\255', - r'\\', - r'\..\.', - r'\\.\\', - r'!"#%&/()=+-', - r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255', - ] - bad = [ - r'..', - r'.a', - r'\\..', - '\\', # yes, we don't want the 'r' prefix! - r'\0', - r'\00', - r'\00Z', - r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', - r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255', - ] - for t in good: - try: - n = dns.name.from_text(t) - except: - self.fail("good test '%s' raised an exception" % t) - for t in bad: - caught = False - try: - n = dns.name.from_text(t) - except: - caught = True - if not caught: - self.fail("bad test '%s' did not raise an exception" % t) - - def testImmutable1(self): - def bad(): - self.origin.labels = () - self.failUnlessRaises(TypeError, bad) - - def testImmutable2(self): - def bad(): - self.origin.labels[0] = 'foo' - self.failUnlessRaises(TypeError, bad) - - def testAbs1(self): - self.failUnless(dns.name.root.is_absolute()) - - def testAbs2(self): - self.failUnless(not dns.name.empty.is_absolute()) - - def testAbs3(self): - self.failUnless(self.origin.is_absolute()) - - def testAbs3(self): - n = dns.name.from_text('foo', origin=None) - self.failUnless(not n.is_absolute()) - - def testWild1(self): - n = dns.name.from_text('*.foo', origin=None) - self.failUnless(n.is_wild()) - - def testWild2(self): - n = dns.name.from_text('*a.foo', origin=None) - self.failUnless(not n.is_wild()) - - def testWild3(self): - n = dns.name.from_text('a.*.foo', origin=None) - self.failUnless(not n.is_wild()) - - def testWild4(self): - self.failUnless(not dns.name.root.is_wild()) - - def testWild5(self): - self.failUnless(not dns.name.empty.is_wild()) - - def testHash1(self): - n1 = dns.name.from_text('fOo.COM') - n2 = dns.name.from_text('foo.com') - self.failUnless(hash(n1) == hash(n2)) - - def testCompare1(self): - n1 = dns.name.from_text('a') - n2 = dns.name.from_text('b') - self.failUnless(n1 < n2) - self.failUnless(n2 > n1) - - def testCompare2(self): - n1 = dns.name.from_text('') - n2 = dns.name.from_text('b') - self.failUnless(n1 < n2) - self.failUnless(n2 > n1) - - def testCompare3(self): - self.failUnless(dns.name.empty < dns.name.root) - self.failUnless(dns.name.root > dns.name.empty) - - def testCompare4(self): - self.failUnless(dns.name.root != 1) - - def testCompare5(self): - self.failUnless(dns.name.root < 1 or dns.name.root > 1) - - def testSubdomain1(self): - self.failUnless(not dns.name.empty.is_subdomain(dns.name.root)) - - def testSubdomain2(self): - self.failUnless(not dns.name.root.is_subdomain(dns.name.empty)) - - def testSubdomain3(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(n.is_subdomain(self.origin)) - - def testSubdomain4(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(n.is_subdomain(dns.name.root)) - - def testSubdomain5(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(n.is_subdomain(n)) - - def testSuperdomain1(self): - self.failUnless(not dns.name.empty.is_superdomain(dns.name.root)) - - def testSuperdomain2(self): - self.failUnless(not dns.name.root.is_superdomain(dns.name.empty)) - - def testSuperdomain3(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(self.origin.is_superdomain(n)) - - def testSuperdomain4(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(dns.name.root.is_superdomain(n)) - - def testSuperdomain5(self): - n = dns.name.from_text('foo', origin=self.origin) - self.failUnless(n.is_superdomain(n)) - - def testCanonicalize1(self): - n = dns.name.from_text('FOO.bar', origin=self.origin) - c = n.canonicalize() - self.failUnless(c.labels == ('foo', 'bar', 'example', '')) - - def testToText1(self): - n = dns.name.from_text('FOO.bar', origin=self.origin) - t = n.to_text() - self.failUnless(t == 'FOO.bar.example.') - - def testToText2(self): - n = dns.name.from_text('FOO.bar', origin=self.origin) - t = n.to_text(True) - self.failUnless(t == 'FOO.bar.example') - - def testToText3(self): - n = dns.name.from_text('FOO.bar', origin=None) - t = n.to_text() - self.failUnless(t == 'FOO.bar') - - def testToText4(self): - t = dns.name.empty.to_text() - self.failUnless(t == '@') - - def testToText5(self): - t = dns.name.root.to_text() - self.failUnless(t == '.') - - def testToText6(self): - n = dns.name.from_text('FOO bar', origin=None) - t = n.to_text() - self.failUnless(t == r'FOO\032bar') - - def testToText7(self): - n = dns.name.from_text(r'FOO\.bar', origin=None) - t = n.to_text() - self.failUnless(t == r'FOO\.bar') - - def testToText8(self): - n = dns.name.from_text(r'\070OO\.bar', origin=None) - t = n.to_text() - self.failUnless(t == r'FOO\.bar') - - def testSlice1(self): - n = dns.name.from_text(r'a.b.c.', origin=None) - s = n[:] - self.failUnless(s == ('a', 'b', 'c', '')) - - def testSlice2(self): - n = dns.name.from_text(r'a.b.c.', origin=None) - s = n[:2] - self.failUnless(s == ('a', 'b')) - - def testSlice3(self): - n = dns.name.from_text(r'a.b.c.', origin=None) - s = n[2:] - self.failUnless(s == ('c', '')) - - def testEmptyLabel1(self): - def bad(): - n = dns.name.Name(['a', '', 'b']) - self.failUnlessRaises(dns.name.EmptyLabel, bad) - - def testEmptyLabel2(self): - def bad(): - n = dns.name.Name(['', 'b']) - self.failUnlessRaises(dns.name.EmptyLabel, bad) - - def testEmptyLabel3(self): - n = dns.name.Name(['b', '']) - self.failUnless(n) - - def testLongLabel(self): - n = dns.name.Name(['a' * 63]) - self.failUnless(n) - - def testLabelTooLong(self): - def bad(): - n = dns.name.Name(['a' * 64, 'b']) - self.failUnlessRaises(dns.name.LabelTooLong, bad) - - def testLongName(self): - n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 62]) - self.failUnless(n) - - def testNameTooLong(self): - def bad(): - n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 63]) - self.failUnlessRaises(dns.name.NameTooLong, bad) - - def testConcat1(self): - n1 = dns.name.Name(['a', 'b']) - n2 = dns.name.Name(['c', 'd']) - e = dns.name.Name(['a', 'b', 'c', 'd']) - r = n1 + n2 - self.failUnless(r == e) - - def testConcat2(self): - n1 = dns.name.Name(['a', 'b']) - n2 = dns.name.Name([]) - e = dns.name.Name(['a', 'b']) - r = n1 + n2 - self.failUnless(r == e) - - def testConcat2(self): - n1 = dns.name.Name([]) - n2 = dns.name.Name(['a', 'b']) - e = dns.name.Name(['a', 'b']) - r = n1 + n2 - self.failUnless(r == e) - - def testConcat3(self): - n1 = dns.name.Name(['a', 'b', '']) - n2 = dns.name.Name([]) - e = dns.name.Name(['a', 'b', '']) - r = n1 + n2 - self.failUnless(r == e) - - def testConcat4(self): - n1 = dns.name.Name(['a', 'b']) - n2 = dns.name.Name(['c', '']) - e = dns.name.Name(['a', 'b', 'c', '']) - r = n1 + n2 - self.failUnless(r == e) - - def testConcat5(self): - def bad(): - n1 = dns.name.Name(['a', 'b', '']) - n2 = dns.name.Name(['c']) - r = n1 + n2 - self.failUnlessRaises(dns.name.AbsoluteConcatenation, bad) - - def testBadEscape(self): - def bad(): - n = dns.name.from_text(r'a.b\0q1.c.') - print n - self.failUnlessRaises(dns.name.BadEscape, bad) - - def testDigestable1(self): - n = dns.name.from_text('FOO.bar') - d = n.to_digestable() - self.failUnless(d == '\x03foo\x03bar\x00') - - def testDigestable2(self): - n1 = dns.name.from_text('FOO.bar') - n2 = dns.name.from_text('foo.BAR.') - d1 = n1.to_digestable() - d2 = n2.to_digestable() - self.failUnless(d1 == d2) - - def testDigestable3(self): - d = dns.name.root.to_digestable() - self.failUnless(d == '\x00') - - def testDigestable4(self): - n = dns.name.from_text('FOO.bar', None) - d = n.to_digestable(dns.name.root) - self.failUnless(d == '\x03foo\x03bar\x00') - - def testBadDigestable(self): - def bad(): - n = dns.name.from_text('FOO.bar', None) - d = n.to_digestable() - self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad) - - def testToWire1(self): - n = dns.name.from_text('FOO.bar') - f = cStringIO.StringIO() - compress = {} - n.to_wire(f, compress) - self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00') - - def testToWire2(self): - n = dns.name.from_text('FOO.bar') - f = cStringIO.StringIO() - compress = {} - n.to_wire(f, compress) - n.to_wire(f, compress) - self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00') - - def testToWire3(self): - n1 = dns.name.from_text('FOO.bar') - n2 = dns.name.from_text('foo.bar') - f = cStringIO.StringIO() - compress = {} - n1.to_wire(f, compress) - n2.to_wire(f, compress) - self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00') - - def testToWire4(self): - n1 = dns.name.from_text('FOO.bar') - n2 = dns.name.from_text('a.foo.bar') - f = cStringIO.StringIO() - compress = {} - n1.to_wire(f, compress) - n2.to_wire(f, compress) - self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\x01\x61\xc0\x00') - - def testToWire5(self): - n1 = dns.name.from_text('FOO.bar') - n2 = dns.name.from_text('a.foo.bar') - f = cStringIO.StringIO() - compress = {} - n1.to_wire(f, compress) - n2.to_wire(f, None) - self.failUnless(f.getvalue() == \ - '\x03FOO\x03bar\x00\x01\x61\x03foo\x03bar\x00') - - def testToWire6(self): - n = dns.name.from_text('FOO.bar') - v = n.to_wire() - self.failUnless(v == '\x03FOO\x03bar\x00') - - def testBadToWire(self): - def bad(): - n = dns.name.from_text('FOO.bar', None) - f = cStringIO.StringIO() - compress = {} - n.to_wire(f, compress) - self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad) - - def testSplit1(self): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(2) - ep = dns.name.from_text('foo', None) - es = dns.name.from_text('bar.', None) - self.failUnless(prefix == ep and suffix == es) - - def testSplit2(self): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(1) - ep = dns.name.from_text('foo.bar', None) - es = dns.name.from_text('.', None) - self.failUnless(prefix == ep and suffix == es) - - def testSplit3(self): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(0) - ep = dns.name.from_text('foo.bar.', None) - es = dns.name.from_text('', None) - self.failUnless(prefix == ep and suffix == es) - - def testSplit4(self): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(3) - ep = dns.name.from_text('', None) - es = dns.name.from_text('foo.bar.', None) - self.failUnless(prefix == ep and suffix == es) - - def testBadSplit1(self): - def bad(): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(-1) - self.failUnlessRaises(ValueError, bad) - - def testBadSplit2(self): - def bad(): - n = dns.name.from_text('foo.bar.') - (prefix, suffix) = n.split(4) - self.failUnlessRaises(ValueError, bad) - - def testRelativize1(self): - n = dns.name.from_text('a.foo.bar.', None) - o = dns.name.from_text('bar.', None) - e = dns.name.from_text('a.foo', None) - self.failUnless(n.relativize(o) == e) - - def testRelativize2(self): - n = dns.name.from_text('a.foo.bar.', None) - o = n - e = dns.name.empty - self.failUnless(n.relativize(o) == e) - - def testRelativize3(self): - n = dns.name.from_text('a.foo.bar.', None) - o = dns.name.from_text('blaz.', None) - e = n - self.failUnless(n.relativize(o) == e) - - def testRelativize4(self): - n = dns.name.from_text('a.foo', None) - o = dns.name.root - e = n - self.failUnless(n.relativize(o) == e) - - def testDerelativize1(self): - n = dns.name.from_text('a.foo', None) - o = dns.name.from_text('bar.', None) - e = dns.name.from_text('a.foo.bar.', None) - self.failUnless(n.derelativize(o) == e) - - def testDerelativize2(self): - n = dns.name.empty - o = dns.name.from_text('a.foo.bar.', None) - e = o - self.failUnless(n.derelativize(o) == e) - - def testDerelativize3(self): - n = dns.name.from_text('a.foo.bar.', None) - o = dns.name.from_text('blaz.', None) - e = n - self.failUnless(n.derelativize(o) == e) - - def testChooseRelativity1(self): - n = dns.name.from_text('a.foo.bar.', None) - o = dns.name.from_text('bar.', None) - e = dns.name.from_text('a.foo', None) - self.failUnless(n.choose_relativity(o, True) == e) - - def testChooseRelativity2(self): - n = dns.name.from_text('a.foo.bar.', None) - o = dns.name.from_text('bar.', None) - e = n - self.failUnless(n.choose_relativity(o, False) == e) - - def testChooseRelativity3(self): - n = dns.name.from_text('a.foo', None) - o = dns.name.from_text('bar.', None) - e = dns.name.from_text('a.foo.bar.', None) - self.failUnless(n.choose_relativity(o, False) == e) - - def testChooseRelativity4(self): - n = dns.name.from_text('a.foo', None) - o = None - e = n - self.failUnless(n.choose_relativity(o, True) == e) - - def testChooseRelativity5(self): - n = dns.name.from_text('a.foo', None) - o = None - e = n - self.failUnless(n.choose_relativity(o, False) == e) - - def testChooseRelativity6(self): - n = dns.name.from_text('a.foo.', None) - o = None - e = n - self.failUnless(n.choose_relativity(o, True) == e) - - def testChooseRelativity7(self): - n = dns.name.from_text('a.foo.', None) - o = None - e = n - self.failUnless(n.choose_relativity(o, False) == e) - - def testFromWire1(self): - w = '\x03foo\x00\xc0\x00' - (n1, cused1) = dns.name.from_wire(w, 0) - (n2, cused2) = dns.name.from_wire(w, cused1) - en1 = dns.name.from_text('foo.') - en2 = en1 - ecused1 = 5 - ecused2 = 2 - self.failUnless(n1 == en1 and cused1 == ecused1 and \ - n2 == en2 and cused2 == ecused2) - - def testFromWire1(self): - w = '\x03foo\x00\x01a\xc0\x00\x01b\xc0\x05' - current = 0 - (n1, cused1) = dns.name.from_wire(w, current) - current += cused1 - (n2, cused2) = dns.name.from_wire(w, current) - current += cused2 - (n3, cused3) = dns.name.from_wire(w, current) - en1 = dns.name.from_text('foo.') - en2 = dns.name.from_text('a.foo.') - en3 = dns.name.from_text('b.a.foo.') - ecused1 = 5 - ecused2 = 4 - ecused3 = 4 - self.failUnless(n1 == en1 and cused1 == ecused1 and \ - n2 == en2 and cused2 == ecused2 and \ - n3 == en3 and cused3 == ecused3) - - def testBadFromWire1(self): - def bad(): - w = '\x03foo\xc0\x04' - (n, cused) = dns.name.from_wire(w, 0) - self.failUnlessRaises(dns.name.BadPointer, bad) - - def testBadFromWire2(self): - def bad(): - w = '\x03foo\xc0\x05' - (n, cused) = dns.name.from_wire(w, 0) - self.failUnlessRaises(dns.name.BadPointer, bad) - - def testBadFromWire3(self): - def bad(): - w = '\xbffoo' - (n, cused) = dns.name.from_wire(w, 0) - self.failUnlessRaises(dns.name.BadLabelType, bad) - - def testBadFromWire4(self): - def bad(): - w = '\x41foo' - (n, cused) = dns.name.from_wire(w, 0) - self.failUnlessRaises(dns.name.BadLabelType, bad) - - def testParent1(self): - n = dns.name.from_text('foo.bar.') - self.failUnless(n.parent() == dns.name.from_text('bar.')) - self.failUnless(n.parent().parent() == dns.name.root) - - def testParent2(self): - n = dns.name.from_text('foo.bar', None) - self.failUnless(n.parent() == dns.name.from_text('bar', None)) - self.failUnless(n.parent().parent() == dns.name.empty) - - def testParent3(self): - def bad(): - n = dns.name.root - n.parent() - self.failUnlessRaises(dns.name.NoParent, bad) - - def testParent4(self): - def bad(): - n = dns.name.empty - n.parent() - self.failUnlessRaises(dns.name.NoParent, bad) - - def testFromUnicode1(self): - n = dns.name.from_text(u'foo.bar') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testFromUnicode2(self): - n = dns.name.from_text(u'foo\u1234bar.bar') - self.failUnless(n.labels == ('xn--foobar-r5z', 'bar', '')) - - def testFromUnicodeAlternateDot1(self): - n = dns.name.from_text(u'foo\u3002bar') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testFromUnicodeAlternateDot2(self): - n = dns.name.from_text(u'foo\uff0ebar') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testFromUnicodeAlternateDot3(self): - n = dns.name.from_text(u'foo\uff61bar') - self.failUnless(n.labels == ('foo', 'bar', '')) - - def testToUnicode1(self): - n = dns.name.from_text(u'foo.bar') - s = n.to_unicode() - self.failUnless(s == u'foo.bar.') - - def testToUnicode2(self): - n = dns.name.from_text(u'foo\u1234bar.bar') - s = n.to_unicode() - self.failUnless(s == u'foo\u1234bar.bar.') - - def testToUnicode3(self): - n = dns.name.from_text('foo.bar') - s = n.to_unicode() - self.failUnless(s == u'foo.bar.') - - def testReverseIPv4(self): - e = dns.name.from_text('1.0.0.127.in-addr.arpa.') - n = dns.reversename.from_address('127.0.0.1') - self.failUnless(e == n) - - def testReverseIPv6(self): - e = dns.name.from_text('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.') - n = dns.reversename.from_address('::1') - self.failUnless(e == n) - - def testBadReverseIPv4(self): - def bad(): - n = dns.reversename.from_address('127.0.foo.1') - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testBadReverseIPv6(self): - def bad(): - n = dns.reversename.from_address('::1::1') - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testForwardIPv4(self): - n = dns.name.from_text('1.0.0.127.in-addr.arpa.') - e = '127.0.0.1' - text = dns.reversename.to_address(n) - self.failUnless(text == e) - - def testForwardIPv6(self): - n = dns.name.from_text('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.') - e = '::1' - text = dns.reversename.to_address(n) - self.failUnless(text == e) - - def testE164ToEnum(self): - text = '+1 650 555 1212' - e = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.') - n = dns.e164.from_e164(text) - self.failUnless(n == e) - - def testEnumToE164(self): - n = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.') - e = '+16505551212' - text = dns.e164.to_e164(n) - self.failUnless(text == e) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/namedict.py b/tests/namedict.py deleted file mode 100644 index e256bfe..0000000 --- a/tests/namedict.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.name -import dns.namedict - -class NameTestCase(unittest.TestCase): - - def setUp(self): - self.ndict = dns.namedict.NameDict() - n1 = dns.name.from_text('foo.bar.') - n2 = dns.name.from_text('bar.') - self.ndict[n1] = 1 - self.ndict[n2] = 2 - self.rndict = dns.namedict.NameDict() - n1 = dns.name.from_text('foo.bar', None) - n2 = dns.name.from_text('bar', None) - self.rndict[n1] = 1 - self.rndict[n2] = 2 - - def testDepth(self): - self.failUnless(self.ndict.max_depth == 3) - - def testLookup1(self): - k = dns.name.from_text('foo.bar.') - self.failUnless(self.ndict[k] == 1) - - def testLookup2(self): - k = dns.name.from_text('foo.bar.') - self.failUnless(self.ndict.get_deepest_match(k)[1] == 1) - - def testLookup3(self): - k = dns.name.from_text('a.b.c.foo.bar.') - self.failUnless(self.ndict.get_deepest_match(k)[1] == 1) - - def testLookup4(self): - k = dns.name.from_text('a.b.c.bar.') - self.failUnless(self.ndict.get_deepest_match(k)[1] == 2) - - def testLookup5(self): - def bad(): - n = dns.name.from_text('a.b.c.') - (k, v) = self.ndict.get_deepest_match(n) - self.failUnlessRaises(KeyError, bad) - - def testLookup6(self): - def bad(): - (k, v) = self.ndict.get_deepest_match(dns.name.empty) - self.failUnlessRaises(KeyError, bad) - - def testLookup7(self): - self.ndict[dns.name.empty] = 100 - n = dns.name.from_text('a.b.c.') - (k, v) = self.ndict.get_deepest_match(n) - self.failUnless(v == 100) - - def testLookup8(self): - def bad(): - self.ndict['foo'] = 100 - self.failUnlessRaises(ValueError, bad) - - def testRelDepth(self): - self.failUnless(self.rndict.max_depth == 2) - - def testRelLookup1(self): - k = dns.name.from_text('foo.bar', None) - self.failUnless(self.rndict[k] == 1) - - def testRelLookup2(self): - k = dns.name.from_text('foo.bar', None) - self.failUnless(self.rndict.get_deepest_match(k)[1] == 1) - - def testRelLookup3(self): - k = dns.name.from_text('a.b.c.foo.bar', None) - self.failUnless(self.rndict.get_deepest_match(k)[1] == 1) - - def testRelLookup4(self): - k = dns.name.from_text('a.b.c.bar', None) - self.failUnless(self.rndict.get_deepest_match(k)[1] == 2) - - def testRelLookup7(self): - self.rndict[dns.name.empty] = 100 - n = dns.name.from_text('a.b.c', None) - (k, v) = self.rndict.get_deepest_match(n) - self.failUnless(v == 100) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/ntoaaton.py b/tests/ntoaaton.py deleted file mode 100644 index e93de2d..0000000 --- a/tests/ntoaaton.py +++ /dev/null @@ -1,203 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.exception -import dns.ipv4 -import dns.ipv6 - -# for convenience -aton4 = dns.ipv4.inet_aton -ntoa4 = dns.ipv4.inet_ntoa -aton6 = dns.ipv6.inet_aton -ntoa6 = dns.ipv6.inet_ntoa - -v4_bad_addrs = ['256.1.1.1', '1.1.1', '1.1.1.1.1', '01.1.1.1', - '+1.1.1.1', '1.1.1.1+', '1..2.3.4', '.1.2.3.4', - '1.2.3.4.'] - -class NtoAAtoNTestCase(unittest.TestCase): - - def test_aton1(self): - a = aton6('::') - self.failUnless(a == '\x00' * 16) - - def test_aton2(self): - a = aton6('::1') - self.failUnless(a == '\x00' * 15 + '\x01') - - def test_aton3(self): - a = aton6('::10.0.0.1') - self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01') - - def test_aton4(self): - a = aton6('abcd::dcba') - self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba') - - def test_aton5(self): - a = aton6('1:2:3:4:5:6:7:8') - self.failUnless(a == \ - '00010002000300040005000600070008'.decode('hex_codec')) - - def test_bad_aton1(self): - def bad(): - a = aton6('abcd:dcba') - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def test_bad_aton2(self): - def bad(): - a = aton6('abcd::dcba::1') - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def test_bad_aton3(self): - def bad(): - a = aton6('1:2:3:4:5:6:7:8:9') - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def test_aton1(self): - a = aton6('::') - self.failUnless(a == '\x00' * 16) - - def test_aton2(self): - a = aton6('::1') - self.failUnless(a == '\x00' * 15 + '\x01') - - def test_aton3(self): - a = aton6('::10.0.0.1') - self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01') - - def test_aton4(self): - a = aton6('abcd::dcba') - self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba') - - def test_ntoa1(self): - b = '00010002000300040005000600070008'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '1:2:3:4:5:6:7:8') - - def test_ntoa2(self): - b = '\x00' * 16 - t = ntoa6(b) - self.failUnless(t == '::') - - def test_ntoa3(self): - b = '\x00' * 15 + '\x01' - t = ntoa6(b) - self.failUnless(t == '::1') - - def test_ntoa4(self): - b = '\x80' + '\x00' * 15 - t = ntoa6(b) - self.failUnless(t == '8000::') - - def test_ntoa5(self): - b = '\x01\xcd' + '\x00' * 12 + '\x03\xef' - t = ntoa6(b) - self.failUnless(t == '1cd::3ef') - - def test_ntoa6(self): - b = 'ffff00000000ffff000000000000ffff'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == 'ffff:0:0:ffff::ffff') - - def test_ntoa7(self): - b = '00000000ffff000000000000ffffffff'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '0:0:ffff::ffff:ffff') - - def test_ntoa8(self): - b = 'ffff0000ffff00000000ffff00000000'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == 'ffff:0:ffff::ffff:0:0') - - def test_ntoa9(self): - b = '0000000000000000000000000a000001'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::10.0.0.1') - - def test_ntoa10(self): - b = '0000000000000000000000010a000001'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::1:a00:1') - - def test_ntoa11(self): - b = '00000000000000000000ffff0a000001'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::ffff:10.0.0.1') - - def test_ntoa12(self): - b = '000000000000000000000000ffffffff'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::255.255.255.255') - - def test_ntoa13(self): - b = '00000000000000000000ffffffffffff'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::ffff:255.255.255.255') - - def test_ntoa14(self): - b = '0000000000000000000000000001ffff'.decode('hex_codec') - t = ntoa6(b) - self.failUnless(t == '::0.1.255.255') - - def test_bad_ntoa1(self): - def bad(): - a = ntoa6('') - self.failUnlessRaises(ValueError, bad) - - def test_bad_ntoa2(self): - def bad(): - a = ntoa6('\x00' * 17) - self.failUnlessRaises(ValueError, bad) - - def test_good_v4_aton(self): - pairs = [('1.2.3.4', '\x01\x02\x03\x04'), - ('255.255.255.255', '\xff\xff\xff\xff'), - ('0.0.0.0', '\x00\x00\x00\x00')] - for (t, b) in pairs: - b1 = aton4(t) - t1 = ntoa4(b1) - self.failUnless(b1 == b) - self.failUnless(t1 == t) - - def test_bad_v4_aton(self): - def make_bad(a): - def bad(): - return aton4(a) - return bad - for addr in v4_bad_addrs: - self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr)) - - def test_bad_v6_aton(self): - addrs = ['+::0', '0::0::', '::0::', '1:2:3:4:5:6:7:8:9', - ':::::::'] - embedded = ['::' + x for x in v4_bad_addrs] - addrs.extend(embedded) - def make_bad(a): - def bad(): - x = aton6(a) - return bad - for addr in addrs: - self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr)) - - def test_rfc5952_section_4_2_2(self): - addr = '2001:db8:0:1:1:1:1:1' - b1 = aton6(addr) - t1 = ntoa6(b1) - self.failUnless(t1 == addr) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/rdtypeandclass.py b/tests/rdtypeandclass.py deleted file mode 100644 index f3c0628..0000000 --- a/tests/rdtypeandclass.py +++ /dev/null @@ -1,123 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.rdataclass -import dns.rdatatype - -class RdTypeAndClassTestCase(unittest.TestCase): - - # Classes - - def test_class_meta1(self): - self.failUnless(dns.rdataclass.is_metaclass(dns.rdataclass.ANY)) - - def test_class_meta2(self): - self.failUnless(not dns.rdataclass.is_metaclass(dns.rdataclass.IN)) - - def test_class_bytext1(self): - self.failUnless(dns.rdataclass.from_text('IN') == dns.rdataclass.IN) - - def test_class_bytext2(self): - self.failUnless(dns.rdataclass.from_text('CLASS1') == - dns.rdataclass.IN) - - def test_class_bytext_bounds1(self): - self.failUnless(dns.rdataclass.from_text('CLASS0') == 0) - self.failUnless(dns.rdataclass.from_text('CLASS65535') == 65535) - - def test_class_bytext_bounds2(self): - def bad(): - junk = dns.rdataclass.from_text('CLASS65536') - self.failUnlessRaises(ValueError, bad) - - def test_class_bytext_unknown(self): - def bad(): - junk = dns.rdataclass.from_text('XXX') - self.failUnlessRaises(dns.rdataclass.UnknownRdataclass, bad) - - def test_class_totext1(self): - self.failUnless(dns.rdataclass.to_text(dns.rdataclass.IN) == 'IN') - - def test_class_totext1(self): - self.failUnless(dns.rdataclass.to_text(999) == 'CLASS999') - - def test_class_totext_bounds1(self): - def bad(): - junk = dns.rdataclass.to_text(-1) - self.failUnlessRaises(ValueError, bad) - - def test_class_totext_bounds2(self): - def bad(): - junk = dns.rdataclass.to_text(65536) - self.failUnlessRaises(ValueError, bad) - - # Types - - def test_type_meta1(self): - self.failUnless(dns.rdatatype.is_metatype(dns.rdatatype.ANY)) - - def test_type_meta2(self): - self.failUnless(dns.rdatatype.is_metatype(dns.rdatatype.OPT)) - - def test_type_meta3(self): - self.failUnless(not dns.rdatatype.is_metatype(dns.rdatatype.A)) - - def test_type_singleton1(self): - self.failUnless(dns.rdatatype.is_singleton(dns.rdatatype.SOA)) - - def test_type_singleton2(self): - self.failUnless(not dns.rdatatype.is_singleton(dns.rdatatype.A)) - - def test_type_bytext1(self): - self.failUnless(dns.rdatatype.from_text('A') == dns.rdatatype.A) - - def test_type_bytext2(self): - self.failUnless(dns.rdatatype.from_text('TYPE1') == - dns.rdatatype.A) - - def test_type_bytext_bounds1(self): - self.failUnless(dns.rdatatype.from_text('TYPE0') == 0) - self.failUnless(dns.rdatatype.from_text('TYPE65535') == 65535) - - def test_type_bytext_bounds2(self): - def bad(): - junk = dns.rdatatype.from_text('TYPE65536') - self.failUnlessRaises(ValueError, bad) - - def test_type_bytext_unknown(self): - def bad(): - junk = dns.rdatatype.from_text('XXX') - self.failUnlessRaises(dns.rdatatype.UnknownRdatatype, bad) - - def test_type_totext1(self): - self.failUnless(dns.rdatatype.to_text(dns.rdatatype.A) == 'A') - - def test_type_totext1(self): - self.failUnless(dns.rdatatype.to_text(999) == 'TYPE999') - - def test_type_totext_bounds1(self): - def bad(): - junk = dns.rdatatype.to_text(-1) - self.failUnlessRaises(ValueError, bad) - - def test_type_totext_bounds2(self): - def bad(): - junk = dns.rdatatype.to_text(65536) - self.failUnlessRaises(ValueError, bad) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/rdtypeanyloc.py b/tests/rdtypeanyloc.py deleted file mode 100644 index 8d9838c..0000000 --- a/tests/rdtypeanyloc.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (C) 2014 Red Hat, Inc. -# Author: Petr Spacek -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED 'AS IS' AND RED HAT DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.rrset -import dns.rdtypes.ANY.LOC - -class RdtypeAnyLocTestCase(unittest.TestCase): - - def testEqual1(self): - '''Test default values for size, horizontal and vertical precision.''' - r1 = dns.rrset.from_text('foo', 300, 'IN', 'LOC', - '49 11 42.400 N 16 36 29.600 E 227.64m') - r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', - '49 11 42.400 N 16 36 29.600 E 227.64m ' - '1.00m 10000.00m 10.00m') - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) - - def testEqual2(self): - '''Test default values for size, horizontal and vertical precision.''' - r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), - (16, 36, 29, 600), 22764.0) # centimeters - r2 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), - (16, 36, 29, 600), 22764.0, # centimeters - 100.0, 1000000.00, 1000.0) # centimeters - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) - - def testEqual3(self): - '''Test size, horizontal and vertical precision parsers: 100 cm == 1 m. - - Parsers in from_text() and __init__() have to produce equal results.''' - r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), - (16, 36, 29, 600), 22764.0, - 200.0, 1000.00, 200.0) # centimeters - r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', - '49 11 42.400 N 16 36 29.600 E 227.64m ' - '2.00m 10.00m 2.00m')[0] - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) - - def testEqual4(self): - '''Test size, horizontal and vertical precision parsers without unit. - - Parsers in from_text() and __init__() have produce equal result - for values with and without trailing "m".''' - r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), - (16, 36, 29, 600), 22764.0, - 200.0, 1000.00, 200.0) # centimeters - r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', - '49 11 42.400 N 16 36 29.600 E 227.64 ' - '2 10 2')[0] # meters without explicit unit - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/resolver.py b/tests/resolver.py deleted file mode 100644 index 6be955b..0000000 --- a/tests/resolver.py +++ /dev/null @@ -1,184 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import cStringIO -import select -import sys -import time -import unittest - -import dns.name -import dns.message -import dns.name -import dns.rdataclass -import dns.rdatatype -import dns.resolver - -resolv_conf = """ - /t/t -# comment 1 -; comment 2 -domain foo -nameserver 10.0.0.1 -nameserver 10.0.0.2 -""" - -message_text = """id 1234 -opcode QUERY -rcode NOERROR -flags QR AA RD -;QUESTION -example. IN A -;ANSWER -example. 1 IN A 10.0.0.1 -;AUTHORITY -;ADDITIONAL -""" - -class FakeAnswer(object): - def __init__(self, expiration): - self.expiration = expiration - -class BaseResolverTests(object): - - if sys.platform != 'win32': - def testRead(self): - f = cStringIO.StringIO(resolv_conf) - r = dns.resolver.Resolver(f) - self.failUnless(r.nameservers == ['10.0.0.1', '10.0.0.2'] and - r.domain == dns.name.from_text('foo')) - - def testCacheExpiration(self): - message = dns.message.from_text(message_text) - name = dns.name.from_text('example.') - answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN, - message) - cache = dns.resolver.Cache() - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - time.sleep(2) - self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) - is None) - - def testCacheCleaning(self): - message = dns.message.from_text(message_text) - name = dns.name.from_text('example.') - answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN, - message) - cache = dns.resolver.Cache(cleaning_interval=1.0) - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - time.sleep(2) - self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) - is None) - - def testZoneForName1(self): - name = dns.name.from_text('www.dnspython.org.') - ezname = dns.name.from_text('dnspython.org.') - zname = dns.resolver.zone_for_name(name) - self.failUnless(zname == ezname) - - def testZoneForName2(self): - name = dns.name.from_text('a.b.www.dnspython.org.') - ezname = dns.name.from_text('dnspython.org.') - zname = dns.resolver.zone_for_name(name) - self.failUnless(zname == ezname) - - def testZoneForName3(self): - name = dns.name.from_text('dnspython.org.') - ezname = dns.name.from_text('dnspython.org.') - zname = dns.resolver.zone_for_name(name) - self.failUnless(zname == ezname) - - def testZoneForName4(self): - def bad(): - name = dns.name.from_text('dnspython.org', None) - zname = dns.resolver.zone_for_name(name) - self.failUnlessRaises(dns.resolver.NotAbsolute, bad) - - def testLRUReplace(self): - cache = dns.resolver.LRUCache(4) - for i in xrange(0, 5): - name = dns.name.from_text('example%d.' % i) - answer = FakeAnswer(time.time() + 1) - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - for i in xrange(0, 5): - name = dns.name.from_text('example%d.' % i) - if i == 0: - self.failUnless(cache.get((name, dns.rdatatype.A, - dns.rdataclass.IN)) - is None) - else: - self.failUnless(not cache.get((name, dns.rdatatype.A, - dns.rdataclass.IN)) - is None) - - def testLRUDoesLRU(self): - cache = dns.resolver.LRUCache(4) - for i in xrange(0, 4): - name = dns.name.from_text('example%d.' % i) - answer = FakeAnswer(time.time() + 1) - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - name = dns.name.from_text('example0.') - cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) - # The LRU is now example1. - name = dns.name.from_text('example4.') - answer = FakeAnswer(time.time() + 1) - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - for i in xrange(0, 5): - name = dns.name.from_text('example%d.' % i) - if i == 1: - self.failUnless(cache.get((name, dns.rdatatype.A, - dns.rdataclass.IN)) - is None) - else: - self.failUnless(not cache.get((name, dns.rdatatype.A, - dns.rdataclass.IN)) - is None) - - def testLRUExpiration(self): - cache = dns.resolver.LRUCache(4) - for i in xrange(0, 4): - name = dns.name.from_text('example%d.' % i) - answer = FakeAnswer(time.time() + 1) - cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) - time.sleep(2) - for i in xrange(0, 4): - name = dns.name.from_text('example%d.' % i) - self.failUnless(cache.get((name, dns.rdatatype.A, - dns.rdataclass.IN)) - is None) - -class PollingMonkeyPatchMixin(object): - def setUp(self): - self.__native_polling_backend = dns.query._polling_backend - dns.query._set_polling_backend(self.polling_backend()) - - unittest.TestCase.setUp(self) - - def tearDown(self): - dns.query._set_polling_backend(self.__native_polling_backend) - - unittest.TestCase.tearDown(self) - -class SelectResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase): - def polling_backend(self): - return dns.query._select_for - -if hasattr(select, 'poll'): - class PollResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase): - def polling_backend(self): - return dns.query._poll_for - -if __name__ == '__main__': - unittest.main() diff --git a/tests/rrset.py b/tests/rrset.py deleted file mode 100644 index be1324b..0000000 --- a/tests/rrset.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.rrset - -class RRsetTestCase(unittest.TestCase): - - def testEqual1(self): - r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2') - r2 = dns.rrset.from_text('FOO', 300, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 == r2) - - def testEqual2(self): - r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2') - r2 = dns.rrset.from_text('FOO', 600, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 == r2) - - def testNotEqual1(self): - r1 = dns.rrset.from_text('fooa', 30, 'in', 'a', '10.0.0.1', '10.0.0.2') - r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 != r2) - - def testNotEqual2(self): - r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.3') - r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 != r2) - - def testNotEqual3(self): - r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.2', - '10.0.0.3') - r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 != r2) - - def testNotEqual4(self): - r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1') - r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') - self.failUnless(r1 != r2) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/set.py b/tests/set.py deleted file mode 100644 index 583d20c..0000000 --- a/tests/set.py +++ /dev/null @@ -1,208 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.set - -# for convenience -S = dns.set.Set - -class SimpleSetTestCase(unittest.TestCase): - - def testLen1(self): - s1 = S() - self.failUnless(len(s1) == 0) - - def testLen2(self): - s1 = S([1, 2, 3]) - self.failUnless(len(s1) == 3) - - def testLen3(self): - s1 = S([1, 2, 3, 3, 3]) - self.failUnless(len(s1) == 3) - - def testUnion1(self): - s1 = S([1, 2, 3]) - s2 = S([1, 2, 3]) - e = S([1, 2, 3]) - self.failUnless(s1 | s2 == e) - - def testUnion2(self): - s1 = S([1, 2, 3]) - s2 = S([]) - e = S([1, 2, 3]) - self.failUnless(s1 | s2 == e) - - def testUnion3(self): - s1 = S([1, 2, 3]) - s2 = S([3, 4]) - e = S([1, 2, 3, 4]) - self.failUnless(s1 | s2 == e) - - def testIntersection1(self): - s1 = S([1, 2, 3]) - s2 = S([1, 2, 3]) - e = S([1, 2, 3]) - self.failUnless(s1 & s2 == e) - - def testIntersection2(self): - s1 = S([0, 1, 2, 3]) - s2 = S([1, 2, 3, 4]) - e = S([1, 2, 3]) - self.failUnless(s1 & s2 == e) - - def testIntersection3(self): - s1 = S([1, 2, 3]) - s2 = S([]) - e = S([]) - self.failUnless(s1 & s2 == e) - - def testIntersection4(self): - s1 = S([1, 2, 3]) - s2 = S([5, 4]) - e = S([]) - self.failUnless(s1 & s2 == e) - - def testDifference1(self): - s1 = S([1, 2, 3]) - s2 = S([5, 4]) - e = S([1, 2, 3]) - self.failUnless(s1 - s2 == e) - - def testDifference2(self): - s1 = S([1, 2, 3]) - s2 = S([]) - e = S([1, 2, 3]) - self.failUnless(s1 - s2 == e) - - def testDifference3(self): - s1 = S([1, 2, 3]) - s2 = S([3, 2]) - e = S([1]) - self.failUnless(s1 - s2 == e) - - def testDifference4(self): - s1 = S([1, 2, 3]) - s2 = S([3, 2, 1]) - e = S([]) - self.failUnless(s1 - s2 == e) - - def testSubset1(self): - s1 = S([1, 2, 3]) - s2 = S([3, 2, 1]) - self.failUnless(s1.issubset(s2)) - - def testSubset2(self): - s1 = S([1, 2, 3]) - self.failUnless(s1.issubset(s1)) - - def testSubset3(self): - s1 = S([]) - s2 = S([1, 2, 3]) - self.failUnless(s1.issubset(s2)) - - def testSubset4(self): - s1 = S([1]) - s2 = S([1, 2, 3]) - self.failUnless(s1.issubset(s2)) - - def testSubset5(self): - s1 = S([]) - s2 = S([]) - self.failUnless(s1.issubset(s2)) - - def testSubset6(self): - s1 = S([1, 4]) - s2 = S([1, 2, 3]) - self.failUnless(not s1.issubset(s2)) - - def testSuperset1(self): - s1 = S([1, 2, 3]) - s2 = S([3, 2, 1]) - self.failUnless(s1.issuperset(s2)) - - def testSuperset2(self): - s1 = S([1, 2, 3]) - self.failUnless(s1.issuperset(s1)) - - def testSuperset3(self): - s1 = S([1, 2, 3]) - s2 = S([]) - self.failUnless(s1.issuperset(s2)) - - def testSuperset4(self): - s1 = S([1, 2, 3]) - s2 = S([1]) - self.failUnless(s1.issuperset(s2)) - - def testSuperset5(self): - s1 = S([]) - s2 = S([]) - self.failUnless(s1.issuperset(s2)) - - def testSuperset6(self): - s1 = S([1, 2, 3]) - s2 = S([1, 4]) - self.failUnless(not s1.issuperset(s2)) - - def testUpdate1(self): - s1 = S([1, 2, 3]) - u = (4, 5, 6) - e = S([1, 2, 3, 4, 5, 6]) - s1.update(u) - self.failUnless(s1 == e) - - def testUpdate2(self): - s1 = S([1, 2, 3]) - u = [] - e = S([1, 2, 3]) - s1.update(u) - self.failUnless(s1 == e) - - def testGetitem(self): - s1 = S([1, 2, 3]) - i0 = s1[0] - i1 = s1[1] - i2 = s1[2] - s2 = S([i0, i1, i2]) - self.failUnless(s1 == s2) - - def testGetslice(self): - s1 = S([1, 2, 3]) - slice = s1[0:2] - self.failUnless(len(slice) == 2) - item = s1[2] - slice.append(item) - s2 = S(slice) - self.failUnless(s1 == s2) - - def testDelitem(self): - s1 = S([1, 2, 3]) - del s1[0] - i1 = s1[0] - i2 = s1[1] - self.failUnless(i1 != i2) - self.failUnless(i1 == 1 or i1 == 2 or i1 == 3) - self.failUnless(i2 == 1 or i2 == 2 or i2 == 3) - - def testDelslice(self): - s1 = S([1, 2, 3]) - del s1[0:2] - i1 = s1[0] - self.failUnless(i1 == 1 or i1 == 2 or i1 == 3) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_bugs.py b/tests/test_bugs.py new file mode 100644 index 0000000..312ec3e --- /dev/null +++ b/tests/test_bugs.py @@ -0,0 +1,49 @@ +# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.rdata +import dns.rdataclass +import dns.rdatatype +import dns.ttl + +class BugsTestCase(unittest.TestCase): + + def test_float_LOC(self): + rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.LOC, + "30 30 0.000 N 100 30 0.000 W 10.00m 20m 2000m 20m") + self.failUnless(rdata.float_latitude == 30.5) + self.failUnless(rdata.float_longitude == -100.5) + + def test_SOA_BIND8_TTL(self): + rdata1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + "a b 100 1s 1m 1h 1d") + rdata2 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + "a b 100 1 60 3600 86400") + self.failUnless(rdata1 == rdata2) + + def test_TTL_bounds_check(self): + def bad(): + ttl = dns.ttl.from_text("2147483648") + self.failUnlessRaises(dns.ttl.BadTTL, bad) + + def test_empty_NSEC3_window(self): + rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NSEC3, + "1 0 100 ABCD SCBCQHKU35969L2A68P3AD59LHF30715") + self.failUnless(rdata.windows == []) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_dnssec.py b/tests/test_dnssec.py new file mode 100644 index 0000000..cbbd3e2 --- /dev/null +++ b/tests/test_dnssec.py @@ -0,0 +1,233 @@ +# Copyright (C) 2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.dnssec +import dns.name +import dns.rdata +import dns.rdataclass +import dns.rdatatype +import dns.rrset + +abs_dnspython_org = dns.name.from_text('dnspython.org') + +abs_keys = { abs_dnspython_org : + dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'DNSKEY', + '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', + '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') + } + +abs_keys_duplicate_keytag = { abs_dnspython_org : + dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'DNSKEY', + '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', + '256 3 5 AwEAAdSSg++++THIS/IS/NOT/THE/CORRECT/KEY++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ AaOSydAF', + '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') + } + +rel_keys = { dns.name.empty : + dns.rrset.from_text('@', 3600, 'IN', 'DNSKEY', + '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=', + '256 3 5 AwEAAdSSghOGjU33IQZgwZM2Hh771VGXX05olJK49FxpSyuEAjDBXY58 LGU9R2Zgeecnk/b9EAhFu/vCV9oECtiTCvwuVAkt9YEweqYDluQInmgP NGMJCKdSLlnX93DkjDw8rMYv5dqXCuSGPlKChfTJOLQxIAxGloS7lL+c 0CTZydAF') + } + +when = 1290250287 + +abs_soa = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'SOA', + 'howl.dnspython.org. hostmaster.dnspython.org. 2010020047 3600 1800 604800 3600') + +abs_other_soa = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'SOA', + 'foo.dnspython.org. hostmaster.dnspython.org. 2010020047 3600 1800 604800 3600') + +abs_soa_rrsig = dns.rrset.from_text('dnspython.org.', 3600, 'IN', 'RRSIG', + 'SOA 5 2 3600 20101127004331 20101119213831 61695 dnspython.org. sDUlltRlFTQw5ITFxOXW3TgmrHeMeNpdqcZ4EXxM9FHhIlte6V9YCnDw t6dvM9jAXdIEi03l9H/RAd9xNNW6gvGMHsBGzpvvqFQxIBR2PoiZA1mX /SWHZFdbt4xjYTtXqpyYvrMK0Dt7bUYPadyhPFCJ1B+I8Zi7B5WJEOd0 8vs=') + +rel_soa = dns.rrset.from_text('@', 3600, 'IN', 'SOA', + 'howl hostmaster 2010020047 3600 1800 604800 3600') + +rel_other_soa = dns.rrset.from_text('@', 3600, 'IN', 'SOA', + 'foo hostmaster 2010020047 3600 1800 604800 3600') + +rel_soa_rrsig = dns.rrset.from_text('@', 3600, 'IN', 'RRSIG', + 'SOA 5 2 3600 20101127004331 20101119213831 61695 @ sDUlltRlFTQw5ITFxOXW3TgmrHeMeNpdqcZ4EXxM9FHhIlte6V9YCnDw t6dvM9jAXdIEi03l9H/RAd9xNNW6gvGMHsBGzpvvqFQxIBR2PoiZA1mX /SWHZFdbt4xjYTtXqpyYvrMK0Dt7bUYPadyhPFCJ1B+I8Zi7B5WJEOd0 8vs=') + +sep_key = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DNSKEY, + '257 3 5 AwEAAenVTr9L1OMlL1/N2ta0Qj9LLLnnmFWIr1dJoAsWM9BQfsbV7kFZ XbAkER/FY9Ji2o7cELxBwAsVBuWn6IUUAJXLH74YbC1anY0lifjgt29z SwDzuB7zmC7yVYZzUunBulVW4zT0tg1aePbpVL2EtTL8VzREqbJbE25R KuQYHZtFwG8S4iBxJUmT2Bbd0921LLxSQgVoFXlQx/gFV2+UERXcJ5ce iX6A6wc02M/pdg/YbJd2rBa0MYL3/Fz/Xltre0tqsImZGxzi6YtYDs45 NC8gH+44egz82e2DATCVM1ICPmRDjXYTLldQiWA2ZXIWnK0iitl5ue24 7EsWJefrIhE=') + +good_ds = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, + '57349 5 2 53A79A3E7488AB44FFC56B2D1109F0699D1796DD977E72108B841F96 E47D7013') + +when2 = 1290425644 + +abs_example = dns.name.from_text('example') + +abs_dsa_keys = { abs_example : + dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', + '257 3 3 CI3nCqyJsiCJHTjrNsJOT4RaszetzcJPYuoH3F9ZTVt3KJXncCVR3bwn 1w0iavKljb9hDlAYSfHbFCp4ic/rvg4p1L8vh5s8ToMjqDNl40A0hUGQ Ybx5hsECyK+qHoajilUX1phYSAD8d9WAGO3fDWzUPBuzR7o85NiZCDxz yXuNVfni0uhj9n1KYhEO5yAbbruDGN89wIZcxMKuQsdUY2GYD93ssnBv a55W6XRABYWayKZ90WkRVODLVYLSn53Pj/wwxGH+XdhIAZJXimrZL4yl My7rtBsLMqq8Ihs4Tows7LqYwY7cp6y/50tw6pj8tFqMYcPUjKZV36l1 M/2t5BVg3i7IK61Aidt6aoC3TDJtzAxg3ZxfjZWJfhHjMJqzQIfbW5b9 q1mjFsW5EUv39RaNnX+3JWPRLyDqD4pIwDyqfutMsdk/Py3paHn82FGp CaOg+nicqZ9TiMZURN/XXy5JoXUNQ3RNvbHCUiPUe18KUkY6mTfnyHld 1l9YCWmzXQVClkx/hOYxjJ4j8Ife58+Obu5X', + '256 3 3 CJE1yb9YRQiw5d2xZrMUMR+cGCTt1bp1KDCefmYKmS+Z1+q9f42ETVhx JRiQwXclYwmxborzIkSZegTNYIV6mrYwbNB27Q44c3UGcspb3PiOw5TC jNPRYEcdwGvDZ2wWy+vkSV/S9tHXY8O6ODiE6abZJDDg/RnITyi+eoDL R3KZ5n/V1f1T1b90rrV6EewhBGQJpQGDogaXb2oHww9Tm6NfXyo7SoMM pbwbzOckXv+GxRPJIQNSF4D4A9E8XCksuzVVdE/0lr37+uoiAiPia38U 5W2QWe/FJAEPLjIp2eTzf0TrADc1pKP1wrA2ASpdzpm/aX3IB5RPp8Ew S9U72eBFZJAUwg635HxJVxH1maG6atzorR566E+e0OZSaxXS9o1o6QqN 3oPlYLGPORDiExilKfez3C/x/yioOupW9K5eKF0gmtaqrHX0oq9s67f/ RIM2xVaKHgG9Vf2cgJIZkhv7sntujr+E4htnRmy9P9BxyFxsItYxPI6Z bzygHAZpGhlI/7ltEGlIwKxyTK3ZKBm67q7B') + } + +abs_dsa_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 2 10800 3600 604800 86400') + +abs_other_dsa_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') + +abs_dsa_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', + 'SOA 3 1 86400 20101129143231 20101122112731 42088 example. CGul9SuBofsktunV8cJs4eRs6u+3NCS3yaPKvBbD+pB2C76OUXDZq9U=') + +example_sep_key = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DNSKEY, + '257 3 3 CI3nCqyJsiCJHTjrNsJOT4RaszetzcJPYuoH3F9ZTVt3KJXncCVR3bwn 1w0iavKljb9hDlAYSfHbFCp4ic/rvg4p1L8vh5s8ToMjqDNl40A0hUGQ Ybx5hsECyK+qHoajilUX1phYSAD8d9WAGO3fDWzUPBuzR7o85NiZCDxz yXuNVfni0uhj9n1KYhEO5yAbbruDGN89wIZcxMKuQsdUY2GYD93ssnBv a55W6XRABYWayKZ90WkRVODLVYLSn53Pj/wwxGH+XdhIAZJXimrZL4yl My7rtBsLMqq8Ihs4Tows7LqYwY7cp6y/50tw6pj8tFqMYcPUjKZV36l1 M/2t5BVg3i7IK61Aidt6aoC3TDJtzAxg3ZxfjZWJfhHjMJqzQIfbW5b9 q1mjFsW5EUv39RaNnX+3JWPRLyDqD4pIwDyqfutMsdk/Py3paHn82FGp CaOg+nicqZ9TiMZURN/XXy5JoXUNQ3RNvbHCUiPUe18KUkY6mTfnyHld 1l9YCWmzXQVClkx/hOYxjJ4j8Ife58+Obu5X') + +example_ds_sha1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, + '18673 3 1 71b71d4f3e11bbd71b4eff12cde69f7f9215bbe7') + +example_ds_sha256 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, + '18673 3 2 eb8344cbbf07c9d3d3d6c81d10c76653e28d8611a65e639ef8f716e4e4e5d913') + +when3 = 1379801800 + +abs_ecdsa256_keys = { abs_example : + dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', + "256 3 13 +3ss1sCpdARVA61DJigEsL/8quo2a8MszKtn2gkkfxgzFs8S2UHtpb4N fY+XFmNW+JK6MsCkI3jHYN8eEQUgMw==", + "257 3 13 eJCEVH7AS3wnoaQpaNlAXH0W8wxymtT9P6P3qjN2ZCV641ED8pF7wZ5V yWfOpgTs6oaZevbJgehl/GaRPUgVyQ==") + } + +abs_ecdsa256_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 4 10800 3600 604800 86400') + +abs_other_ecdsa256_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') + +abs_ecdsa256_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', + "SOA 13 1 86400 20130921221753 20130921221638 7460 example. Sm09SOGz1ULB5D/duwdE2Zpn8bWbVBM77H6N1wPkc42LevvVO+kZEjpq 2nq4GOMJcih52667GIAbMrwmU5P2MQ==") + +when4 = 1379804850 + +abs_ecdsa384_keys = { abs_example : + dns.rrset.from_text('example.', 86400, 'IN', 'DNSKEY', + "256 3 14 1bG8qWviKNXQX3BIuG6/T5jrP1FISiLW/8qGF6BsM9DQtWYhhZUA3Owr OAEiyHAhQwjkN2kTvWiAYoPN80Ii+5ff9/atzY4F9W50P4l75Dj9PYrL HN/hLUgWMNVc9pvA", + "257 3 14 mSub2n0KRt6u2FaD5XJ3oQu0R4XvB/9vUJcyW6+oo0y+KzfQeTdkf1ro ZMVKoyWXW9zUKBYGJpMUIdbAxzrYi7f5HyZ3yDpBFz1hw9+o3CX+gtgb +RyhHfJDwwFXBid9") + } + +abs_ecdsa384_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 2 10800 3600 604800 86400') + +abs_other_ecdsa384_soa = dns.rrset.from_text('example.', 86400, 'IN', 'SOA', + 'ns1.example. hostmaster.example. 2 10800 3600 604800 86401') + +abs_ecdsa384_soa_rrsig = dns.rrset.from_text('example.', 86400, 'IN', 'RRSIG', + "SOA 14 1 86400 20130929021229 20130921230729 63571 example. CrnCu34EeeRz0fEhL9PLlwjpBKGYW8QjBjFQTwd+ViVLRAS8tNkcDwQE NhSV89NEjj7ze1a/JcCfcJ+/mZgnvH4NHLNg3Tf6KuLZsgs2I4kKQXEk 37oIHravPEOlGYNI") + +class DNSSECValidatorTestCase(unittest.TestCase): + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testAbsoluteRSAGood(self): + dns.dnssec.validate(abs_soa, abs_soa_rrsig, abs_keys, None, when) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testDuplicateKeytag(self): + dns.dnssec.validate(abs_soa, abs_soa_rrsig, abs_keys_duplicate_keytag, None, when) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testAbsoluteRSABad(self): + def bad(): + dns.dnssec.validate(abs_other_soa, abs_soa_rrsig, abs_keys, None, + when) + self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testRelativeRSAGood(self): + dns.dnssec.validate(rel_soa, rel_soa_rrsig, rel_keys, + abs_dnspython_org, when) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testRelativeRSABad(self): + def bad(): + dns.dnssec.validate(rel_other_soa, rel_soa_rrsig, rel_keys, + abs_dnspython_org, when) + self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) + + def testMakeSHA256DS(self): + ds = dns.dnssec.make_ds(abs_dnspython_org, sep_key, 'SHA256') + self.failUnless(ds == good_ds) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testAbsoluteDSAGood(self): + dns.dnssec.validate(abs_dsa_soa, abs_dsa_soa_rrsig, abs_dsa_keys, None, + when2) + + @unittest.skipIf(not dns.dnssec._have_pycrypto, + "PyCrypto cannot be imported") + def testAbsoluteDSABad(self): + def bad(): + dns.dnssec.validate(abs_other_dsa_soa, abs_dsa_soa_rrsig, + abs_dsa_keys, None, when2) + self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) + + def testMakeExampleSHA1DS(self): + ds = dns.dnssec.make_ds(abs_example, example_sep_key, 'SHA1') + self.failUnless(ds == example_ds_sha1) + + def testMakeExampleSHA256DS(self): + ds = dns.dnssec.make_ds(abs_example, example_sep_key, 'SHA256') + self.failUnless(ds == example_ds_sha256) + + @unittest.skipIf(not dns.dnssec._have_ecdsa, + "python ECDSA cannot be imported") + def testAbsoluteECDSA256Good(self): + dns.dnssec.validate(abs_ecdsa256_soa, abs_ecdsa256_soa_rrsig, + abs_ecdsa256_keys, None, when3) + + @unittest.skipIf(not dns.dnssec._have_ecdsa, + "python ECDSA cannot be imported") + def testAbsoluteECDSA256Bad(self): + def bad(): + dns.dnssec.validate(abs_other_ecdsa256_soa, abs_ecdsa256_soa_rrsig, + abs_ecdsa256_keys, None, when3) + self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) + + @unittest.skipIf(not dns.dnssec._have_ecdsa, + "python ECDSA can not be imported") + def testAbsoluteECDSA384Good(self): + dns.dnssec.validate(abs_ecdsa384_soa, abs_ecdsa384_soa_rrsig, + abs_ecdsa384_keys, None, when4) + + @unittest.skipIf(not dns.dnssec._have_ecdsa, + "python ECDSA can not be imported") + def testAbsoluteECDSA384Bad(self): + def bad(): + dns.dnssec.validate(abs_other_ecdsa384_soa, abs_ecdsa384_soa_rrsig, + abs_ecdsa384_keys, None, when4) + self.failUnlessRaises(dns.dnssec.ValidationFailure, bad) + + +if __name__ == '__main__': + import_ok = False + try: + import Crypto.Util.number + import_ok = True + except: + pass + if import_ok: + unittest.main() + else: + print 'skipping DNSSEC tests because pycrypto is not installed' diff --git a/tests/test_flags.py b/tests/test_flags.py new file mode 100644 index 0000000..b3cf671 --- /dev/null +++ b/tests/test_flags.py @@ -0,0 +1,59 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.flags +import dns.rcode +import dns.opcode + +class FlagsTestCase(unittest.TestCase): + + def test_rcode1(self): + self.failUnless(dns.rcode.from_text('FORMERR') == dns.rcode.FORMERR) + + def test_rcode2(self): + self.failUnless(dns.rcode.to_text(dns.rcode.FORMERR) == "FORMERR") + + def test_rcode3(self): + self.failUnless(dns.rcode.to_flags(dns.rcode.FORMERR) == (1, 0)) + + def test_rcode4(self): + self.failUnless(dns.rcode.to_flags(dns.rcode.BADVERS) == \ + (0, 0x01000000)) + + def test_rcode6(self): + self.failUnless(dns.rcode.from_flags(0, 0x01000000) == \ + dns.rcode.BADVERS) + + def test_rcode6(self): + self.failUnless(dns.rcode.from_flags(5, 0) == dns.rcode.REFUSED) + + def test_rcode7(self): + def bad(): + dns.rcode.to_flags(4096) + self.failUnlessRaises(ValueError, bad) + + def test_flags1(self): + self.failUnless(dns.flags.from_text("RA RD AA QR") == \ + dns.flags.QR|dns.flags.AA|dns.flags.RD|dns.flags.RA) + + def test_flags2(self): + flags = dns.flags.QR|dns.flags.AA|dns.flags.RD|dns.flags.RA + self.failUnless(dns.flags.to_text(flags) == "QR AA RD RA") + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_generate.py b/tests/test_generate.py new file mode 100644 index 0000000..43c5f9a --- /dev/null +++ b/tests/test_generate.py @@ -0,0 +1,499 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import sys +sys.path.insert(0, '../') # Force the local project to be *the* dns + +import cStringIO +import filecmp +import os +import unittest + +import dns.exception +import dns.rdata +import dns.rdataclass +import dns.rdatatype +import dns.rrset +import dns.zone + +import pprint + +pp = pprint.PrettyPrinter(indent=2) + +import pdb +example_text = """$TTL 1h +$ORIGIN 0.0.192.IN-ADDR.ARPA. +$GENERATE 1-2 0 CNAME SERVER$.EXAMPLE. +""" + +example_text1 = """$TTL 1h +$ORIGIN 0.0.192.IN-ADDR.ARPA. +$GENERATE 1-10 fooo$ CNAME $.0 +""" + +example_text2 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 3-5 foo$ A 10.0.0.$ +""" + +example_text3 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 4-8/2 foo$ A 10.0.0.$ +""" + +example_text4 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-13 wp-db${-10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. +""" + +example_text5 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-13 wp-db${10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. +""" + +example_text6 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-13 wp-db${+10,2,d}.services.mozilla.com 0 CNAME SERVER.FOOBAR. +""" + +example_text7 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-13 sync${-10}.db IN A 10.10.16.0 +""" + +example_text8 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-12 wp-db${-10,2,d} IN A 10.10.16.0 +""" + +example_text9 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 11-12 wp-db${-10,2,d} IN A 10.10.16.0 +$GENERATE 11-13 sync${-10}.db IN A 10.10.16.0 +""" +example_text10 = """$TTL 1h +@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +$GENERATE 27-28 $.2 PTR zlb${-26}.oob +""" + + +class GenerateTestCase(unittest.TestCase): + + def testFromText(self): + def bad(): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + self.failUnlessRaises(dns.zone.NoSOA, bad) + + def testFromText1(self): + def bad(): + z = dns.zone.from_text(example_text1, 'example.', relativize=True) + self.failUnlessRaises(dns.zone.NoSOA, bad) + + def testIterateAllRdatas2(self): + z = dns.zone.from_text(example_text2, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + (dns.name.from_text('foo3', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.3')), + (dns.name.from_text('foo4', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.4')), + (dns.name.from_text('foo5', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.5'))] + + exl.sort() + self.failUnless(l == exl) + + def testIterateAllRdatas3(self): + z = dns.zone.from_text(example_text3, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + (dns.name.from_text('foo4', None), 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.4')), + (dns.name.from_text('foo6', None), 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.6')), + (dns.name.from_text('foo8', None), 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.8'))] + exl.sort() + self.failUnless(l == exl) + def testGenerate1(self): + z = dns.zone.from_text(example_text4, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + + (dns.name.from_text('wp-db01.services.mozilla.com', None), + 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db02.services.mozilla.com', None), + 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db03.services.mozilla.com', None), + 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.'))] + exl.sort() + self.failUnless(l == exl) + + def testGenerate2(self): + z = dns.zone.from_text(example_text5, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + + (dns.name.from_text('wp-db21.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db22.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db23.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.'))] + exl.sort() + self.failUnless(l == exl) + + def testGenerate3(self): + z = dns.zone.from_text(example_text6, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + (dns.name.from_text('wp-db21.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db22.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.')), + + (dns.name.from_text('wp-db23.services.mozilla.com', None), 0L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.CNAME, + 'SERVER.FOOBAR.'))] + exl.sort() + self.failUnless(l == exl) + + def testGenerate4(self): + z = dns.zone.from_text(example_text7, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + + (dns.name.from_text('sync1.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + + (dns.name.from_text('sync2.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + + (dns.name.from_text('sync3.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0'))] + exl.sort() + self.failUnless(l == exl) + + def testGenerate6(self): + z = dns.zone.from_text(example_text9, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + + (dns.name.from_text('wp-db01', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + (dns.name.from_text('wp-db02', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + + (dns.name.from_text('sync1.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + + (dns.name.from_text('sync2.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0')), + + (dns.name.from_text('sync3.db', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.10.16.0'))] + exl.sort() + self.failUnless(l == exl) + + def testGenerate7(self): + z = dns.zone.from_text(example_text10, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2')), + + (dns.name.from_text('27.2', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, + 'zlb1.oob')), + + (dns.name.from_text('28.2', None), 3600L, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.PTR, + 'zlb2.oob'))] + + exl.sort() + self.failUnless(l == exl) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_grange.py b/tests/test_grange.py new file mode 100644 index 0000000..1bb7dbb --- /dev/null +++ b/tests/test_grange.py @@ -0,0 +1,95 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import sys +sys.path.insert(0, '../') + +import cStringIO +import filecmp +import os +import unittest + +import dns +import dns.exception +import dns.grange + +import pdb + + + +class GRangeTestCase(unittest.TestCase): + + def testFromText1(self): + start, stop, step = dns.grange.from_text('1-1') + self.assertEqual(start, 1) + self.assertEqual(stop, 1) + self.assertEqual(step, 1) + + def testFromText2(self): + start, stop, step = dns.grange.from_text('1-4') + self.assertEqual(start, 1) + self.assertEqual(stop, 4) + self.assertEqual(step, 1) + + def testFromText3(self): + start, stop, step = dns.grange.from_text('4-255') + self.assertEqual(start, 4) + self.assertEqual(stop, 255) + self.assertEqual(step, 1) + + def testFromText4(self): + start, stop, step = dns.grange.from_text('1-1/1') + self.assertEqual(start, 1) + self.assertEqual(stop, 1) + self.assertEqual(step, 1) + + def testFromText5(self): + start, stop, step = dns.grange.from_text('1-4/2') + self.assertEqual(start, 1) + self.assertEqual(stop, 4) + self.assertEqual(step, 2) + + def testFromText6(self): + start, stop, step = dns.grange.from_text('4-255/77') + self.assertEqual(start, 4) + self.assertEqual(stop, 255) + self.assertEqual(step, 77) + + def testFailFromText1(self): + def bad(): + start = 2 + stop = 1 + step = 1 + dns.grange.from_text('%d-%d/%d' % (start, stop, step)) + self.assertRaises(AssertionError, bad) + + def testFailFromText2(self): + def bad(): + start = '-1' + stop = 3 + step = 1 + dns.grange.from_text('%s-%d/%d' % (start, stop, step)) + self.assertRaises(dns.exception.SyntaxError, bad) + + def testFailFromText2(self): + def bad(): + start = 1 + stop = 4 + step = '-2' + dns.grange.from_text('%d-%d/%s' % (start, stop, step)) + self.assertRaises(dns.exception.SyntaxError, bad) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_message.py b/tests/test_message.py new file mode 100644 index 0000000..931bb19 --- /dev/null +++ b/tests/test_message.py @@ -0,0 +1,179 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import cStringIO +import os +import unittest + +import dns.exception +import dns.message + +query_text = """id 1234 +opcode QUERY +rcode NOERROR +flags RD +edns 0 +eflags DO +payload 4096 +;QUESTION +wwww.dnspython.org. IN A +;ANSWER +;AUTHORITY +;ADDITIONAL""" + +goodhex = '04d201000001000000000001047777777709646e73707974686f6e' \ + '036f726700000100010000291000000080000000' + +goodwire = goodhex.decode('hex_codec') + +answer_text = """id 1234 +opcode QUERY +rcode NOERROR +flags QR AA RD +;QUESTION +dnspython.org. IN SOA +;ANSWER +dnspython.org. 3600 IN SOA woof.dnspython.org. hostmaster.dnspython.org. 2003052700 3600 1800 604800 3600 +;AUTHORITY +dnspython.org. 3600 IN NS ns1.staff.nominum.org. +dnspython.org. 3600 IN NS ns2.staff.nominum.org. +dnspython.org. 3600 IN NS woof.play-bow.org. +;ADDITIONAL +woof.play-bow.org. 3600 IN A 204.152.186.150 +""" + +goodhex2 = '04d2 8500 0001 0001 0003 0001' \ + '09646e73707974686f6e036f726700 0006 0001' \ + 'c00c 0006 0001 00000e10 0028 ' \ + '04776f6f66c00c 0a686f73746d6173746572c00c' \ + '7764289c 00000e10 00000708 00093a80 00000e10' \ + 'c00c 0002 0001 00000e10 0014' \ + '036e7331057374616666076e6f6d696e756dc016' \ + 'c00c 0002 0001 00000e10 0006 036e7332c063' \ + 'c00c 0002 0001 00000e10 0010 04776f6f6608706c61792d626f77c016' \ + 'c091 0001 0001 00000e10 0004 cc98ba96' + + +goodwire2 = goodhex2.replace(' ', '').decode('hex_codec') + +query_text_2 = """id 1234 +opcode QUERY +rcode 4095 +flags RD +edns 0 +eflags DO +payload 4096 +;QUESTION +wwww.dnspython.org. IN A +;ANSWER +;AUTHORITY +;ADDITIONAL""" + +goodhex3 = '04d2010f0001000000000001047777777709646e73707974686f6e' \ + '036f726700000100010000291000ff0080000000' + +goodwire3 = goodhex3.decode('hex_codec') + +class MessageTestCase(unittest.TestCase): + + def test_comparison_eq1(self): + q1 = dns.message.from_text(query_text) + q2 = dns.message.from_text(query_text) + self.failUnless(q1 == q2) + + def test_comparison_ne1(self): + q1 = dns.message.from_text(query_text) + q2 = dns.message.from_text(query_text) + q2.id = 10 + self.failUnless(q1 != q2) + + def test_comparison_ne2(self): + q1 = dns.message.from_text(query_text) + q2 = dns.message.from_text(query_text) + q2.question = [] + self.failUnless(q1 != q2) + + def test_comparison_ne3(self): + q1 = dns.message.from_text(query_text) + self.failUnless(q1 != 1) + + def test_EDNS_to_wire1(self): + q = dns.message.from_text(query_text) + w = q.to_wire() + self.failUnless(w == goodwire) + + def test_EDNS_from_wire1(self): + m = dns.message.from_wire(goodwire) + self.failUnless(str(m) == query_text) + + def test_EDNS_to_wire2(self): + q = dns.message.from_text(query_text_2) + w = q.to_wire() + self.failUnless(w == goodwire3) + + def test_EDNS_from_wire2(self): + m = dns.message.from_wire(goodwire3) + self.failUnless(str(m) == query_text_2) + + def test_TooBig(self): + def bad(): + q = dns.message.from_text(query_text) + for i in xrange(0, 25): + rrset = dns.rrset.from_text('foo%d.' % i, 3600, + dns.rdataclass.IN, + dns.rdatatype.A, + '10.0.0.%d' % i) + q.additional.append(rrset) + w = q.to_wire(max_size=512) + self.failUnlessRaises(dns.exception.TooBig, bad) + + def test_answer1(self): + a = dns.message.from_text(answer_text) + wire = a.to_wire(want_shuffle=False) + self.failUnless(wire == goodwire2) + + def test_TrailingJunk(self): + def bad(): + badwire = goodwire + '\x00' + m = dns.message.from_wire(badwire) + self.failUnlessRaises(dns.message.TrailingJunk, bad) + + def test_ShortHeader(self): + def bad(): + badwire = '\x00' * 11 + m = dns.message.from_wire(badwire) + self.failUnlessRaises(dns.message.ShortHeader, bad) + + def test_RespondingToResponse(self): + def bad(): + q = dns.message.make_query('foo', 'A') + r1 = dns.message.make_response(q) + r2 = dns.message.make_response(r1) + self.failUnlessRaises(dns.exception.FormError, bad) + + def test_ExtendedRcodeSetting(self): + m = dns.message.make_query('foo', 'A') + m.set_rcode(4095) + self.failUnless(m.rcode() == 4095) + m.set_rcode(2) + self.failUnless(m.rcode() == 2) + + def test_EDNSVersionCoherence(self): + m = dns.message.make_query('foo', 'A') + m.use_edns(1) + self.failUnless((m.ednsflags >> 16) & 0xFF == 1) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_name.py b/tests/test_name.py new file mode 100644 index 0000000..e30e43d --- /dev/null +++ b/tests/test_name.py @@ -0,0 +1,697 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import cStringIO +import socket + +import dns.name +import dns.reversename +import dns.e164 + +class NameTestCase(unittest.TestCase): + def setUp(self): + self.origin = dns.name.from_text('example.') + + def testFromTextRel1(self): + n = dns.name.from_text('foo.bar') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testFromTextRel2(self): + n = dns.name.from_text('foo.bar', origin=self.origin) + self.failUnless(n.labels == ('foo', 'bar', 'example', '')) + + def testFromTextRel3(self): + n = dns.name.from_text('foo.bar', origin=None) + self.failUnless(n.labels == ('foo', 'bar')) + + def testFromTextRel4(self): + n = dns.name.from_text('@', origin=None) + self.failUnless(n == dns.name.empty) + + def testFromTextRel5(self): + n = dns.name.from_text('@', origin=self.origin) + self.failUnless(n == self.origin) + + def testFromTextAbs1(self): + n = dns.name.from_text('foo.bar.') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testTortureFromText(self): + good = [ + r'.', + r'a', + r'a.', + r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + r'\000.\008.\010.\032.\046.\092.\099.\255', + r'\\', + r'\..\.', + r'\\.\\', + r'!"#%&/()=+-', + r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255', + ] + bad = [ + r'..', + r'.a', + r'\\..', + '\\', # yes, we don't want the 'r' prefix! + r'\0', + r'\00', + r'\00Z', + r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255', + ] + for t in good: + try: + n = dns.name.from_text(t) + except: + self.fail("good test '%s' raised an exception" % t) + for t in bad: + caught = False + try: + n = dns.name.from_text(t) + except: + caught = True + if not caught: + self.fail("bad test '%s' did not raise an exception" % t) + + def testImmutable1(self): + def bad(): + self.origin.labels = () + self.failUnlessRaises(TypeError, bad) + + def testImmutable2(self): + def bad(): + self.origin.labels[0] = 'foo' + self.failUnlessRaises(TypeError, bad) + + def testAbs1(self): + self.failUnless(dns.name.root.is_absolute()) + + def testAbs2(self): + self.failUnless(not dns.name.empty.is_absolute()) + + def testAbs3(self): + self.failUnless(self.origin.is_absolute()) + + def testAbs3(self): + n = dns.name.from_text('foo', origin=None) + self.failUnless(not n.is_absolute()) + + def testWild1(self): + n = dns.name.from_text('*.foo', origin=None) + self.failUnless(n.is_wild()) + + def testWild2(self): + n = dns.name.from_text('*a.foo', origin=None) + self.failUnless(not n.is_wild()) + + def testWild3(self): + n = dns.name.from_text('a.*.foo', origin=None) + self.failUnless(not n.is_wild()) + + def testWild4(self): + self.failUnless(not dns.name.root.is_wild()) + + def testWild5(self): + self.failUnless(not dns.name.empty.is_wild()) + + def testHash1(self): + n1 = dns.name.from_text('fOo.COM') + n2 = dns.name.from_text('foo.com') + self.failUnless(hash(n1) == hash(n2)) + + def testCompare1(self): + n1 = dns.name.from_text('a') + n2 = dns.name.from_text('b') + self.failUnless(n1 < n2) + self.failUnless(n2 > n1) + + def testCompare2(self): + n1 = dns.name.from_text('') + n2 = dns.name.from_text('b') + self.failUnless(n1 < n2) + self.failUnless(n2 > n1) + + def testCompare3(self): + self.failUnless(dns.name.empty < dns.name.root) + self.failUnless(dns.name.root > dns.name.empty) + + def testCompare4(self): + self.failUnless(dns.name.root != 1) + + def testCompare5(self): + self.failUnless(dns.name.root < 1 or dns.name.root > 1) + + def testSubdomain1(self): + self.failUnless(not dns.name.empty.is_subdomain(dns.name.root)) + + def testSubdomain2(self): + self.failUnless(not dns.name.root.is_subdomain(dns.name.empty)) + + def testSubdomain3(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(n.is_subdomain(self.origin)) + + def testSubdomain4(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(n.is_subdomain(dns.name.root)) + + def testSubdomain5(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(n.is_subdomain(n)) + + def testSuperdomain1(self): + self.failUnless(not dns.name.empty.is_superdomain(dns.name.root)) + + def testSuperdomain2(self): + self.failUnless(not dns.name.root.is_superdomain(dns.name.empty)) + + def testSuperdomain3(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(self.origin.is_superdomain(n)) + + def testSuperdomain4(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(dns.name.root.is_superdomain(n)) + + def testSuperdomain5(self): + n = dns.name.from_text('foo', origin=self.origin) + self.failUnless(n.is_superdomain(n)) + + def testCanonicalize1(self): + n = dns.name.from_text('FOO.bar', origin=self.origin) + c = n.canonicalize() + self.failUnless(c.labels == ('foo', 'bar', 'example', '')) + + def testToText1(self): + n = dns.name.from_text('FOO.bar', origin=self.origin) + t = n.to_text() + self.failUnless(t == 'FOO.bar.example.') + + def testToText2(self): + n = dns.name.from_text('FOO.bar', origin=self.origin) + t = n.to_text(True) + self.failUnless(t == 'FOO.bar.example') + + def testToText3(self): + n = dns.name.from_text('FOO.bar', origin=None) + t = n.to_text() + self.failUnless(t == 'FOO.bar') + + def testToText4(self): + t = dns.name.empty.to_text() + self.failUnless(t == '@') + + def testToText5(self): + t = dns.name.root.to_text() + self.failUnless(t == '.') + + def testToText6(self): + n = dns.name.from_text('FOO bar', origin=None) + t = n.to_text() + self.failUnless(t == r'FOO\032bar') + + def testToText7(self): + n = dns.name.from_text(r'FOO\.bar', origin=None) + t = n.to_text() + self.failUnless(t == r'FOO\.bar') + + def testToText8(self): + n = dns.name.from_text(r'\070OO\.bar', origin=None) + t = n.to_text() + self.failUnless(t == r'FOO\.bar') + + def testSlice1(self): + n = dns.name.from_text(r'a.b.c.', origin=None) + s = n[:] + self.failUnless(s == ('a', 'b', 'c', '')) + + def testSlice2(self): + n = dns.name.from_text(r'a.b.c.', origin=None) + s = n[:2] + self.failUnless(s == ('a', 'b')) + + def testSlice3(self): + n = dns.name.from_text(r'a.b.c.', origin=None) + s = n[2:] + self.failUnless(s == ('c', '')) + + def testEmptyLabel1(self): + def bad(): + n = dns.name.Name(['a', '', 'b']) + self.failUnlessRaises(dns.name.EmptyLabel, bad) + + def testEmptyLabel2(self): + def bad(): + n = dns.name.Name(['', 'b']) + self.failUnlessRaises(dns.name.EmptyLabel, bad) + + def testEmptyLabel3(self): + n = dns.name.Name(['b', '']) + self.failUnless(n) + + def testLongLabel(self): + n = dns.name.Name(['a' * 63]) + self.failUnless(n) + + def testLabelTooLong(self): + def bad(): + n = dns.name.Name(['a' * 64, 'b']) + self.failUnlessRaises(dns.name.LabelTooLong, bad) + + def testLongName(self): + n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 62]) + self.failUnless(n) + + def testNameTooLong(self): + def bad(): + n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 63]) + self.failUnlessRaises(dns.name.NameTooLong, bad) + + def testConcat1(self): + n1 = dns.name.Name(['a', 'b']) + n2 = dns.name.Name(['c', 'd']) + e = dns.name.Name(['a', 'b', 'c', 'd']) + r = n1 + n2 + self.failUnless(r == e) + + def testConcat2(self): + n1 = dns.name.Name(['a', 'b']) + n2 = dns.name.Name([]) + e = dns.name.Name(['a', 'b']) + r = n1 + n2 + self.failUnless(r == e) + + def testConcat2(self): + n1 = dns.name.Name([]) + n2 = dns.name.Name(['a', 'b']) + e = dns.name.Name(['a', 'b']) + r = n1 + n2 + self.failUnless(r == e) + + def testConcat3(self): + n1 = dns.name.Name(['a', 'b', '']) + n2 = dns.name.Name([]) + e = dns.name.Name(['a', 'b', '']) + r = n1 + n2 + self.failUnless(r == e) + + def testConcat4(self): + n1 = dns.name.Name(['a', 'b']) + n2 = dns.name.Name(['c', '']) + e = dns.name.Name(['a', 'b', 'c', '']) + r = n1 + n2 + self.failUnless(r == e) + + def testConcat5(self): + def bad(): + n1 = dns.name.Name(['a', 'b', '']) + n2 = dns.name.Name(['c']) + r = n1 + n2 + self.failUnlessRaises(dns.name.AbsoluteConcatenation, bad) + + def testBadEscape(self): + def bad(): + n = dns.name.from_text(r'a.b\0q1.c.') + print n + self.failUnlessRaises(dns.name.BadEscape, bad) + + def testDigestable1(self): + n = dns.name.from_text('FOO.bar') + d = n.to_digestable() + self.failUnless(d == '\x03foo\x03bar\x00') + + def testDigestable2(self): + n1 = dns.name.from_text('FOO.bar') + n2 = dns.name.from_text('foo.BAR.') + d1 = n1.to_digestable() + d2 = n2.to_digestable() + self.failUnless(d1 == d2) + + def testDigestable3(self): + d = dns.name.root.to_digestable() + self.failUnless(d == '\x00') + + def testDigestable4(self): + n = dns.name.from_text('FOO.bar', None) + d = n.to_digestable(dns.name.root) + self.failUnless(d == '\x03foo\x03bar\x00') + + def testBadDigestable(self): + def bad(): + n = dns.name.from_text('FOO.bar', None) + d = n.to_digestable() + self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad) + + def testToWire1(self): + n = dns.name.from_text('FOO.bar') + f = cStringIO.StringIO() + compress = {} + n.to_wire(f, compress) + self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00') + + def testToWire2(self): + n = dns.name.from_text('FOO.bar') + f = cStringIO.StringIO() + compress = {} + n.to_wire(f, compress) + n.to_wire(f, compress) + self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00') + + def testToWire3(self): + n1 = dns.name.from_text('FOO.bar') + n2 = dns.name.from_text('foo.bar') + f = cStringIO.StringIO() + compress = {} + n1.to_wire(f, compress) + n2.to_wire(f, compress) + self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00') + + def testToWire4(self): + n1 = dns.name.from_text('FOO.bar') + n2 = dns.name.from_text('a.foo.bar') + f = cStringIO.StringIO() + compress = {} + n1.to_wire(f, compress) + n2.to_wire(f, compress) + self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\x01\x61\xc0\x00') + + def testToWire5(self): + n1 = dns.name.from_text('FOO.bar') + n2 = dns.name.from_text('a.foo.bar') + f = cStringIO.StringIO() + compress = {} + n1.to_wire(f, compress) + n2.to_wire(f, None) + self.failUnless(f.getvalue() == \ + '\x03FOO\x03bar\x00\x01\x61\x03foo\x03bar\x00') + + def testToWire6(self): + n = dns.name.from_text('FOO.bar') + v = n.to_wire() + self.failUnless(v == '\x03FOO\x03bar\x00') + + def testBadToWire(self): + def bad(): + n = dns.name.from_text('FOO.bar', None) + f = cStringIO.StringIO() + compress = {} + n.to_wire(f, compress) + self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad) + + def testSplit1(self): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(2) + ep = dns.name.from_text('foo', None) + es = dns.name.from_text('bar.', None) + self.failUnless(prefix == ep and suffix == es) + + def testSplit2(self): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(1) + ep = dns.name.from_text('foo.bar', None) + es = dns.name.from_text('.', None) + self.failUnless(prefix == ep and suffix == es) + + def testSplit3(self): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(0) + ep = dns.name.from_text('foo.bar.', None) + es = dns.name.from_text('', None) + self.failUnless(prefix == ep and suffix == es) + + def testSplit4(self): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(3) + ep = dns.name.from_text('', None) + es = dns.name.from_text('foo.bar.', None) + self.failUnless(prefix == ep and suffix == es) + + def testBadSplit1(self): + def bad(): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(-1) + self.failUnlessRaises(ValueError, bad) + + def testBadSplit2(self): + def bad(): + n = dns.name.from_text('foo.bar.') + (prefix, suffix) = n.split(4) + self.failUnlessRaises(ValueError, bad) + + def testRelativize1(self): + n = dns.name.from_text('a.foo.bar.', None) + o = dns.name.from_text('bar.', None) + e = dns.name.from_text('a.foo', None) + self.failUnless(n.relativize(o) == e) + + def testRelativize2(self): + n = dns.name.from_text('a.foo.bar.', None) + o = n + e = dns.name.empty + self.failUnless(n.relativize(o) == e) + + def testRelativize3(self): + n = dns.name.from_text('a.foo.bar.', None) + o = dns.name.from_text('blaz.', None) + e = n + self.failUnless(n.relativize(o) == e) + + def testRelativize4(self): + n = dns.name.from_text('a.foo', None) + o = dns.name.root + e = n + self.failUnless(n.relativize(o) == e) + + def testDerelativize1(self): + n = dns.name.from_text('a.foo', None) + o = dns.name.from_text('bar.', None) + e = dns.name.from_text('a.foo.bar.', None) + self.failUnless(n.derelativize(o) == e) + + def testDerelativize2(self): + n = dns.name.empty + o = dns.name.from_text('a.foo.bar.', None) + e = o + self.failUnless(n.derelativize(o) == e) + + def testDerelativize3(self): + n = dns.name.from_text('a.foo.bar.', None) + o = dns.name.from_text('blaz.', None) + e = n + self.failUnless(n.derelativize(o) == e) + + def testChooseRelativity1(self): + n = dns.name.from_text('a.foo.bar.', None) + o = dns.name.from_text('bar.', None) + e = dns.name.from_text('a.foo', None) + self.failUnless(n.choose_relativity(o, True) == e) + + def testChooseRelativity2(self): + n = dns.name.from_text('a.foo.bar.', None) + o = dns.name.from_text('bar.', None) + e = n + self.failUnless(n.choose_relativity(o, False) == e) + + def testChooseRelativity3(self): + n = dns.name.from_text('a.foo', None) + o = dns.name.from_text('bar.', None) + e = dns.name.from_text('a.foo.bar.', None) + self.failUnless(n.choose_relativity(o, False) == e) + + def testChooseRelativity4(self): + n = dns.name.from_text('a.foo', None) + o = None + e = n + self.failUnless(n.choose_relativity(o, True) == e) + + def testChooseRelativity5(self): + n = dns.name.from_text('a.foo', None) + o = None + e = n + self.failUnless(n.choose_relativity(o, False) == e) + + def testChooseRelativity6(self): + n = dns.name.from_text('a.foo.', None) + o = None + e = n + self.failUnless(n.choose_relativity(o, True) == e) + + def testChooseRelativity7(self): + n = dns.name.from_text('a.foo.', None) + o = None + e = n + self.failUnless(n.choose_relativity(o, False) == e) + + def testFromWire1(self): + w = '\x03foo\x00\xc0\x00' + (n1, cused1) = dns.name.from_wire(w, 0) + (n2, cused2) = dns.name.from_wire(w, cused1) + en1 = dns.name.from_text('foo.') + en2 = en1 + ecused1 = 5 + ecused2 = 2 + self.failUnless(n1 == en1 and cused1 == ecused1 and \ + n2 == en2 and cused2 == ecused2) + + def testFromWire1(self): + w = '\x03foo\x00\x01a\xc0\x00\x01b\xc0\x05' + current = 0 + (n1, cused1) = dns.name.from_wire(w, current) + current += cused1 + (n2, cused2) = dns.name.from_wire(w, current) + current += cused2 + (n3, cused3) = dns.name.from_wire(w, current) + en1 = dns.name.from_text('foo.') + en2 = dns.name.from_text('a.foo.') + en3 = dns.name.from_text('b.a.foo.') + ecused1 = 5 + ecused2 = 4 + ecused3 = 4 + self.failUnless(n1 == en1 and cused1 == ecused1 and \ + n2 == en2 and cused2 == ecused2 and \ + n3 == en3 and cused3 == ecused3) + + def testBadFromWire1(self): + def bad(): + w = '\x03foo\xc0\x04' + (n, cused) = dns.name.from_wire(w, 0) + self.failUnlessRaises(dns.name.BadPointer, bad) + + def testBadFromWire2(self): + def bad(): + w = '\x03foo\xc0\x05' + (n, cused) = dns.name.from_wire(w, 0) + self.failUnlessRaises(dns.name.BadPointer, bad) + + def testBadFromWire3(self): + def bad(): + w = '\xbffoo' + (n, cused) = dns.name.from_wire(w, 0) + self.failUnlessRaises(dns.name.BadLabelType, bad) + + def testBadFromWire4(self): + def bad(): + w = '\x41foo' + (n, cused) = dns.name.from_wire(w, 0) + self.failUnlessRaises(dns.name.BadLabelType, bad) + + def testParent1(self): + n = dns.name.from_text('foo.bar.') + self.failUnless(n.parent() == dns.name.from_text('bar.')) + self.failUnless(n.parent().parent() == dns.name.root) + + def testParent2(self): + n = dns.name.from_text('foo.bar', None) + self.failUnless(n.parent() == dns.name.from_text('bar', None)) + self.failUnless(n.parent().parent() == dns.name.empty) + + def testParent3(self): + def bad(): + n = dns.name.root + n.parent() + self.failUnlessRaises(dns.name.NoParent, bad) + + def testParent4(self): + def bad(): + n = dns.name.empty + n.parent() + self.failUnlessRaises(dns.name.NoParent, bad) + + def testFromUnicode1(self): + n = dns.name.from_text(u'foo.bar') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testFromUnicode2(self): + n = dns.name.from_text(u'foo\u1234bar.bar') + self.failUnless(n.labels == ('xn--foobar-r5z', 'bar', '')) + + def testFromUnicodeAlternateDot1(self): + n = dns.name.from_text(u'foo\u3002bar') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testFromUnicodeAlternateDot2(self): + n = dns.name.from_text(u'foo\uff0ebar') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testFromUnicodeAlternateDot3(self): + n = dns.name.from_text(u'foo\uff61bar') + self.failUnless(n.labels == ('foo', 'bar', '')) + + def testToUnicode1(self): + n = dns.name.from_text(u'foo.bar') + s = n.to_unicode() + self.failUnless(s == u'foo.bar.') + + def testToUnicode2(self): + n = dns.name.from_text(u'foo\u1234bar.bar') + s = n.to_unicode() + self.failUnless(s == u'foo\u1234bar.bar.') + + def testToUnicode3(self): + n = dns.name.from_text('foo.bar') + s = n.to_unicode() + self.failUnless(s == u'foo.bar.') + + def testReverseIPv4(self): + e = dns.name.from_text('1.0.0.127.in-addr.arpa.') + n = dns.reversename.from_address('127.0.0.1') + self.failUnless(e == n) + + def testReverseIPv6(self): + e = dns.name.from_text('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.') + n = dns.reversename.from_address('::1') + self.failUnless(e == n) + + def testBadReverseIPv4(self): + def bad(): + n = dns.reversename.from_address('127.0.foo.1') + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testBadReverseIPv6(self): + def bad(): + n = dns.reversename.from_address('::1::1') + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testForwardIPv4(self): + n = dns.name.from_text('1.0.0.127.in-addr.arpa.') + e = '127.0.0.1' + text = dns.reversename.to_address(n) + self.failUnless(text == e) + + def testForwardIPv6(self): + n = dns.name.from_text('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.') + e = '::1' + text = dns.reversename.to_address(n) + self.failUnless(text == e) + + def testE164ToEnum(self): + text = '+1 650 555 1212' + e = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.') + n = dns.e164.from_e164(text) + self.failUnless(n == e) + + def testEnumToE164(self): + n = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.') + e = '+16505551212' + text = dns.e164.to_e164(n) + self.failUnless(text == e) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_namedict.py b/tests/test_namedict.py new file mode 100644 index 0000000..e256bfe --- /dev/null +++ b/tests/test_namedict.py @@ -0,0 +1,102 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.name +import dns.namedict + +class NameTestCase(unittest.TestCase): + + def setUp(self): + self.ndict = dns.namedict.NameDict() + n1 = dns.name.from_text('foo.bar.') + n2 = dns.name.from_text('bar.') + self.ndict[n1] = 1 + self.ndict[n2] = 2 + self.rndict = dns.namedict.NameDict() + n1 = dns.name.from_text('foo.bar', None) + n2 = dns.name.from_text('bar', None) + self.rndict[n1] = 1 + self.rndict[n2] = 2 + + def testDepth(self): + self.failUnless(self.ndict.max_depth == 3) + + def testLookup1(self): + k = dns.name.from_text('foo.bar.') + self.failUnless(self.ndict[k] == 1) + + def testLookup2(self): + k = dns.name.from_text('foo.bar.') + self.failUnless(self.ndict.get_deepest_match(k)[1] == 1) + + def testLookup3(self): + k = dns.name.from_text('a.b.c.foo.bar.') + self.failUnless(self.ndict.get_deepest_match(k)[1] == 1) + + def testLookup4(self): + k = dns.name.from_text('a.b.c.bar.') + self.failUnless(self.ndict.get_deepest_match(k)[1] == 2) + + def testLookup5(self): + def bad(): + n = dns.name.from_text('a.b.c.') + (k, v) = self.ndict.get_deepest_match(n) + self.failUnlessRaises(KeyError, bad) + + def testLookup6(self): + def bad(): + (k, v) = self.ndict.get_deepest_match(dns.name.empty) + self.failUnlessRaises(KeyError, bad) + + def testLookup7(self): + self.ndict[dns.name.empty] = 100 + n = dns.name.from_text('a.b.c.') + (k, v) = self.ndict.get_deepest_match(n) + self.failUnless(v == 100) + + def testLookup8(self): + def bad(): + self.ndict['foo'] = 100 + self.failUnlessRaises(ValueError, bad) + + def testRelDepth(self): + self.failUnless(self.rndict.max_depth == 2) + + def testRelLookup1(self): + k = dns.name.from_text('foo.bar', None) + self.failUnless(self.rndict[k] == 1) + + def testRelLookup2(self): + k = dns.name.from_text('foo.bar', None) + self.failUnless(self.rndict.get_deepest_match(k)[1] == 1) + + def testRelLookup3(self): + k = dns.name.from_text('a.b.c.foo.bar', None) + self.failUnless(self.rndict.get_deepest_match(k)[1] == 1) + + def testRelLookup4(self): + k = dns.name.from_text('a.b.c.bar', None) + self.failUnless(self.rndict.get_deepest_match(k)[1] == 2) + + def testRelLookup7(self): + self.rndict[dns.name.empty] = 100 + n = dns.name.from_text('a.b.c', None) + (k, v) = self.rndict.get_deepest_match(n) + self.failUnless(v == 100) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_ntoaaton.py b/tests/test_ntoaaton.py new file mode 100644 index 0000000..e93de2d --- /dev/null +++ b/tests/test_ntoaaton.py @@ -0,0 +1,203 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.exception +import dns.ipv4 +import dns.ipv6 + +# for convenience +aton4 = dns.ipv4.inet_aton +ntoa4 = dns.ipv4.inet_ntoa +aton6 = dns.ipv6.inet_aton +ntoa6 = dns.ipv6.inet_ntoa + +v4_bad_addrs = ['256.1.1.1', '1.1.1', '1.1.1.1.1', '01.1.1.1', + '+1.1.1.1', '1.1.1.1+', '1..2.3.4', '.1.2.3.4', + '1.2.3.4.'] + +class NtoAAtoNTestCase(unittest.TestCase): + + def test_aton1(self): + a = aton6('::') + self.failUnless(a == '\x00' * 16) + + def test_aton2(self): + a = aton6('::1') + self.failUnless(a == '\x00' * 15 + '\x01') + + def test_aton3(self): + a = aton6('::10.0.0.1') + self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01') + + def test_aton4(self): + a = aton6('abcd::dcba') + self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba') + + def test_aton5(self): + a = aton6('1:2:3:4:5:6:7:8') + self.failUnless(a == \ + '00010002000300040005000600070008'.decode('hex_codec')) + + def test_bad_aton1(self): + def bad(): + a = aton6('abcd:dcba') + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def test_bad_aton2(self): + def bad(): + a = aton6('abcd::dcba::1') + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def test_bad_aton3(self): + def bad(): + a = aton6('1:2:3:4:5:6:7:8:9') + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def test_aton1(self): + a = aton6('::') + self.failUnless(a == '\x00' * 16) + + def test_aton2(self): + a = aton6('::1') + self.failUnless(a == '\x00' * 15 + '\x01') + + def test_aton3(self): + a = aton6('::10.0.0.1') + self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01') + + def test_aton4(self): + a = aton6('abcd::dcba') + self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba') + + def test_ntoa1(self): + b = '00010002000300040005000600070008'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '1:2:3:4:5:6:7:8') + + def test_ntoa2(self): + b = '\x00' * 16 + t = ntoa6(b) + self.failUnless(t == '::') + + def test_ntoa3(self): + b = '\x00' * 15 + '\x01' + t = ntoa6(b) + self.failUnless(t == '::1') + + def test_ntoa4(self): + b = '\x80' + '\x00' * 15 + t = ntoa6(b) + self.failUnless(t == '8000::') + + def test_ntoa5(self): + b = '\x01\xcd' + '\x00' * 12 + '\x03\xef' + t = ntoa6(b) + self.failUnless(t == '1cd::3ef') + + def test_ntoa6(self): + b = 'ffff00000000ffff000000000000ffff'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == 'ffff:0:0:ffff::ffff') + + def test_ntoa7(self): + b = '00000000ffff000000000000ffffffff'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '0:0:ffff::ffff:ffff') + + def test_ntoa8(self): + b = 'ffff0000ffff00000000ffff00000000'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == 'ffff:0:ffff::ffff:0:0') + + def test_ntoa9(self): + b = '0000000000000000000000000a000001'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::10.0.0.1') + + def test_ntoa10(self): + b = '0000000000000000000000010a000001'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::1:a00:1') + + def test_ntoa11(self): + b = '00000000000000000000ffff0a000001'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::ffff:10.0.0.1') + + def test_ntoa12(self): + b = '000000000000000000000000ffffffff'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::255.255.255.255') + + def test_ntoa13(self): + b = '00000000000000000000ffffffffffff'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::ffff:255.255.255.255') + + def test_ntoa14(self): + b = '0000000000000000000000000001ffff'.decode('hex_codec') + t = ntoa6(b) + self.failUnless(t == '::0.1.255.255') + + def test_bad_ntoa1(self): + def bad(): + a = ntoa6('') + self.failUnlessRaises(ValueError, bad) + + def test_bad_ntoa2(self): + def bad(): + a = ntoa6('\x00' * 17) + self.failUnlessRaises(ValueError, bad) + + def test_good_v4_aton(self): + pairs = [('1.2.3.4', '\x01\x02\x03\x04'), + ('255.255.255.255', '\xff\xff\xff\xff'), + ('0.0.0.0', '\x00\x00\x00\x00')] + for (t, b) in pairs: + b1 = aton4(t) + t1 = ntoa4(b1) + self.failUnless(b1 == b) + self.failUnless(t1 == t) + + def test_bad_v4_aton(self): + def make_bad(a): + def bad(): + return aton4(a) + return bad + for addr in v4_bad_addrs: + self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr)) + + def test_bad_v6_aton(self): + addrs = ['+::0', '0::0::', '::0::', '1:2:3:4:5:6:7:8:9', + ':::::::'] + embedded = ['::' + x for x in v4_bad_addrs] + addrs.extend(embedded) + def make_bad(a): + def bad(): + x = aton6(a) + return bad + for addr in addrs: + self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr)) + + def test_rfc5952_section_4_2_2(self): + addr = '2001:db8:0:1:1:1:1:1' + b1 = aton6(addr) + t1 = ntoa6(b1) + self.failUnless(t1 == addr) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_rdtypeandclass.py b/tests/test_rdtypeandclass.py new file mode 100644 index 0000000..f3c0628 --- /dev/null +++ b/tests/test_rdtypeandclass.py @@ -0,0 +1,123 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.rdataclass +import dns.rdatatype + +class RdTypeAndClassTestCase(unittest.TestCase): + + # Classes + + def test_class_meta1(self): + self.failUnless(dns.rdataclass.is_metaclass(dns.rdataclass.ANY)) + + def test_class_meta2(self): + self.failUnless(not dns.rdataclass.is_metaclass(dns.rdataclass.IN)) + + def test_class_bytext1(self): + self.failUnless(dns.rdataclass.from_text('IN') == dns.rdataclass.IN) + + def test_class_bytext2(self): + self.failUnless(dns.rdataclass.from_text('CLASS1') == + dns.rdataclass.IN) + + def test_class_bytext_bounds1(self): + self.failUnless(dns.rdataclass.from_text('CLASS0') == 0) + self.failUnless(dns.rdataclass.from_text('CLASS65535') == 65535) + + def test_class_bytext_bounds2(self): + def bad(): + junk = dns.rdataclass.from_text('CLASS65536') + self.failUnlessRaises(ValueError, bad) + + def test_class_bytext_unknown(self): + def bad(): + junk = dns.rdataclass.from_text('XXX') + self.failUnlessRaises(dns.rdataclass.UnknownRdataclass, bad) + + def test_class_totext1(self): + self.failUnless(dns.rdataclass.to_text(dns.rdataclass.IN) == 'IN') + + def test_class_totext1(self): + self.failUnless(dns.rdataclass.to_text(999) == 'CLASS999') + + def test_class_totext_bounds1(self): + def bad(): + junk = dns.rdataclass.to_text(-1) + self.failUnlessRaises(ValueError, bad) + + def test_class_totext_bounds2(self): + def bad(): + junk = dns.rdataclass.to_text(65536) + self.failUnlessRaises(ValueError, bad) + + # Types + + def test_type_meta1(self): + self.failUnless(dns.rdatatype.is_metatype(dns.rdatatype.ANY)) + + def test_type_meta2(self): + self.failUnless(dns.rdatatype.is_metatype(dns.rdatatype.OPT)) + + def test_type_meta3(self): + self.failUnless(not dns.rdatatype.is_metatype(dns.rdatatype.A)) + + def test_type_singleton1(self): + self.failUnless(dns.rdatatype.is_singleton(dns.rdatatype.SOA)) + + def test_type_singleton2(self): + self.failUnless(not dns.rdatatype.is_singleton(dns.rdatatype.A)) + + def test_type_bytext1(self): + self.failUnless(dns.rdatatype.from_text('A') == dns.rdatatype.A) + + def test_type_bytext2(self): + self.failUnless(dns.rdatatype.from_text('TYPE1') == + dns.rdatatype.A) + + def test_type_bytext_bounds1(self): + self.failUnless(dns.rdatatype.from_text('TYPE0') == 0) + self.failUnless(dns.rdatatype.from_text('TYPE65535') == 65535) + + def test_type_bytext_bounds2(self): + def bad(): + junk = dns.rdatatype.from_text('TYPE65536') + self.failUnlessRaises(ValueError, bad) + + def test_type_bytext_unknown(self): + def bad(): + junk = dns.rdatatype.from_text('XXX') + self.failUnlessRaises(dns.rdatatype.UnknownRdatatype, bad) + + def test_type_totext1(self): + self.failUnless(dns.rdatatype.to_text(dns.rdatatype.A) == 'A') + + def test_type_totext1(self): + self.failUnless(dns.rdatatype.to_text(999) == 'TYPE999') + + def test_type_totext_bounds1(self): + def bad(): + junk = dns.rdatatype.to_text(-1) + self.failUnlessRaises(ValueError, bad) + + def test_type_totext_bounds2(self): + def bad(): + junk = dns.rdatatype.to_text(65536) + self.failUnlessRaises(ValueError, bad) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_rdtypeanyloc.py b/tests/test_rdtypeanyloc.py new file mode 100644 index 0000000..8d9838c --- /dev/null +++ b/tests/test_rdtypeanyloc.py @@ -0,0 +1,68 @@ +# Copyright (C) 2014 Red Hat, Inc. +# Author: Petr Spacek +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED 'AS IS' AND RED HAT DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.rrset +import dns.rdtypes.ANY.LOC + +class RdtypeAnyLocTestCase(unittest.TestCase): + + def testEqual1(self): + '''Test default values for size, horizontal and vertical precision.''' + r1 = dns.rrset.from_text('foo', 300, 'IN', 'LOC', + '49 11 42.400 N 16 36 29.600 E 227.64m') + r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', + '49 11 42.400 N 16 36 29.600 E 227.64m ' + '1.00m 10000.00m 10.00m') + self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + + def testEqual2(self): + '''Test default values for size, horizontal and vertical precision.''' + r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), + (16, 36, 29, 600), 22764.0) # centimeters + r2 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), + (16, 36, 29, 600), 22764.0, # centimeters + 100.0, 1000000.00, 1000.0) # centimeters + self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + + def testEqual3(self): + '''Test size, horizontal and vertical precision parsers: 100 cm == 1 m. + + Parsers in from_text() and __init__() have to produce equal results.''' + r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), + (16, 36, 29, 600), 22764.0, + 200.0, 1000.00, 200.0) # centimeters + r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', + '49 11 42.400 N 16 36 29.600 E 227.64m ' + '2.00m 10.00m 2.00m')[0] + self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + + def testEqual4(self): + '''Test size, horizontal and vertical precision parsers without unit. + + Parsers in from_text() and __init__() have produce equal result + for values with and without trailing "m".''' + r1 = dns.rdtypes.ANY.LOC.LOC(1, 29, (49, 11, 42, 400), + (16, 36, 29, 600), 22764.0, + 200.0, 1000.00, 200.0) # centimeters + r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', + '49 11 42.400 N 16 36 29.600 E 227.64 ' + '2 10 2')[0] # meters without explicit unit + self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_resolver.py b/tests/test_resolver.py new file mode 100644 index 0000000..6be955b --- /dev/null +++ b/tests/test_resolver.py @@ -0,0 +1,184 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import cStringIO +import select +import sys +import time +import unittest + +import dns.name +import dns.message +import dns.name +import dns.rdataclass +import dns.rdatatype +import dns.resolver + +resolv_conf = """ + /t/t +# comment 1 +; comment 2 +domain foo +nameserver 10.0.0.1 +nameserver 10.0.0.2 +""" + +message_text = """id 1234 +opcode QUERY +rcode NOERROR +flags QR AA RD +;QUESTION +example. IN A +;ANSWER +example. 1 IN A 10.0.0.1 +;AUTHORITY +;ADDITIONAL +""" + +class FakeAnswer(object): + def __init__(self, expiration): + self.expiration = expiration + +class BaseResolverTests(object): + + if sys.platform != 'win32': + def testRead(self): + f = cStringIO.StringIO(resolv_conf) + r = dns.resolver.Resolver(f) + self.failUnless(r.nameservers == ['10.0.0.1', '10.0.0.2'] and + r.domain == dns.name.from_text('foo')) + + def testCacheExpiration(self): + message = dns.message.from_text(message_text) + name = dns.name.from_text('example.') + answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN, + message) + cache = dns.resolver.Cache() + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + time.sleep(2) + self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) + is None) + + def testCacheCleaning(self): + message = dns.message.from_text(message_text) + name = dns.name.from_text('example.') + answer = dns.resolver.Answer(name, dns.rdatatype.A, dns.rdataclass.IN, + message) + cache = dns.resolver.Cache(cleaning_interval=1.0) + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + time.sleep(2) + self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) + is None) + + def testZoneForName1(self): + name = dns.name.from_text('www.dnspython.org.') + ezname = dns.name.from_text('dnspython.org.') + zname = dns.resolver.zone_for_name(name) + self.failUnless(zname == ezname) + + def testZoneForName2(self): + name = dns.name.from_text('a.b.www.dnspython.org.') + ezname = dns.name.from_text('dnspython.org.') + zname = dns.resolver.zone_for_name(name) + self.failUnless(zname == ezname) + + def testZoneForName3(self): + name = dns.name.from_text('dnspython.org.') + ezname = dns.name.from_text('dnspython.org.') + zname = dns.resolver.zone_for_name(name) + self.failUnless(zname == ezname) + + def testZoneForName4(self): + def bad(): + name = dns.name.from_text('dnspython.org', None) + zname = dns.resolver.zone_for_name(name) + self.failUnlessRaises(dns.resolver.NotAbsolute, bad) + + def testLRUReplace(self): + cache = dns.resolver.LRUCache(4) + for i in xrange(0, 5): + name = dns.name.from_text('example%d.' % i) + answer = FakeAnswer(time.time() + 1) + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + for i in xrange(0, 5): + name = dns.name.from_text('example%d.' % i) + if i == 0: + self.failUnless(cache.get((name, dns.rdatatype.A, + dns.rdataclass.IN)) + is None) + else: + self.failUnless(not cache.get((name, dns.rdatatype.A, + dns.rdataclass.IN)) + is None) + + def testLRUDoesLRU(self): + cache = dns.resolver.LRUCache(4) + for i in xrange(0, 4): + name = dns.name.from_text('example%d.' % i) + answer = FakeAnswer(time.time() + 1) + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + name = dns.name.from_text('example0.') + cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) + # The LRU is now example1. + name = dns.name.from_text('example4.') + answer = FakeAnswer(time.time() + 1) + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + for i in xrange(0, 5): + name = dns.name.from_text('example%d.' % i) + if i == 1: + self.failUnless(cache.get((name, dns.rdatatype.A, + dns.rdataclass.IN)) + is None) + else: + self.failUnless(not cache.get((name, dns.rdatatype.A, + dns.rdataclass.IN)) + is None) + + def testLRUExpiration(self): + cache = dns.resolver.LRUCache(4) + for i in xrange(0, 4): + name = dns.name.from_text('example%d.' % i) + answer = FakeAnswer(time.time() + 1) + cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) + time.sleep(2) + for i in xrange(0, 4): + name = dns.name.from_text('example%d.' % i) + self.failUnless(cache.get((name, dns.rdatatype.A, + dns.rdataclass.IN)) + is None) + +class PollingMonkeyPatchMixin(object): + def setUp(self): + self.__native_polling_backend = dns.query._polling_backend + dns.query._set_polling_backend(self.polling_backend()) + + unittest.TestCase.setUp(self) + + def tearDown(self): + dns.query._set_polling_backend(self.__native_polling_backend) + + unittest.TestCase.tearDown(self) + +class SelectResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase): + def polling_backend(self): + return dns.query._select_for + +if hasattr(select, 'poll'): + class PollResolverTestCase(PollingMonkeyPatchMixin, BaseResolverTests, unittest.TestCase): + def polling_backend(self): + return dns.query._poll_for + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_rrset.py b/tests/test_rrset.py new file mode 100644 index 0000000..be1324b --- /dev/null +++ b/tests/test_rrset.py @@ -0,0 +1,54 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.rrset + +class RRsetTestCase(unittest.TestCase): + + def testEqual1(self): + r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2') + r2 = dns.rrset.from_text('FOO', 300, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 == r2) + + def testEqual2(self): + r1 = dns.rrset.from_text('foo', 300, 'in', 'a', '10.0.0.1', '10.0.0.2') + r2 = dns.rrset.from_text('FOO', 600, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 == r2) + + def testNotEqual1(self): + r1 = dns.rrset.from_text('fooa', 30, 'in', 'a', '10.0.0.1', '10.0.0.2') + r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 != r2) + + def testNotEqual2(self): + r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.3') + r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 != r2) + + def testNotEqual3(self): + r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1', '10.0.0.2', + '10.0.0.3') + r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 != r2) + + def testNotEqual4(self): + r1 = dns.rrset.from_text('foo', 30, 'in', 'a', '10.0.0.1') + r2 = dns.rrset.from_text('FOO', 30, 'in', 'a', '10.0.0.2', '10.0.0.1') + self.failUnless(r1 != r2) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_set.py b/tests/test_set.py new file mode 100644 index 0000000..583d20c --- /dev/null +++ b/tests/test_set.py @@ -0,0 +1,208 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.set + +# for convenience +S = dns.set.Set + +class SimpleSetTestCase(unittest.TestCase): + + def testLen1(self): + s1 = S() + self.failUnless(len(s1) == 0) + + def testLen2(self): + s1 = S([1, 2, 3]) + self.failUnless(len(s1) == 3) + + def testLen3(self): + s1 = S([1, 2, 3, 3, 3]) + self.failUnless(len(s1) == 3) + + def testUnion1(self): + s1 = S([1, 2, 3]) + s2 = S([1, 2, 3]) + e = S([1, 2, 3]) + self.failUnless(s1 | s2 == e) + + def testUnion2(self): + s1 = S([1, 2, 3]) + s2 = S([]) + e = S([1, 2, 3]) + self.failUnless(s1 | s2 == e) + + def testUnion3(self): + s1 = S([1, 2, 3]) + s2 = S([3, 4]) + e = S([1, 2, 3, 4]) + self.failUnless(s1 | s2 == e) + + def testIntersection1(self): + s1 = S([1, 2, 3]) + s2 = S([1, 2, 3]) + e = S([1, 2, 3]) + self.failUnless(s1 & s2 == e) + + def testIntersection2(self): + s1 = S([0, 1, 2, 3]) + s2 = S([1, 2, 3, 4]) + e = S([1, 2, 3]) + self.failUnless(s1 & s2 == e) + + def testIntersection3(self): + s1 = S([1, 2, 3]) + s2 = S([]) + e = S([]) + self.failUnless(s1 & s2 == e) + + def testIntersection4(self): + s1 = S([1, 2, 3]) + s2 = S([5, 4]) + e = S([]) + self.failUnless(s1 & s2 == e) + + def testDifference1(self): + s1 = S([1, 2, 3]) + s2 = S([5, 4]) + e = S([1, 2, 3]) + self.failUnless(s1 - s2 == e) + + def testDifference2(self): + s1 = S([1, 2, 3]) + s2 = S([]) + e = S([1, 2, 3]) + self.failUnless(s1 - s2 == e) + + def testDifference3(self): + s1 = S([1, 2, 3]) + s2 = S([3, 2]) + e = S([1]) + self.failUnless(s1 - s2 == e) + + def testDifference4(self): + s1 = S([1, 2, 3]) + s2 = S([3, 2, 1]) + e = S([]) + self.failUnless(s1 - s2 == e) + + def testSubset1(self): + s1 = S([1, 2, 3]) + s2 = S([3, 2, 1]) + self.failUnless(s1.issubset(s2)) + + def testSubset2(self): + s1 = S([1, 2, 3]) + self.failUnless(s1.issubset(s1)) + + def testSubset3(self): + s1 = S([]) + s2 = S([1, 2, 3]) + self.failUnless(s1.issubset(s2)) + + def testSubset4(self): + s1 = S([1]) + s2 = S([1, 2, 3]) + self.failUnless(s1.issubset(s2)) + + def testSubset5(self): + s1 = S([]) + s2 = S([]) + self.failUnless(s1.issubset(s2)) + + def testSubset6(self): + s1 = S([1, 4]) + s2 = S([1, 2, 3]) + self.failUnless(not s1.issubset(s2)) + + def testSuperset1(self): + s1 = S([1, 2, 3]) + s2 = S([3, 2, 1]) + self.failUnless(s1.issuperset(s2)) + + def testSuperset2(self): + s1 = S([1, 2, 3]) + self.failUnless(s1.issuperset(s1)) + + def testSuperset3(self): + s1 = S([1, 2, 3]) + s2 = S([]) + self.failUnless(s1.issuperset(s2)) + + def testSuperset4(self): + s1 = S([1, 2, 3]) + s2 = S([1]) + self.failUnless(s1.issuperset(s2)) + + def testSuperset5(self): + s1 = S([]) + s2 = S([]) + self.failUnless(s1.issuperset(s2)) + + def testSuperset6(self): + s1 = S([1, 2, 3]) + s2 = S([1, 4]) + self.failUnless(not s1.issuperset(s2)) + + def testUpdate1(self): + s1 = S([1, 2, 3]) + u = (4, 5, 6) + e = S([1, 2, 3, 4, 5, 6]) + s1.update(u) + self.failUnless(s1 == e) + + def testUpdate2(self): + s1 = S([1, 2, 3]) + u = [] + e = S([1, 2, 3]) + s1.update(u) + self.failUnless(s1 == e) + + def testGetitem(self): + s1 = S([1, 2, 3]) + i0 = s1[0] + i1 = s1[1] + i2 = s1[2] + s2 = S([i0, i1, i2]) + self.failUnless(s1 == s2) + + def testGetslice(self): + s1 = S([1, 2, 3]) + slice = s1[0:2] + self.failUnless(len(slice) == 2) + item = s1[2] + slice.append(item) + s2 = S(slice) + self.failUnless(s1 == s2) + + def testDelitem(self): + s1 = S([1, 2, 3]) + del s1[0] + i1 = s1[0] + i2 = s1[1] + self.failUnless(i1 != i2) + self.failUnless(i1 == 1 or i1 == 2 or i1 == 3) + self.failUnless(i2 == 1 or i2 == 2 or i2 == 3) + + def testDelslice(self): + s1 = S([1, 2, 3]) + del s1[0:2] + i1 = s1[0] + self.failUnless(i1 == 1 or i1 == 2 or i1 == 3) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_tokenizer.py b/tests/test_tokenizer.py new file mode 100644 index 0000000..1d561ae --- /dev/null +++ b/tests/test_tokenizer.py @@ -0,0 +1,190 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.exception +import dns.tokenizer + +Token = dns.tokenizer.Token + +class TokenizerTestCase(unittest.TestCase): + + def testQuotedString1(self): + tok = dns.tokenizer.Tokenizer(r'"foo"') + token = tok.get() + self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, 'foo')) + + def testQuotedString2(self): + tok = dns.tokenizer.Tokenizer(r'""') + token = tok.get() + self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, '')) + + def testQuotedString3(self): + tok = dns.tokenizer.Tokenizer(r'"\"foo\""') + token = tok.get() + self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, '"foo"')) + + def testQuotedString4(self): + tok = dns.tokenizer.Tokenizer(r'"foo\010bar"') + token = tok.get() + self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, 'foo\x0abar')) + + def testQuotedString5(self): + def bad(): + tok = dns.tokenizer.Tokenizer(r'"foo') + token = tok.get() + self.failUnlessRaises(dns.exception.UnexpectedEnd, bad) + + def testQuotedString6(self): + def bad(): + tok = dns.tokenizer.Tokenizer(r'"foo\01') + token = tok.get() + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testQuotedString7(self): + def bad(): + tok = dns.tokenizer.Tokenizer('"foo\nbar"') + token = tok.get() + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testEmpty1(self): + tok = dns.tokenizer.Tokenizer('') + token = tok.get() + self.failUnless(token.is_eof()) + + def testEmpty2(self): + tok = dns.tokenizer.Tokenizer('') + token1 = tok.get() + token2 = tok.get() + self.failUnless(token1.is_eof() and token2.is_eof()) + + def testEOL(self): + tok = dns.tokenizer.Tokenizer('\n') + token1 = tok.get() + token2 = tok.get() + self.failUnless(token1.is_eol() and token2.is_eof()) + + def testWS1(self): + tok = dns.tokenizer.Tokenizer(' \n') + token1 = tok.get() + self.failUnless(token1.is_eol()) + + def testWS2(self): + tok = dns.tokenizer.Tokenizer(' \n') + token1 = tok.get(want_leading=True) + self.failUnless(token1.is_whitespace()) + + def testComment1(self): + tok = dns.tokenizer.Tokenizer(' ;foo\n') + token1 = tok.get() + self.failUnless(token1.is_eol()) + + def testComment2(self): + tok = dns.tokenizer.Tokenizer(' ;foo\n') + token1 = tok.get(want_comment = True) + token2 = tok.get() + self.failUnless(token1 == Token(dns.tokenizer.COMMENT, 'foo') and + token2.is_eol()) + + def testComment3(self): + tok = dns.tokenizer.Tokenizer(' ;foo bar\n') + token1 = tok.get(want_comment = True) + token2 = tok.get() + self.failUnless(token1 == Token(dns.tokenizer.COMMENT, 'foo bar') and + token2.is_eol()) + + def testMultiline1(self): + tok = dns.tokenizer.Tokenizer('( foo\n\n bar\n)') + tokens = list(iter(tok)) + self.failUnless(tokens == [Token(dns.tokenizer.IDENTIFIER, 'foo'), + Token(dns.tokenizer.IDENTIFIER, 'bar')]) + + def testMultiline2(self): + tok = dns.tokenizer.Tokenizer('( foo\n\n bar\n)\n') + tokens = list(iter(tok)) + self.failUnless(tokens == [Token(dns.tokenizer.IDENTIFIER, 'foo'), + Token(dns.tokenizer.IDENTIFIER, 'bar'), + Token(dns.tokenizer.EOL, '\n')]) + def testMultiline3(self): + def bad(): + tok = dns.tokenizer.Tokenizer('foo)') + tokens = list(iter(tok)) + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testMultiline4(self): + def bad(): + tok = dns.tokenizer.Tokenizer('((foo)') + tokens = list(iter(tok)) + self.failUnlessRaises(dns.exception.SyntaxError, bad) + + def testUnget1(self): + tok = dns.tokenizer.Tokenizer('foo') + t1 = tok.get() + tok.unget(t1) + t2 = tok.get() + self.failUnless(t1 == t2 and t1.ttype == dns.tokenizer.IDENTIFIER and \ + t1.value == 'foo') + + def testUnget2(self): + def bad(): + tok = dns.tokenizer.Tokenizer('foo') + t1 = tok.get() + tok.unget(t1) + tok.unget(t1) + self.failUnlessRaises(dns.tokenizer.UngetBufferFull, bad) + + def testGetEOL1(self): + tok = dns.tokenizer.Tokenizer('\n') + t = tok.get_eol() + self.failUnless(t == '\n') + + def testGetEOL2(self): + tok = dns.tokenizer.Tokenizer('') + t = tok.get_eol() + self.failUnless(t == '') + + def testEscapedDelimiter1(self): + tok = dns.tokenizer.Tokenizer(r'ch\ ld') + t = tok.get() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\ ld') + + def testEscapedDelimiter2(self): + tok = dns.tokenizer.Tokenizer(r'ch\032ld') + t = tok.get() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\032ld') + + def testEscapedDelimiter3(self): + tok = dns.tokenizer.Tokenizer(r'ch\ild') + t = tok.get() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\ild') + + def testEscapedDelimiter1u(self): + tok = dns.tokenizer.Tokenizer(r'ch\ ld') + t = tok.get().unescape() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch ld') + + def testEscapedDelimiter2u(self): + tok = dns.tokenizer.Tokenizer(r'ch\032ld') + t = tok.get().unescape() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == 'ch ld') + + def testEscapedDelimiter3u(self): + tok = dns.tokenizer.Tokenizer(r'ch\ild') + t = tok.get().unescape() + self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'child') + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_update.py b/tests/test_update.py new file mode 100644 index 0000000..92ddb56 --- /dev/null +++ b/tests/test_update.py @@ -0,0 +1,114 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import unittest + +import dns.update +import dns.rdata +import dns.rdataset + +goodhex = '0001 2800 0001 0005 0007 0000' \ + '076578616d706c6500 0006 0001' \ + '03666f6fc00c 00ff 00ff 00000000 0000' \ + 'c019 0001 00ff 00000000 0000' \ + '03626172c00c 0001 0001 00000000 0004 0a000005' \ + '05626c617a32c00c 00ff 00fe 00000000 0000' \ + 'c049 0001 00fe 00000000 0000' \ + 'c019 0001 00ff 00000000 0000' \ + 'c019 0001 0001 0000012c 0004 0a000001' \ + 'c019 0001 0001 0000012c 0004 0a000002' \ + 'c035 0001 0001 0000012c 0004 0a000003' \ + 'c035 0001 00fe 00000000 0004 0a000004' \ + '04626c617ac00c 0001 00ff 00000000 0000' \ + 'c049 00ff 00ff 00000000 0000' + +goodwire = goodhex.replace(' ', '').decode('hex_codec') + +update_text="""id 1 +opcode UPDATE +rcode NOERROR +;ZONE +example. IN SOA +;PREREQ +foo ANY ANY +foo ANY A +bar 0 IN A 10.0.0.5 +blaz2 NONE ANY +blaz2 NONE A +;UPDATE +foo ANY A +foo 300 IN A 10.0.0.1 +foo 300 IN A 10.0.0.2 +bar 300 IN A 10.0.0.3 +bar 0 NONE A 10.0.0.4 +blaz ANY A +blaz2 ANY ANY +""" + +class UpdateTestCase(unittest.TestCase): + + def test_to_wire1(self): + update = dns.update.Update('example') + update.id = 1 + update.present('foo') + update.present('foo', 'a') + update.present('bar', 'a', '10.0.0.5') + update.absent('blaz2') + update.absent('blaz2', 'a') + update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') + update.add('bar', 300, 'a', '10.0.0.3') + update.delete('bar', 'a', '10.0.0.4') + update.delete('blaz','a') + update.delete('blaz2') + self.failUnless(update.to_wire() == goodwire) + + def test_to_wire2(self): + update = dns.update.Update('example') + update.id = 1 + update.present('foo') + update.present('foo', 'a') + update.present('bar', 'a', '10.0.0.5') + update.absent('blaz2') + update.absent('blaz2', 'a') + update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') + update.add('bar', 300, dns.rdata.from_text(1, 1, '10.0.0.3')) + update.delete('bar', 'a', '10.0.0.4') + update.delete('blaz','a') + update.delete('blaz2') + self.failUnless(update.to_wire() == goodwire) + + def test_to_wire3(self): + update = dns.update.Update('example') + update.id = 1 + update.present('foo') + update.present('foo', 'a') + update.present('bar', 'a', '10.0.0.5') + update.absent('blaz2') + update.absent('blaz2', 'a') + update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') + update.add('bar', dns.rdataset.from_text(1, 1, 300, '10.0.0.3')) + update.delete('bar', 'a', '10.0.0.4') + update.delete('blaz','a') + update.delete('blaz2') + self.failUnless(update.to_wire() == goodwire) + + def test_from_text1(self): + update = dns.message.from_text(update_text) + w = update.to_wire(origin=dns.name.from_text('example'), + want_shuffle=False) + self.failUnless(w == goodwire) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_zone.py b/tests/test_zone.py new file mode 100644 index 0000000..31e7405 --- /dev/null +++ b/tests/test_zone.py @@ -0,0 +1,389 @@ +# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import cStringIO +import filecmp +import os +import unittest + +import dns.exception +import dns.rdata +import dns.rdataclass +import dns.rdatatype +import dns.rrset +import dns.zone + +example_text = """$TTL 3600 +$ORIGIN example. +@ soa foo bar 1 2 3 4 5 +@ ns ns1 +@ ns ns2 +ns1 a 10.0.0.1 +ns2 a 10.0.0.2 +$TTL 300 +$ORIGIN foo.example. +bar mx 0 blaz +""" + +example_text_output = """@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.2 +""" + +something_quite_similar = """@ 3600 IN SOA foo bar 1 2 3 4 5 +@ 3600 IN NS ns1 +@ 3600 IN NS ns2 +bar.foo 300 IN MX 0 blaz.foo +ns1 3600 IN A 10.0.0.1 +ns2 3600 IN A 10.0.0.3 +""" + +something_different = """@ 3600 IN SOA fooa bar 1 2 3 4 5 +@ 3600 IN NS ns11 +@ 3600 IN NS ns21 +bar.fooa 300 IN MX 0 blaz.fooa +ns11 3600 IN A 10.0.0.11 +ns21 3600 IN A 10.0.0.21 +""" + +ttl_example_text = """$TTL 1h +$ORIGIN example. +@ soa foo bar 1 2 3 4 5 +@ ns ns1 +@ ns ns2 +ns1 1d1s a 10.0.0.1 +ns2 1w1D1h1m1S a 10.0.0.2 +""" + +no_soa_text = """$TTL 1h +$ORIGIN example. +@ ns ns1 +@ ns ns2 +ns1 1d1s a 10.0.0.1 +ns2 1w1D1h1m1S a 10.0.0.2 +""" + +no_ns_text = """$TTL 1h +$ORIGIN example. +@ soa foo bar 1 2 3 4 5 +""" + +include_text = """$INCLUDE "example" +""" + +bad_directive_text = """$FOO bar +$ORIGIN example. +@ soa foo bar 1 2 3 4 5 +@ ns ns1 +@ ns ns2 +ns1 1d1s a 10.0.0.1 +ns2 1w1D1h1m1S a 10.0.0.2 +""" + +_keep_output = False + +class ZoneTestCase(unittest.TestCase): + + def testFromFile1(self): + z = dns.zone.from_file('example', 'example') + ok = False + try: + z.to_file('example1.out', nl='\x0a') + ok = filecmp.cmp('example1.out', 'example1.good') + finally: + if not _keep_output: + os.unlink('example1.out') + self.failUnless(ok) + + def testFromFile2(self): + z = dns.zone.from_file('example', 'example', relativize=False) + ok = False + try: + z.to_file('example2.out', relativize=False, nl='\x0a') + ok = filecmp.cmp('example2.out', 'example2.good') + finally: + if not _keep_output: + os.unlink('example2.out') + self.failUnless(ok) + + def testFromText(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + f = cStringIO.StringIO() + names = z.nodes.keys() + names.sort() + for n in names: + print >> f, z[n].to_text(n) + self.failUnless(f.getvalue() == example_text_output) + + def testTorture1(self): + # + # Read a zone containing all our supported RR types, and + # for each RR in the zone, convert the rdata into wire format + # and then back out, and see if we get equal rdatas. + # + f = cStringIO.StringIO() + o = dns.name.from_text('example.') + z = dns.zone.from_file('example', o) + for (name, node) in z.iteritems(): + for rds in node: + for rd in rds: + f.seek(0) + f.truncate() + rd.to_wire(f, origin=o) + wire = f.getvalue() + rd2 = dns.rdata.from_wire(rds.rdclass, rds.rdtype, + wire, 0, len(wire), + origin = o) + self.failUnless(rd == rd2) + + def testEqual(self): + z1 = dns.zone.from_text(example_text, 'example.', relativize=True) + z2 = dns.zone.from_text(example_text_output, 'example.', + relativize=True) + self.failUnless(z1 == z2) + + def testNotEqual1(self): + z1 = dns.zone.from_text(example_text, 'example.', relativize=True) + z2 = dns.zone.from_text(something_quite_similar, 'example.', + relativize=True) + self.failUnless(z1 != z2) + + def testNotEqual2(self): + z1 = dns.zone.from_text(example_text, 'example.', relativize=True) + z2 = dns.zone.from_text(something_different, 'example.', + relativize=True) + self.failUnless(z1 != z2) + + def testNotEqual3(self): + z1 = dns.zone.from_text(example_text, 'example.', relativize=True) + z2 = dns.zone.from_text(something_different, 'example2.', + relativize=True) + self.failUnless(z1 != z2) + + def testFindRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rds = z.find_rdataset('@', 'soa') + exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') + self.failUnless(rds == exrds) + + def testFindRdataset2(self): + def bad(): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rds = z.find_rdataset('@', 'loc') + self.failUnlessRaises(KeyError, bad) + + def testFindRRset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rrs = z.find_rrset('@', 'soa') + exrrs = dns.rrset.from_text('@', 300, 'IN', 'SOA', 'foo bar 1 2 3 4 5') + self.failUnless(rrs == exrrs) + + def testFindRRset2(self): + def bad(): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rrs = z.find_rrset('@', 'loc') + self.failUnlessRaises(KeyError, bad) + + def testGetRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rds = z.get_rdataset('@', 'soa') + exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') + self.failUnless(rds == exrds) + + def testGetRdataset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rds = z.get_rdataset('@', 'loc') + self.failUnless(rds == None) + + def testGetRRset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rrs = z.get_rrset('@', 'soa') + exrrs = dns.rrset.from_text('@', 300, 'IN', 'SOA', 'foo bar 1 2 3 4 5') + self.failUnless(rrs == exrrs) + + def testGetRRset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rrs = z.get_rrset('@', 'loc') + self.failUnless(rrs == None) + + def testReplaceRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rdataset = dns.rdataset.from_text('in', 'ns', 300, 'ns3', 'ns4') + z.replace_rdataset('@', rdataset) + rds = z.get_rdataset('@', 'ns') + self.failUnless(rds is rdataset) + + def testReplaceRdataset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + rdataset = dns.rdataset.from_text('in', 'txt', 300, '"foo"') + z.replace_rdataset('@', rdataset) + rds = z.get_rdataset('@', 'txt') + self.failUnless(rds is rdataset) + + def testDeleteRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + z.delete_rdataset('@', 'ns') + rds = z.get_rdataset('@', 'ns') + self.failUnless(rds is None) + + def testDeleteRdataset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + z.delete_rdataset('ns1', 'a') + node = z.get_node('ns1') + self.failUnless(node is None) + + def testNodeFindRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) + exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') + self.failUnless(rds == exrds) + + def testNodeFindRdataset2(self): + def bad(): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) + self.failUnlessRaises(KeyError, bad) + + def testNodeGetRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) + exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') + self.failUnless(rds == exrds) + + def testNodeGetRdataset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) + self.failUnless(rds == None) + + def testNodeDeleteRdataset1(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) + rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) + self.failUnless(rds == None) + + def testNodeDeleteRdataset2(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + node = z['@'] + rds = node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) + rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) + self.failUnless(rds == None) + + def testIterateRdatasets(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + ns = [n for n, r in z.iterate_rdatasets('A')] + ns.sort() + self.failUnless(ns == [dns.name.from_text('ns1', None), + dns.name.from_text('ns2', None)]) + + def testIterateAllRdatasets(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + ns = [n for n, r in z.iterate_rdatasets()] + ns.sort() + self.failUnless(ns == [dns.name.from_text('@', None), + dns.name.from_text('@', None), + dns.name.from_text('bar.foo', None), + dns.name.from_text('ns1', None), + dns.name.from_text('ns2', None)]) + + def testIterateRdatas(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + l = list(z.iterate_rdatas('A')) + l.sort() + exl = [(dns.name.from_text('ns1', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2'))] + self.failUnless(l == exl) + + def testIterateAllRdatas(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True) + l = list(z.iterate_rdatas()) + l.sort() + exl = [(dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns1')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, + 'ns2')), + (dns.name.from_text('@', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, + 'foo bar 1 2 3 4 5')), + (dns.name.from_text('bar.foo', None), + 300, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, + '0 blaz.foo')), + (dns.name.from_text('ns1', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.1')), + (dns.name.from_text('ns2', None), + 3600, + dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, + '10.0.0.2'))] + self.failUnless(l == exl) + + def testTTLs(self): + z = dns.zone.from_text(ttl_example_text, 'example.', relativize=True) + n = z['@'] + rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) + self.failUnless(rds.ttl == 3600) + n = z['ns1'] + rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A) + self.failUnless(rds.ttl == 86401) + n = z['ns2'] + rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A) + self.failUnless(rds.ttl == 694861) + + def testNoSOA(self): + def bad(): + z = dns.zone.from_text(no_soa_text, 'example.', + relativize=True) + self.failUnlessRaises(dns.zone.NoSOA, bad) + + def testNoNS(self): + def bad(): + z = dns.zone.from_text(no_ns_text, 'example.', + relativize=True) + self.failUnlessRaises(dns.zone.NoNS, bad) + + def testInclude(self): + z1 = dns.zone.from_text(include_text, 'example.', relativize=True, + allow_include=True) + z2 = dns.zone.from_file('example', 'example.', relativize=True) + self.failUnless(z1 == z2) + + def testBadDirective(self): + def bad(): + z = dns.zone.from_text(bad_directive_text, 'example.', + relativize=True) + self.failUnlessRaises(dns.exception.SyntaxError, bad) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/tokenizer.py b/tests/tokenizer.py deleted file mode 100644 index 1d561ae..0000000 --- a/tests/tokenizer.py +++ /dev/null @@ -1,190 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.exception -import dns.tokenizer - -Token = dns.tokenizer.Token - -class TokenizerTestCase(unittest.TestCase): - - def testQuotedString1(self): - tok = dns.tokenizer.Tokenizer(r'"foo"') - token = tok.get() - self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, 'foo')) - - def testQuotedString2(self): - tok = dns.tokenizer.Tokenizer(r'""') - token = tok.get() - self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, '')) - - def testQuotedString3(self): - tok = dns.tokenizer.Tokenizer(r'"\"foo\""') - token = tok.get() - self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, '"foo"')) - - def testQuotedString4(self): - tok = dns.tokenizer.Tokenizer(r'"foo\010bar"') - token = tok.get() - self.failUnless(token == Token(dns.tokenizer.QUOTED_STRING, 'foo\x0abar')) - - def testQuotedString5(self): - def bad(): - tok = dns.tokenizer.Tokenizer(r'"foo') - token = tok.get() - self.failUnlessRaises(dns.exception.UnexpectedEnd, bad) - - def testQuotedString6(self): - def bad(): - tok = dns.tokenizer.Tokenizer(r'"foo\01') - token = tok.get() - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testQuotedString7(self): - def bad(): - tok = dns.tokenizer.Tokenizer('"foo\nbar"') - token = tok.get() - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testEmpty1(self): - tok = dns.tokenizer.Tokenizer('') - token = tok.get() - self.failUnless(token.is_eof()) - - def testEmpty2(self): - tok = dns.tokenizer.Tokenizer('') - token1 = tok.get() - token2 = tok.get() - self.failUnless(token1.is_eof() and token2.is_eof()) - - def testEOL(self): - tok = dns.tokenizer.Tokenizer('\n') - token1 = tok.get() - token2 = tok.get() - self.failUnless(token1.is_eol() and token2.is_eof()) - - def testWS1(self): - tok = dns.tokenizer.Tokenizer(' \n') - token1 = tok.get() - self.failUnless(token1.is_eol()) - - def testWS2(self): - tok = dns.tokenizer.Tokenizer(' \n') - token1 = tok.get(want_leading=True) - self.failUnless(token1.is_whitespace()) - - def testComment1(self): - tok = dns.tokenizer.Tokenizer(' ;foo\n') - token1 = tok.get() - self.failUnless(token1.is_eol()) - - def testComment2(self): - tok = dns.tokenizer.Tokenizer(' ;foo\n') - token1 = tok.get(want_comment = True) - token2 = tok.get() - self.failUnless(token1 == Token(dns.tokenizer.COMMENT, 'foo') and - token2.is_eol()) - - def testComment3(self): - tok = dns.tokenizer.Tokenizer(' ;foo bar\n') - token1 = tok.get(want_comment = True) - token2 = tok.get() - self.failUnless(token1 == Token(dns.tokenizer.COMMENT, 'foo bar') and - token2.is_eol()) - - def testMultiline1(self): - tok = dns.tokenizer.Tokenizer('( foo\n\n bar\n)') - tokens = list(iter(tok)) - self.failUnless(tokens == [Token(dns.tokenizer.IDENTIFIER, 'foo'), - Token(dns.tokenizer.IDENTIFIER, 'bar')]) - - def testMultiline2(self): - tok = dns.tokenizer.Tokenizer('( foo\n\n bar\n)\n') - tokens = list(iter(tok)) - self.failUnless(tokens == [Token(dns.tokenizer.IDENTIFIER, 'foo'), - Token(dns.tokenizer.IDENTIFIER, 'bar'), - Token(dns.tokenizer.EOL, '\n')]) - def testMultiline3(self): - def bad(): - tok = dns.tokenizer.Tokenizer('foo)') - tokens = list(iter(tok)) - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testMultiline4(self): - def bad(): - tok = dns.tokenizer.Tokenizer('((foo)') - tokens = list(iter(tok)) - self.failUnlessRaises(dns.exception.SyntaxError, bad) - - def testUnget1(self): - tok = dns.tokenizer.Tokenizer('foo') - t1 = tok.get() - tok.unget(t1) - t2 = tok.get() - self.failUnless(t1 == t2 and t1.ttype == dns.tokenizer.IDENTIFIER and \ - t1.value == 'foo') - - def testUnget2(self): - def bad(): - tok = dns.tokenizer.Tokenizer('foo') - t1 = tok.get() - tok.unget(t1) - tok.unget(t1) - self.failUnlessRaises(dns.tokenizer.UngetBufferFull, bad) - - def testGetEOL1(self): - tok = dns.tokenizer.Tokenizer('\n') - t = tok.get_eol() - self.failUnless(t == '\n') - - def testGetEOL2(self): - tok = dns.tokenizer.Tokenizer('') - t = tok.get_eol() - self.failUnless(t == '') - - def testEscapedDelimiter1(self): - tok = dns.tokenizer.Tokenizer(r'ch\ ld') - t = tok.get() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\ ld') - - def testEscapedDelimiter2(self): - tok = dns.tokenizer.Tokenizer(r'ch\032ld') - t = tok.get() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\032ld') - - def testEscapedDelimiter3(self): - tok = dns.tokenizer.Tokenizer(r'ch\ild') - t = tok.get() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch\ild') - - def testEscapedDelimiter1u(self): - tok = dns.tokenizer.Tokenizer(r'ch\ ld') - t = tok.get().unescape() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'ch ld') - - def testEscapedDelimiter2u(self): - tok = dns.tokenizer.Tokenizer(r'ch\032ld') - t = tok.get().unescape() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == 'ch ld') - - def testEscapedDelimiter3u(self): - tok = dns.tokenizer.Tokenizer(r'ch\ild') - t = tok.get().unescape() - self.failUnless(t.ttype == dns.tokenizer.IDENTIFIER and t.value == r'child') - -if __name__ == '__main__': - unittest.main() diff --git a/tests/update.py b/tests/update.py deleted file mode 100644 index 92ddb56..0000000 --- a/tests/update.py +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import unittest - -import dns.update -import dns.rdata -import dns.rdataset - -goodhex = '0001 2800 0001 0005 0007 0000' \ - '076578616d706c6500 0006 0001' \ - '03666f6fc00c 00ff 00ff 00000000 0000' \ - 'c019 0001 00ff 00000000 0000' \ - '03626172c00c 0001 0001 00000000 0004 0a000005' \ - '05626c617a32c00c 00ff 00fe 00000000 0000' \ - 'c049 0001 00fe 00000000 0000' \ - 'c019 0001 00ff 00000000 0000' \ - 'c019 0001 0001 0000012c 0004 0a000001' \ - 'c019 0001 0001 0000012c 0004 0a000002' \ - 'c035 0001 0001 0000012c 0004 0a000003' \ - 'c035 0001 00fe 00000000 0004 0a000004' \ - '04626c617ac00c 0001 00ff 00000000 0000' \ - 'c049 00ff 00ff 00000000 0000' - -goodwire = goodhex.replace(' ', '').decode('hex_codec') - -update_text="""id 1 -opcode UPDATE -rcode NOERROR -;ZONE -example. IN SOA -;PREREQ -foo ANY ANY -foo ANY A -bar 0 IN A 10.0.0.5 -blaz2 NONE ANY -blaz2 NONE A -;UPDATE -foo ANY A -foo 300 IN A 10.0.0.1 -foo 300 IN A 10.0.0.2 -bar 300 IN A 10.0.0.3 -bar 0 NONE A 10.0.0.4 -blaz ANY A -blaz2 ANY ANY -""" - -class UpdateTestCase(unittest.TestCase): - - def test_to_wire1(self): - update = dns.update.Update('example') - update.id = 1 - update.present('foo') - update.present('foo', 'a') - update.present('bar', 'a', '10.0.0.5') - update.absent('blaz2') - update.absent('blaz2', 'a') - update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') - update.add('bar', 300, 'a', '10.0.0.3') - update.delete('bar', 'a', '10.0.0.4') - update.delete('blaz','a') - update.delete('blaz2') - self.failUnless(update.to_wire() == goodwire) - - def test_to_wire2(self): - update = dns.update.Update('example') - update.id = 1 - update.present('foo') - update.present('foo', 'a') - update.present('bar', 'a', '10.0.0.5') - update.absent('blaz2') - update.absent('blaz2', 'a') - update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') - update.add('bar', 300, dns.rdata.from_text(1, 1, '10.0.0.3')) - update.delete('bar', 'a', '10.0.0.4') - update.delete('blaz','a') - update.delete('blaz2') - self.failUnless(update.to_wire() == goodwire) - - def test_to_wire3(self): - update = dns.update.Update('example') - update.id = 1 - update.present('foo') - update.present('foo', 'a') - update.present('bar', 'a', '10.0.0.5') - update.absent('blaz2') - update.absent('blaz2', 'a') - update.replace('foo', 300, 'a', '10.0.0.1', '10.0.0.2') - update.add('bar', dns.rdataset.from_text(1, 1, 300, '10.0.0.3')) - update.delete('bar', 'a', '10.0.0.4') - update.delete('blaz','a') - update.delete('blaz2') - self.failUnless(update.to_wire() == goodwire) - - def test_from_text1(self): - update = dns.message.from_text(update_text) - w = update.to_wire(origin=dns.name.from_text('example'), - want_shuffle=False) - self.failUnless(w == goodwire) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/utest.py b/tests/utest.py new file mode 100644 index 0000000..32c1d75 --- /dev/null +++ b/tests/utest.py @@ -0,0 +1,8 @@ +import os.path +import sys +import unittest + +if __name__ == '__main__': + sys.path.insert(0, os.path.realpath('..')) + suites = unittest.defaultTestLoader.discover('.') + unittest.TextTestRunner(verbosity=2).run(suites) diff --git a/tests/zone.py b/tests/zone.py deleted file mode 100644 index 31e7405..0000000 --- a/tests/zone.py +++ /dev/null @@ -1,389 +0,0 @@ -# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose with or without fee is hereby granted, -# provided that the above copyright notice and this permission notice -# appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import cStringIO -import filecmp -import os -import unittest - -import dns.exception -import dns.rdata -import dns.rdataclass -import dns.rdatatype -import dns.rrset -import dns.zone - -example_text = """$TTL 3600 -$ORIGIN example. -@ soa foo bar 1 2 3 4 5 -@ ns ns1 -@ ns ns2 -ns1 a 10.0.0.1 -ns2 a 10.0.0.2 -$TTL 300 -$ORIGIN foo.example. -bar mx 0 blaz -""" - -example_text_output = """@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.2 -""" - -something_quite_similar = """@ 3600 IN SOA foo bar 1 2 3 4 5 -@ 3600 IN NS ns1 -@ 3600 IN NS ns2 -bar.foo 300 IN MX 0 blaz.foo -ns1 3600 IN A 10.0.0.1 -ns2 3600 IN A 10.0.0.3 -""" - -something_different = """@ 3600 IN SOA fooa bar 1 2 3 4 5 -@ 3600 IN NS ns11 -@ 3600 IN NS ns21 -bar.fooa 300 IN MX 0 blaz.fooa -ns11 3600 IN A 10.0.0.11 -ns21 3600 IN A 10.0.0.21 -""" - -ttl_example_text = """$TTL 1h -$ORIGIN example. -@ soa foo bar 1 2 3 4 5 -@ ns ns1 -@ ns ns2 -ns1 1d1s a 10.0.0.1 -ns2 1w1D1h1m1S a 10.0.0.2 -""" - -no_soa_text = """$TTL 1h -$ORIGIN example. -@ ns ns1 -@ ns ns2 -ns1 1d1s a 10.0.0.1 -ns2 1w1D1h1m1S a 10.0.0.2 -""" - -no_ns_text = """$TTL 1h -$ORIGIN example. -@ soa foo bar 1 2 3 4 5 -""" - -include_text = """$INCLUDE "example" -""" - -bad_directive_text = """$FOO bar -$ORIGIN example. -@ soa foo bar 1 2 3 4 5 -@ ns ns1 -@ ns ns2 -ns1 1d1s a 10.0.0.1 -ns2 1w1D1h1m1S a 10.0.0.2 -""" - -_keep_output = False - -class ZoneTestCase(unittest.TestCase): - - def testFromFile1(self): - z = dns.zone.from_file('example', 'example') - ok = False - try: - z.to_file('example1.out', nl='\x0a') - ok = filecmp.cmp('example1.out', 'example1.good') - finally: - if not _keep_output: - os.unlink('example1.out') - self.failUnless(ok) - - def testFromFile2(self): - z = dns.zone.from_file('example', 'example', relativize=False) - ok = False - try: - z.to_file('example2.out', relativize=False, nl='\x0a') - ok = filecmp.cmp('example2.out', 'example2.good') - finally: - if not _keep_output: - os.unlink('example2.out') - self.failUnless(ok) - - def testFromText(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - f = cStringIO.StringIO() - names = z.nodes.keys() - names.sort() - for n in names: - print >> f, z[n].to_text(n) - self.failUnless(f.getvalue() == example_text_output) - - def testTorture1(self): - # - # Read a zone containing all our supported RR types, and - # for each RR in the zone, convert the rdata into wire format - # and then back out, and see if we get equal rdatas. - # - f = cStringIO.StringIO() - o = dns.name.from_text('example.') - z = dns.zone.from_file('example', o) - for (name, node) in z.iteritems(): - for rds in node: - for rd in rds: - f.seek(0) - f.truncate() - rd.to_wire(f, origin=o) - wire = f.getvalue() - rd2 = dns.rdata.from_wire(rds.rdclass, rds.rdtype, - wire, 0, len(wire), - origin = o) - self.failUnless(rd == rd2) - - def testEqual(self): - z1 = dns.zone.from_text(example_text, 'example.', relativize=True) - z2 = dns.zone.from_text(example_text_output, 'example.', - relativize=True) - self.failUnless(z1 == z2) - - def testNotEqual1(self): - z1 = dns.zone.from_text(example_text, 'example.', relativize=True) - z2 = dns.zone.from_text(something_quite_similar, 'example.', - relativize=True) - self.failUnless(z1 != z2) - - def testNotEqual2(self): - z1 = dns.zone.from_text(example_text, 'example.', relativize=True) - z2 = dns.zone.from_text(something_different, 'example.', - relativize=True) - self.failUnless(z1 != z2) - - def testNotEqual3(self): - z1 = dns.zone.from_text(example_text, 'example.', relativize=True) - z2 = dns.zone.from_text(something_different, 'example2.', - relativize=True) - self.failUnless(z1 != z2) - - def testFindRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rds = z.find_rdataset('@', 'soa') - exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') - self.failUnless(rds == exrds) - - def testFindRdataset2(self): - def bad(): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rds = z.find_rdataset('@', 'loc') - self.failUnlessRaises(KeyError, bad) - - def testFindRRset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rrs = z.find_rrset('@', 'soa') - exrrs = dns.rrset.from_text('@', 300, 'IN', 'SOA', 'foo bar 1 2 3 4 5') - self.failUnless(rrs == exrrs) - - def testFindRRset2(self): - def bad(): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rrs = z.find_rrset('@', 'loc') - self.failUnlessRaises(KeyError, bad) - - def testGetRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rds = z.get_rdataset('@', 'soa') - exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') - self.failUnless(rds == exrds) - - def testGetRdataset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rds = z.get_rdataset('@', 'loc') - self.failUnless(rds == None) - - def testGetRRset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rrs = z.get_rrset('@', 'soa') - exrrs = dns.rrset.from_text('@', 300, 'IN', 'SOA', 'foo bar 1 2 3 4 5') - self.failUnless(rrs == exrrs) - - def testGetRRset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rrs = z.get_rrset('@', 'loc') - self.failUnless(rrs == None) - - def testReplaceRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rdataset = dns.rdataset.from_text('in', 'ns', 300, 'ns3', 'ns4') - z.replace_rdataset('@', rdataset) - rds = z.get_rdataset('@', 'ns') - self.failUnless(rds is rdataset) - - def testReplaceRdataset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - rdataset = dns.rdataset.from_text('in', 'txt', 300, '"foo"') - z.replace_rdataset('@', rdataset) - rds = z.get_rdataset('@', 'txt') - self.failUnless(rds is rdataset) - - def testDeleteRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - z.delete_rdataset('@', 'ns') - rds = z.get_rdataset('@', 'ns') - self.failUnless(rds is None) - - def testDeleteRdataset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - z.delete_rdataset('ns1', 'a') - node = z.get_node('ns1') - self.failUnless(node is None) - - def testNodeFindRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) - exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') - self.failUnless(rds == exrds) - - def testNodeFindRdataset2(self): - def bad(): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) - self.failUnlessRaises(KeyError, bad) - - def testNodeGetRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) - exrds = dns.rdataset.from_text('IN', 'SOA', 300, 'foo bar 1 2 3 4 5') - self.failUnless(rds == exrds) - - def testNodeGetRdataset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) - self.failUnless(rds == None) - - def testNodeDeleteRdataset1(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) - rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) - self.failUnless(rds == None) - - def testNodeDeleteRdataset2(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - node = z['@'] - rds = node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) - rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC) - self.failUnless(rds == None) - - def testIterateRdatasets(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - ns = [n for n, r in z.iterate_rdatasets('A')] - ns.sort() - self.failUnless(ns == [dns.name.from_text('ns1', None), - dns.name.from_text('ns2', None)]) - - def testIterateAllRdatasets(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - ns = [n for n, r in z.iterate_rdatasets()] - ns.sort() - self.failUnless(ns == [dns.name.from_text('@', None), - dns.name.from_text('@', None), - dns.name.from_text('bar.foo', None), - dns.name.from_text('ns1', None), - dns.name.from_text('ns2', None)]) - - def testIterateRdatas(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - l = list(z.iterate_rdatas('A')) - l.sort() - exl = [(dns.name.from_text('ns1', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2'))] - self.failUnless(l == exl) - - def testIterateAllRdatas(self): - z = dns.zone.from_text(example_text, 'example.', relativize=True) - l = list(z.iterate_rdatas()) - l.sort() - exl = [(dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns1')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, - 'ns2')), - (dns.name.from_text('@', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.SOA, - 'foo bar 1 2 3 4 5')), - (dns.name.from_text('bar.foo', None), - 300, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, - '0 blaz.foo')), - (dns.name.from_text('ns1', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.1')), - (dns.name.from_text('ns2', None), - 3600, - dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, - '10.0.0.2'))] - self.failUnless(l == exl) - - def testTTLs(self): - z = dns.zone.from_text(ttl_example_text, 'example.', relativize=True) - n = z['@'] - rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA) - self.failUnless(rds.ttl == 3600) - n = z['ns1'] - rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A) - self.failUnless(rds.ttl == 86401) - n = z['ns2'] - rds = n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A) - self.failUnless(rds.ttl == 694861) - - def testNoSOA(self): - def bad(): - z = dns.zone.from_text(no_soa_text, 'example.', - relativize=True) - self.failUnlessRaises(dns.zone.NoSOA, bad) - - def testNoNS(self): - def bad(): - z = dns.zone.from_text(no_ns_text, 'example.', - relativize=True) - self.failUnlessRaises(dns.zone.NoNS, bad) - - def testInclude(self): - z1 = dns.zone.from_text(include_text, 'example.', relativize=True, - allow_include=True) - z2 = dns.zone.from_file('example', 'example.', relativize=True) - self.failUnless(z1 == z2) - - def testBadDirective(self): - def bad(): - z = dns.zone.from_text(bad_directive_text, 'example.', - relativize=True) - self.failUnlessRaises(dns.exception.SyntaxError, bad) - -if __name__ == '__main__': - unittest.main() -- cgit v1.2.1