blob: 115e66c2160a4501f97afe3e8cdd3a292a8df5d2 [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
Serhiy Storchaka56507c72013-11-26 22:47:16 +02004import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03005import os
Serhiy Storchakacce440f2014-01-10 15:06:59 +02006import shutil
Victor Stinnerb9d01992014-10-21 22:33:10 +02007import subprocess
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008import uuid
9
10def importable(name):
11 try:
12 __import__(name)
13 return True
14 except:
15 return False
16
Serhiy Storchaka7d15b542013-05-31 22:31:02 +030017class TestUUID(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018 last_node = None
19 source2node = {}
20
21 def test_UUID(self):
22 equal = self.assertEqual
23 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000024 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 time, clock_seq, variant, version) in [
26 ('00000000-0000-0000-0000-000000000000',
27 '{00000000-0000-0000-0000-000000000000}',
28 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000029 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
30 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031 (0, 0, 0, 0, 0, 0),
32 0,
33 'urn:uuid:00000000-0000-0000-0000-000000000000',
34 0, 0, uuid.RESERVED_NCS, None),
35 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
36 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
37 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000038 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
39 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 +000040 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
41 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Guido van Rossume2a383d2007-01-15 16:59:06 +000043 0x607040500010203, 0x809, uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
45 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
46 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000047 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
48 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 +000049 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
50 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000051 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Guido van Rossume2a383d2007-01-15 16:59:06 +000052 0x82e946702d9e6d5, 0xf9b, uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053 ('12345678-1234-5678-1234-567812345678',
54 '{12345678-1234-5678-1234-567812345678}',
55 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000056 b'\x12\x34\x56\x78'*4,
57 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
59 0x12345678123456781234567812345678,
60 'urn:uuid:12345678-1234-5678-1234-567812345678',
Guido van Rossume2a383d2007-01-15 16:59:06 +000061 0x678123412345678, 0x1234, uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
63 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
64 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000065 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
66 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 +000067 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
68 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000070 0x1d19dad6ba7b810, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
72 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
73 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000074 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
75 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 +000076 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
77 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000079 0x1d19dad6ba7b811, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
81 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
82 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000083 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
84 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 +000085 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
86 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000088 0x1d19dad6ba7b812, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
90 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
91 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000092 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
93 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 +000094 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
95 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000097 0x1d19dad6ba7b814, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
99 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
100 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000101 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
102 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 +0000103 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
104 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000106 0x1d19dc07d444840, 0x3245, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000107 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
108 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
109 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000110 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
111 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 +0000112 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
113 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000115 0xc7e9d22e902893a, 0x27b8, uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000116 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
117 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
118 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000119 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
120 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 +0000121 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
122 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000124 0xef86f54eb424026, 0x24d0, uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000125 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
126 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
127 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000128 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
129 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 +0000130 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
131 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000132 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000133 0x1d07decf81d4fae, 0x2765, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
135 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
136 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000137 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
138 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 +0000139 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
140 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000142 0xffefffefffefdfc, 0x3ffe, uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
144 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
145 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000146 b'\xff'*16,
147 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000148 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
149 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000151 0xfffffffffffffff, 0x3fff, uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152 ]:
153 equivalents = []
154 # Construct each UUID in several different ways.
155 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000156 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
157 uuid.UUID(fields=fields), uuid.UUID(int=integer),
158 uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000159 # Test all conversions and properties of the UUID object.
160 equal(str(u), string)
161 equal(int(u), integer)
162 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000163 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000164 equal(u.fields, fields)
165 equal(u.time_low, fields[0])
166 equal(u.time_mid, fields[1])
167 equal(u.time_hi_version, fields[2])
168 equal(u.clock_seq_hi_variant, fields[3])
169 equal(u.clock_seq_low, fields[4])
170 equal(u.node, fields[5])
171 equal(u.hex, hex)
172 equal(u.int, integer)
173 equal(u.urn, urn)
174 equal(u.time, time)
175 equal(u.clock_seq, clock_seq)
176 equal(u.variant, variant)
177 equal(u.version, version)
178 equivalents.append(u)
179
180 # Different construction methods should give the same UUID.
181 for u in equivalents:
182 for v in equivalents:
183 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000184
185 # Bug 7380: "bytes" and "bytes_le" should give the same type.
186 equal(type(u.bytes), builtins.bytes)
187 equal(type(u.bytes_le), builtins.bytes)
188
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189 ascending.append(u)
190
191 # Test comparison of UUIDs.
192 for i in range(len(ascending)):
193 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000194 equal(i < j, ascending[i] < ascending[j])
195 equal(i <= j, ascending[i] <= ascending[j])
196 equal(i == j, ascending[i] == ascending[j])
197 equal(i > j, ascending[i] > ascending[j])
198 equal(i >= j, ascending[i] >= ascending[j])
199 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000200
201 # Test sorting of UUIDs (above list is in ascending order).
202 resorted = ascending[:]
203 resorted.reverse()
204 resorted.sort()
205 equal(ascending, resorted)
206
207 def test_exceptions(self):
208 badvalue = lambda f: self.assertRaises(ValueError, f)
209 badtype = lambda f: self.assertRaises(TypeError, f)
210
211 # Badly formed hex strings.
212 badvalue(lambda: uuid.UUID(''))
213 badvalue(lambda: uuid.UUID('abc'))
214 badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
215 badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
216 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
217
218 # Badly formed bytes.
219 badvalue(lambda: uuid.UUID(bytes='abc'))
220 badvalue(lambda: uuid.UUID(bytes='\0'*15))
221 badvalue(lambda: uuid.UUID(bytes='\0'*17))
222
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000223 # Badly formed bytes_le.
224 badvalue(lambda: uuid.UUID(bytes_le='abc'))
225 badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
226 badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
227
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228 # Badly formed fields.
229 badvalue(lambda: uuid.UUID(fields=(1,)))
230 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
231 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
232
233 # Field values out of range.
234 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000235 badvalue(lambda: uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000237 badvalue(lambda: uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000239 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000241 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000243 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000244 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000245 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246
247 # Version number out of range.
248 badvalue(lambda: uuid.UUID('00'*16, version=0))
249 badvalue(lambda: uuid.UUID('00'*16, version=6))
250
251 # Integer value out of range.
252 badvalue(lambda: uuid.UUID(int=-1))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000253 badvalue(lambda: uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254
255 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000256 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257 uuid.UUID(h)
258 uuid.UUID(hex=h)
259 uuid.UUID(bytes=b)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000260 uuid.UUID(bytes_le=b)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261 uuid.UUID(fields=f)
262 uuid.UUID(int=i)
263
264 # Wrong number of arguments (positional).
265 badtype(lambda: uuid.UUID())
266 badtype(lambda: uuid.UUID(h, b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000267 badtype(lambda: uuid.UUID(h, b, b))
268 badtype(lambda: uuid.UUID(h, b, b, f))
269 badtype(lambda: uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000271 # Duplicate arguments.
272 for hh in [[], [('hex', h)]]:
273 for bb in [[], [('bytes', b)]]:
274 for bble in [[], [('bytes_le', b)]]:
275 for ii in [[], [('int', i)]]:
276 for ff in [[], [('fields', f)]]:
277 args = dict(hh + bb + bble + ii + ff)
278 if len(args) != 0:
279 badtype(lambda: uuid.UUID(h, **args))
280 if len(args) != 1:
281 badtype(lambda: uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282
283 # Immutability.
284 u = uuid.UUID(h)
285 badtype(lambda: setattr(u, 'hex', h))
286 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000287 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288 badtype(lambda: setattr(u, 'fields', f))
289 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000290 badtype(lambda: setattr(u, 'time_low', 0))
291 badtype(lambda: setattr(u, 'time_mid', 0))
292 badtype(lambda: setattr(u, 'time_hi_version', 0))
293 badtype(lambda: setattr(u, 'time_hi_version', 0))
294 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
295 badtype(lambda: setattr(u, 'clock_seq_low', 0))
296 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297
298 def check_node(self, node, source):
Stefan Krah6108ea82010-04-11 16:49:20 +0000299 message = "%012x is not an RFC 4122 node ID" % node
300 self.assertTrue(0 < node, message)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000301 self.assertTrue(node < (1 << 48), message)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302
303 TestUUID.source2node[source] = node
304 if TestUUID.last_node:
305 if TestUUID.last_node != node:
306 msg = "different sources disagree on node:\n"
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000307 for s, n in TestUUID.source2node.items():
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 msg += " from source %r, node was %012x\n" % (s, n)
309 # There's actually no reason to expect the MAC addresses
310 # to agree across various methods -- e.g., a box may have
311 # multiple network interfaces, and different ways of getting
312 # a MAC address may favor different HW.
313 ##self.fail(msg)
314 else:
315 TestUUID.last_node = node
316
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300317 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000318 def test_ifconfig_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300319 node = uuid._ifconfig_getnode()
320 if node is not None:
321 self.check_node(node, 'ifconfig')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300323 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324 def test_ipconfig_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300325 node = uuid._ipconfig_getnode()
326 if node is not None:
327 self.check_node(node, 'ipconfig')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000328
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300329 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
330 @unittest.skipUnless(importable('netbios'), 'requires netbios')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000331 def test_netbios_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300332 self.check_node(uuid._netbios_getnode(), 'netbios')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000333
334 def test_random_getnode(self):
335 node = uuid._random_getnode()
Stefan Krah6108ea82010-04-11 16:49:20 +0000336 # Least significant bit of first octet must be set.
337 self.assertTrue(node & 0x010000000000)
338 self.assertTrue(node < (1 << 48))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000339
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300340 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
341 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 def test_unixdll_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300343 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
344 self.check_node(uuid._unixdll_getnode(), 'unixdll')
345 except TypeError:
346 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000347
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300348 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
349 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000350 def test_windll_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300351 self.check_node(uuid._windll_getnode(), 'windll')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352
353 def test_getnode(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000354 node1 = uuid.getnode()
355 self.check_node(node1, "getnode1")
356
357 # Test it again to ensure consistency.
358 node2 = uuid.getnode()
359 self.check_node(node2, "getnode2")
360
361 self.assertEqual(node1, node2)
362
Serhiy Storchakac303cfd2013-11-27 08:57:51 +0200363 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200364 def test_find_mac(self):
Victor Stinnerb9d01992014-10-21 22:33:10 +0200365 data = '''
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200366fake hwaddr
367cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
368eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
369'''
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200370
Victor Stinnerb9d01992014-10-21 22:33:10 +0200371 popen = unittest.mock.MagicMock()
372 popen.stdout = io.BytesIO(data.encode())
Serhiy Storchakacce440f2014-01-10 15:06:59 +0200373
Victor Stinnerb9d01992014-10-21 22:33:10 +0200374 with unittest.mock.patch.object(shutil, 'which',
375 return_value='/sbin/ifconfig'):
376 with unittest.mock.patch.object(subprocess, 'Popen',
377 return_value=popen):
378 mac = uuid._find_mac(
379 command='ifconfig',
380 arg='',
381 hw_identifiers=[b'hwaddr'],
382 get_index=lambda x: x + 1,
383 )
384
385 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200386
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300387 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388 def test_uuid1(self):
389 equal = self.assertEqual
390
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000391 # Make sure uuid1() generates UUIDs that are actually version 1.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000392 for u in [uuid.uuid1() for i in range(10)]:
393 equal(u.variant, uuid.RFC_4122)
394 equal(u.version, 1)
395
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000396 # Make sure the generated UUIDs are actually unique.
397 uuids = {}
398 for u in [uuid.uuid1() for i in range(1000)]:
399 uuids[u] = 1
400 equal(len(uuids.keys()), 1000)
401
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000402 # Make sure the supplied node ID appears in the UUID.
403 u = uuid.uuid1(0)
404 equal(u.node, 0)
405 u = uuid.uuid1(0x123456789abc)
406 equal(u.node, 0x123456789abc)
407 u = uuid.uuid1(0xffffffffffff)
408 equal(u.node, 0xffffffffffff)
409
410 # Make sure the supplied clock sequence appears in the UUID.
411 u = uuid.uuid1(0x123456789abc, 0)
412 equal(u.node, 0x123456789abc)
413 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
414 u = uuid.uuid1(0x123456789abc, 0x1234)
415 equal(u.node, 0x123456789abc)
416 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
417 u.clock_seq_low, 0x1234)
418 u = uuid.uuid1(0x123456789abc, 0x3fff)
419 equal(u.node, 0x123456789abc)
420 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
421 u.clock_seq_low, 0x3fff)
422
423 def test_uuid3(self):
424 equal = self.assertEqual
425
426 # Test some known version-3 UUIDs.
427 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
428 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
429 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
430 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
431 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
432 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
433 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
434 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
435 ]:
436 equal(u.variant, uuid.RFC_4122)
437 equal(u.version, 3)
438 equal(u, uuid.UUID(v))
439 equal(str(u), v)
440
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300441 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 def test_uuid4(self):
443 equal = self.assertEqual
444
445 # Make sure uuid4() generates UUIDs that are actually version 4.
446 for u in [uuid.uuid4() for i in range(10)]:
447 equal(u.variant, uuid.RFC_4122)
448 equal(u.version, 4)
449
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000450 # Make sure the generated UUIDs are actually unique.
451 uuids = {}
452 for u in [uuid.uuid4() for i in range(1000)]:
453 uuids[u] = 1
454 equal(len(uuids.keys()), 1000)
455
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000456 def test_uuid5(self):
457 equal = self.assertEqual
458
459 # Test some known version-5 UUIDs.
460 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
461 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
462 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
463 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
464 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
465 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
466 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
467 'cc957dd1-a972-5349-98cd-874190002798'),
468 ]:
469 equal(u.variant, uuid.RFC_4122)
470 equal(u.version, 5)
471 equal(u, uuid.UUID(v))
472 equal(str(u), v)
473
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300474 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000475 def testIssue8621(self):
Ronald Oussorenac764d32010-05-05 15:32:33 +0000476 # On at least some versions of OSX uuid.uuid4 generates
477 # the same sequence of UUIDs in the parent and any
478 # children started using fork.
479 fds = os.pipe()
480 pid = os.fork()
481 if pid == 0:
482 os.close(fds[0])
483 value = uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000484 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000485 os._exit(0)
486
487 else:
488 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100489 self.addCleanup(os.close, fds[0])
Ronald Oussorenac764d32010-05-05 15:32:33 +0000490 parent_value = uuid.uuid4().hex
491 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000492 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000493
494 self.assertNotEqual(parent_value, child_value)
495
496
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300498 unittest.main()