-
Notifications
You must be signed in to change notification settings - Fork 11
/
manufacturer_test.py
129 lines (112 loc) · 4.79 KB
/
manufacturer_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# manufacturer_test.py
# Copyright (C) 2015 Simon Newton
import unittest
import urllib2
import pprint
from socket import error as SocketError
from urllib2 import HTTPError
from urllib2 import URLError
from ssl import SSLError
class TestManufacturers(unittest.TestCase):
""" Test the manufacturer data files are valid."""
def setUp(self):
globals = {}
locals = {}
execfile("data/manufacturer_data.py", globals, locals)
self.data = locals['MANUFACTURER_DATA']
globals = {}
locals = {}
execfile("data/manufacturer_links.py", globals, locals)
self.links = locals['MANUFACTURER_LINKS']
def test_ManufacturerData(self):
seen_ids = set()
for manufacturer_data in self.data:
self.assertEqual(tuple, type(manufacturer_data))
self.assertEqual(2, len(manufacturer_data))
esta_id, name = manufacturer_data
self.assertEqual(int, type(esta_id))
self.assertEqual(str, type(name))
self.assertNotIn(esta_id, seen_ids,
("ESTA ID 0x%04x is present twice in manufacturers" %
esta_id))
seen_ids.add(esta_id)
# check that ESTA exists
self.assertIn(0, seen_ids)
# check that an ESTA test ID at the end of the file exists
self.assertIn(0x7FF0, seen_ids)
def test_ManufacturerLinks(self):
esta_ids = set()
seen_ids = set()
for manufacturer_data in self.data:
esta_id, name = manufacturer_data
esta_ids.add(esta_id)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
for manufacturer_link in self.links:
self.assertEqual(tuple, type(manufacturer_link))
self.assertEqual(2, len(manufacturer_link))
esta_id, link = manufacturer_link
self.assertEqual(int, type(esta_id))
self.assertEqual(str, type(link))
# Check we have a corresponding entry in the manufacturer data
self.assertIn(esta_id, esta_ids,
("ESTA ID 0x%04x is not present in the manufacturer data" %
esta_id))
# Check we've not seen a URL for this ID before
self.assertNotIn(esta_id, seen_ids,
"ESTA ID 0x%04x is present twice in links" % esta_id)
seen_ids.add(esta_id)
# Check the link is valid
try:
# Some web servers, and Cloudflare, block us unless we have a
# non-python User Agent
ua = {'User-Agent': 'Mozilla/5.0 (KHTML, like Gecko)',
'referer': 'http://example.com'}
request = urllib2.Request(link, headers=ua)
response = opener.open(request)
except URLError as e:
if hasattr(e, 'reason'):
if hasattr(e, 'code'):
pprint.pprint(e.code)
if hasattr(e, 'headers'):
pprint.pprint(vars(e.headers))
# TODO(Peter): Various URLs fail SSL validation due to an incomplete
# chain, others just don't like our CI testing of valid pages,
# skip all these error for now
if not ((type(e.reason) is SSLError and
(link == 'https://www.arri.com/' or
link == 'https://www.diconfiberoptics.com/' or
link == 'https://www.enttec.com/')) or
(type(e) is HTTPError and
(link == 'http://www.compulite.com/' or
link == 'https://www.lutron.com/en-US/Pages/default.aspx' or
link == 'https://www.panasonic.com/'))):
self.fail("Link %s failed due to %s, reason type: %s" % (link, e.reason, type(e)))
elif hasattr(e, 'code'):
self.fail("The server couldn't fulfill the request for %s. Error "
"code: %s, reason type: %s" % (link, e.code, type(e.reason)))
except SocketError as e:
if hasattr(e, 'errno'):
self.fail("Link %s failed due to socket error %s" % (link, e.errno))
else:
self.assertEqual(response.code, 200,
"Failed to fetch URL %s got status %d" %
(link, response.code))
# optional, check that ESTA exists
self.assertIn(0, seen_ids)
if __name__ == '__main__':
unittest.main()