blob: 10105b00dc7c8dc5e95c9066e03a06b28a42c408 [file] [log] [blame]
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03001import unittest
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
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007import uuid
8
9def importable(name):
10 try:
11 __import__(name)
12 return True
13 except:
14 return False
15
Serhiy Storchaka7d15b542013-05-31 22:31:02 +030016class TestUUID(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000017 last_node = None
18 source2node = {}
19
20 def test_UUID(self):
21 equal = self.assertEqual
22 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000023 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024 time, clock_seq, variant, version) in [
25 ('00000000-0000-0000-0000-000000000000',
26 '{00000000-0000-0000-0000-000000000000}',
27 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000028 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
29 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030 (0, 0, 0, 0, 0, 0),
31 0,
32 'urn:uuid:00000000-0000-0000-0000-000000000000',
33 0, 0, uuid.RESERVED_NCS, None),
34 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
35 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
36 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000037 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
38 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 +000039 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
40 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Guido van Rossume2a383d2007-01-15 16:59:06 +000042 0x607040500010203, 0x809, uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
44 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
45 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000046 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
47 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 +000048 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
49 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Guido van Rossume2a383d2007-01-15 16:59:06 +000051 0x82e946702d9e6d5, 0xf9b, uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 ('12345678-1234-5678-1234-567812345678',
53 '{12345678-1234-5678-1234-567812345678}',
54 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000055 b'\x12\x34\x56\x78'*4,
56 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
58 0x12345678123456781234567812345678,
59 'urn:uuid:12345678-1234-5678-1234-567812345678',
Guido van Rossume2a383d2007-01-15 16:59:06 +000060 0x678123412345678, 0x1234, uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
62 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
63 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000064 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
65 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 +000066 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
67 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000069 0x1d19dad6ba7b810, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
71 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
72 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000073 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
74 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 +000075 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
76 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000078 0x1d19dad6ba7b811, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
80 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
81 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000082 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
83 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 +000084 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
85 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000087 0x1d19dad6ba7b812, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
89 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
90 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000091 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
92 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 +000093 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
94 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000096 0x1d19dad6ba7b814, 0xb4, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
98 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
99 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000100 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
101 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 +0000102 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
103 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000105 0x1d19dc07d444840, 0x3245, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
107 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
108 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000109 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
110 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 +0000111 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
112 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000113 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000114 0xc7e9d22e902893a, 0x27b8, uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000115 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
116 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
117 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000118 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
119 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 +0000120 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
121 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000122 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000123 0xef86f54eb424026, 0x24d0, uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
125 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
126 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000127 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
128 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 +0000129 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
130 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000131 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000132 0x1d07decf81d4fae, 0x2765, uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
134 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
135 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000136 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
137 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 +0000138 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
139 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000141 0xffefffefffefdfc, 0x3ffe, uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
143 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
144 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000145 b'\xff'*16,
146 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000147 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
148 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000150 0xfffffffffffffff, 0x3fff, uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151 ]:
152 equivalents = []
153 # Construct each UUID in several different ways.
154 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000155 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
156 uuid.UUID(fields=fields), uuid.UUID(int=integer),
157 uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000158 # Test all conversions and properties of the UUID object.
159 equal(str(u), string)
160 equal(int(u), integer)
161 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000162 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000163 equal(u.fields, fields)
164 equal(u.time_low, fields[0])
165 equal(u.time_mid, fields[1])
166 equal(u.time_hi_version, fields[2])
167 equal(u.clock_seq_hi_variant, fields[3])
168 equal(u.clock_seq_low, fields[4])
169 equal(u.node, fields[5])
170 equal(u.hex, hex)
171 equal(u.int, integer)
172 equal(u.urn, urn)
173 equal(u.time, time)
174 equal(u.clock_seq, clock_seq)
175 equal(u.variant, variant)
176 equal(u.version, version)
177 equivalents.append(u)
178
179 # Different construction methods should give the same UUID.
180 for u in equivalents:
181 for v in equivalents:
182 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000183
184 # Bug 7380: "bytes" and "bytes_le" should give the same type.
185 equal(type(u.bytes), builtins.bytes)
186 equal(type(u.bytes_le), builtins.bytes)
187
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188 ascending.append(u)
189
190 # Test comparison of UUIDs.
191 for i in range(len(ascending)):
192 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000193 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 equal(i >= j, ascending[i] >= ascending[j])
198 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000199
200 # Test sorting of UUIDs (above list is in ascending order).
201 resorted = ascending[:]
202 resorted.reverse()
203 resorted.sort()
204 equal(ascending, resorted)
205
206 def test_exceptions(self):
207 badvalue = lambda f: self.assertRaises(ValueError, f)
208 badtype = lambda f: self.assertRaises(TypeError, f)
209
210 # Badly formed hex strings.
211 badvalue(lambda: uuid.UUID(''))
212 badvalue(lambda: uuid.UUID('abc'))
213 badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
214 badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
215 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
216
217 # Badly formed bytes.
218 badvalue(lambda: uuid.UUID(bytes='abc'))
219 badvalue(lambda: uuid.UUID(bytes='\0'*15))
220 badvalue(lambda: uuid.UUID(bytes='\0'*17))
221
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000222 # Badly formed bytes_le.
223 badvalue(lambda: uuid.UUID(bytes_le='abc'))
224 badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
225 badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
226
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 # Badly formed fields.
228 badvalue(lambda: uuid.UUID(fields=(1,)))
229 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
230 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
231
232 # Field values out of range.
233 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000234 badvalue(lambda: uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000235 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000236 badvalue(lambda: uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000237 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000238 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000239 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000240 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000241 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000242 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000244 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245
246 # Version number out of range.
247 badvalue(lambda: uuid.UUID('00'*16, version=0))
248 badvalue(lambda: uuid.UUID('00'*16, version=6))
249
250 # Integer value out of range.
251 badvalue(lambda: uuid.UUID(int=-1))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000252 badvalue(lambda: uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253
254 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000255 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256 uuid.UUID(h)
257 uuid.UUID(hex=h)
258 uuid.UUID(bytes=b)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000259 uuid.UUID(bytes_le=b)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260 uuid.UUID(fields=f)
261 uuid.UUID(int=i)
262
263 # Wrong number of arguments (positional).
264 badtype(lambda: uuid.UUID())
265 badtype(lambda: uuid.UUID(h, b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000266 badtype(lambda: uuid.UUID(h, b, b))
267 badtype(lambda: uuid.UUID(h, b, b, f))
268 badtype(lambda: uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000270 # Duplicate arguments.
271 for hh in [[], [('hex', h)]]:
272 for bb in [[], [('bytes', b)]]:
273 for bble in [[], [('bytes_le', b)]]:
274 for ii in [[], [('int', i)]]:
275 for ff in [[], [('fields', f)]]:
276 args = dict(hh + bb + bble + ii + ff)
277 if len(args) != 0:
278 badtype(lambda: uuid.UUID(h, **args))
279 if len(args) != 1:
280 badtype(lambda: uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281
282 # Immutability.
283 u = uuid.UUID(h)
284 badtype(lambda: setattr(u, 'hex', h))
285 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000286 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 badtype(lambda: setattr(u, 'fields', f))
288 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000289 badtype(lambda: setattr(u, 'time_low', 0))
290 badtype(lambda: setattr(u, 'time_mid', 0))
291 badtype(lambda: setattr(u, 'time_hi_version', 0))
292 badtype(lambda: setattr(u, 'time_hi_version', 0))
293 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
294 badtype(lambda: setattr(u, 'clock_seq_low', 0))
295 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296
297 def check_node(self, node, source):
Stefan Krah6108ea82010-04-11 16:49:20 +0000298 message = "%012x is not an RFC 4122 node ID" % node
299 self.assertTrue(0 < node, message)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000300 self.assertTrue(node < (1 << 48), message)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301
302 TestUUID.source2node[source] = node
303 if TestUUID.last_node:
304 if TestUUID.last_node != node:
305 msg = "different sources disagree on node:\n"
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000306 for s, n in TestUUID.source2node.items():
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307 msg += " from source %r, node was %012x\n" % (s, n)
308 # There's actually no reason to expect the MAC addresses
309 # to agree across various methods -- e.g., a box may have
310 # multiple network interfaces, and different ways of getting
311 # a MAC address may favor different HW.
312 ##self.fail(msg)
313 else:
314 TestUUID.last_node = node
315
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300316 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000317 def test_ifconfig_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300318 node = uuid._ifconfig_getnode()
319 if node is not None:
320 self.check_node(node, 'ifconfig')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000321
Serhiy Storchakae66bb962014-11-07 12:19:40 +0200322 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
323 def test_arp_getnode(self):
324 node = uuid._arp_getnode()
325 if node is not None:
326 self.check_node(node, 'arp')
327
328 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
329 def test_lanscan_getnode(self):
330 node = uuid._lanscan_getnode()
331 if node is not None:
332 self.check_node(node, 'lanscan')
333
334 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
335 def test_netstat_getnode(self):
336 node = uuid._netstat_getnode()
337 if node is not None:
338 self.check_node(node, 'netstat')
339
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300340 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341 def test_ipconfig_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300342 node = uuid._ipconfig_getnode()
343 if node is not None:
344 self.check_node(node, 'ipconfig')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300346 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
347 @unittest.skipUnless(importable('netbios'), 'requires netbios')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000348 def test_netbios_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300349 self.check_node(uuid._netbios_getnode(), 'netbios')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000350
351 def test_random_getnode(self):
352 node = uuid._random_getnode()
Stefan Krah6108ea82010-04-11 16:49:20 +0000353 # Least significant bit of first octet must be set.
354 self.assertTrue(node & 0x010000000000)
355 self.assertTrue(node < (1 << 48))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000356
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300357 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
358 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000359 def test_unixdll_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300360 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
361 self.check_node(uuid._unixdll_getnode(), 'unixdll')
362 except TypeError:
363 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300365 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
366 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 def test_windll_getnode(self):
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300368 self.check_node(uuid._windll_getnode(), 'windll')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000369
370 def test_getnode(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000371 node1 = uuid.getnode()
372 self.check_node(node1, "getnode1")
373
374 # Test it again to ensure consistency.
375 node2 = uuid.getnode()
376 self.check_node(node2, "getnode2")
377
378 self.assertEqual(node1, node2)
379
Serhiy Storchakac303cfd2013-11-27 08:57:51 +0200380 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200381 def test_find_mac(self):
382 data = '''\
383
384fake hwaddr
385cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
386eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
387'''
388 def mock_popen(cmd):
389 return io.StringIO(data)
390
Serhiy Storchakacce440f2014-01-10 15:06:59 +0200391 if shutil.which('ifconfig') is None:
392 path = os.pathsep.join(('/sbin', '/usr/sbin'))
393 if shutil.which('ifconfig', path=path) is None:
394 self.skipTest('requires ifconfig')
395
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200396 with support.swap_attr(os, 'popen', mock_popen):
397 mac = uuid._find_mac(
398 command='ifconfig',
399 args='',
400 hw_identifiers=['hwaddr'],
401 get_index=lambda x: x + 1,
402 )
403 self.assertEqual(mac, 0x1234567890ab)
404
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300405 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000406 def test_uuid1(self):
407 equal = self.assertEqual
408
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000409 # Make sure uuid1() generates UUIDs that are actually version 1.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000410 for u in [uuid.uuid1() for i in range(10)]:
411 equal(u.variant, uuid.RFC_4122)
412 equal(u.version, 1)
413
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000414 # Make sure the generated UUIDs are actually unique.
415 uuids = {}
416 for u in [uuid.uuid1() for i in range(1000)]:
417 uuids[u] = 1
418 equal(len(uuids.keys()), 1000)
419
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420 # Make sure the supplied node ID appears in the UUID.
421 u = uuid.uuid1(0)
422 equal(u.node, 0)
423 u = uuid.uuid1(0x123456789abc)
424 equal(u.node, 0x123456789abc)
425 u = uuid.uuid1(0xffffffffffff)
426 equal(u.node, 0xffffffffffff)
427
428 # Make sure the supplied clock sequence appears in the UUID.
429 u = uuid.uuid1(0x123456789abc, 0)
430 equal(u.node, 0x123456789abc)
431 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
432 u = uuid.uuid1(0x123456789abc, 0x1234)
433 equal(u.node, 0x123456789abc)
434 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
435 u.clock_seq_low, 0x1234)
436 u = uuid.uuid1(0x123456789abc, 0x3fff)
437 equal(u.node, 0x123456789abc)
438 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
439 u.clock_seq_low, 0x3fff)
440
441 def test_uuid3(self):
442 equal = self.assertEqual
443
444 # Test some known version-3 UUIDs.
445 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
446 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
447 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
448 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
449 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
450 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
451 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
452 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
453 ]:
454 equal(u.variant, uuid.RFC_4122)
455 equal(u.version, 3)
456 equal(u, uuid.UUID(v))
457 equal(str(u), v)
458
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300459 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000460 def test_uuid4(self):
461 equal = self.assertEqual
462
463 # Make sure uuid4() generates UUIDs that are actually version 4.
464 for u in [uuid.uuid4() for i in range(10)]:
465 equal(u.variant, uuid.RFC_4122)
466 equal(u.version, 4)
467
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000468 # Make sure the generated UUIDs are actually unique.
469 uuids = {}
470 for u in [uuid.uuid4() for i in range(1000)]:
471 uuids[u] = 1
472 equal(len(uuids.keys()), 1000)
473
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000474 def test_uuid5(self):
475 equal = self.assertEqual
476
477 # Test some known version-5 UUIDs.
478 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
479 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
480 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
481 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
482 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
483 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
484 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
485 'cc957dd1-a972-5349-98cd-874190002798'),
486 ]:
487 equal(u.variant, uuid.RFC_4122)
488 equal(u.version, 5)
489 equal(u, uuid.UUID(v))
490 equal(str(u), v)
491
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300492 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000493 def testIssue8621(self):
Ronald Oussorenac764d32010-05-05 15:32:33 +0000494 # On at least some versions of OSX uuid.uuid4 generates
495 # the same sequence of UUIDs in the parent and any
496 # children started using fork.
497 fds = os.pipe()
498 pid = os.fork()
499 if pid == 0:
500 os.close(fds[0])
501 value = uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000502 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000503 os._exit(0)
504
505 else:
506 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100507 self.addCleanup(os.close, fds[0])
Ronald Oussorenac764d32010-05-05 15:32:33 +0000508 parent_value = uuid.uuid4().hex
509 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000510 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000511
512 self.assertNotEqual(parent_value, child_value)
513
514
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300516 unittest.main()