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