• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import test_support
3import io
4import os
5import uuid
6
7def importable(name):
8    try:
9        __import__(name)
10        return True
11    except:
12        return False
13
14class TestUUID(unittest.TestCase):
15    def test_UUID(self):
16        equal = self.assertEqual
17        ascending = []
18        for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
19             time, clock_seq, variant, version) in [
20            ('00000000-0000-0000-0000-000000000000',
21             '{00000000-0000-0000-0000-000000000000}',
22             '00000000000000000000000000000000',
23             '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
24             '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
25             (0, 0, 0, 0, 0, 0),
26             0,
27             'urn:uuid:00000000-0000-0000-0000-000000000000',
28             0, 0, uuid.RESERVED_NCS, None),
29            ('00010203-0405-0607-0809-0a0b0c0d0e0f',
30             '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
31             '000102030405060708090a0b0c0d0e0f',
32             '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
33             '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
34             (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
35             0x000102030405060708090a0b0c0d0e0fL,
36             'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
37             0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
38            ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
39             '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
40             '02d9e6d59467382e8f9b9300a64ac3cd',
41             '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
42             '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
43             (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
44             0x02d9e6d59467382e8f9b9300a64ac3cdL,
45             'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
46             0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
47            ('12345678-1234-5678-1234-567812345678',
48             '{12345678-1234-5678-1234-567812345678}',
49             '12345678123456781234567812345678',
50             '\x12\x34\x56\x78'*4,
51             '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
52             (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
53             0x12345678123456781234567812345678,
54             'urn:uuid:12345678-1234-5678-1234-567812345678',
55             0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
56            ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
57             '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
58             '6ba7b8109dad11d180b400c04fd430c8',
59             '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
60             '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
61             (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
62             0x6ba7b8109dad11d180b400c04fd430c8L,
63             'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
64             0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
65            ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
66             '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
67             '6ba7b8119dad11d180b400c04fd430c8',
68             '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
69             '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70             (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
71             0x6ba7b8119dad11d180b400c04fd430c8L,
72             'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
73             0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
74            ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
75             '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
76             '6ba7b8129dad11d180b400c04fd430c8',
77             '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
78             '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79             (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
80             0x6ba7b8129dad11d180b400c04fd430c8L,
81             'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
82             0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
83            ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
84             '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
85             '6ba7b8149dad11d180b400c04fd430c8',
86             '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
87             '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88             (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
89             0x6ba7b8149dad11d180b400c04fd430c8L,
90             'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
91             0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
92            ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
93             '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
94             '7d4448409dc011d1b2455ffdce74fad2',
95             '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
96             '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
97             (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
98             0x7d4448409dc011d1b2455ffdce74fad2L,
99             'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
100             0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
101            ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
102             '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
103             'e902893a9d223c7ea7b8d6e313b71d9f',
104             '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
105             '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
106             (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
107             0xe902893a9d223c7ea7b8d6e313b71d9fL,
108             'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
109             0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
110            ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
111             '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
112             'eb4240266f544ef8a4d0bb658a1fc6cf',
113             '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
114             '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
115             (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
116             0xeb4240266f544ef8a4d0bb658a1fc6cfL,
117             'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
118             0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
119            ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
120             '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
121             'f81d4fae7dec11d0a76500a0c91e6bf6',
122             '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
123             '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
124             (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
125             0xf81d4fae7dec11d0a76500a0c91e6bf6L,
126             'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
127             0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
128            ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
129             '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
130             'fffefdfcfffefffefffefffefdfcfbfa',
131             '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
132             '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
133             (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
134             0xfffefdfcfffefffefffefffefdfcfbfaL,
135             'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
136             0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
137            ('ffffffff-ffff-ffff-ffff-ffffffffffff',
138             '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
139             'ffffffffffffffffffffffffffffffff',
140             '\xff'*16,
141             '\xff'*16,
142             (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
143             0xffffffffffffffffffffffffffffffffL,
144             'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
145             0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
146            ]:
147            equivalents = []
148            # Construct each UUID in several different ways.
149            for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
150                      uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
151                      uuid.UUID(fields=fields), uuid.UUID(int=integer),
152                      uuid.UUID(urn)]:
153                # Test all conversions and properties of the UUID object.
154                equal(str(u), string)
155                equal(int(u), integer)
156                equal(u.bytes, bytes)
157                equal(u.bytes_le, bytes_le)
158                equal(u.fields, fields)
159                equal(u.time_low, fields[0])
160                equal(u.time_mid, fields[1])
161                equal(u.time_hi_version, fields[2])
162                equal(u.clock_seq_hi_variant, fields[3])
163                equal(u.clock_seq_low, fields[4])
164                equal(u.node, fields[5])
165                equal(u.hex, hex)
166                equal(u.int, integer)
167                equal(u.urn, urn)
168                equal(u.time, time)
169                equal(u.clock_seq, clock_seq)
170                equal(u.variant, variant)
171                equal(u.version, version)
172                equivalents.append(u)
173
174            # Different construction methods should give the same UUID.
175            for u in equivalents:
176                for v in equivalents:
177                    equal(u, v)
178            ascending.append(u)
179
180        # Test comparison of UUIDs.
181        for i in range(len(ascending)):
182            for j in range(len(ascending)):
183                equal(cmp(i, j), cmp(ascending[i], ascending[j]))
184
185        # Test sorting of UUIDs (above list is in ascending order).
186        resorted = ascending[:]
187        resorted.reverse()
188        resorted.sort()
189        equal(ascending, resorted)
190
191    def test_exceptions(self):
192        badvalue = lambda f: self.assertRaises(ValueError, f)
193        badtype = lambda f: self.assertRaises(TypeError, f)
194
195        # Badly formed hex strings.
196        badvalue(lambda: uuid.UUID(''))
197        badvalue(lambda: uuid.UUID('abc'))
198        badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
199        badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
200        badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
201
202        # Badly formed bytes.
203        badvalue(lambda: uuid.UUID(bytes='abc'))
204        badvalue(lambda: uuid.UUID(bytes='\0'*15))
205        badvalue(lambda: uuid.UUID(bytes='\0'*17))
206
207        # Badly formed bytes_le.
208        badvalue(lambda: uuid.UUID(bytes_le='abc'))
209        badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
210        badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
211
212        # Badly formed fields.
213        badvalue(lambda: uuid.UUID(fields=(1,)))
214        badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
215        badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
216
217        # Field values out of range.
218        badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
219        badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
220        badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
221        badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
222        badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
223        badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
224        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
225        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
226        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
227        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
228        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
229        badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
230
231        # Version number out of range.
232        badvalue(lambda: uuid.UUID('00'*16, version=0))
233        badvalue(lambda: uuid.UUID('00'*16, version=6))
234
235        # Integer value out of range.
236        badvalue(lambda: uuid.UUID(int=-1))
237        badvalue(lambda: uuid.UUID(int=1<<128L))
238
239        # Must supply exactly one of hex, bytes, fields, int.
240        h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
241        uuid.UUID(h)
242        uuid.UUID(hex=h)
243        uuid.UUID(bytes=b)
244        uuid.UUID(bytes_le=b)
245        uuid.UUID(fields=f)
246        uuid.UUID(int=i)
247
248        # Wrong number of arguments (positional).
249        badtype(lambda: uuid.UUID())
250        badtype(lambda: uuid.UUID(h, b))
251        badtype(lambda: uuid.UUID(h, b, b))
252        badtype(lambda: uuid.UUID(h, b, b, f))
253        badtype(lambda: uuid.UUID(h, b, b, f, i))
254
255        # Duplicate arguments.
256        for hh in [[], [('hex', h)]]:
257            for bb in [[], [('bytes', b)]]:
258                for bble in [[], [('bytes_le', b)]]:
259                    for ii in [[], [('int', i)]]:
260                        for ff in [[], [('fields', f)]]:
261                            args = dict(hh + bb + bble + ii + ff)
262                            if len(args) != 0:
263                                badtype(lambda: uuid.UUID(h, **args))
264                            if len(args) != 1:
265                                badtype(lambda: uuid.UUID(**args))
266
267        # Immutability.
268        u = uuid.UUID(h)
269        badtype(lambda: setattr(u, 'hex', h))
270        badtype(lambda: setattr(u, 'bytes', b))
271        badtype(lambda: setattr(u, 'bytes_le', b))
272        badtype(lambda: setattr(u, 'fields', f))
273        badtype(lambda: setattr(u, 'int', i))
274        badtype(lambda: setattr(u, 'time_low', 0))
275        badtype(lambda: setattr(u, 'time_mid', 0))
276        badtype(lambda: setattr(u, 'time_hi_version', 0))
277        badtype(lambda: setattr(u, 'time_hi_version', 0))
278        badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
279        badtype(lambda: setattr(u, 'clock_seq_low', 0))
280        badtype(lambda: setattr(u, 'node', 0))
281
282    def test_getnode(self):
283        node1 = uuid.getnode()
284        self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
285
286        # Test it again to ensure consistency.
287        node2 = uuid.getnode()
288        self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
289
290    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
291    def test_uuid1(self):
292        equal = self.assertEqual
293
294        # Make sure uuid1() generates UUIDs that are actually version 1.
295        for u in [uuid.uuid1() for i in range(10)]:
296            equal(u.variant, uuid.RFC_4122)
297            equal(u.version, 1)
298
299        # Make sure the generated UUIDs are actually unique.
300        uuids = {}
301        for u in [uuid.uuid1() for i in range(1000)]:
302            uuids[u] = 1
303        equal(len(uuids.keys()), 1000)
304
305        # Make sure the supplied node ID appears in the UUID.
306        u = uuid.uuid1(0)
307        equal(u.node, 0)
308        u = uuid.uuid1(0x123456789abc)
309        equal(u.node, 0x123456789abc)
310        u = uuid.uuid1(0xffffffffffff)
311        equal(u.node, 0xffffffffffff)
312
313        # Make sure the supplied clock sequence appears in the UUID.
314        u = uuid.uuid1(0x123456789abc, 0)
315        equal(u.node, 0x123456789abc)
316        equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
317        u = uuid.uuid1(0x123456789abc, 0x1234)
318        equal(u.node, 0x123456789abc)
319        equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
320                         u.clock_seq_low, 0x1234)
321        u = uuid.uuid1(0x123456789abc, 0x3fff)
322        equal(u.node, 0x123456789abc)
323        equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
324                         u.clock_seq_low, 0x3fff)
325
326    def test_uuid3(self):
327        equal = self.assertEqual
328
329        # Test some known version-3 UUIDs.
330        for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
331                      '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
332                     (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
333                      '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
334                     (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
335                      'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
336                     (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
337                      '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
338                    ]:
339            equal(u.variant, uuid.RFC_4122)
340            equal(u.version, 3)
341            equal(u, uuid.UUID(v))
342            equal(str(u), v)
343
344    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
345    def test_uuid4(self):
346        equal = self.assertEqual
347
348        # Make sure uuid4() generates UUIDs that are actually version 4.
349        for u in [uuid.uuid4() for i in range(10)]:
350            equal(u.variant, uuid.RFC_4122)
351            equal(u.version, 4)
352
353        # Make sure the generated UUIDs are actually unique.
354        uuids = {}
355        for u in [uuid.uuid4() for i in range(1000)]:
356            uuids[u] = 1
357        equal(len(uuids.keys()), 1000)
358
359    def test_uuid5(self):
360        equal = self.assertEqual
361
362        # Test some known version-5 UUIDs.
363        for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
364                      '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
365                     (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
366                      '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
367                     (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
368                      '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
369                     (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
370                      'cc957dd1-a972-5349-98cd-874190002798'),
371                    ]:
372            equal(u.variant, uuid.RFC_4122)
373            equal(u.version, 5)
374            equal(u, uuid.UUID(v))
375            equal(str(u), v)
376
377    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
378    def testIssue8621(self):
379        # On at least some versions of OSX uuid.uuid4 generates
380        # the same sequence of UUIDs in the parent and any
381        # children started using fork.
382        fds = os.pipe()
383        pid = os.fork()
384        if pid == 0:
385            os.close(fds[0])
386            value = uuid.uuid4()
387            os.write(fds[1], value.hex)
388            os._exit(0)
389
390        else:
391            os.close(fds[1])
392            self.addCleanup(os.close, fds[0])
393            parent_value = uuid.uuid4().hex
394            os.waitpid(pid, 0)
395            child_value = os.read(fds[0], 100)
396
397            self.assertNotEqual(parent_value, child_value)
398
399
400class TestInternals(unittest.TestCase):
401    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
402    def test_find_mac(self):
403        data = '''\
404
405fake hwaddr
406cscotun0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
407eth0      Link encap:Ethernet  HWaddr 12:34:56:78:90:ab
408'''
409        def mock_popen(cmd):
410            return io.BytesIO(data)
411
412        path = os.environ.get("PATH", os.defpath).split(os.pathsep)
413        path.extend(('/sbin', '/usr/sbin'))
414        for dir in path:
415            executable = os.path.join(dir, 'ifconfig')
416            if (os.path.exists(executable) and
417                os.access(executable, os.F_OK | os.X_OK) and
418                not os.path.isdir(executable)):
419                break
420        else:
421            self.skipTest('requires ifconfig')
422
423        with test_support.swap_attr(os, 'popen', mock_popen):
424            mac = uuid._find_mac(
425                command='ifconfig',
426                args='',
427                hw_identifiers=['hwaddr'],
428                get_index=lambda x: x + 1,
429            )
430            self.assertEqual(mac, 0x1234567890ab)
431
432    def check_node(self, node, requires=None, network=False):
433        if requires and node is None:
434            self.skipTest('requires ' + requires)
435        hex = '%012x' % node
436        if test_support.verbose >= 2:
437            print hex + ' ',
438        if network:
439            # 47 bit will never be set in IEEE 802 addresses obtained
440            # from network cards.
441            self.assertFalse(node & 0x010000000000, hex)
442        self.assertTrue(0 < node < (1L << 48),
443                        "%s is not an RFC 4122 node ID" % hex)
444
445    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
446    def test_ifconfig_getnode(self):
447        node = uuid._ifconfig_getnode()
448        self.check_node(node, 'ifconfig', True)
449
450    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
451    def test_arp_getnode(self):
452        node = uuid._arp_getnode()
453        self.check_node(node, 'arp', True)
454
455    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
456    def test_lanscan_getnode(self):
457        node = uuid._lanscan_getnode()
458        self.check_node(node, 'lanscan', True)
459
460    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
461    def test_netstat_getnode(self):
462        node = uuid._netstat_getnode()
463        self.check_node(node, 'netstat', True)
464
465    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
466    def test_ipconfig_getnode(self):
467        node = uuid._ipconfig_getnode()
468        self.check_node(node, 'ipconfig', True)
469
470    @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
471    @unittest.skipUnless(importable('netbios'), 'requires netbios')
472    def test_netbios_getnode(self):
473        node = uuid._netbios_getnode()
474        self.check_node(node, network=True)
475
476    def test_random_getnode(self):
477        node = uuid._random_getnode()
478        # Least significant bit of first octet must be set.
479        self.assertTrue(node & 0x010000000000, '%012x' % node)
480        self.check_node(node)
481
482    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
483    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
484    def test_unixdll_getnode(self):
485        try: # Issues 1481, 3581: _uuid_generate_time() might be None.
486            node = uuid._unixdll_getnode()
487        except TypeError:
488            self.skipTest('requires uuid_generate_time')
489        self.check_node(node)
490
491    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
492    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
493    def test_windll_getnode(self):
494        node = uuid._windll_getnode()
495        self.check_node(node)
496
497
498def test_main():
499    test_support.run_unittest(TestUUID, TestInternals)
500
501if __name__ == '__main__':
502    test_main()
503