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