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