|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +""" Unit tests for zeroconf.exceptions """ |
| 6 | + |
| 7 | +import logging |
| 8 | +import unittest |
| 9 | +import unittest.mock |
| 10 | + |
| 11 | +import zeroconf as r |
| 12 | +from zeroconf import ( |
| 13 | + ServiceInfo, |
| 14 | + Zeroconf, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +log = logging.getLogger('zeroconf') |
| 19 | +original_logging_level = logging.NOTSET |
| 20 | + |
| 21 | + |
| 22 | +def setup_module(): |
| 23 | + global original_logging_level |
| 24 | + original_logging_level = log.level |
| 25 | + log.setLevel(logging.DEBUG) |
| 26 | + |
| 27 | + |
| 28 | +def teardown_module(): |
| 29 | + if original_logging_level != logging.NOTSET: |
| 30 | + log.setLevel(original_logging_level) |
| 31 | + |
| 32 | + |
| 33 | +class Exceptions(unittest.TestCase): |
| 34 | + |
| 35 | + browser = None # type: Zeroconf |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def setUpClass(cls): |
| 39 | + cls.browser = Zeroconf(interfaces=['127.0.0.1']) |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def tearDownClass(cls): |
| 43 | + cls.browser.close() |
| 44 | + del cls.browser |
| 45 | + |
| 46 | + def test_bad_service_info_name(self): |
| 47 | + self.assertRaises(r.BadTypeInNameException, self.browser.get_service_info, "type", "type_not") |
| 48 | + |
| 49 | + def test_bad_service_names(self): |
| 50 | + bad_names_to_try = ( |
| 51 | + '', |
| 52 | + 'local', |
| 53 | + '_tcp.local.', |
| 54 | + '_udp.local.', |
| 55 | + '._udp.local.', |
| 56 | + '_@._tcp.local.', |
| 57 | + '_A@._tcp.local.', |
| 58 | + '_x--x._tcp.local.', |
| 59 | + '_-x._udp.local.', |
| 60 | + '_x-._tcp.local.', |
| 61 | + '_22._udp.local.', |
| 62 | + '_2-2._tcp.local.', |
| 63 | + '_1234567890-abcde._udp.local.', |
| 64 | + '\x00._x._udp.local.', |
| 65 | + ) |
| 66 | + for name in bad_names_to_try: |
| 67 | + self.assertRaises(r.BadTypeInNameException, self.browser.get_service_info, name, 'x.' + name) |
| 68 | + |
| 69 | + def test_bad_local_names_for_get_service_info(self): |
| 70 | + bad_names_to_try = ( |
| 71 | + 'homekitdev._nothttp._tcp.local.', |
| 72 | + 'homekitdev._http._udp.local.', |
| 73 | + ) |
| 74 | + for name in bad_names_to_try: |
| 75 | + self.assertRaises( |
| 76 | + r.BadTypeInNameException, self.browser.get_service_info, '_http._tcp.local.', name |
| 77 | + ) |
| 78 | + |
| 79 | + def test_good_instance_names(self): |
| 80 | + assert r.service_type_name('.._x._tcp.local.') == '_x._tcp.local.' |
| 81 | + assert r.service_type_name('x.sub._http._tcp.local.') == '_http._tcp.local.' |
| 82 | + assert ( |
| 83 | + r.service_type_name('6d86f882b90facee9170ad3439d72a4d6ee9f511._zget._http._tcp.local.') |
| 84 | + == '_http._tcp.local.' |
| 85 | + ) |
| 86 | + |
| 87 | + def test_good_instance_names_without_protocol(self): |
| 88 | + good_names_to_try = ( |
| 89 | + "Rachio-C73233.local.", |
| 90 | + 'YeelightColorBulb-3AFD.local.', |
| 91 | + 'YeelightTunableBulb-7220.local.', |
| 92 | + "AlexanderHomeAssistant 74651D.local.", |
| 93 | + 'iSmartGate-152.local.', |
| 94 | + 'MyQ-FGA.local.', |
| 95 | + 'lutron-02c4392a.local.', |
| 96 | + 'WICED-hap-3E2734.local.', |
| 97 | + 'MyHost.local.', |
| 98 | + 'MyHost.sub.local.', |
| 99 | + ) |
| 100 | + for name in good_names_to_try: |
| 101 | + assert r.service_type_name(name, strict=False) == 'local.' |
| 102 | + |
| 103 | + for name in good_names_to_try: |
| 104 | + # Raises without strict=False |
| 105 | + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) |
| 106 | + |
| 107 | + def test_bad_types(self): |
| 108 | + bad_names_to_try = ( |
| 109 | + '._x._tcp.local.', |
| 110 | + 'a' * 64 + '._sub._http._tcp.local.', |
| 111 | + 'a' * 62 + u'â._sub._http._tcp.local.', |
| 112 | + ) |
| 113 | + for name in bad_names_to_try: |
| 114 | + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) |
| 115 | + |
| 116 | + def test_bad_sub_types(self): |
| 117 | + bad_names_to_try = ( |
| 118 | + '_sub._http._tcp.local.', |
| 119 | + '._sub._http._tcp.local.', |
| 120 | + '\x7f._sub._http._tcp.local.', |
| 121 | + '\x1f._sub._http._tcp.local.', |
| 122 | + ) |
| 123 | + for name in bad_names_to_try: |
| 124 | + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) |
| 125 | + |
| 126 | + def test_good_service_names(self): |
| 127 | + good_names_to_try = ( |
| 128 | + ('_x._tcp.local.', '_x._tcp.local.'), |
| 129 | + ('_x._udp.local.', '_x._udp.local.'), |
| 130 | + ('_12345-67890-abc._udp.local.', '_12345-67890-abc._udp.local.'), |
| 131 | + ('x._sub._http._tcp.local.', '_http._tcp.local.'), |
| 132 | + ('a' * 63 + '._sub._http._tcp.local.', '_http._tcp.local.'), |
| 133 | + ('a' * 61 + u'â._sub._http._tcp.local.', '_http._tcp.local.'), |
| 134 | + ) |
| 135 | + |
| 136 | + for name, result in good_names_to_try: |
| 137 | + assert r.service_type_name(name) == result |
| 138 | + |
| 139 | + assert r.service_type_name('_one_two._tcp.local.', strict=False) == '_one_two._tcp.local.' |
| 140 | + |
| 141 | + def test_invalid_addresses(self): |
| 142 | + type_ = "_test-srvc-type._tcp.local." |
| 143 | + name = "xxxyyy" |
| 144 | + registration_name = "%s.%s" % (name, type_) |
| 145 | + |
| 146 | + bad = ('127.0.0.1', '::1', 42) |
| 147 | + for addr in bad: |
| 148 | + self.assertRaisesRegex( |
| 149 | + TypeError, |
| 150 | + 'Addresses must be bytes', |
| 151 | + ServiceInfo, |
| 152 | + type_, |
| 153 | + registration_name, |
| 154 | + port=80, |
| 155 | + addresses=[addr], |
| 156 | + ) |
0 commit comments