blob: 7af1d7aec797c5f950646ece1e8d98c434b320cd [file] [log] [blame]
Victor Stinnerb9d01992014-10-21 22:33:10 +02001import unittest.mock
Serhiy Storchaka56507c72013-11-26 22:47:16 +02002from test import support
Georg Brandl1d523e12009-12-19 18:23:28 +00003import builtins
Antoine Pitroua106aec2017-09-28 23:03:06 +02004import contextlib
Serhiy Storchaka56507c72013-11-26 22:47:16 +02005import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03006import os
Serhiy Storchakacce440f2014-01-10 15:06:59 +02007import shutil
Victor Stinnerb9d01992014-10-21 22:33:10 +02008import subprocess
Antoine Pitroua106aec2017-09-28 23:03:06 +02009
10py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
11c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
12
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013
14def importable(name):
15 try:
16 __import__(name)
17 return True
18 except:
19 return False
20
Antoine Pitroua106aec2017-09-28 23:03:06 +020021
22class BaseTestUUID:
23 uuid = None
24
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 def test_UUID(self):
26 equal = self.assertEqual
27 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000028 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029 time, clock_seq, variant, version) in [
30 ('00000000-0000-0000-0000-000000000000',
31 '{00000000-0000-0000-0000-000000000000}',
32 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000033 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
34 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035 (0, 0, 0, 0, 0, 0),
36 0,
37 'urn:uuid:00000000-0000-0000-0000-000000000000',
Antoine Pitroua106aec2017-09-28 23:03:06 +020038 0, 0, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
40 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
41 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000042 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
43 b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Guido van Rossume2a383d2007-01-15 16:59:06 +000044 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
45 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Antoine Pitroua106aec2017-09-28 23:03:06 +020047 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
49 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
50 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000051 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
52 b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Guido van Rossume2a383d2007-01-15 16:59:06 +000053 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
54 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Antoine Pitroua106aec2017-09-28 23:03:06 +020056 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 ('12345678-1234-5678-1234-567812345678',
58 '{12345678-1234-5678-1234-567812345678}',
59 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000060 b'\x12\x34\x56\x78'*4,
61 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
63 0x12345678123456781234567812345678,
64 'urn:uuid:12345678-1234-5678-1234-567812345678',
Antoine Pitroua106aec2017-09-28 23:03:06 +020065 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
67 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
68 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000069 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70 b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000071 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
72 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020074 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
76 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
77 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000078 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79 b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000080 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
81 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020083 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
85 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
86 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000087 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88 b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000089 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
90 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020092 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
94 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
95 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000096 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
97 b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000098 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
99 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200101 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
103 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
104 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000105 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
106 b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000107 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
108 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200110 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
112 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
113 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000114 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
115 b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000116 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
117 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000118 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200119 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
121 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
122 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000123 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
124 b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000125 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
126 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200128 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
130 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
131 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000132 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
133 b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000134 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
135 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000136 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200137 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000138 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
139 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
140 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000141 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
142 b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000143 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
144 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200146 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
148 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
149 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000150 b'\xff'*16,
151 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000152 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
153 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000154 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200155 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 ]:
157 equivalents = []
158 # Construct each UUID in several different ways.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200159 for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex),
160 self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le),
161 self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer),
162 self.uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000163 # Test all conversions and properties of the UUID object.
164 equal(str(u), string)
165 equal(int(u), integer)
166 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000167 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000168 equal(u.fields, fields)
169 equal(u.time_low, fields[0])
170 equal(u.time_mid, fields[1])
171 equal(u.time_hi_version, fields[2])
172 equal(u.clock_seq_hi_variant, fields[3])
173 equal(u.clock_seq_low, fields[4])
174 equal(u.node, fields[5])
175 equal(u.hex, hex)
176 equal(u.int, integer)
177 equal(u.urn, urn)
178 equal(u.time, time)
179 equal(u.clock_seq, clock_seq)
180 equal(u.variant, variant)
181 equal(u.version, version)
182 equivalents.append(u)
183
184 # Different construction methods should give the same UUID.
185 for u in equivalents:
186 for v in equivalents:
187 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000188
189 # Bug 7380: "bytes" and "bytes_le" should give the same type.
190 equal(type(u.bytes), builtins.bytes)
191 equal(type(u.bytes_le), builtins.bytes)
192
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000193 ascending.append(u)
194
195 # Test comparison of UUIDs.
196 for i in range(len(ascending)):
197 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000198 equal(i < j, ascending[i] < ascending[j])
199 equal(i <= j, ascending[i] <= ascending[j])
200 equal(i == j, ascending[i] == ascending[j])
201 equal(i > j, ascending[i] > ascending[j])
202 equal(i >= j, ascending[i] >= ascending[j])
203 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204
205 # Test sorting of UUIDs (above list is in ascending order).
206 resorted = ascending[:]
207 resorted.reverse()
208 resorted.sort()
209 equal(ascending, resorted)
210
211 def test_exceptions(self):
212 badvalue = lambda f: self.assertRaises(ValueError, f)
213 badtype = lambda f: self.assertRaises(TypeError, f)
214
215 # Badly formed hex strings.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200216 badvalue(lambda: self.uuid.UUID(''))
217 badvalue(lambda: self.uuid.UUID('abc'))
218 badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
219 badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
220 badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221
222 # Badly formed bytes.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200223 badvalue(lambda: self.uuid.UUID(bytes='abc'))
224 badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
225 badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000227 # Badly formed bytes_le.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200228 badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
229 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
230 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000231
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000232 # Badly formed fields.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200233 badvalue(lambda: self.uuid.UUID(fields=(1,)))
234 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5)))
235 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236
237 # Field values out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200238 badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
239 badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
240 badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
241 badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
242 badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
243 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
244 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
245 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
246 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
247 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
248 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
249 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250
251 # Version number out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200252 badvalue(lambda: self.uuid.UUID('00'*16, version=0))
253 badvalue(lambda: self.uuid.UUID('00'*16, version=6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254
255 # Integer value out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200256 badvalue(lambda: self.uuid.UUID(int=-1))
257 badvalue(lambda: self.uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258
259 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000260 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Antoine Pitroua106aec2017-09-28 23:03:06 +0200261 self.uuid.UUID(h)
262 self.uuid.UUID(hex=h)
263 self.uuid.UUID(bytes=b)
264 self.uuid.UUID(bytes_le=b)
265 self.uuid.UUID(fields=f)
266 self.uuid.UUID(int=i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267
268 # Wrong number of arguments (positional).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200269 badtype(lambda: self.uuid.UUID())
270 badtype(lambda: self.uuid.UUID(h, b))
271 badtype(lambda: self.uuid.UUID(h, b, b))
272 badtype(lambda: self.uuid.UUID(h, b, b, f))
273 badtype(lambda: self.uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000274
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000275 # Duplicate arguments.
276 for hh in [[], [('hex', h)]]:
277 for bb in [[], [('bytes', b)]]:
278 for bble in [[], [('bytes_le', b)]]:
279 for ii in [[], [('int', i)]]:
280 for ff in [[], [('fields', f)]]:
281 args = dict(hh + bb + bble + ii + ff)
282 if len(args) != 0:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200283 badtype(lambda: self.uuid.UUID(h, **args))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000284 if len(args) != 1:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200285 badtype(lambda: self.uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286
287 # Immutability.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200288 u = self.uuid.UUID(h)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289 badtype(lambda: setattr(u, 'hex', h))
290 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000291 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292 badtype(lambda: setattr(u, 'fields', f))
293 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000294 badtype(lambda: setattr(u, 'time_low', 0))
295 badtype(lambda: setattr(u, 'time_mid', 0))
296 badtype(lambda: setattr(u, 'time_hi_version', 0))
297 badtype(lambda: setattr(u, 'time_hi_version', 0))
298 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
299 badtype(lambda: setattr(u, 'clock_seq_low', 0))
300 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301
Berker Peksag6b5e4a82016-12-31 20:08:16 +0300302 # Comparison with a non-UUID object
303 badtype(lambda: u < object())
304 badtype(lambda: u > object())
305
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 def test_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200307 node1 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200308 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309
310 # Test it again to ensure consistency.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200311 node2 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200312 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200313
Bo Bayles6b273f72018-01-23 19:11:44 -0600314 # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
315 # need not necessarily be 48 bits (e.g., EUI-64).
316 def test_uuid1_eui64(self):
317 # Confirm that uuid.getnode ignores hardware addresses larger than 48
318 # bits. Mock out each platform's *_getnode helper functions to return
319 # something just larger than 48 bits to test. This will cause
320 # uuid.getnode to fall back on uuid._random_getnode, which will
321 # generate a valid value.
322 too_large_getter = lambda: 1 << 48
323 with unittest.mock.patch.multiple(
324 self.uuid,
325 _node=None, # Ignore any cached node value.
326 _NODE_GETTERS_WIN32=[too_large_getter],
327 _NODE_GETTERS_UNIX=[too_large_getter],
328 ):
329 node = self.uuid.getnode()
330 self.assertTrue(0 < node < (1 << 48), '%012x' % node)
331
332 # Confirm that uuid1 can use the generated node, i.e., the that
333 # uuid.getnode fell back on uuid._random_getnode() rather than using
334 # the value from too_large_getter above.
335 try:
336 self.uuid.uuid1(node=node)
337 except ValueError as e:
338 self.fail('uuid1 was given an invalid node ID')
339
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000340 def test_uuid1(self):
341 equal = self.assertEqual
342
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000343 # Make sure uuid1() generates UUIDs that are actually version 1.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200344 for u in [self.uuid.uuid1() for i in range(10)]:
345 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000346 equal(u.version, 1)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200347 self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe,
348 self.uuid.SafeUUID.unsafe,
349 self.uuid.SafeUUID.unknown})
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000350
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000351 # Make sure the generated UUIDs are actually unique.
352 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200353 for u in [self.uuid.uuid1() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000354 uuids[u] = 1
355 equal(len(uuids.keys()), 1000)
356
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000357 # Make sure the supplied node ID appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200358 u = self.uuid.uuid1(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000359 equal(u.node, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200360 u = self.uuid.uuid1(0x123456789abc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361 equal(u.node, 0x123456789abc)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200362 u = self.uuid.uuid1(0xffffffffffff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000363 equal(u.node, 0xffffffffffff)
364
365 # Make sure the supplied clock sequence appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200366 u = self.uuid.uuid1(0x123456789abc, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 equal(u.node, 0x123456789abc)
368 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200369 u = self.uuid.uuid1(0x123456789abc, 0x1234)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000370 equal(u.node, 0x123456789abc)
371 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
372 u.clock_seq_low, 0x1234)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200373 u = self.uuid.uuid1(0x123456789abc, 0x3fff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000374 equal(u.node, 0x123456789abc)
375 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
376 u.clock_seq_low, 0x3fff)
377
Antoine Pitroua106aec2017-09-28 23:03:06 +0200378 # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns
379 # self.uuid.SafeUUID.unknown
Victor Stinnerc209b702017-04-19 13:01:03 +0200380 @support.requires_mac_ver(10, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200381 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500382 def test_uuid1_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200383 if not self.uuid._has_uuid_generate_time_safe:
384 self.skipTest('requires uuid_generate_time_safe(3)')
385
386 u = self.uuid.uuid1()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500387 # uuid_generate_time_safe() may return 0 or -1 but what it returns is
388 # dependent on the underlying platform support. At least it cannot be
389 # unknown (unless I suppose the platform is buggy).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200390 self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500391
Antoine Pitroua106aec2017-09-28 23:03:06 +0200392 @contextlib.contextmanager
393 def mock_generate_time_safe(self, safe_value):
394 """
395 Mock uuid._generate_time_safe() to return a given *safe_value*.
396 """
397 if os.name != 'posix':
398 self.skipTest('POSIX-only test')
399 self.uuid._load_system_functions()
400 f = self.uuid._generate_time_safe
401 if f is None:
402 self.skipTest('need uuid._generate_time_safe')
403 with unittest.mock.patch.object(self.uuid, '_generate_time_safe',
404 lambda: (f()[0], safe_value)):
405 yield
406
407 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500408 def test_uuid1_unknown(self):
409 # Even if the platform has uuid_generate_time_safe(), let's mock it to
410 # be uuid_generate_time() and ensure the safety is unknown.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200411 with self.mock_generate_time_safe(None):
412 u = self.uuid.uuid1()
413 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500414
Antoine Pitroua106aec2017-09-28 23:03:06 +0200415 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500416 def test_uuid1_is_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200417 with self.mock_generate_time_safe(0):
418 u = self.uuid.uuid1()
419 self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500420
Antoine Pitroua106aec2017-09-28 23:03:06 +0200421 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500422 def test_uuid1_is_unsafe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200423 with self.mock_generate_time_safe(-1):
424 u = self.uuid.uuid1()
425 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500426
Antoine Pitroua106aec2017-09-28 23:03:06 +0200427 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500428 def test_uuid1_bogus_return_value(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200429 with self.mock_generate_time_safe(3):
430 u = self.uuid.uuid1()
431 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500432
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000433 def test_uuid3(self):
434 equal = self.assertEqual
435
436 # Test some known version-3 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200437 for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200439 (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200441 (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200443 (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
445 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200446 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447 equal(u.version, 3)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200448 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 equal(str(u), v)
450
451 def test_uuid4(self):
452 equal = self.assertEqual
453
454 # Make sure uuid4() generates UUIDs that are actually version 4.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200455 for u in [self.uuid.uuid4() for i in range(10)]:
456 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457 equal(u.version, 4)
458
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000459 # Make sure the generated UUIDs are actually unique.
460 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200461 for u in [self.uuid.uuid4() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000462 uuids[u] = 1
463 equal(len(uuids.keys()), 1000)
464
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465 def test_uuid5(self):
466 equal = self.assertEqual
467
468 # Test some known version-5 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200469 for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000470 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200471 (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000472 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200473 (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000474 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200475 (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000476 'cc957dd1-a972-5349-98cd-874190002798'),
477 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200478 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000479 equal(u.version, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200480 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000481 equal(str(u), v)
482
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300483 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000484 def testIssue8621(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200485 # On at least some versions of OSX self.uuid.uuid4 generates
Ronald Oussorenac764d32010-05-05 15:32:33 +0000486 # the same sequence of UUIDs in the parent and any
487 # children started using fork.
488 fds = os.pipe()
489 pid = os.fork()
490 if pid == 0:
491 os.close(fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200492 value = self.uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000493 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000494 os._exit(0)
495
496 else:
497 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100498 self.addCleanup(os.close, fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200499 parent_value = self.uuid.uuid4().hex
Ronald Oussorenac764d32010-05-05 15:32:33 +0000500 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000501 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000502
503 self.assertNotEqual(parent_value, child_value)
504
505
Antoine Pitroua106aec2017-09-28 23:03:06 +0200506class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
507 uuid = py_uuid
508
509@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
510class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
511 uuid = c_uuid
512
513
514class BaseTestInternals:
515 uuid = None
516
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200517 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
518 def test_find_mac(self):
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200519 data = '''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200520fake hwaddr
521cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
522eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
523'''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200524
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200525 popen = unittest.mock.MagicMock()
526 popen.stdout = io.BytesIO(data.encode())
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200527
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200528 with unittest.mock.patch.object(shutil, 'which',
529 return_value='/sbin/ifconfig'):
530 with unittest.mock.patch.object(subprocess, 'Popen',
531 return_value=popen):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200532 mac = self.uuid._find_mac(
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200533 command='ifconfig',
534 args='',
535 hw_identifiers=[b'hwaddr'],
536 get_index=lambda x: x + 1,
537 )
538
539 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200540
Barry Warsaw23df2d12017-11-28 17:26:04 -0500541 def check_node(self, node, requires=None):
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200542 if requires and node is None:
543 self.skipTest('requires ' + requires)
544 hex = '%012x' % node
545 if support.verbose >= 2:
546 print(hex, end=' ')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200547 self.assertTrue(0 < node < (1 << 48),
548 "%s is not an RFC 4122 node ID" % hex)
549
550 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
551 def test_ifconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200552 node = self.uuid._ifconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500553 self.check_node(node, 'ifconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200554
555 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200556 def test_ip_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200557 node = self.uuid._ip_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500558 self.check_node(node, 'ip')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200559
560 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200561 def test_arp_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200562 node = self.uuid._arp_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500563 self.check_node(node, 'arp')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200564
565 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
566 def test_lanscan_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200567 node = self.uuid._lanscan_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500568 self.check_node(node, 'lanscan')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200569
570 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
571 def test_netstat_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200572 node = self.uuid._netstat_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500573 self.check_node(node, 'netstat')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200574
575 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
576 def test_ipconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200577 node = self.uuid._ipconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500578 self.check_node(node, 'ipconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200579
580 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
581 @unittest.skipUnless(importable('netbios'), 'requires netbios')
582 def test_netbios_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200583 node = self.uuid._netbios_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500584 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200585
586 def test_random_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200587 node = self.uuid._random_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500588 # The multicast bit, i.e. the least significant bit of first octet,
589 # must be set for randomly generated MAC addresses. See RFC 4122,
590 # $4.1.6.
591 self.assertTrue(node & (1 << 40), '%012x' % node)
Victor Stinnerc9409f72017-11-28 00:30:21 +0100592 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200593
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200594 node2 = self.uuid._random_getnode()
595 self.assertNotEqual(node2, node, '%012x' % node)
596
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200597 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Antoine Pitroua106aec2017-09-28 23:03:06 +0200598 def test_unix_getnode(self):
599 if not importable('_uuid') and not importable('ctypes'):
600 self.skipTest("neither _uuid extension nor ctypes available")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200601 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200602 node = self.uuid._unix_getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200603 except TypeError:
604 self.skipTest('requires uuid_generate_time')
Victor Stinnerc9409f72017-11-28 00:30:21 +0100605 self.check_node(node, 'unix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200606
607 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
608 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
609 def test_windll_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200610 node = self.uuid._windll_getnode()
Victor Stinnerc9409f72017-11-28 00:30:21 +0100611 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200612
613
Antoine Pitroua106aec2017-09-28 23:03:06 +0200614class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase):
615 uuid = py_uuid
616
617@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
618class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase):
619 uuid = c_uuid
620
621
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000622if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300623 unittest.main()