Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 = [] |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 19 | for (string, curly, hex, bytes, bytes_le, fields, integer, urn, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | time, clock_seq, variant, version) in [ |
| 21 | ('00000000-0000-0000-0000-000000000000', |
| 22 | '{00000000-0000-0000-0000-000000000000}', |
| 23 | '00000000000000000000000000000000', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 24 | b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
| 25 | b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 33 | b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
| 34 | b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 35 | (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f), |
| 36 | 0x000102030405060708090a0b0c0d0e0f, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 37 | 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 38 | 0x607040500010203, 0x809, uuid.RESERVED_NCS, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 39 | ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
| 40 | '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', |
| 41 | '02d9e6d59467382e8f9b9300a64ac3cd', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 42 | b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
| 43 | b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 44 | (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd), |
| 45 | 0x02d9e6d59467382e8f9b9300a64ac3cd, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 46 | 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 47 | 0x82e946702d9e6d5, 0xf9b, uuid.RFC_4122, 3), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 48 | ('12345678-1234-5678-1234-567812345678', |
| 49 | '{12345678-1234-5678-1234-567812345678}', |
| 50 | '12345678123456781234567812345678', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 51 | b'\x12\x34\x56\x78'*4, |
| 52 | b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), |
| 54 | 0x12345678123456781234567812345678, |
| 55 | 'urn:uuid:12345678-1234-5678-1234-567812345678', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 56 | 0x678123412345678, 0x1234, uuid.RESERVED_NCS, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | ('6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
| 58 | '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', |
| 59 | '6ba7b8109dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 60 | b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 61 | b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 62 | (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 63 | 0x6ba7b8109dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 65 | 0x1d19dad6ba7b810, 0xb4, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | ('6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
| 67 | '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', |
| 68 | '6ba7b8119dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 69 | b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 70 | b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 71 | (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 72 | 0x6ba7b8119dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 74 | 0x1d19dad6ba7b811, 0xb4, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | ('6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
| 76 | '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', |
| 77 | '6ba7b8129dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 78 | b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 79 | b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 80 | (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 81 | 0x6ba7b8129dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 83 | 0x1d19dad6ba7b812, 0xb4, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 84 | ('6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
| 85 | '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', |
| 86 | '6ba7b8149dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 87 | b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 88 | b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 89 | (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 90 | 0x6ba7b8149dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 92 | 0x1d19dad6ba7b814, 0xb4, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | ('7d444840-9dc0-11d1-b245-5ffdce74fad2', |
| 94 | '{7d444840-9dc0-11d1-b245-5ffdce74fad2}', |
| 95 | '7d4448409dc011d1b2455ffdce74fad2', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 96 | b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
| 97 | b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 98 | (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2), |
| 99 | 0x7d4448409dc011d1b2455ffdce74fad2, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 101 | 0x1d19dc07d444840, 0x3245, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 102 | ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
| 103 | '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', |
| 104 | 'e902893a9d223c7ea7b8d6e313b71d9f', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 105 | b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
| 106 | b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 107 | (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f), |
| 108 | 0xe902893a9d223c7ea7b8d6e313b71d9f, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 110 | 0xc7e9d22e902893a, 0x27b8, uuid.RFC_4122, 3), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
| 112 | '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', |
| 113 | 'eb4240266f544ef8a4d0bb658a1fc6cf', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 114 | b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
| 115 | b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 116 | (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf), |
| 117 | 0xeb4240266f544ef8a4d0bb658a1fc6cf, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 119 | 0xef86f54eb424026, 0x24d0, uuid.RFC_4122, 4), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 121 | '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', |
| 122 | 'f81d4fae7dec11d0a76500a0c91e6bf6', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 123 | b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
| 124 | b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 125 | (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6), |
| 126 | 0xf81d4fae7dec11d0a76500a0c91e6bf6, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 128 | 0x1d07decf81d4fae, 0x2765, uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 129 | ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
| 130 | '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', |
| 131 | 'fffefdfcfffefffefffefffefdfcfbfa', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 132 | b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
| 133 | b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 134 | (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa), |
| 135 | 0xfffefdfcfffefffefffefffefdfcfbfa, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 136 | 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 137 | 0xffefffefffefdfc, 0x3ffe, uuid.RESERVED_FUTURE, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 138 | ('ffffffff-ffff-ffff-ffff-ffffffffffff', |
| 139 | '{ffffffff-ffff-ffff-ffff-ffffffffffff}', |
| 140 | 'ffffffffffffffffffffffffffffffff', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 141 | b'\xff'*16, |
| 142 | b'\xff'*16, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 143 | (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff), |
| 144 | 0xffffffffffffffffffffffffffffffff, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 145 | 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 146 | 0xfffffffffffffff, 0x3fff, uuid.RESERVED_FUTURE, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 147 | ]: |
| 148 | equivalents = [] |
| 149 | # Construct each UUID in several different ways. |
| 150 | for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex), |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 151 | uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le), |
| 152 | uuid.UUID(fields=fields), uuid.UUID(int=integer), |
| 153 | uuid.UUID(urn)]: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 158 | equal(u.bytes_le, bytes_le) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 220 | badvalue(lambda: uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 222 | badvalue(lambda: uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 223 | badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 224 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 225 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 226 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 227 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 228 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 229 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1))) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 230 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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)) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 238 | badvalue(lambda: uuid.UUID(int=1<<128)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 239 | |
| 240 | # Must supply exactly one of hex, bytes, fields, int. |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 241 | h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 242 | uuid.UUID(h) |
| 243 | uuid.UUID(hex=h) |
| 244 | uuid.UUID(bytes=b) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 245 | uuid.UUID(bytes_le=b) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 267 | |
| 268 | # Immutability. |
| 269 | u = uuid.UUID(h) |
| 270 | badtype(lambda: setattr(u, 'hex', h)) |
| 271 | badtype(lambda: setattr(u, 'bytes', b)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 272 | badtype(lambda: setattr(u, 'bytes_le', b)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 273 | badtype(lambda: setattr(u, 'fields', f)) |
| 274 | badtype(lambda: setattr(u, 'int', i)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 282 | |
| 283 | def check_node(self, node, source): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 284 | individual_group_bit = (node >> 40) & 1 |
| 285 | universal_local_bit = (node >> 40) & 2 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | message = "%012x doesn't look like a real MAC address" % node |
| 287 | self.assertEqual(individual_group_bit, 0, message) |
| 288 | self.assertEqual(universal_local_bit, 0, message) |
| 289 | self.assertNotEqual(node, 0, message) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 290 | self.assertNotEqual(node, 0xffffffffffff, message) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 291 | self.assert_(0 <= node, message) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 292 | self.assert_(node < (1 << 48), message) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 293 | |
| 294 | TestUUID.source2node[source] = node |
| 295 | if TestUUID.last_node: |
| 296 | if TestUUID.last_node != node: |
| 297 | msg = "different sources disagree on node:\n" |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 298 | for s, n in TestUUID.source2node.items(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | msg += " from source %r, node was %012x\n" % (s, n) |
| 300 | # There's actually no reason to expect the MAC addresses |
| 301 | # to agree across various methods -- e.g., a box may have |
| 302 | # multiple network interfaces, and different ways of getting |
| 303 | # a MAC address may favor different HW. |
| 304 | ##self.fail(msg) |
| 305 | else: |
| 306 | TestUUID.last_node = node |
| 307 | |
| 308 | def test_ifconfig_getnode(self): |
| 309 | import sys |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 310 | print(""" WARNING: uuid._ifconfig_getnode is unreliable on many platforms. |
| 311 | It is disabled until the code and/or test can be fixed properly.""", file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 312 | return |
| 313 | |
| 314 | import os |
| 315 | if os.name == 'posix': |
| 316 | node = uuid._ifconfig_getnode() |
| 317 | if node is not None: |
| 318 | self.check_node(node, 'ifconfig') |
| 319 | |
| 320 | def test_ipconfig_getnode(self): |
| 321 | import os |
| 322 | if os.name == 'nt': |
| 323 | node = uuid._ipconfig_getnode() |
| 324 | if node is not None: |
| 325 | self.check_node(node, 'ipconfig') |
| 326 | |
| 327 | def test_netbios_getnode(self): |
| 328 | if importable('win32wnet') and importable('netbios'): |
| 329 | self.check_node(uuid._netbios_getnode(), 'netbios') |
| 330 | |
| 331 | def test_random_getnode(self): |
| 332 | node = uuid._random_getnode() |
| 333 | self.assert_(0 <= node) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 334 | self.assert_(node < (1 <<48)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 335 | |
| 336 | def test_unixdll_getnode(self): |
| 337 | import sys |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 338 | print(""" WARNING: uuid._unixdll_getnode is unreliable on many platforms. |
| 339 | It is disabled until the code and/or test can be fixed properly.""", file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 340 | return |
| 341 | |
| 342 | import os |
| 343 | if importable('ctypes') and os.name == 'posix': |
| 344 | self.check_node(uuid._unixdll_getnode(), 'unixdll') |
| 345 | |
| 346 | def test_windll_getnode(self): |
| 347 | import os |
| 348 | if importable('ctypes') and os.name == 'nt': |
| 349 | self.check_node(uuid._windll_getnode(), 'windll') |
| 350 | |
| 351 | def test_getnode(self): |
| 352 | import sys |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 353 | print(""" WARNING: uuid.getnode is unreliable on many platforms. |
| 354 | It is disabled until the code and/or test can be fixed properly.""", file=sys.__stdout__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 355 | return |
| 356 | |
| 357 | node1 = uuid.getnode() |
| 358 | self.check_node(node1, "getnode1") |
| 359 | |
| 360 | # Test it again to ensure consistency. |
| 361 | node2 = uuid.getnode() |
| 362 | self.check_node(node2, "getnode2") |
| 363 | |
| 364 | self.assertEqual(node1, node2) |
| 365 | |
| 366 | def test_uuid1(self): |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 367 | # uuid1 requires ctypes. |
| 368 | try: |
| 369 | import ctypes |
| 370 | except ImportError: |
| 371 | return |
| 372 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 373 | equal = self.assertEqual |
| 374 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 375 | # Make sure uuid1() generates UUIDs that are actually version 1. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 376 | for u in [uuid.uuid1() for i in range(10)]: |
| 377 | equal(u.variant, uuid.RFC_4122) |
| 378 | equal(u.version, 1) |
| 379 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 380 | # Make sure the generated UUIDs are actually unique. |
| 381 | uuids = {} |
| 382 | for u in [uuid.uuid1() for i in range(1000)]: |
| 383 | uuids[u] = 1 |
| 384 | equal(len(uuids.keys()), 1000) |
| 385 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 386 | # Make sure the supplied node ID appears in the UUID. |
| 387 | u = uuid.uuid1(0) |
| 388 | equal(u.node, 0) |
| 389 | u = uuid.uuid1(0x123456789abc) |
| 390 | equal(u.node, 0x123456789abc) |
| 391 | u = uuid.uuid1(0xffffffffffff) |
| 392 | equal(u.node, 0xffffffffffff) |
| 393 | |
| 394 | # Make sure the supplied clock sequence appears in the UUID. |
| 395 | u = uuid.uuid1(0x123456789abc, 0) |
| 396 | equal(u.node, 0x123456789abc) |
| 397 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) |
| 398 | u = uuid.uuid1(0x123456789abc, 0x1234) |
| 399 | equal(u.node, 0x123456789abc) |
| 400 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 401 | u.clock_seq_low, 0x1234) |
| 402 | u = uuid.uuid1(0x123456789abc, 0x3fff) |
| 403 | equal(u.node, 0x123456789abc) |
| 404 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 405 | u.clock_seq_low, 0x3fff) |
| 406 | |
| 407 | def test_uuid3(self): |
| 408 | equal = self.assertEqual |
| 409 | |
| 410 | # Test some known version-3 UUIDs. |
| 411 | for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), |
| 412 | '6fa459ea-ee8a-3ca4-894e-db77e160355e'), |
| 413 | (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'), |
| 414 | '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), |
| 415 | (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'), |
| 416 | 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), |
| 417 | (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'), |
| 418 | '658d3002-db6b-3040-a1d1-8ddd7d189a4d'), |
| 419 | ]: |
| 420 | equal(u.variant, uuid.RFC_4122) |
| 421 | equal(u.version, 3) |
| 422 | equal(u, uuid.UUID(v)) |
| 423 | equal(str(u), v) |
| 424 | |
| 425 | def test_uuid4(self): |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 426 | # uuid4 requires ctypes. |
| 427 | try: |
| 428 | import ctypes |
| 429 | except ImportError: |
| 430 | return |
| 431 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 432 | equal = self.assertEqual |
| 433 | |
| 434 | # Make sure uuid4() generates UUIDs that are actually version 4. |
| 435 | for u in [uuid.uuid4() for i in range(10)]: |
| 436 | equal(u.variant, uuid.RFC_4122) |
| 437 | equal(u.version, 4) |
| 438 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 439 | # Make sure the generated UUIDs are actually unique. |
| 440 | uuids = {} |
| 441 | for u in [uuid.uuid4() for i in range(1000)]: |
| 442 | uuids[u] = 1 |
| 443 | equal(len(uuids.keys()), 1000) |
| 444 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 445 | def test_uuid5(self): |
| 446 | equal = self.assertEqual |
| 447 | |
| 448 | # Test some known version-5 UUIDs. |
| 449 | for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'), |
| 450 | '886313e1-3b8a-5372-9b90-0c9aee199e5d'), |
| 451 | (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'), |
| 452 | '4c565f0d-3f5a-5890-b41b-20cf47701c5e'), |
| 453 | (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'), |
| 454 | '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), |
| 455 | (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'), |
| 456 | 'cc957dd1-a972-5349-98cd-874190002798'), |
| 457 | ]: |
| 458 | equal(u.variant, uuid.RFC_4122) |
| 459 | equal(u.version, 5) |
| 460 | equal(u, uuid.UUID(v)) |
| 461 | equal(str(u), v) |
| 462 | |
| 463 | |
| 464 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 465 | support.run_unittest(TestUUID) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 466 | |
| 467 | if __name__ == '__main__': |
| 468 | test_main() |