1import unittest 2from test import test_support 3import io 4import os 5import uuid 6 7def importable(name): 8 try: 9 __import__(name) 10 return True 11 except: 12 return False 13 14class TestUUID(unittest.TestCase): 15 def test_UUID(self): 16 equal = self.assertEqual 17 ascending = [] 18 for (string, curly, hex, bytes, bytes_le, fields, integer, urn, 19 time, clock_seq, variant, version) in [ 20 ('00000000-0000-0000-0000-000000000000', 21 '{00000000-0000-0000-0000-000000000000}', 22 '00000000000000000000000000000000', 23 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 24 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 25 (0, 0, 0, 0, 0, 0), 26 0, 27 'urn:uuid:00000000-0000-0000-0000-000000000000', 28 0, 0, uuid.RESERVED_NCS, None), 29 ('00010203-0405-0607-0809-0a0b0c0d0e0f', 30 '{00010203-0405-0607-0809-0a0b0c0d0e0f}', 31 '000102030405060708090a0b0c0d0e0f', 32 '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', 33 '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', 34 (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL), 35 0x000102030405060708090a0b0c0d0e0fL, 36 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 37 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None), 38 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 39 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', 40 '02d9e6d59467382e8f9b9300a64ac3cd', 41 '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', 42 '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', 43 (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL), 44 0x02d9e6d59467382e8f9b9300a64ac3cdL, 45 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 46 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3), 47 ('12345678-1234-5678-1234-567812345678', 48 '{12345678-1234-5678-1234-567812345678}', 49 '12345678123456781234567812345678', 50 '\x12\x34\x56\x78'*4, 51 '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', 52 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), 53 0x12345678123456781234567812345678, 54 'urn:uuid:12345678-1234-5678-1234-567812345678', 55 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None), 56 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 57 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', 58 '6ba7b8109dad11d180b400c04fd430c8', 59 '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 60 '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 61 (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 62 0x6ba7b8109dad11d180b400c04fd430c8L, 63 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 64 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1), 65 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8', 66 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', 67 '6ba7b8119dad11d180b400c04fd430c8', 68 '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 69 '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 70 (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 71 0x6ba7b8119dad11d180b400c04fd430c8L, 72 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 73 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1), 74 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8', 75 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', 76 '6ba7b8129dad11d180b400c04fd430c8', 77 '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 78 '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 79 (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 80 0x6ba7b8129dad11d180b400c04fd430c8L, 81 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 82 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1), 83 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8', 84 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', 85 '6ba7b8149dad11d180b400c04fd430c8', 86 '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 87 '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', 88 (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L), 89 0x6ba7b8149dad11d180b400c04fd430c8L, 90 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 91 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1), 92 ('7d444840-9dc0-11d1-b245-5ffdce74fad2', 93 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}', 94 '7d4448409dc011d1b2455ffdce74fad2', 95 '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', 96 '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', 97 (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L), 98 0x7d4448409dc011d1b2455ffdce74fad2L, 99 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 100 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1), 101 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 102 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', 103 'e902893a9d223c7ea7b8d6e313b71d9f', 104 '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', 105 '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', 106 (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL), 107 0xe902893a9d223c7ea7b8d6e313b71d9fL, 108 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 109 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3), 110 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 111 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', 112 'eb4240266f544ef8a4d0bb658a1fc6cf', 113 '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', 114 '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', 115 (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL), 116 0xeb4240266f544ef8a4d0bb658a1fc6cfL, 117 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 118 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4), 119 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 120 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', 121 'f81d4fae7dec11d0a76500a0c91e6bf6', 122 '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', 123 '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', 124 (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L), 125 0xf81d4fae7dec11d0a76500a0c91e6bf6L, 126 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 127 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1), 128 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 129 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', 130 'fffefdfcfffefffefffefffefdfcfbfa', 131 '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', 132 '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', 133 (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL), 134 0xfffefdfcfffefffefffefffefdfcfbfaL, 135 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 136 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None), 137 ('ffffffff-ffff-ffff-ffff-ffffffffffff', 138 '{ffffffff-ffff-ffff-ffff-ffffffffffff}', 139 'ffffffffffffffffffffffffffffffff', 140 '\xff'*16, 141 '\xff'*16, 142 (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL), 143 0xffffffffffffffffffffffffffffffffL, 144 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', 145 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None), 146 ]: 147 equivalents = [] 148 # Construct each UUID in several different ways. 149 for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex), 150 uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le), 151 uuid.UUID(fields=fields), uuid.UUID(int=integer), 152 uuid.UUID(urn)]: 153 # Test all conversions and properties of the UUID object. 154 equal(str(u), string) 155 equal(int(u), integer) 156 equal(u.bytes, bytes) 157 equal(u.bytes_le, bytes_le) 158 equal(u.fields, fields) 159 equal(u.time_low, fields[0]) 160 equal(u.time_mid, fields[1]) 161 equal(u.time_hi_version, fields[2]) 162 equal(u.clock_seq_hi_variant, fields[3]) 163 equal(u.clock_seq_low, fields[4]) 164 equal(u.node, fields[5]) 165 equal(u.hex, hex) 166 equal(u.int, integer) 167 equal(u.urn, urn) 168 equal(u.time, time) 169 equal(u.clock_seq, clock_seq) 170 equal(u.variant, variant) 171 equal(u.version, version) 172 equivalents.append(u) 173 174 # Different construction methods should give the same UUID. 175 for u in equivalents: 176 for v in equivalents: 177 equal(u, v) 178 ascending.append(u) 179 180 # Test comparison of UUIDs. 181 for i in range(len(ascending)): 182 for j in range(len(ascending)): 183 equal(cmp(i, j), cmp(ascending[i], ascending[j])) 184 185 # Test sorting of UUIDs (above list is in ascending order). 186 resorted = ascending[:] 187 resorted.reverse() 188 resorted.sort() 189 equal(ascending, resorted) 190 191 def test_exceptions(self): 192 badvalue = lambda f: self.assertRaises(ValueError, f) 193 badtype = lambda f: self.assertRaises(TypeError, f) 194 195 # Badly formed hex strings. 196 badvalue(lambda: uuid.UUID('')) 197 badvalue(lambda: uuid.UUID('abc')) 198 badvalue(lambda: uuid.UUID('1234567812345678123456781234567')) 199 badvalue(lambda: uuid.UUID('123456781234567812345678123456789')) 200 badvalue(lambda: uuid.UUID('123456781234567812345678z2345678')) 201 202 # Badly formed bytes. 203 badvalue(lambda: uuid.UUID(bytes='abc')) 204 badvalue(lambda: uuid.UUID(bytes='\0'*15)) 205 badvalue(lambda: uuid.UUID(bytes='\0'*17)) 206 207 # Badly formed bytes_le. 208 badvalue(lambda: uuid.UUID(bytes_le='abc')) 209 badvalue(lambda: uuid.UUID(bytes_le='\0'*15)) 210 badvalue(lambda: uuid.UUID(bytes_le='\0'*17)) 211 212 # Badly formed fields. 213 badvalue(lambda: uuid.UUID(fields=(1,))) 214 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5))) 215 badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7))) 216 217 # Field values out of range. 218 badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0))) 219 badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0))) 220 badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0))) 221 badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0))) 222 badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0))) 223 badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0))) 224 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0))) 225 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0))) 226 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0))) 227 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0))) 228 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1))) 229 badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L))) 230 231 # Version number out of range. 232 badvalue(lambda: uuid.UUID('00'*16, version=0)) 233 badvalue(lambda: uuid.UUID('00'*16, version=6)) 234 235 # Integer value out of range. 236 badvalue(lambda: uuid.UUID(int=-1)) 237 badvalue(lambda: uuid.UUID(int=1<<128L)) 238 239 # Must supply exactly one of hex, bytes, fields, int. 240 h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0 241 uuid.UUID(h) 242 uuid.UUID(hex=h) 243 uuid.UUID(bytes=b) 244 uuid.UUID(bytes_le=b) 245 uuid.UUID(fields=f) 246 uuid.UUID(int=i) 247 248 # Wrong number of arguments (positional). 249 badtype(lambda: uuid.UUID()) 250 badtype(lambda: uuid.UUID(h, b)) 251 badtype(lambda: uuid.UUID(h, b, b)) 252 badtype(lambda: uuid.UUID(h, b, b, f)) 253 badtype(lambda: uuid.UUID(h, b, b, f, i)) 254 255 # Duplicate arguments. 256 for hh in [[], [('hex', h)]]: 257 for bb in [[], [('bytes', b)]]: 258 for bble in [[], [('bytes_le', b)]]: 259 for ii in [[], [('int', i)]]: 260 for ff in [[], [('fields', f)]]: 261 args = dict(hh + bb + bble + ii + ff) 262 if len(args) != 0: 263 badtype(lambda: uuid.UUID(h, **args)) 264 if len(args) != 1: 265 badtype(lambda: uuid.UUID(**args)) 266 267 # Immutability. 268 u = uuid.UUID(h) 269 badtype(lambda: setattr(u, 'hex', h)) 270 badtype(lambda: setattr(u, 'bytes', b)) 271 badtype(lambda: setattr(u, 'bytes_le', b)) 272 badtype(lambda: setattr(u, 'fields', f)) 273 badtype(lambda: setattr(u, 'int', i)) 274 badtype(lambda: setattr(u, 'time_low', 0)) 275 badtype(lambda: setattr(u, 'time_mid', 0)) 276 badtype(lambda: setattr(u, 'time_hi_version', 0)) 277 badtype(lambda: setattr(u, 'time_hi_version', 0)) 278 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0)) 279 badtype(lambda: setattr(u, 'clock_seq_low', 0)) 280 badtype(lambda: setattr(u, 'node', 0)) 281 282 def test_getnode(self): 283 node1 = uuid.getnode() 284 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1) 285 286 # Test it again to ensure consistency. 287 node2 = uuid.getnode() 288 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2)) 289 290 # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers 291 # need not necessarily be 48 bits (e.g., EUI-64). 292 def test_uuid1_eui64(self): 293 # Confirm that uuid.getnode ignores hardware addresses larger than 48 294 # bits. Mock out each platform's *_getnode helper functions to return 295 # something just larger than 48 bits to test. This will cause 296 # uuid.getnode to fall back on uuid._random_getnode, which will 297 # generate a valid value. 298 too_large_getter = lambda: 1 << 48 299 300 uuid_real__node = uuid._node 301 uuid_real__NODE_GETTERS_WIN32 = uuid._NODE_GETTERS_WIN32 302 uuid_real__NODE_GETTERS_UNIX = uuid._NODE_GETTERS_UNIX 303 uuid._node = None 304 uuid._NODE_GETTERS_WIN32 = [too_large_getter] 305 uuid._NODE_GETTERS_UNIX = [too_large_getter] 306 try: 307 node = uuid.getnode() 308 finally: 309 uuid._node = uuid_real__node 310 uuid._NODE_GETTERS_WIN32 = uuid_real__NODE_GETTERS_WIN32 311 uuid._NODE_GETTERS_UNIX = uuid_real__NODE_GETTERS_UNIX 312 313 self.assertTrue(0 < node < (1 << 48), '%012x' % node) 314 315 # Confirm that uuid1 can use the generated node, i.e., the that 316 # uuid.getnode fell back on uuid._random_getnode() rather than using 317 # the value from too_large_getter above. 318 try: 319 uuid.uuid1(node=node) 320 except ValueError as e: 321 self.fail('uuid1 was given an invalid node ID') 322 323 @unittest.skipUnless(importable('ctypes'), 'requires ctypes') 324 def test_uuid1(self): 325 equal = self.assertEqual 326 327 # Make sure uuid1() generates UUIDs that are actually version 1. 328 for u in [uuid.uuid1() for i in range(10)]: 329 equal(u.variant, uuid.RFC_4122) 330 equal(u.version, 1) 331 332 # Make sure the generated UUIDs are actually unique. 333 uuids = {} 334 for u in [uuid.uuid1() for i in range(1000)]: 335 uuids[u] = 1 336 equal(len(uuids.keys()), 1000) 337 338 # Make sure the supplied node ID appears in the UUID. 339 u = uuid.uuid1(0) 340 equal(u.node, 0) 341 u = uuid.uuid1(0x123456789abc) 342 equal(u.node, 0x123456789abc) 343 u = uuid.uuid1(0xffffffffffff) 344 equal(u.node, 0xffffffffffff) 345 346 # Make sure the supplied clock sequence appears in the UUID. 347 u = uuid.uuid1(0x123456789abc, 0) 348 equal(u.node, 0x123456789abc) 349 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) 350 u = uuid.uuid1(0x123456789abc, 0x1234) 351 equal(u.node, 0x123456789abc) 352 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | 353 u.clock_seq_low, 0x1234) 354 u = uuid.uuid1(0x123456789abc, 0x3fff) 355 equal(u.node, 0x123456789abc) 356 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | 357 u.clock_seq_low, 0x3fff) 358 359 def test_uuid3(self): 360 equal = self.assertEqual 361 362 # Test some known version-3 UUIDs. 363 for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), 364 '6fa459ea-ee8a-3ca4-894e-db77e160355e'), 365 (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'), 366 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), 367 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'), 368 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), 369 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'), 370 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'), 371 ]: 372 equal(u.variant, uuid.RFC_4122) 373 equal(u.version, 3) 374 equal(u, uuid.UUID(v)) 375 equal(str(u), v) 376 377 @unittest.skipUnless(importable('ctypes'), 'requires ctypes') 378 def test_uuid4(self): 379 equal = self.assertEqual 380 381 # Make sure uuid4() generates UUIDs that are actually version 4. 382 for u in [uuid.uuid4() for i in range(10)]: 383 equal(u.variant, uuid.RFC_4122) 384 equal(u.version, 4) 385 386 # Make sure the generated UUIDs are actually unique. 387 uuids = {} 388 for u in [uuid.uuid4() for i in range(1000)]: 389 uuids[u] = 1 390 equal(len(uuids.keys()), 1000) 391 392 def test_uuid5(self): 393 equal = self.assertEqual 394 395 # Test some known version-5 UUIDs. 396 for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'), 397 '886313e1-3b8a-5372-9b90-0c9aee199e5d'), 398 (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'), 399 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'), 400 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'), 401 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), 402 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'), 403 'cc957dd1-a972-5349-98cd-874190002798'), 404 ]: 405 equal(u.variant, uuid.RFC_4122) 406 equal(u.version, 5) 407 equal(u, uuid.UUID(v)) 408 equal(str(u), v) 409 410 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 411 def testIssue8621(self): 412 # On at least some versions of OSX uuid.uuid4 generates 413 # the same sequence of UUIDs in the parent and any 414 # children started using fork. 415 fds = os.pipe() 416 pid = os.fork() 417 if pid == 0: 418 os.close(fds[0]) 419 value = uuid.uuid4() 420 os.write(fds[1], value.hex) 421 os._exit(0) 422 423 else: 424 os.close(fds[1]) 425 self.addCleanup(os.close, fds[0]) 426 parent_value = uuid.uuid4().hex 427 os.waitpid(pid, 0) 428 child_value = os.read(fds[0], 100) 429 430 self.assertNotEqual(parent_value, child_value) 431 432 433class TestInternals(unittest.TestCase): 434 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 435 def test_find_mac(self): 436 data = '''\ 437 438fake hwaddr 439cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 440eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab 441''' 442 def mock_popen(cmd): 443 return io.BytesIO(data) 444 445 path = os.environ.get("PATH", os.defpath).split(os.pathsep) 446 path.extend(('/sbin', '/usr/sbin')) 447 for dir in path: 448 executable = os.path.join(dir, 'ifconfig') 449 if (os.path.exists(executable) and 450 os.access(executable, os.F_OK | os.X_OK) and 451 not os.path.isdir(executable)): 452 break 453 else: 454 self.skipTest('requires ifconfig') 455 456 with test_support.swap_attr(os, 'popen', mock_popen): 457 mac = uuid._find_mac( 458 command='ifconfig', 459 args='', 460 hw_identifiers=['hwaddr'], 461 get_index=lambda x: x + 1, 462 ) 463 self.assertEqual(mac, 0x1234567890ab) 464 465 def check_node(self, node, requires=None): 466 if requires and node is None: 467 self.skipTest('requires ' + requires) 468 hex = '%012x' % node 469 if test_support.verbose >= 2: 470 print hex + ' ', 471 self.assertTrue(0 < node < (1L << 48), 472 "%s is not an RFC 4122 node ID" % hex) 473 474 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 475 def test_ifconfig_getnode(self): 476 node = uuid._ifconfig_getnode() 477 self.check_node(node, 'ifconfig') 478 479 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 480 def test_arp_getnode(self): 481 node = uuid._arp_getnode() 482 self.check_node(node, 'arp') 483 484 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 485 def test_lanscan_getnode(self): 486 node = uuid._lanscan_getnode() 487 self.check_node(node, 'lanscan') 488 489 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 490 def test_netstat_getnode(self): 491 node = uuid._netstat_getnode() 492 self.check_node(node, 'netstat') 493 494 @unittest.skipUnless(os.name == 'nt', 'requires Windows') 495 def test_ipconfig_getnode(self): 496 node = uuid._ipconfig_getnode() 497 self.check_node(node, 'ipconfig') 498 499 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet') 500 @unittest.skipUnless(importable('netbios'), 'requires netbios') 501 def test_netbios_getnode(self): 502 node = uuid._netbios_getnode() 503 self.check_node(node) 504 505 def test_random_getnode(self): 506 node = uuid._random_getnode() 507 # The multicast bit, i.e. the least significant bit of first octet, 508 # must be set for randomly generated MAC addresses. See RFC 4122, 509 # $4.1.6. 510 self.assertTrue(node & (1 << 40), '%012x' % node) 511 self.check_node(node) 512 513 @unittest.skipUnless(os.name == 'posix', 'requires Posix') 514 @unittest.skipUnless(importable('ctypes'), 'requires ctypes') 515 def test_unixdll_getnode(self): 516 try: # Issues 1481, 3581: _uuid_generate_time() might be None. 517 node = uuid._unixdll_getnode() 518 except TypeError: 519 self.skipTest('requires uuid_generate_time') 520 self.check_node(node) 521 522 @unittest.skipUnless(os.name == 'nt', 'requires Windows') 523 @unittest.skipUnless(importable('ctypes'), 'requires ctypes') 524 def test_windll_getnode(self): 525 node = uuid._windll_getnode() 526 self.check_node(node) 527 528 529def test_main(): 530 test_support.run_unittest(TestUUID, TestInternals) 531 532if __name__ == '__main__': 533 test_main() 534