Add NEWS entry as per RDM's suggestion (the bug was actually present
[python.git] / Lib / test / test_uuid.py
blobdc28b1e2fc0227f8046a9cc683e62dd4c956df6d
1 from unittest import TestCase
2 from test import test_support
3 import uuid
5 def importable(name):
6 try:
7 __import__(name)
8 return True
9 except:
10 return False
12 class TestUUID(TestCase):
13 last_node = None
14 source2node = {}
16 def test_UUID(self):
17 equal = self.assertEqual
18 ascending = []
19 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
20 time, clock_seq, variant, version) in [
21 ('00000000-0000-0000-0000-000000000000',
22 '{00000000-0000-0000-0000-000000000000}',
23 '00000000000000000000000000000000',
24 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
25 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
26 (0, 0, 0, 0, 0, 0),
28 'urn:uuid:00000000-0000-0000-0000-000000000000',
29 0, 0, uuid.RESERVED_NCS, None),
30 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
31 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
32 '000102030405060708090a0b0c0d0e0f',
33 '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
34 '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
35 (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
36 0x000102030405060708090a0b0c0d0e0fL,
37 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
38 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
39 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
40 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
41 '02d9e6d59467382e8f9b9300a64ac3cd',
42 '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
43 '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
44 (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
45 0x02d9e6d59467382e8f9b9300a64ac3cdL,
46 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
47 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
48 ('12345678-1234-5678-1234-567812345678',
49 '{12345678-1234-5678-1234-567812345678}',
50 '12345678123456781234567812345678',
51 '\x12\x34\x56\x78'*4,
52 '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
53 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
54 0x12345678123456781234567812345678,
55 'urn:uuid:12345678-1234-5678-1234-567812345678',
56 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
57 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
58 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
59 '6ba7b8109dad11d180b400c04fd430c8',
60 '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
61 '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
62 (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
63 0x6ba7b8109dad11d180b400c04fd430c8L,
64 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
65 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
66 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
67 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
68 '6ba7b8119dad11d180b400c04fd430c8',
69 '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70 '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
71 (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
72 0x6ba7b8119dad11d180b400c04fd430c8L,
73 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
74 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
75 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
76 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
77 '6ba7b8129dad11d180b400c04fd430c8',
78 '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79 '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
80 (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
81 0x6ba7b8129dad11d180b400c04fd430c8L,
82 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
83 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
84 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
85 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
86 '6ba7b8149dad11d180b400c04fd430c8',
87 '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88 '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
89 (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
90 0x6ba7b8149dad11d180b400c04fd430c8L,
91 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
92 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
93 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
94 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
95 '7d4448409dc011d1b2455ffdce74fad2',
96 '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
97 '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
98 (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
99 0x7d4448409dc011d1b2455ffdce74fad2L,
100 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
101 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
102 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
103 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
104 'e902893a9d223c7ea7b8d6e313b71d9f',
105 '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
106 '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
107 (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
108 0xe902893a9d223c7ea7b8d6e313b71d9fL,
109 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
110 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
111 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
112 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
113 'eb4240266f544ef8a4d0bb658a1fc6cf',
114 '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
115 '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
116 (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
117 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
118 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
119 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
120 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
121 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
122 'f81d4fae7dec11d0a76500a0c91e6bf6',
123 '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
124 '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
125 (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
126 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
127 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
128 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
129 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
130 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
131 'fffefdfcfffefffefffefffefdfcfbfa',
132 '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
133 '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
134 (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
135 0xfffefdfcfffefffefffefffefdfcfbfaL,
136 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
137 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
138 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
139 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
140 'ffffffffffffffffffffffffffffffff',
141 '\xff'*16,
142 '\xff'*16,
143 (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
144 0xffffffffffffffffffffffffffffffffL,
145 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
146 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
148 equivalents = []
149 # Construct each UUID in several different ways.
150 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
151 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
152 uuid.UUID(fields=fields), uuid.UUID(int=integer),
153 uuid.UUID(urn)]:
154 # Test all conversions and properties of the UUID object.
155 equal(str(u), string)
156 equal(int(u), integer)
157 equal(u.bytes, bytes)
158 equal(u.bytes_le, bytes_le)
159 equal(u.fields, fields)
160 equal(u.time_low, fields[0])
161 equal(u.time_mid, fields[1])
162 equal(u.time_hi_version, fields[2])
163 equal(u.clock_seq_hi_variant, fields[3])
164 equal(u.clock_seq_low, fields[4])
165 equal(u.node, fields[5])
166 equal(u.hex, hex)
167 equal(u.int, integer)
168 equal(u.urn, urn)
169 equal(u.time, time)
170 equal(u.clock_seq, clock_seq)
171 equal(u.variant, variant)
172 equal(u.version, version)
173 equivalents.append(u)
175 # Different construction methods should give the same UUID.
176 for u in equivalents:
177 for v in equivalents:
178 equal(u, v)
179 ascending.append(u)
181 # Test comparison of UUIDs.
182 for i in range(len(ascending)):
183 for j in range(len(ascending)):
184 equal(cmp(i, j), cmp(ascending[i], ascending[j]))
186 # Test sorting of UUIDs (above list is in ascending order).
187 resorted = ascending[:]
188 resorted.reverse()
189 resorted.sort()
190 equal(ascending, resorted)
192 def test_exceptions(self):
193 badvalue = lambda f: self.assertRaises(ValueError, f)
194 badtype = lambda f: self.assertRaises(TypeError, f)
196 # Badly formed hex strings.
197 badvalue(lambda: uuid.UUID(''))
198 badvalue(lambda: uuid.UUID('abc'))
199 badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
200 badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
201 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
203 # Badly formed bytes.
204 badvalue(lambda: uuid.UUID(bytes='abc'))
205 badvalue(lambda: uuid.UUID(bytes='\0'*15))
206 badvalue(lambda: uuid.UUID(bytes='\0'*17))
208 # Badly formed bytes_le.
209 badvalue(lambda: uuid.UUID(bytes_le='abc'))
210 badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
211 badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
213 # Badly formed fields.
214 badvalue(lambda: uuid.UUID(fields=(1,)))
215 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
216 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
218 # Field values out of range.
219 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
220 badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
221 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
222 badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
223 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
224 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
225 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
226 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
227 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
228 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
229 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
230 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
232 # Version number out of range.
233 badvalue(lambda: uuid.UUID('00'*16, version=0))
234 badvalue(lambda: uuid.UUID('00'*16, version=6))
236 # Integer value out of range.
237 badvalue(lambda: uuid.UUID(int=-1))
238 badvalue(lambda: uuid.UUID(int=1<<128L))
240 # Must supply exactly one of hex, bytes, fields, int.
241 h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
242 uuid.UUID(h)
243 uuid.UUID(hex=h)
244 uuid.UUID(bytes=b)
245 uuid.UUID(bytes_le=b)
246 uuid.UUID(fields=f)
247 uuid.UUID(int=i)
249 # Wrong number of arguments (positional).
250 badtype(lambda: uuid.UUID())
251 badtype(lambda: uuid.UUID(h, b))
252 badtype(lambda: uuid.UUID(h, b, b))
253 badtype(lambda: uuid.UUID(h, b, b, f))
254 badtype(lambda: uuid.UUID(h, b, b, f, i))
256 # Duplicate arguments.
257 for hh in [[], [('hex', h)]]:
258 for bb in [[], [('bytes', b)]]:
259 for bble in [[], [('bytes_le', b)]]:
260 for ii in [[], [('int', i)]]:
261 for ff in [[], [('fields', f)]]:
262 args = dict(hh + bb + bble + ii + ff)
263 if len(args) != 0:
264 badtype(lambda: uuid.UUID(h, **args))
265 if len(args) != 1:
266 badtype(lambda: uuid.UUID(**args))
268 # Immutability.
269 u = uuid.UUID(h)
270 badtype(lambda: setattr(u, 'hex', h))
271 badtype(lambda: setattr(u, 'bytes', b))
272 badtype(lambda: setattr(u, 'bytes_le', b))
273 badtype(lambda: setattr(u, 'fields', f))
274 badtype(lambda: setattr(u, 'int', i))
275 badtype(lambda: setattr(u, 'time_low', 0))
276 badtype(lambda: setattr(u, 'time_mid', 0))
277 badtype(lambda: setattr(u, 'time_hi_version', 0))
278 badtype(lambda: setattr(u, 'time_hi_version', 0))
279 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
280 badtype(lambda: setattr(u, 'clock_seq_low', 0))
281 badtype(lambda: setattr(u, 'node', 0))
283 def check_node(self, node, source):
284 individual_group_bit = (node >> 40L) & 1
285 universal_local_bit = (node >> 40L) & 2
286 message = "%012x doesn't look like a real MAC address" % node
287 self.assertEqual(individual_group_bit, 0, message)
288 self.assertEqual(universal_local_bit, 0, message)
289 self.assertNotEqual(node, 0, message)
290 self.assertNotEqual(node, 0xffffffffffffL, message)
291 self.assertTrue(0 <= node, message)
292 self.assertTrue(node < (1L << 48), message)
294 TestUUID.source2node[source] = node
295 if TestUUID.last_node:
296 if TestUUID.last_node != node:
297 msg = "different sources disagree on node:\n"
298 for s, n in TestUUID.source2node.iteritems():
299 msg += " from source %r, node was %012x\n" % (s, n)
300 # There's actually no reason to expect the MAC addresses
301 # to agree across various methods -- e.g., a box may have
302 # multiple network interfaces, and different ways of getting
303 # a MAC address may favor different HW.
304 ##self.fail(msg)
305 else:
306 TestUUID.last_node = node
308 def test_ifconfig_getnode(self):
309 import sys
310 print >>sys.__stdout__, \
311 """ WARNING: uuid._ifconfig_getnode is unreliable on many platforms.
312 It is disabled until the code and/or test can be fixed properly."""
313 return
315 import os
316 if os.name == 'posix':
317 node = uuid._ifconfig_getnode()
318 if node is not None:
319 self.check_node(node, 'ifconfig')
321 def test_ipconfig_getnode(self):
322 import os
323 if os.name == 'nt':
324 node = uuid._ipconfig_getnode()
325 if node is not None:
326 self.check_node(node, 'ipconfig')
328 def test_netbios_getnode(self):
329 if importable('win32wnet') and importable('netbios'):
330 self.check_node(uuid._netbios_getnode(), 'netbios')
332 def test_random_getnode(self):
333 node = uuid._random_getnode()
334 self.assertTrue(0 <= node)
335 self.assertTrue(node < (1L <<48))
337 def test_unixdll_getnode(self):
338 import sys
339 print >>sys.__stdout__, \
340 """ WARNING: uuid._unixdll_getnode is unreliable on many platforms.
341 It is disabled until the code and/or test can be fixed properly."""
342 return
344 import os
345 if importable('ctypes') and os.name == 'posix':
346 self.check_node(uuid._unixdll_getnode(), 'unixdll')
348 def test_windll_getnode(self):
349 import os
350 if importable('ctypes') and os.name == 'nt':
351 self.check_node(uuid._windll_getnode(), 'windll')
353 def test_getnode(self):
354 import sys
355 print >>sys.__stdout__, \
356 """ WARNING: uuid.getnode is unreliable on many platforms.
357 It is disabled until the code and/or test can be fixed properly."""
358 return
360 node1 = uuid.getnode()
361 self.check_node(node1, "getnode1")
363 # Test it again to ensure consistency.
364 node2 = uuid.getnode()
365 self.check_node(node2, "getnode2")
367 self.assertEqual(node1, node2)
369 def test_uuid1(self):
370 # uuid1 requires ctypes.
371 try:
372 import ctypes
373 except ImportError:
374 return
376 equal = self.assertEqual
378 # Make sure uuid1() generates UUIDs that are actually version 1.
379 for u in [uuid.uuid1() for i in range(10)]:
380 equal(u.variant, uuid.RFC_4122)
381 equal(u.version, 1)
383 # Make sure the generated UUIDs are actually unique.
384 uuids = {}
385 for u in [uuid.uuid1() for i in range(1000)]:
386 uuids[u] = 1
387 equal(len(uuids.keys()), 1000)
389 # Make sure the supplied node ID appears in the UUID.
390 u = uuid.uuid1(0)
391 equal(u.node, 0)
392 u = uuid.uuid1(0x123456789abc)
393 equal(u.node, 0x123456789abc)
394 u = uuid.uuid1(0xffffffffffff)
395 equal(u.node, 0xffffffffffff)
397 # Make sure the supplied clock sequence appears in the UUID.
398 u = uuid.uuid1(0x123456789abc, 0)
399 equal(u.node, 0x123456789abc)
400 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
401 u = uuid.uuid1(0x123456789abc, 0x1234)
402 equal(u.node, 0x123456789abc)
403 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
404 u.clock_seq_low, 0x1234)
405 u = uuid.uuid1(0x123456789abc, 0x3fff)
406 equal(u.node, 0x123456789abc)
407 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
408 u.clock_seq_low, 0x3fff)
410 def test_uuid3(self):
411 equal = self.assertEqual
413 # Test some known version-3 UUIDs.
414 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
415 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
416 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
417 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
418 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
419 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
420 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
421 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
423 equal(u.variant, uuid.RFC_4122)
424 equal(u.version, 3)
425 equal(u, uuid.UUID(v))
426 equal(str(u), v)
428 def test_uuid4(self):
429 # uuid4 requires ctypes.
430 try:
431 import ctypes
432 except ImportError:
433 return
435 equal = self.assertEqual
437 # Make sure uuid4() generates UUIDs that are actually version 4.
438 for u in [uuid.uuid4() for i in range(10)]:
439 equal(u.variant, uuid.RFC_4122)
440 equal(u.version, 4)
442 # Make sure the generated UUIDs are actually unique.
443 uuids = {}
444 for u in [uuid.uuid4() for i in range(1000)]:
445 uuids[u] = 1
446 equal(len(uuids.keys()), 1000)
448 def test_uuid5(self):
449 equal = self.assertEqual
451 # Test some known version-5 UUIDs.
452 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
453 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
454 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
455 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
456 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
457 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
458 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
459 'cc957dd1-a972-5349-98cd-874190002798'),
461 equal(u.variant, uuid.RFC_4122)
462 equal(u.version, 5)
463 equal(u, uuid.UUID(v))
464 equal(str(u), v)
467 def test_main():
468 test_support.run_unittest(TestUUID)
470 if __name__ == '__main__':
471 test_main()