blob: bccfcb746e64f08699c53852ec92774689829f1c [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 Storchaka6d9d30d2013-11-26 22:47:05 +02003import io
Serhiy Storchaka95787fd2013-05-31 22:34:53 +03004import os
Tim Peters2adc6262006-06-13 00:30:50 +00005import uuid
6
7def importable(name):
8 try:
9 __import__(name)
10 return True
11 except:
12 return False
13
Serhiy Storchaka95787fd2013-05-31 22:34:53 +030014class TestUUID(unittest.TestCase):
Tim Peters2adc6262006-06-13 00:30:50 +000015 last_node = None
Tim Peters08310d62006-07-27 20:47:24 +000016 source2node = {}
Tim Peters2adc6262006-06-13 00:30:50 +000017
18 def test_UUID(self):
19 equal = self.assertEqual
20 ascending = []
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000021 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Tim Peters2adc6262006-06-13 00:30:50 +000022 time, clock_seq, variant, version) in [
23 ('00000000-0000-0000-0000-000000000000',
24 '{00000000-0000-0000-0000-000000000000}',
25 '00000000000000000000000000000000',
26 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000027 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Tim Peters2adc6262006-06-13 00:30:50 +000028 (0, 0, 0, 0, 0, 0),
29 0,
30 'urn:uuid:00000000-0000-0000-0000-000000000000',
31 0, 0, uuid.RESERVED_NCS, None),
32 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
33 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
34 '000102030405060708090a0b0c0d0e0f',
35 '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000036 '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Tim Peters2adc6262006-06-13 00:30:50 +000037 (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
38 0x000102030405060708090a0b0c0d0e0fL,
39 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
40 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
41 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
42 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
43 '02d9e6d59467382e8f9b9300a64ac3cd',
44 '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000045 '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Tim Peters2adc6262006-06-13 00:30:50 +000046 (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
47 0x02d9e6d59467382e8f9b9300a64ac3cdL,
48 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
49 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
50 ('12345678-1234-5678-1234-567812345678',
51 '{12345678-1234-5678-1234-567812345678}',
52 '12345678123456781234567812345678',
53 '\x12\x34\x56\x78'*4,
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000054 '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Tim Peters2adc6262006-06-13 00:30:50 +000055 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
56 0x12345678123456781234567812345678,
57 'urn:uuid:12345678-1234-5678-1234-567812345678',
58 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
59 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
60 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
61 '6ba7b8109dad11d180b400c04fd430c8',
62 '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000063 '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000064 (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
65 0x6ba7b8109dad11d180b400c04fd430c8L,
66 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
67 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
68 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
69 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
70 '6ba7b8119dad11d180b400c04fd430c8',
71 '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000072 '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000073 (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
74 0x6ba7b8119dad11d180b400c04fd430c8L,
75 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
76 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
77 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
78 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
79 '6ba7b8129dad11d180b400c04fd430c8',
80 '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000081 '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000082 (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
83 0x6ba7b8129dad11d180b400c04fd430c8L,
84 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
85 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
86 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
87 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
88 '6ba7b8149dad11d180b400c04fd430c8',
89 '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000090 '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Tim Peters2adc6262006-06-13 00:30:50 +000091 (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
92 0x6ba7b8149dad11d180b400c04fd430c8L,
93 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
94 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
95 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
96 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
97 '7d4448409dc011d1b2455ffdce74fad2',
98 '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +000099 '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Tim Peters2adc6262006-06-13 00:30:50 +0000100 (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
101 0x7d4448409dc011d1b2455ffdce74fad2L,
102 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
103 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
104 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
105 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
106 'e902893a9d223c7ea7b8d6e313b71d9f',
107 '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000108 '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Tim Peters2adc6262006-06-13 00:30:50 +0000109 (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
110 0xe902893a9d223c7ea7b8d6e313b71d9fL,
111 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
112 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
113 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
114 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
115 'eb4240266f544ef8a4d0bb658a1fc6cf',
116 '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000117 '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Tim Peters2adc6262006-06-13 00:30:50 +0000118 (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
119 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
120 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
121 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
122 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
123 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
124 'f81d4fae7dec11d0a76500a0c91e6bf6',
125 '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000126 '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Tim Peters2adc6262006-06-13 00:30:50 +0000127 (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
128 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
129 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
130 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
131 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
132 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
133 'fffefdfcfffefffefffefffefdfcfbfa',
134 '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000135 '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Tim Peters2adc6262006-06-13 00:30:50 +0000136 (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
137 0xfffefdfcfffefffefffefffefdfcfbfaL,
138 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
139 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
140 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
141 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
142 'ffffffffffffffffffffffffffffffff',
143 '\xff'*16,
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000144 '\xff'*16,
Tim Peters2adc6262006-06-13 00:30:50 +0000145 (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
146 0xffffffffffffffffffffffffffffffffL,
147 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
148 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
149 ]:
150 equivalents = []
151 # Construct each UUID in several different ways.
152 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000153 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
154 uuid.UUID(fields=fields), uuid.UUID(int=integer),
155 uuid.UUID(urn)]:
Tim Peters2adc6262006-06-13 00:30:50 +0000156 # Test all conversions and properties of the UUID object.
157 equal(str(u), string)
158 equal(int(u), integer)
159 equal(u.bytes, bytes)
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000160 equal(u.bytes_le, bytes_le)
Tim Peters2adc6262006-06-13 00:30:50 +0000161 equal(u.fields, fields)
162 equal(u.time_low, fields[0])
163 equal(u.time_mid, fields[1])
164 equal(u.time_hi_version, fields[2])
165 equal(u.clock_seq_hi_variant, fields[3])
166 equal(u.clock_seq_low, fields[4])
167 equal(u.node, fields[5])
168 equal(u.hex, hex)
169 equal(u.int, integer)
170 equal(u.urn, urn)
171 equal(u.time, time)
172 equal(u.clock_seq, clock_seq)
173 equal(u.variant, variant)
174 equal(u.version, version)
175 equivalents.append(u)
176
177 # Different construction methods should give the same UUID.
178 for u in equivalents:
179 for v in equivalents:
180 equal(u, v)
181 ascending.append(u)
182
183 # Test comparison of UUIDs.
184 for i in range(len(ascending)):
185 for j in range(len(ascending)):
186 equal(cmp(i, j), cmp(ascending[i], ascending[j]))
187
188 # Test sorting of UUIDs (above list is in ascending order).
189 resorted = ascending[:]
190 resorted.reverse()
191 resorted.sort()
192 equal(ascending, resorted)
193
194 def test_exceptions(self):
195 badvalue = lambda f: self.assertRaises(ValueError, f)
196 badtype = lambda f: self.assertRaises(TypeError, f)
197
198 # Badly formed hex strings.
199 badvalue(lambda: uuid.UUID(''))
200 badvalue(lambda: uuid.UUID('abc'))
201 badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
202 badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
203 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
204
205 # Badly formed bytes.
206 badvalue(lambda: uuid.UUID(bytes='abc'))
207 badvalue(lambda: uuid.UUID(bytes='\0'*15))
208 badvalue(lambda: uuid.UUID(bytes='\0'*17))
209
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000210 # Badly formed bytes_le.
211 badvalue(lambda: uuid.UUID(bytes_le='abc'))
212 badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
213 badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
214
Tim Peters2adc6262006-06-13 00:30:50 +0000215 # Badly formed fields.
216 badvalue(lambda: uuid.UUID(fields=(1,)))
217 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
218 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
219
220 # Field values out of range.
221 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
222 badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
223 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
224 badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
225 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
226 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
227 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
228 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
229 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
230 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
231 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
232 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
233
234 # Version number out of range.
235 badvalue(lambda: uuid.UUID('00'*16, version=0))
236 badvalue(lambda: uuid.UUID('00'*16, version=6))
237
238 # Integer value out of range.
239 badvalue(lambda: uuid.UUID(int=-1))
240 badvalue(lambda: uuid.UUID(int=1<<128L))
241
242 # Must supply exactly one of hex, bytes, fields, int.
243 h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
244 uuid.UUID(h)
245 uuid.UUID(hex=h)
246 uuid.UUID(bytes=b)
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000247 uuid.UUID(bytes_le=b)
Tim Peters2adc6262006-06-13 00:30:50 +0000248 uuid.UUID(fields=f)
249 uuid.UUID(int=i)
250
251 # Wrong number of arguments (positional).
252 badtype(lambda: uuid.UUID())
253 badtype(lambda: uuid.UUID(h, b))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000254 badtype(lambda: uuid.UUID(h, b, b))
255 badtype(lambda: uuid.UUID(h, b, b, f))
256 badtype(lambda: uuid.UUID(h, b, b, f, i))
Tim Peters2adc6262006-06-13 00:30:50 +0000257
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000258 # Duplicate arguments.
259 for hh in [[], [('hex', h)]]:
260 for bb in [[], [('bytes', b)]]:
261 for bble in [[], [('bytes_le', b)]]:
262 for ii in [[], [('int', i)]]:
263 for ff in [[], [('fields', f)]]:
264 args = dict(hh + bb + bble + ii + ff)
265 if len(args) != 0:
266 badtype(lambda: uuid.UUID(h, **args))
267 if len(args) != 1:
268 badtype(lambda: uuid.UUID(**args))
Tim Peters2adc6262006-06-13 00:30:50 +0000269
270 # Immutability.
271 u = uuid.UUID(h)
272 badtype(lambda: setattr(u, 'hex', h))
273 badtype(lambda: setattr(u, 'bytes', b))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000274 badtype(lambda: setattr(u, 'bytes_le', b))
Tim Peters2adc6262006-06-13 00:30:50 +0000275 badtype(lambda: setattr(u, 'fields', f))
276 badtype(lambda: setattr(u, 'int', i))
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000277 badtype(lambda: setattr(u, 'time_low', 0))
278 badtype(lambda: setattr(u, 'time_mid', 0))
279 badtype(lambda: setattr(u, 'time_hi_version', 0))
280 badtype(lambda: setattr(u, 'time_hi_version', 0))
281 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
282 badtype(lambda: setattr(u, 'clock_seq_low', 0))
283 badtype(lambda: setattr(u, 'node', 0))
Tim Peters2adc6262006-06-13 00:30:50 +0000284
Tim Peters08310d62006-07-27 20:47:24 +0000285 def check_node(self, node, source):
Stefan Kraha1858392010-04-11 15:15:54 +0000286 message = "%012x is not an RFC 4122 node ID" % node
287 self.assertTrue(0 < node, message)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000288 self.assertTrue(node < (1L << 48), message)
Tim Peters2adc6262006-06-13 00:30:50 +0000289
Tim Peters08310d62006-07-27 20:47:24 +0000290 TestUUID.source2node[source] = node
Tim Peters2adc6262006-06-13 00:30:50 +0000291 if TestUUID.last_node:
Tim Peters08310d62006-07-27 20:47:24 +0000292 if TestUUID.last_node != node:
293 msg = "different sources disagree on node:\n"
294 for s, n in TestUUID.source2node.iteritems():
295 msg += " from source %r, node was %012x\n" % (s, n)
Tim Peters750c4422006-07-28 04:51:59 +0000296 # There's actually no reason to expect the MAC addresses
297 # to agree across various methods -- e.g., a box may have
298 # multiple network interfaces, and different ways of getting
299 # a MAC address may favor different HW.
300 ##self.fail(msg)
Tim Peters2adc6262006-06-13 00:30:50 +0000301 else:
302 TestUUID.last_node = node
303
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300304 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Tim Peters2adc6262006-06-13 00:30:50 +0000305 def test_ifconfig_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300306 node = uuid._ifconfig_getnode()
307 if node is not None:
308 self.check_node(node, 'ifconfig')
Tim Peters2adc6262006-06-13 00:30:50 +0000309
Serhiy Storchaka4535b112014-11-07 12:19:23 +0200310 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
311 def test_arp_getnode(self):
312 node = uuid._arp_getnode()
313 if node is not None:
314 self.check_node(node, 'arp')
315
316 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
317 def test_lanscan_getnode(self):
318 node = uuid._lanscan_getnode()
319 if node is not None:
320 self.check_node(node, 'lanscan')
321
322 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
323 def test_netstat_getnode(self):
324 node = uuid._netstat_getnode()
325 if node is not None:
326 self.check_node(node, 'netstat')
327
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300328 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
Tim Peters2adc6262006-06-13 00:30:50 +0000329 def test_ipconfig_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300330 node = uuid._ipconfig_getnode()
331 if node is not None:
332 self.check_node(node, 'ipconfig')
Tim Peters2adc6262006-06-13 00:30:50 +0000333
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300334 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
335 @unittest.skipUnless(importable('netbios'), 'requires netbios')
Tim Peters2adc6262006-06-13 00:30:50 +0000336 def test_netbios_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300337 self.check_node(uuid._netbios_getnode(), 'netbios')
Tim Peters2adc6262006-06-13 00:30:50 +0000338
339 def test_random_getnode(self):
340 node = uuid._random_getnode()
Stefan Kraha1858392010-04-11 15:15:54 +0000341 # Least significant bit of first octet must be set.
342 self.assertTrue(node & 0x010000000000)
343 self.assertTrue(node < (1L << 48))
Tim Peters2adc6262006-06-13 00:30:50 +0000344
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300345 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
346 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000347 def test_unixdll_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300348 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
349 self.check_node(uuid._unixdll_getnode(), 'unixdll')
350 except TypeError:
351 pass
Tim Peters2adc6262006-06-13 00:30:50 +0000352
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300353 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
354 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000355 def test_windll_getnode(self):
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300356 self.check_node(uuid._windll_getnode(), 'windll')
Tim Peters2adc6262006-06-13 00:30:50 +0000357
358 def test_getnode(self):
Tim Peters750c4422006-07-28 04:51:59 +0000359 node1 = uuid.getnode()
360 self.check_node(node1, "getnode1")
Tim Peters2adc6262006-06-13 00:30:50 +0000361
362 # Test it again to ensure consistency.
Tim Peters750c4422006-07-28 04:51:59 +0000363 node2 = uuid.getnode()
364 self.check_node(node2, "getnode2")
365
366 self.assertEqual(node1, node2)
Tim Peters2adc6262006-06-13 00:30:50 +0000367
Serhiy Storchakacf6a3e22013-11-27 08:57:33 +0200368 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka6d9d30d2013-11-26 22:47:05 +0200369 def test_find_mac(self):
370 data = '''\
371
372fake hwaddr
373cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
374eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
375'''
376 def mock_popen(cmd):
377 return io.BytesIO(data)
378
Serhiy Storchaka0f11d0f2014-01-10 15:05:27 +0200379 path = os.environ.get("PATH", os.defpath).split(os.pathsep)
380 path.extend(('/sbin', '/usr/sbin'))
381 for dir in path:
382 executable = os.path.join(dir, 'ifconfig')
383 if (os.path.exists(executable) and
384 os.access(executable, os.F_OK | os.X_OK) and
385 not os.path.isdir(executable)):
386 break
387 else:
388 self.skipTest('requires ifconfig')
389
Serhiy Storchaka6d9d30d2013-11-26 22:47:05 +0200390 with test_support.swap_attr(os, 'popen', mock_popen):
391 mac = uuid._find_mac(
392 command='ifconfig',
393 args='',
394 hw_identifiers=['hwaddr'],
395 get_index=lambda x: x + 1,
396 )
397 self.assertEqual(mac, 0x1234567890ab)
398
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300399 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000400 def test_uuid1(self):
401 equal = self.assertEqual
402
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000403 # Make sure uuid1() generates UUIDs that are actually version 1.
Tim Peters2adc6262006-06-13 00:30:50 +0000404 for u in [uuid.uuid1() for i in range(10)]:
405 equal(u.variant, uuid.RFC_4122)
406 equal(u.version, 1)
407
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000408 # Make sure the generated UUIDs are actually unique.
409 uuids = {}
410 for u in [uuid.uuid1() for i in range(1000)]:
411 uuids[u] = 1
412 equal(len(uuids.keys()), 1000)
413
Tim Peters2adc6262006-06-13 00:30:50 +0000414 # Make sure the supplied node ID appears in the UUID.
415 u = uuid.uuid1(0)
416 equal(u.node, 0)
417 u = uuid.uuid1(0x123456789abc)
418 equal(u.node, 0x123456789abc)
419 u = uuid.uuid1(0xffffffffffff)
420 equal(u.node, 0xffffffffffff)
421
422 # Make sure the supplied clock sequence appears in the UUID.
423 u = uuid.uuid1(0x123456789abc, 0)
424 equal(u.node, 0x123456789abc)
425 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
426 u = uuid.uuid1(0x123456789abc, 0x1234)
427 equal(u.node, 0x123456789abc)
428 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
429 u.clock_seq_low, 0x1234)
430 u = uuid.uuid1(0x123456789abc, 0x3fff)
431 equal(u.node, 0x123456789abc)
432 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
433 u.clock_seq_low, 0x3fff)
434
435 def test_uuid3(self):
436 equal = self.assertEqual
437
438 # Test some known version-3 UUIDs.
439 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
440 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
441 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
442 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
443 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
444 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
445 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
446 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
447 ]:
448 equal(u.variant, uuid.RFC_4122)
449 equal(u.version, 3)
450 equal(u, uuid.UUID(v))
451 equal(str(u), v)
452
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300453 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
Tim Peters2adc6262006-06-13 00:30:50 +0000454 def test_uuid4(self):
455 equal = self.assertEqual
456
457 # Make sure uuid4() generates UUIDs that are actually version 4.
458 for u in [uuid.uuid4() for i in range(10)]:
459 equal(u.variant, uuid.RFC_4122)
460 equal(u.version, 4)
461
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000462 # Make sure the generated UUIDs are actually unique.
463 uuids = {}
Martin v. Löwis2eb8c4f2006-08-18 03:47:18 +0000464 for u in [uuid.uuid4() for i in range(1000)]:
Ka-Ping Yeeb1cb56a2006-08-16 07:02:50 +0000465 uuids[u] = 1
466 equal(len(uuids.keys()), 1000)
467
Tim Peters2adc6262006-06-13 00:30:50 +0000468 def test_uuid5(self):
469 equal = self.assertEqual
470
471 # Test some known version-5 UUIDs.
472 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
473 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
474 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
475 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
476 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
477 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
478 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
479 'cc957dd1-a972-5349-98cd-874190002798'),
480 ]:
481 equal(u.variant, uuid.RFC_4122)
482 equal(u.version, 5)
483 equal(u, uuid.UUID(v))
484 equal(str(u), v)
485
Serhiy Storchaka95787fd2013-05-31 22:34:53 +0300486 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussoren33206962010-05-05 14:48:37 +0000487 def testIssue8621(self):
Ronald Oussoren33206962010-05-05 14:48:37 +0000488 # On at least some versions of OSX uuid.uuid4 generates
489 # the same sequence of UUIDs in the parent and any
490 # children started using fork.
491 fds = os.pipe()
492 pid = os.fork()
493 if pid == 0:
494 os.close(fds[0])
495 value = uuid.uuid4()
496 os.write(fds[1], value.hex)
Ronald Oussoren91165c02010-05-05 15:32:39 +0000497 os._exit(0)
Ronald Oussoren33206962010-05-05 14:48:37 +0000498
499 else:
500 os.close(fds[1])
Richard Oudkerk045e4572013-06-10 16:27:45 +0100501 self.addCleanup(os.close, fds[0])
Ronald Oussoren33206962010-05-05 14:48:37 +0000502 parent_value = uuid.uuid4().hex
503 os.waitpid(pid, 0)
504 child_value = os.read(fds[0], 100)
505
506 self.assertNotEqual(parent_value, child_value)
507
508
509
510
Georg Brandlf102fc52006-07-27 15:05:36 +0000511
512def test_main():
513 test_support.run_unittest(TestUUID)
514
Tim Peters2adc6262006-06-13 00:30:50 +0000515if __name__ == '__main__':
Georg Brandlf102fc52006-07-27 15:05:36 +0000516 test_main()