Georg Brandl | f102fc5 | 2006-07-27 15:05:36 +0000 | [diff] [blame] | 1 | from unittest import TestCase |
| 2 | from test import test_support |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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 |
Tim Peters | 08310d6 | 2006-07-27 20:47:24 +0000 | [diff] [blame] | 14 | source2node = {} |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 15 | |
| 16 | def test_UUID(self): |
| 17 | equal = self.assertEqual |
| 18 | ascending = [] |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 19 | for (string, curly, hex, bytes, bytes_le, fields, integer, urn, |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 25 | '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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', |
| 33 | '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 34 | '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 43 | '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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, |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 52 | '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 61 | '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 70 | '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 79 | '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 88 | '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 97 | '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 106 | '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 115 | '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 124 | '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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', |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 133 | '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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, |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 142 | '\xff'*16, |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 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), |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +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)]: |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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) |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 158 | equal(u.bytes_le, bytes_le) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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 | |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +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 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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))) |
| 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) |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 245 | uuid.UUID(bytes_le=b) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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)) |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +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)) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 255 | |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +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)) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +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)) |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 272 | badtype(lambda: setattr(u, 'bytes_le', b)) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 273 | badtype(lambda: setattr(u, 'fields', f)) |
| 274 | badtype(lambda: setattr(u, 'int', i)) |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +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)) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 282 | |
Tim Peters | 08310d6 | 2006-07-27 20:47:24 +0000 | [diff] [blame] | 283 | def check_node(self, node, source): |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 284 | individual_group_bit = (node >> 40L) & 1 |
| 285 | universal_local_bit = (node >> 40L) & 2 |
| 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) |
| 290 | self.assertNotEqual(node, 0xffffffffffffL, message) |
| 291 | self.assert_(0 <= node, message) |
Tim Peters | 08310d6 | 2006-07-27 20:47:24 +0000 | [diff] [blame] | 292 | self.assert_(node < (1L << 48), message) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 293 | |
Tim Peters | 08310d6 | 2006-07-27 20:47:24 +0000 | [diff] [blame] | 294 | TestUUID.source2node[source] = node |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 295 | if TestUUID.last_node: |
Tim Peters | 08310d6 | 2006-07-27 20:47:24 +0000 | [diff] [blame] | 296 | if TestUUID.last_node != node: |
| 297 | msg = "different sources disagree on node:\n" |
| 298 | for s, n in TestUUID.source2node.iteritems(): |
| 299 | msg += " from source %r, node was %012x\n" % (s, n) |
Tim Peters | 750c442 | 2006-07-28 04:51:59 +0000 | [diff] [blame] | 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) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 305 | else: |
| 306 | TestUUID.last_node = node |
| 307 | |
| 308 | def test_ifconfig_getnode(self): |
Neal Norwitz | 2fde3bd | 2006-07-29 19:29:35 +0000 | [diff] [blame] | 309 | import sys |
| 310 | print >>sys.__stdout__, \ |
Tim Peters | da9face | 2006-07-30 00:58:15 +0000 | [diff] [blame] | 311 | """ WARNING: uuid._ifconfig_getnode is unreliable on many platforms. |
Neal Norwitz | 2fde3bd | 2006-07-29 19:29:35 +0000 | [diff] [blame] | 312 | It is disabled until the code and/or test can be fixed properly.""" |
| 313 | return |
| 314 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 315 | import os |
| 316 | if os.name == 'posix': |
Georg Brandl | 75a832d | 2006-07-27 16:08:15 +0000 | [diff] [blame] | 317 | node = uuid._ifconfig_getnode() |
| 318 | if node is not None: |
| 319 | self.check_node(node, 'ifconfig') |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 320 | |
| 321 | def test_ipconfig_getnode(self): |
| 322 | import os |
| 323 | if os.name == 'nt': |
Georg Brandl | 75a832d | 2006-07-27 16:08:15 +0000 | [diff] [blame] | 324 | node = uuid._ipconfig_getnode() |
| 325 | if node is not None: |
| 326 | self.check_node(node, 'ipconfig') |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 327 | |
| 328 | def test_netbios_getnode(self): |
| 329 | if importable('win32wnet') and importable('netbios'): |
| 330 | self.check_node(uuid._netbios_getnode(), 'netbios') |
| 331 | |
| 332 | def test_random_getnode(self): |
| 333 | node = uuid._random_getnode() |
| 334 | self.assert_(0 <= node) |
Tim Peters | 750c442 | 2006-07-28 04:51:59 +0000 | [diff] [blame] | 335 | self.assert_(node < (1L <<48)) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 336 | |
| 337 | def test_unixdll_getnode(self): |
Neal Norwitz | 2fde3bd | 2006-07-29 19:29:35 +0000 | [diff] [blame] | 338 | import sys |
| 339 | print >>sys.__stdout__, \ |
Tim Peters | da9face | 2006-07-30 00:58:15 +0000 | [diff] [blame] | 340 | """ WARNING: uuid._unixdll_getnode is unreliable on many platforms. |
Neal Norwitz | 2fde3bd | 2006-07-29 19:29:35 +0000 | [diff] [blame] | 341 | It is disabled until the code and/or test can be fixed properly.""" |
| 342 | return |
| 343 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 344 | import os |
| 345 | if importable('ctypes') and os.name == 'posix': |
| 346 | self.check_node(uuid._unixdll_getnode(), 'unixdll') |
| 347 | |
| 348 | def test_windll_getnode(self): |
| 349 | import os |
| 350 | if importable('ctypes') and os.name == 'nt': |
| 351 | self.check_node(uuid._windll_getnode(), 'windll') |
| 352 | |
| 353 | def test_getnode(self): |
Neal Norwitz | b5a701b | 2006-07-29 20:37:08 +0000 | [diff] [blame] | 354 | import sys |
| 355 | print >>sys.__stdout__, \ |
Tim Peters | da9face | 2006-07-30 00:58:15 +0000 | [diff] [blame] | 356 | """ WARNING: uuid.getnode is unreliable on many platforms. |
Neal Norwitz | b5a701b | 2006-07-29 20:37:08 +0000 | [diff] [blame] | 357 | It is disabled until the code and/or test can be fixed properly.""" |
| 358 | return |
| 359 | |
Tim Peters | 750c442 | 2006-07-28 04:51:59 +0000 | [diff] [blame] | 360 | node1 = uuid.getnode() |
| 361 | self.check_node(node1, "getnode1") |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 362 | |
| 363 | # Test it again to ensure consistency. |
Tim Peters | 750c442 | 2006-07-28 04:51:59 +0000 | [diff] [blame] | 364 | node2 = uuid.getnode() |
| 365 | self.check_node(node2, "getnode2") |
| 366 | |
| 367 | self.assertEqual(node1, node2) |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 368 | |
| 369 | def test_uuid1(self): |
| 370 | equal = self.assertEqual |
| 371 | |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 372 | # Make sure uuid1() generates UUIDs that are actually version 1. |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 373 | for u in [uuid.uuid1() for i in range(10)]: |
| 374 | equal(u.variant, uuid.RFC_4122) |
| 375 | equal(u.version, 1) |
| 376 | |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 377 | # Make sure the generated UUIDs are actually unique. |
| 378 | uuids = {} |
| 379 | for u in [uuid.uuid1() for i in range(1000)]: |
| 380 | uuids[u] = 1 |
| 381 | equal(len(uuids.keys()), 1000) |
| 382 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 383 | # Make sure the supplied node ID appears in the UUID. |
| 384 | u = uuid.uuid1(0) |
| 385 | equal(u.node, 0) |
| 386 | u = uuid.uuid1(0x123456789abc) |
| 387 | equal(u.node, 0x123456789abc) |
| 388 | u = uuid.uuid1(0xffffffffffff) |
| 389 | equal(u.node, 0xffffffffffff) |
| 390 | |
| 391 | # Make sure the supplied clock sequence appears in the UUID. |
| 392 | u = uuid.uuid1(0x123456789abc, 0) |
| 393 | equal(u.node, 0x123456789abc) |
| 394 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) |
| 395 | u = uuid.uuid1(0x123456789abc, 0x1234) |
| 396 | equal(u.node, 0x123456789abc) |
| 397 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 398 | u.clock_seq_low, 0x1234) |
| 399 | u = uuid.uuid1(0x123456789abc, 0x3fff) |
| 400 | equal(u.node, 0x123456789abc) |
| 401 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 402 | u.clock_seq_low, 0x3fff) |
| 403 | |
| 404 | def test_uuid3(self): |
| 405 | equal = self.assertEqual |
| 406 | |
| 407 | # Test some known version-3 UUIDs. |
| 408 | for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), |
| 409 | '6fa459ea-ee8a-3ca4-894e-db77e160355e'), |
| 410 | (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'), |
| 411 | '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), |
| 412 | (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'), |
| 413 | 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), |
| 414 | (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'), |
| 415 | '658d3002-db6b-3040-a1d1-8ddd7d189a4d'), |
| 416 | ]: |
| 417 | equal(u.variant, uuid.RFC_4122) |
| 418 | equal(u.version, 3) |
| 419 | equal(u, uuid.UUID(v)) |
| 420 | equal(str(u), v) |
| 421 | |
| 422 | def test_uuid4(self): |
| 423 | equal = self.assertEqual |
| 424 | |
| 425 | # Make sure uuid4() generates UUIDs that are actually version 4. |
| 426 | for u in [uuid.uuid4() for i in range(10)]: |
| 427 | equal(u.variant, uuid.RFC_4122) |
| 428 | equal(u.version, 4) |
| 429 | |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 430 | # Make sure the generated UUIDs are actually unique. |
| 431 | uuids = {} |
Martin v. Löwis | 2eb8c4f | 2006-08-18 03:47:18 +0000 | [diff] [blame] | 432 | for u in [uuid.uuid4() for i in range(1000)]: |
Ka-Ping Yee | b1cb56a | 2006-08-16 07:02:50 +0000 | [diff] [blame] | 433 | uuids[u] = 1 |
| 434 | equal(len(uuids.keys()), 1000) |
| 435 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 436 | def test_uuid5(self): |
| 437 | equal = self.assertEqual |
| 438 | |
| 439 | # Test some known version-5 UUIDs. |
| 440 | for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'), |
| 441 | '886313e1-3b8a-5372-9b90-0c9aee199e5d'), |
| 442 | (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'), |
| 443 | '4c565f0d-3f5a-5890-b41b-20cf47701c5e'), |
| 444 | (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'), |
| 445 | '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), |
| 446 | (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'), |
| 447 | 'cc957dd1-a972-5349-98cd-874190002798'), |
| 448 | ]: |
| 449 | equal(u.variant, uuid.RFC_4122) |
| 450 | equal(u.version, 5) |
| 451 | equal(u, uuid.UUID(v)) |
| 452 | equal(str(u), v) |
| 453 | |
Georg Brandl | f102fc5 | 2006-07-27 15:05:36 +0000 | [diff] [blame] | 454 | |
| 455 | def test_main(): |
| 456 | test_support.run_unittest(TestUUID) |
| 457 | |
Tim Peters | 2adc626 | 2006-06-13 00:30:50 +0000 | [diff] [blame] | 458 | if __name__ == '__main__': |
Georg Brandl | f102fc5 | 2006-07-27 15:05:36 +0000 | [diff] [blame] | 459 | test_main() |