blob: 9de3d789c5e2952107c28ebe1ab8d2f1b70a20a0 [file] [log] [blame]
Serhiy Storchaka95787fd2013-05-31 22:34:53 +03001import unittest
Georg Brandlf102fc52006-07-27 15:05:36 +00002from test import test_support
Serhiy Storchaka95787fd2013-05-31 22:34:53 +03003import os
Tim Peters2adc6262006-06-13 00:30:50 +00004import uuid
5
6def importable(name):
7 try:
8 __import__(name)
9 return True
10 except:
11 return False
12
Serhiy Storchaka95787fd2013-05-31 22:34:53 +030013class TestUUID(unittest.TestCase):
Tim Peters2adc6262006-06-13 00:30:50 +000014 last_node = None
Tim Peters08310d62006-07-27 20:47:24 +000015 source2node = {}
Tim Peters2adc6262006-06-13 00:30:50 +000016
17 def test_UUID(self):
18 equal = self.assertEqual
19 ascending = []
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000020 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Tim Peters2adc6262006-06-13 00:30:50 +000021 time, clock_seq, variant, version) in [
22 ('00000000-0000-0000-0000-000000000000',
23 '{00000000-0000-0000-0000-000000000000}',
24 '00000000000000000000000000000000',
25 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000026 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Tim Peters2adc6262006-06-13 00:30:50 +000027 (0, 0, 0, 0, 0, 0),
28 0,
29 'urn:uuid:00000000-0000-0000-0000-000000000000',
30 0, 0, uuid.RESERVED_NCS, None),
31 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
32 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
33 '000102030405060708090a0b0c0d0e0f',
34 '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000035 '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Tim Peters2adc6262006-06-13 00:30:50 +000036 (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
37 0x000102030405060708090a0b0c0d0e0fL,
38 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
39 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
40 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
41 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
42 '02d9e6d59467382e8f9b9300a64ac3cd',
43 '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000044 '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Tim Peters2adc6262006-06-13 00:30:50 +000045 (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
46 0x02d9e6d59467382e8f9b9300a64ac3cdL,
47 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
48 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
49 ('12345678-1234-5678-1234-567812345678',
50 '{12345678-1234-5678-1234-567812345678}',
51 '12345678123456781234567812345678',
52 '\x12\x34\x56\x78'*4,
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000053 '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Tim Peters2adc6262006-06-13 00:30:50 +000054 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
55 0x12345678123456781234567812345678,
56 'urn:uuid:12345678-1234-5678-1234-567812345678',
57 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
58 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
59 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
60 '6ba7b8109dad11d180b400c04fd430c8',
61 '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000062 '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000063 (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
64 0x6ba7b8109dad11d180b400c04fd430c8L,
65 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
66 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
67 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
68 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
69 '6ba7b8119dad11d180b400c04fd430c8',
70 '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000071 '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000072 (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
73 0x6ba7b8119dad11d180b400c04fd430c8L,
74 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
75 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
76 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
77 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
78 '6ba7b8129dad11d180b400c04fd430c8',
79 '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000080 '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000081 (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
82 0x6ba7b8129dad11d180b400c04fd430c8L,
83 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
84 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
85 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
86 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
87 '6ba7b8149dad11d180b400c04fd430c8',
88 '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000089 '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000090 (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
91 0x6ba7b8149dad11d180b400c04fd430c8L,
92 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
93 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
94 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
95 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
96 '7d4448409dc011d1b2455ffdce74fad2',
97 '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000098 '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Tim Peters2adc6262006-06-13 00:30:50 +000099 (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
100 0x7d4448409dc011d1b2455ffdce74fad2L,
101 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
102 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
103 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
104 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
105 'e902893a9d223c7ea7b8d6e313b71d9f',
106 '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000107 '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Tim Peters2adc6262006-06-13 00:30:50 +0000108 (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
109 0xe902893a9d223c7ea7b8d6e313b71d9fL,
110 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
111 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
112 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
113 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
114 'eb4240266f544ef8a4d0bb658a1fc6cf',
115 '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000116 '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Tim Peters2adc6262006-06-13 00:30:50 +0000117 (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
118 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
119 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
120 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
121 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
122 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
123 'f81d4fae7dec11d0a76500a0c91e6bf6',
124 '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000125 '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Tim Peters2adc6262006-06-13 00:30:50 +0000126 (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
127 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
128 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
129 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
130 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
131 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
132 'fffefdfcfffefffefffefffefdfcfbfa',
133 '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000134 '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Tim Peters2adc6262006-06-13 00:30:50 +0000135 (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
136 0xfffefdfcfffefffefffefffefdfcfbfaL,
137 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
138 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
139 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
140 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
141 'ffffffffffffffffffffffffffffffff',
142 '\xff'*16,
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000143 '\xff'*16,
Tim Peters2adc6262006-06-13 00:30:50 +0000144 (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
145 0xffffffffffffffffffffffffffffffffL,
146 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
147 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
148 ]:
149 equivalents = []
150 # Construct each UUID in several different ways.
151 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000152 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
153 uuid.UUID(fields=fields), uuid.UUID(int=integer),
154 uuid.UUID(urn)]:
Tim Peters2adc6262006-06-13 00:30:50 +0000155 # Test all conversions and properties of the UUID object.
156 equal(str(u), string)
157 equal(int(u), integer)
158 equal(u.bytes, bytes)
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000159 equal(u.bytes_le, bytes_le)
Tim Peters2adc6262006-06-13 00:30:50 +0000160 equal(u.fields, fields)
161 equal(u.time_low, fields[0])
162 equal(u.time_mid, fields[1])
163 equal(u.time_hi_version, fields[2])
164 equal(u.clock_seq_hi_variant, fields[3])
165 equal(u.clock_seq_low, fields[4])
166 equal(u.node, fields[5])
167 equal(u.hex, hex)
168 equal(u.int, integer)
169 equal(u.urn, urn)
170 equal(u.time, time)
171 equal(u.clock_seq, clock_seq)
172 equal(u.variant, variant)
173 equal(u.version, version)
174 equivalents.append(u)
175
176 # Different construction methods should give the same UUID.
177 for u in equivalents:
178 for v in equivalents:
179 equal(u, v)
180 ascending.append(u)
181
182 # Test comparison of UUIDs.
183 for i in range(len(ascending)):
184 for j in range(len(ascending)):
185 equal(cmp(i, j), cmp(ascending[i], ascending[j]))
186
187 # Test sorting of UUIDs (above list is in ascending order).
188 resorted = ascending[:]
189 resorted.reverse()
190 resorted.sort()
191 equal(ascending, resorted)
192
193 def test_exceptions(self):
194 badvalue = lambda f: self.assertRaises(ValueError, f)
195 badtype = lambda f: self.assertRaises(TypeError, f)
196
197 # Badly formed hex strings.
198 badvalue(lambda: uuid.UUID(''))
199 badvalue(lambda: uuid.UUID('abc'))
200 badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
201 badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
202 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
203
204 # Badly formed bytes.
205 badvalue(lambda: uuid.UUID(bytes='abc'))
206 badvalue(lambda: uuid.UUID(bytes='\0'*15))
207 badvalue(lambda: uuid.UUID(bytes='\0'*17))
208
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000209 # Badly formed bytes_le.
210 badvalue(lambda: uuid.UUID(bytes_le='abc'))
211 badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
212 badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
213
Tim Peters2adc6262006-06-13 00:30:50 +0000214 # Badly formed fields.
215 badvalue(lambda: uuid.UUID(fields=(1,)))
216 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
217 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
218
219 # Field values out of range.
220 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
221 badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
222 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
223 badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
224 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
225 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
226 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
227 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
228 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
229 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
230 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
231 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
232
233 # Version number out of range.
234 badvalue(lambda: uuid.UUID('00'*16, version=0))
235 badvalue(lambda: uuid.UUID('00'*16, version=6))
236
237 # Integer value out of range.
238 badvalue(lambda: uuid.UUID(int=-1))
239 badvalue(lambda: uuid.UUID(int=1<<128L))
240
241 # Must supply exactly one of hex, bytes, fields, int.
242 h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
243 uuid.UUID(h)
244 uuid.UUID(hex=h)
245 uuid.UUID(bytes=b)
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000246 uuid.UUID(bytes_le=b)
Tim Peters2adc6262006-06-13 00:30:50 +0000247 uuid.UUID(fields=f)
248 uuid.UUID(int=i)
249
250 # Wrong number of arguments (positional).
251 badtype(lambda: uuid.UUID())
252 badtype(lambda: uuid.UUID(h, b))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000253 badtype(lambda: uuid.UUID(h, b, b))
254 badtype(lambda: uuid.UUID(h, b, b, f))
255 badtype(lambda: uuid.UUID(h, b, b, f, i))
Tim Peters2adc6262006-06-13 00:30:50 +0000256
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000257 # Duplicate arguments.
258 for hh in [[], [('hex', h)]]:
259 for bb in [[], [('bytes', b)]]:
260 for bble in [[], [('bytes_le', b)]]:
261 for ii in [[], [('int', i)]]:
262 for ff in [[], [('fields', f)]]:
263 args = dict(hh + bb + bble + ii + ff)
264 if len(args) != 0:
265 badtype(lambda: uuid.UUID(h, **args))
266 if len(args) != 1:
267 badtype(lambda: uuid.UUID(**args))
Tim Peters2adc6262006-06-13 00:30:50 +0000268
269 # Immutability.
270 u = uuid.UUID(h)
271 badtype(lambda: setattr(u, 'hex', h))
272 badtype(lambda: setattr(u, 'bytes', b))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000273 badtype(lambda: setattr(u, 'bytes_le', b))
Tim Peters2adc6262006-06-13 00:30:50 +0000274 badtype(lambda: setattr(u, 'fields', f))
275 badtype(lambda: setattr(u, 'int', i))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000276 badtype(lambda: setattr(u, 'time_low', 0))
277 badtype(lambda: setattr(u, 'time_mid', 0))
278 badtype(lambda: setattr(u, 'time_hi_version', 0))
279 badtype(lambda: setattr(u, 'time_hi_version', 0))
280 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
281 badtype(lambda: setattr(u, 'clock_seq_low', 0))
282 badtype(lambda: setattr(u, 'node', 0))
Tim Peters2adc6262006-06-13 00:30:50 +0000283
Tim Peters08310d62006-07-27 20:47:24 +0000284 def check_node(self, node, source):
Stefan Kraha1858392010-04-11 15:15:54 +0000285 message = "%012x is not an RFC 4122 node ID" % node
286 self.assertTrue(0 < node, message)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000287 self.assertTrue(node < (1L << 48), message)
Tim Peters2adc6262006-06-13 00:30:50 +0000288
Tim Peters08310d62006-07-27 20:47:24 +0000289 TestUUID.source2node[source] = node
Tim Peters2adc6262006-06-13 00:30:50 +0000290 if TestUUID.last_node:
Tim Peters08310d62006-07-27 20:47:24 +0000291 if TestUUID.last_node != node:
292 msg = "different sources disagree on node:\n"
293 for s, n in TestUUID.source2node.iteritems():
294 msg += " from source %r, node was %012x\n" % (s, n)
Tim Peters750c4422006-07-28 04:51:59 +0000295 # There's actually no reason to expect the MAC addresses
296 # to agree across various methods -- e.g., a box may have
297 # multiple network interfaces, and different ways of getting
298 # a MAC address may favor different HW.
299 ##self.fail(msg)
Tim Peters2adc6262006-06-13 00:30:50 +0000300 else:
301 TestUUID.last_node = node
302
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300303 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Tim Peters2adc6262006-06-13 00:30:50 +0000304 def test_ifconfig_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300305 node = uuid._ifconfig_getnode()
306 if node is not None:
307 self.check_node(node, 'ifconfig')
Tim Peters2adc6262006-06-13 00:30:50 +0000308
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300309 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
Tim Peters2adc6262006-06-13 00:30:50 +0000310 def test_ipconfig_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300311 node = uuid._ipconfig_getnode()
312 if node is not None:
313 self.check_node(node, 'ipconfig')
Tim Peters2adc6262006-06-13 00:30:50 +0000314
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300315 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
316 @unittest.skipUnless(importable('netbios'), 'requires netbios')
Tim Peters2adc6262006-06-13 00:30:50 +0000317 def test_netbios_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300318 self.check_node(uuid._netbios_getnode(), 'netbios')
Tim Peters2adc6262006-06-13 00:30:50 +0000319
320 def test_random_getnode(self):
321 node = uuid._random_getnode()
Stefan Kraha1858392010-04-11 15:15:54 +0000322 # Least significant bit of first octet must be set.
323 self.assertTrue(node & 0x010000000000)
324 self.assertTrue(node < (1L << 48))
Tim Peters2adc6262006-06-13 00:30:50 +0000325
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300326 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
327 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000328 def test_unixdll_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300329 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
330 self.check_node(uuid._unixdll_getnode(), 'unixdll')
331 except TypeError:
332 pass
Tim Peters2adc6262006-06-13 00:30:50 +0000333
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300334 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
335 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000336 def test_windll_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300337 self.check_node(uuid._windll_getnode(), 'windll')
Tim Peters2adc6262006-06-13 00:30:50 +0000338
339 def test_getnode(self):
Tim Peters750c4422006-07-28 04:51:59 +0000340 node1 = uuid.getnode()
341 self.check_node(node1, "getnode1")
Tim Peters2adc6262006-06-13 00:30:50 +0000342
343 # Test it again to ensure consistency.
Tim Peters750c4422006-07-28 04:51:59 +0000344 node2 = uuid.getnode()
345 self.check_node(node2, "getnode2")
346
347 self.assertEqual(node1, node2)
Tim Peters2adc6262006-06-13 00:30:50 +0000348
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300349 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000350 def test_uuid1(self):
351 equal = self.assertEqual
352
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000353 # Make sure uuid1() generates UUIDs that are actually version 1.
Tim Peters2adc6262006-06-13 00:30:50 +0000354 for u in [uuid.uuid1() for i in range(10)]:
355 equal(u.variant, uuid.RFC_4122)
356 equal(u.version, 1)
357
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000358 # Make sure the generated UUIDs are actually unique.
359 uuids = {}
360 for u in [uuid.uuid1() for i in range(1000)]:
361 uuids[u] = 1
362 equal(len(uuids.keys()), 1000)
363
Tim Peters2adc6262006-06-13 00:30:50 +0000364 # Make sure the supplied node ID appears in the UUID.
365 u = uuid.uuid1(0)
366 equal(u.node, 0)
367 u = uuid.uuid1(0x123456789abc)
368 equal(u.node, 0x123456789abc)
369 u = uuid.uuid1(0xffffffffffff)
370 equal(u.node, 0xffffffffffff)
371
372 # Make sure the supplied clock sequence appears in the UUID.
373 u = uuid.uuid1(0x123456789abc, 0)
374 equal(u.node, 0x123456789abc)
375 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
376 u = uuid.uuid1(0x123456789abc, 0x1234)
377 equal(u.node, 0x123456789abc)
378 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
379 u.clock_seq_low, 0x1234)
380 u = uuid.uuid1(0x123456789abc, 0x3fff)
381 equal(u.node, 0x123456789abc)
382 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
383 u.clock_seq_low, 0x3fff)
384
385 def test_uuid3(self):
386 equal = self.assertEqual
387
388 # Test some known version-3 UUIDs.
389 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
390 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
391 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
392 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
393 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
394 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
395 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
396 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
397 ]:
398 equal(u.variant, uuid.RFC_4122)
399 equal(u.version, 3)
400 equal(u, uuid.UUID(v))
401 equal(str(u), v)
402
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300403 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000404 def test_uuid4(self):
405 equal = self.assertEqual
406
407 # Make sure uuid4() generates UUIDs that are actually version 4.
408 for u in [uuid.uuid4() for i in range(10)]:
409 equal(u.variant, uuid.RFC_4122)
410 equal(u.version, 4)
411
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000412 # Make sure the generated UUIDs are actually unique.
413 uuids = {}
Martin v. Löwis2eb8c4f2006-08-18 03:47:18 +0000414 for u in [uuid.uuid4() for i in range(1000)]:
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000415 uuids[u] = 1
416 equal(len(uuids.keys()), 1000)
417
Tim Peters2adc6262006-06-13 00:30:50 +0000418 def test_uuid5(self):
419 equal = self.assertEqual
420
421 # Test some known version-5 UUIDs.
422 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
423 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
424 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
425 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
426 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
427 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
428 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
429 'cc957dd1-a972-5349-98cd-874190002798'),
430 ]:
431 equal(u.variant, uuid.RFC_4122)
432 equal(u.version, 5)
433 equal(u, uuid.UUID(v))
434 equal(str(u), v)
435
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300436 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussoren33206962010-05-05 14:48:37 +0000437 def testIssue8621(self):
Ronald Oussoren33206962010-05-05 14:48:37 +0000438 # On at least some versions of OSX uuid.uuid4 generates
439 # the same sequence of UUIDs in the parent and any
440 # children started using fork.
441 fds = os.pipe()
442 pid = os.fork()
443 if pid == 0:
444 os.close(fds[0])
445 value = uuid.uuid4()
446 os.write(fds[1], value.hex)
Ronald Oussoren91165c02010-05-05 15:32:39 +0000447 os._exit(0)
Ronald Oussoren33206962010-05-05 14:48:37 +0000448
449 else:
450 os.close(fds[1])
Richard Oudkerk045e4572013-06-10 16:27:45 +0100451 self.addCleanup(os.close, fds[0])
Ronald Oussoren33206962010-05-05 14:48:37 +0000452 parent_value = uuid.uuid4().hex
453 os.waitpid(pid, 0)
454 child_value = os.read(fds[0], 100)
455
456 self.assertNotEqual(parent_value, child_value)
457
458
459
460
Georg Brandlf102fc52006-07-27 15:05:36 +0000461
462def test_main():
463 test_support.run_unittest(TestUUID)
464
Tim Peters2adc6262006-06-13 00:30:50 +0000465if __name__ == '__main__':
Georg Brandlf102fc52006-07-27 15:05:36 +0000466 test_main()