| 1 |
import unittest |
| 2 |
from test import support |
| 3 |
from test.support import import_helper |
| 4 |
from test.support.script_helper import assert_python_ok |
| 5 |
import builtins |
| 6 |
import contextlib |
| 7 |
import copy |
| 8 |
import enum |
| 9 |
import io |
| 10 |
import os |
| 11 |
import pickle |
| 12 |
import sys |
| 13 |
import weakref |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid']) |
| 17 |
c_uuid = import_helper.import_fresh_module('uuid', fresh=['_uuid']) |
| 18 |
|
| 19 |
def importable(name): |
| 20 |
try: |
| 21 |
__import__(name) |
| 22 |
return True |
| 23 |
except ModuleNotFoundError: |
| 24 |
return False |
| 25 |
|
| 26 |
|
| 27 |
def mock_get_command_stdout(data): |
| 28 |
def get_command_stdout(command, args): |
| 29 |
return io.BytesIO(data.encode()) |
| 30 |
return get_command_stdout |
| 31 |
|
| 32 |
|
| 33 |
class BaseTestUUID: |
| 34 |
uuid = None |
| 35 |
|
| 36 |
def test_safe_uuid_enum(self): |
| 37 |
class CheckedSafeUUID(enum.Enum): |
| 38 |
safe = 0 |
| 39 |
unsafe = -1 |
| 40 |
unknown = None |
| 41 |
enum._test_simple_enum(CheckedSafeUUID, py_uuid.SafeUUID) |
| 42 |
|
| 43 |
def test_UUID(self): |
| 44 |
equal = self.assertEqual |
| 45 |
ascending = [] |
| 46 |
for (string, curly, hex, bytes, bytes_le, fields, integer, urn, |
| 47 |
time, clock_seq, variant, version) in [ |
| 48 |
('00000000-0000-0000-0000-000000000000', |
| 49 |
'{00000000-0000-0000-0000-000000000000}', |
| 50 |
'00000000000000000000000000000000', |
| 51 |
b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
| 52 |
b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
| 53 |
(0, 0, 0, 0, 0, 0), |
| 54 |
0, |
| 55 |
'urn:uuid:00000000-0000-0000-0000-000000000000', |
| 56 |
0, 0, self.uuid.RESERVED_NCS, None), |
| 57 |
('00010203-0405-0607-0809-0a0b0c0d0e0f', |
| 58 |
'{00010203-0405-0607-0809-0a0b0c0d0e0f}', |
| 59 |
'000102030405060708090a0b0c0d0e0f', |
| 60 |
b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
| 61 |
b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
| 62 |
(0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f), |
| 63 |
0x000102030405060708090a0b0c0d0e0f, |
| 64 |
'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', |
| 65 |
0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None), |
| 66 |
('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
| 67 |
'{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', |
| 68 |
'02d9e6d59467382e8f9b9300a64ac3cd', |
| 69 |
b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
| 70 |
b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
| 71 |
(0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd), |
| 72 |
0x02d9e6d59467382e8f9b9300a64ac3cd, |
| 73 |
'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
| 74 |
0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3), |
| 75 |
('12345678-1234-5678-1234-567812345678', |
| 76 |
'{12345678-1234-5678-1234-567812345678}', |
| 77 |
'12345678123456781234567812345678', |
| 78 |
b'\x12\x34\x56\x78'*4, |
| 79 |
b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', |
| 80 |
(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), |
| 81 |
0x12345678123456781234567812345678, |
| 82 |
'urn:uuid:12345678-1234-5678-1234-567812345678', |
| 83 |
0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None), |
| 84 |
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
| 85 |
'{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', |
| 86 |
'6ba7b8109dad11d180b400c04fd430c8', |
| 87 |
b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 88 |
b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 89 |
(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 90 |
0x6ba7b8109dad11d180b400c04fd430c8, |
| 91 |
'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
| 92 |
0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1), |
| 93 |
('6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
| 94 |
'{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', |
| 95 |
'6ba7b8119dad11d180b400c04fd430c8', |
| 96 |
b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 97 |
b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 98 |
(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 99 |
0x6ba7b8119dad11d180b400c04fd430c8, |
| 100 |
'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
| 101 |
0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1), |
| 102 |
('6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
| 103 |
'{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', |
| 104 |
'6ba7b8129dad11d180b400c04fd430c8', |
| 105 |
b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 106 |
b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 107 |
(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 108 |
0x6ba7b8129dad11d180b400c04fd430c8, |
| 109 |
'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
| 110 |
0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1), |
| 111 |
('6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
| 112 |
'{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', |
| 113 |
'6ba7b8149dad11d180b400c04fd430c8', |
| 114 |
b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 115 |
b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 116 |
(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 117 |
0x6ba7b8149dad11d180b400c04fd430c8, |
| 118 |
'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
| 119 |
0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1), |
| 120 |
('7d444840-9dc0-11d1-b245-5ffdce74fad2', |
| 121 |
'{7d444840-9dc0-11d1-b245-5ffdce74fad2}', |
| 122 |
'7d4448409dc011d1b2455ffdce74fad2', |
| 123 |
b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
| 124 |
b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
| 125 |
(0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2), |
| 126 |
0x7d4448409dc011d1b2455ffdce74fad2, |
| 127 |
'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', |
| 128 |
0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1), |
| 129 |
('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
| 130 |
'{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', |
| 131 |
'e902893a9d223c7ea7b8d6e313b71d9f', |
| 132 |
b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
| 133 |
b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
| 134 |
(0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f), |
| 135 |
0xe902893a9d223c7ea7b8d6e313b71d9f, |
| 136 |
'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
| 137 |
0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3), |
| 138 |
('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
| 139 |
'{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', |
| 140 |
'eb4240266f544ef8a4d0bb658a1fc6cf', |
| 141 |
b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
| 142 |
b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
| 143 |
(0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf), |
| 144 |
0xeb4240266f544ef8a4d0bb658a1fc6cf, |
| 145 |
'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
| 146 |
0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4), |
| 147 |
('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 148 |
'{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', |
| 149 |
'f81d4fae7dec11d0a76500a0c91e6bf6', |
| 150 |
b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
| 151 |
b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
| 152 |
(0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6), |
| 153 |
0xf81d4fae7dec11d0a76500a0c91e6bf6, |
| 154 |
'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 155 |
0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1), |
| 156 |
('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
| 157 |
'{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', |
| 158 |
'fffefdfcfffefffefffefffefdfcfbfa', |
| 159 |
b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
| 160 |
b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
| 161 |
(0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa), |
| 162 |
0xfffefdfcfffefffefffefffefdfcfbfa, |
| 163 |
'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
| 164 |
0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None), |
| 165 |
('ffffffff-ffff-ffff-ffff-ffffffffffff', |
| 166 |
'{ffffffff-ffff-ffff-ffff-ffffffffffff}', |
| 167 |
'ffffffffffffffffffffffffffffffff', |
| 168 |
b'\xff'*16, |
| 169 |
b'\xff'*16, |
| 170 |
(0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff), |
| 171 |
0xffffffffffffffffffffffffffffffff, |
| 172 |
'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', |
| 173 |
0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None), |
| 174 |
]: |
| 175 |
equivalents = [] |
| 176 |
# Construct each UUID in several different ways. |
| 177 |
for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex), |
| 178 |
self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le), |
| 179 |
self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer), |
| 180 |
self.uuid.UUID(urn)]: |
| 181 |
# Test all conversions and properties of the UUID object. |
| 182 |
equal(str(u), string) |
| 183 |
equal(int(u), integer) |
| 184 |
equal(u.bytes, bytes) |
| 185 |
equal(u.bytes_le, bytes_le) |
| 186 |
equal(u.fields, fields) |
| 187 |
equal(u.time_low, fields[0]) |
| 188 |
equal(u.time_mid, fields[1]) |
| 189 |
equal(u.time_hi_version, fields[2]) |
| 190 |
equal(u.clock_seq_hi_variant, fields[3]) |
| 191 |
equal(u.clock_seq_low, fields[4]) |
| 192 |
equal(u.node, fields[5]) |
| 193 |
equal(u.hex, hex) |
| 194 |
equal(u.int, integer) |
| 195 |
equal(u.urn, urn) |
| 196 |
equal(u.time, time) |
| 197 |
equal(u.clock_seq, clock_seq) |
| 198 |
equal(u.variant, variant) |
| 199 |
equal(u.version, version) |
| 200 |
equivalents.append(u) |
| 201 |
|
| 202 |
# Different construction methods should give the same UUID. |
| 203 |
for u in equivalents: |
| 204 |
for v in equivalents: |
| 205 |
equal(u, v) |
| 206 |
|
| 207 |
# Bug 7380: "bytes" and "bytes_le" should give the same type. |
| 208 |
equal(type(u.bytes), builtins.bytes) |
| 209 |
equal(type(u.bytes_le), builtins.bytes) |
| 210 |
|
| 211 |
ascending.append(u) |
| 212 |
|
| 213 |
# Test comparison of UUIDs. |
| 214 |
for i in range(len(ascending)): |
| 215 |
for j in range(len(ascending)): |
| 216 |
equal(i < j, ascending[i] < ascending[j]) |
| 217 |
equal(i <= j, ascending[i] <= ascending[j]) |
| 218 |
equal(i == j, ascending[i] == ascending[j]) |
| 219 |
equal(i > j, ascending[i] > ascending[j]) |
| 220 |
equal(i >= j, ascending[i] >= ascending[j]) |
| 221 |
equal(i != j, ascending[i] != ascending[j]) |
| 222 |
|
| 223 |
# Test sorting of UUIDs (above list is in ascending order). |
| 224 |
resorted = ascending[:] |
| 225 |
resorted.reverse() |
| 226 |
resorted.sort() |
| 227 |
equal(ascending, resorted) |
| 228 |
|
| 229 |
def test_exceptions(self): |
| 230 |
badvalue = lambda f: self.assertRaises(ValueError, f) |
| 231 |
badtype = lambda f: self.assertRaises(TypeError, f) |
| 232 |
|
| 233 |
# Badly formed hex strings. |
| 234 |
badvalue(lambda: self.uuid.UUID('')) |
| 235 |
badvalue(lambda: self.uuid.UUID('abc')) |
| 236 |
badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567')) |
| 237 |
badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789')) |
| 238 |
badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678')) |
| 239 |
|
| 240 |
# Badly formed bytes. |
| 241 |
badvalue(lambda: self.uuid.UUID(bytes='abc')) |
| 242 |
badvalue(lambda: self.uuid.UUID(bytes='\0'*15)) |
| 243 |
badvalue(lambda: self.uuid.UUID(bytes='\0'*17)) |
| 244 |
|
| 245 |
# Badly formed bytes_le. |
| 246 |
badvalue(lambda: self.uuid.UUID(bytes_le='abc')) |
| 247 |
badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15)) |
| 248 |
badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17)) |
| 249 |
|
| 250 |
# Badly formed fields. |
| 251 |
badvalue(lambda: self.uuid.UUID(fields=(1,))) |
| 252 |
badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5))) |
| 253 |
badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7))) |
| 254 |
|
| 255 |
# Field values out of range. |
| 256 |
badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0))) |
| 257 |
badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0))) |
| 258 |
badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0))) |
| 259 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0))) |
| 260 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0))) |
| 261 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0))) |
| 262 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0))) |
| 263 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0))) |
| 264 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0))) |
| 265 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0))) |
| 266 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1))) |
| 267 |
badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000))) |
| 268 |
|
| 269 |
# Version number out of range. |
| 270 |
badvalue(lambda: self.uuid.UUID('00'*16, version=0)) |
| 271 |
badvalue(lambda: self.uuid.UUID('00'*16, version=6)) |
| 272 |
|
| 273 |
# Integer value out of range. |
| 274 |
badvalue(lambda: self.uuid.UUID(int=-1)) |
| 275 |
badvalue(lambda: self.uuid.UUID(int=1<<128)) |
| 276 |
|
| 277 |
# Must supply exactly one of hex, bytes, fields, int. |
| 278 |
h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0 |
| 279 |
self.uuid.UUID(h) |
| 280 |
self.uuid.UUID(hex=h) |
| 281 |
self.uuid.UUID(bytes=b) |
| 282 |
self.uuid.UUID(bytes_le=b) |
| 283 |
self.uuid.UUID(fields=f) |
| 284 |
self.uuid.UUID(int=i) |
| 285 |
|
| 286 |
# Wrong number of arguments (positional). |
| 287 |
badtype(lambda: self.uuid.UUID()) |
| 288 |
badtype(lambda: self.uuid.UUID(h, b)) |
| 289 |
badtype(lambda: self.uuid.UUID(h, b, b)) |
| 290 |
badtype(lambda: self.uuid.UUID(h, b, b, f)) |
| 291 |
badtype(lambda: self.uuid.UUID(h, b, b, f, i)) |
| 292 |
|
| 293 |
# Duplicate arguments. |
| 294 |
for hh in [[], [('hex', h)]]: |
| 295 |
for bb in [[], [('bytes', b)]]: |
| 296 |
for bble in [[], [('bytes_le', b)]]: |
| 297 |
for ii in [[], [('int', i)]]: |
| 298 |
for ff in [[], [('fields', f)]]: |
| 299 |
args = dict(hh + bb + bble + ii + ff) |
| 300 |
if len(args) != 0: |
| 301 |
badtype(lambda: self.uuid.UUID(h, **args)) |
| 302 |
if len(args) != 1: |
| 303 |
badtype(lambda: self.uuid.UUID(**args)) |
| 304 |
|
| 305 |
# Immutability. |
| 306 |
u = self.uuid.UUID(h) |
| 307 |
badtype(lambda: setattr(u, 'hex', h)) |
| 308 |
badtype(lambda: setattr(u, 'bytes', b)) |
| 309 |
badtype(lambda: setattr(u, 'bytes_le', b)) |
| 310 |
badtype(lambda: setattr(u, 'fields', f)) |
| 311 |
badtype(lambda: setattr(u, 'int', i)) |
| 312 |
badtype(lambda: setattr(u, 'time_low', 0)) |
| 313 |
badtype(lambda: setattr(u, 'time_mid', 0)) |
| 314 |
badtype(lambda: setattr(u, 'time_hi_version', 0)) |
| 315 |
badtype(lambda: setattr(u, 'time_hi_version', 0)) |
| 316 |
badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0)) |
| 317 |
badtype(lambda: setattr(u, 'clock_seq_low', 0)) |
| 318 |
badtype(lambda: setattr(u, 'node', 0)) |
| 319 |
|
| 320 |
# Comparison with a non-UUID object |
| 321 |
badtype(lambda: u < object()) |
| 322 |
badtype(lambda: u > object()) |
| 323 |
|
| 324 |
def test_getnode(self): |
| 325 |
node1 = self.uuid.getnode() |
| 326 |
self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1) |
| 327 |
|
| 328 |
# Test it again to ensure consistency. |
| 329 |
node2 = self.uuid.getnode() |
| 330 |
self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2)) |
| 331 |
|
| 332 |
def test_pickle_roundtrip(self): |
| 333 |
def check(actual, expected): |
| 334 |
self.assertEqual(actual, expected) |
| 335 |
self.assertEqual(actual.is_safe, expected.is_safe) |
| 336 |
|
| 337 |
with support.swap_item(sys.modules, 'uuid', self.uuid): |
| 338 |
for is_safe in self.uuid.SafeUUID: |
| 339 |
u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 340 |
is_safe=is_safe) |
| 341 |
check(copy.copy(u), u) |
| 342 |
check(copy.deepcopy(u), u) |
| 343 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 344 |
with self.subTest(protocol=proto): |
| 345 |
check(pickle.loads(pickle.dumps(u, proto)), u) |
| 346 |
|
| 347 |
def test_unpickle_previous_python_versions(self): |
| 348 |
def check(actual, expected): |
| 349 |
self.assertEqual(actual, expected) |
| 350 |
self.assertEqual(actual.is_safe, expected.is_safe) |
| 351 |
|
| 352 |
pickled_uuids = [ |
| 353 |
# Python 2.7, protocol 0 |
| 354 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 355 |
b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.', |
| 356 |
# Python 2.7, protocol 1 |
| 357 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 358 |
b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.', |
| 359 |
# Python 2.7, protocol 2 |
| 360 |
b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}' |
| 361 |
b'\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 362 |
# Python 3.6, protocol 0 |
| 363 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 364 |
b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.', |
| 365 |
# Python 3.6, protocol 1 |
| 366 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 367 |
b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L' |
| 368 |
b'\nsb.', |
| 369 |
# Python 3.6, protocol 2 |
| 370 |
b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 371 |
b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 372 |
# Python 3.6, protocol 3 |
| 373 |
b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 374 |
b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 375 |
# Python 3.6, protocol 4 |
| 376 |
b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI' |
| 377 |
b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%' |
| 378 |
b'\xd8\x00sb.', |
| 379 |
# Python 3.7, protocol 0 |
| 380 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 381 |
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 382 |
b'cuuid\nSafeUUID\n(NtRsb.', |
| 383 |
# Python 3.7, protocol 1 |
| 384 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 385 |
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 386 |
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.', |
| 387 |
# Python 3.7, protocol 2 |
| 388 |
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 389 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 390 |
b'id\nSafeUUID\nN\x85Rub.', |
| 391 |
# Python 3.7, protocol 3 |
| 392 |
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 393 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 394 |
b'id\nSafeUUID\nN\x85Rub.', |
| 395 |
# Python 3.7, protocol 4 |
| 396 |
b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 397 |
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 398 |
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub' |
| 399 |
b'.', |
| 400 |
] |
| 401 |
pickled_uuids_safe = [ |
| 402 |
# Python 3.7, protocol 0 |
| 403 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 404 |
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 405 |
b'cuuid\nSafeUUID\n(I0\ntRsb.', |
| 406 |
# Python 3.7, protocol 1 |
| 407 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 408 |
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 409 |
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.', |
| 410 |
# Python 3.7, protocol 2 |
| 411 |
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 412 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 413 |
b'id\nSafeUUID\nK\x00\x85Rub.', |
| 414 |
# Python 3.7, protocol 3 |
| 415 |
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 416 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 417 |
b'id\nSafeUUID\nK\x00\x85Rub.', |
| 418 |
# Python 3.7, protocol 4 |
| 419 |
b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 420 |
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 421 |
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00' |
| 422 |
b'\x85Rub.', |
| 423 |
] |
| 424 |
pickled_uuids_unsafe = [ |
| 425 |
# Python 3.7, protocol 0 |
| 426 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 427 |
b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 428 |
b'cuuid\nSafeUUID\n(I-1\ntRsb.', |
| 429 |
# Python 3.7, protocol 1 |
| 430 |
b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 431 |
b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 432 |
b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR' |
| 433 |
b'ub.', |
| 434 |
# Python 3.7, protocol 2 |
| 435 |
b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 436 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 437 |
b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 438 |
# Python 3.7, protocol 3 |
| 439 |
b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 440 |
b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 441 |
b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 442 |
# Python 3.7, protocol 4 |
| 443 |
b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 444 |
b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 445 |
b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff' |
| 446 |
b'\xff\xff\xff\x85Rub.', |
| 447 |
] |
| 448 |
|
| 449 |
u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5') |
| 450 |
u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 451 |
is_safe=self.uuid.SafeUUID.safe) |
| 452 |
u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 453 |
is_safe=self.uuid.SafeUUID.unsafe) |
| 454 |
|
| 455 |
with support.swap_item(sys.modules, 'uuid', self.uuid): |
| 456 |
for pickled in pickled_uuids: |
| 457 |
# is_safe was added in 3.7. When unpickling values from older |
| 458 |
# versions, is_safe will be missing, so it should be set to |
| 459 |
# SafeUUID.unknown. |
| 460 |
check(pickle.loads(pickled), u) |
| 461 |
for pickled in pickled_uuids_safe: |
| 462 |
check(pickle.loads(pickled), u_safe) |
| 463 |
for pickled in pickled_uuids_unsafe: |
| 464 |
check(pickle.loads(pickled), u_unsafe) |
| 465 |
|
| 466 |
# bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers |
| 467 |
# need not necessarily be 48 bits (e.g., EUI-64). |
| 468 |
def test_uuid1_eui64(self): |
| 469 |
# Confirm that uuid.getnode ignores hardware addresses larger than 48 |
| 470 |
# bits. Mock out each platform's *_getnode helper functions to return |
| 471 |
# something just larger than 48 bits to test. This will cause |
| 472 |
# uuid.getnode to fall back on uuid._random_getnode, which will |
| 473 |
# generate a valid value. |
| 474 |
too_large_getter = lambda: 1 << 48 |
| 475 |
with mock.patch.multiple( |
| 476 |
self.uuid, |
| 477 |
_node=None, # Ignore any cached node value. |
| 478 |
_GETTERS=[too_large_getter], |
| 479 |
): |
| 480 |
node = self.uuid.getnode() |
| 481 |
self.assertTrue(0 < node < (1 << 48), '%012x' % node) |
| 482 |
|
| 483 |
# Confirm that uuid1 can use the generated node, i.e., the that |
| 484 |
# uuid.getnode fell back on uuid._random_getnode() rather than using |
| 485 |
# the value from too_large_getter above. |
| 486 |
try: |
| 487 |
self.uuid.uuid1(node=node) |
| 488 |
except ValueError: |
| 489 |
self.fail('uuid1 was given an invalid node ID') |
| 490 |
|
| 491 |
def test_uuid1(self): |
| 492 |
equal = self.assertEqual |
| 493 |
|
| 494 |
# Make sure uuid1() generates UUIDs that are actually version 1. |
| 495 |
for u in [self.uuid.uuid1() for i in range(10)]: |
| 496 |
equal(u.variant, self.uuid.RFC_4122) |
| 497 |
equal(u.version, 1) |
| 498 |
self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe, |
| 499 |
self.uuid.SafeUUID.unsafe, |
| 500 |
self.uuid.SafeUUID.unknown}) |
| 501 |
|
| 502 |
# Make sure the generated UUIDs are actually unique. |
| 503 |
uuids = {} |
| 504 |
for u in [self.uuid.uuid1() for i in range(1000)]: |
| 505 |
uuids[u] = 1 |
| 506 |
equal(len(uuids.keys()), 1000) |
| 507 |
|
| 508 |
# Make sure the supplied node ID appears in the UUID. |
| 509 |
u = self.uuid.uuid1(0) |
| 510 |
equal(u.node, 0) |
| 511 |
u = self.uuid.uuid1(0x123456789abc) |
| 512 |
equal(u.node, 0x123456789abc) |
| 513 |
u = self.uuid.uuid1(0xffffffffffff) |
| 514 |
equal(u.node, 0xffffffffffff) |
| 515 |
|
| 516 |
# Make sure the supplied clock sequence appears in the UUID. |
| 517 |
u = self.uuid.uuid1(0x123456789abc, 0) |
| 518 |
equal(u.node, 0x123456789abc) |
| 519 |
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) |
| 520 |
u = self.uuid.uuid1(0x123456789abc, 0x1234) |
| 521 |
equal(u.node, 0x123456789abc) |
| 522 |
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 523 |
u.clock_seq_low, 0x1234) |
| 524 |
u = self.uuid.uuid1(0x123456789abc, 0x3fff) |
| 525 |
equal(u.node, 0x123456789abc) |
| 526 |
equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 527 |
u.clock_seq_low, 0x3fff) |
| 528 |
|
| 529 |
# bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns |
| 530 |
# self.uuid.SafeUUID.unknown |
| 531 |
@support.requires_mac_ver(10, 5) |
| 532 |
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
| 533 |
def test_uuid1_safe(self): |
| 534 |
try: |
| 535 |
import _uuid |
| 536 |
except ImportError: |
| 537 |
has_uuid_generate_time_safe = False |
| 538 |
else: |
| 539 |
has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe |
| 540 |
|
| 541 |
if not has_uuid_generate_time_safe or not self.uuid._generate_time_safe: |
| 542 |
self.skipTest('requires uuid_generate_time_safe(3)') |
| 543 |
|
| 544 |
u = self.uuid.uuid1() |
| 545 |
# uuid_generate_time_safe() may return 0 or -1 but what it returns is |
| 546 |
# dependent on the underlying platform support. At least it cannot be |
| 547 |
# unknown (unless I suppose the platform is buggy). |
| 548 |
self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
| 549 |
|
| 550 |
@contextlib.contextmanager |
| 551 |
def mock_generate_time_safe(self, safe_value): |
| 552 |
""" |
| 553 |
Mock uuid._generate_time_safe() to return a given *safe_value*. |
| 554 |
""" |
| 555 |
if os.name != 'posix': |
| 556 |
self.skipTest('POSIX-only test') |
| 557 |
f = self.uuid._generate_time_safe |
| 558 |
if f is None: |
| 559 |
self.skipTest('need uuid._generate_time_safe') |
| 560 |
with mock.patch.object(self.uuid, '_generate_time_safe', |
| 561 |
lambda: (f()[0], safe_value)): |
| 562 |
yield |
| 563 |
|
| 564 |
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
| 565 |
def test_uuid1_unknown(self): |
| 566 |
# Even if the platform has uuid_generate_time_safe(), let's mock it to |
| 567 |
# be uuid_generate_time() and ensure the safety is unknown. |
| 568 |
with self.mock_generate_time_safe(None): |
| 569 |
u = self.uuid.uuid1() |
| 570 |
self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
| 571 |
|
| 572 |
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
| 573 |
def test_uuid1_is_safe(self): |
| 574 |
with self.mock_generate_time_safe(0): |
| 575 |
u = self.uuid.uuid1() |
| 576 |
self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe) |
| 577 |
|
| 578 |
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
| 579 |
def test_uuid1_is_unsafe(self): |
| 580 |
with self.mock_generate_time_safe(-1): |
| 581 |
u = self.uuid.uuid1() |
| 582 |
self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe) |
| 583 |
|
| 584 |
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
| 585 |
def test_uuid1_bogus_return_value(self): |
| 586 |
with self.mock_generate_time_safe(3): |
| 587 |
u = self.uuid.uuid1() |
| 588 |
self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
| 589 |
|
| 590 |
def test_uuid1_time(self): |
| 591 |
with mock.patch.object(self.uuid, '_generate_time_safe', None), \ |
| 592 |
mock.patch.object(self.uuid, '_last_timestamp', None), \ |
| 593 |
mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \ |
| 594 |
mock.patch('time.time_ns', return_value=1545052026752910643), \ |
| 595 |
mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random |
| 596 |
u = self.uuid.uuid1() |
| 597 |
self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) |
| 598 |
|
| 599 |
with mock.patch.object(self.uuid, '_generate_time_safe', None), \ |
| 600 |
mock.patch.object(self.uuid, '_last_timestamp', None), \ |
| 601 |
mock.patch('time.time_ns', return_value=1545052026752910643): |
| 602 |
u = self.uuid.uuid1(node=93328246233727, clock_seq=5317) |
| 603 |
self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) |
| 604 |
|
| 605 |
def test_uuid3(self): |
| 606 |
equal = self.assertEqual |
| 607 |
|
| 608 |
# Test some known version-3 UUIDs with name passed as a byte object |
| 609 |
for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, b'python.org'), |
| 610 |
'6fa459ea-ee8a-3ca4-894e-db77e160355e'), |
| 611 |
(self.uuid.uuid3(self.uuid.NAMESPACE_URL, b'http://python.org/'), |
| 612 |
'9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), |
| 613 |
(self.uuid.uuid3(self.uuid.NAMESPACE_OID, b'1.3.6.1'), |
| 614 |
'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), |
| 615 |
(self.uuid.uuid3(self.uuid.NAMESPACE_X500, b'c=ca'), |
| 616 |
'658d3002-db6b-3040-a1d1-8ddd7d189a4d'), |
| 617 |
]: |
| 618 |
equal(u.variant, self.uuid.RFC_4122) |
| 619 |
equal(u.version, 3) |
| 620 |
equal(u, self.uuid.UUID(v)) |
| 621 |
equal(str(u), v) |
| 622 |
|
| 623 |
# Test some known version-3 UUIDs with name passed as a string |
| 624 |
for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'), |
| 625 |
'6fa459ea-ee8a-3ca4-894e-db77e160355e'), |
| 626 |
(self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'), |
| 627 |
'9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), |
| 628 |
(self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'), |
| 629 |
'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), |
| 630 |
(self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'), |
| 631 |
'658d3002-db6b-3040-a1d1-8ddd7d189a4d'), |
| 632 |
]: |
| 633 |
equal(u.variant, self.uuid.RFC_4122) |
| 634 |
equal(u.version, 3) |
| 635 |
equal(u, self.uuid.UUID(v)) |
| 636 |
equal(str(u), v) |
| 637 |
|
| 638 |
def test_uuid4(self): |
| 639 |
equal = self.assertEqual |
| 640 |
|
| 641 |
# Make sure uuid4() generates UUIDs that are actually version 4. |
| 642 |
for u in [self.uuid.uuid4() for i in range(10)]: |
| 643 |
equal(u.variant, self.uuid.RFC_4122) |
| 644 |
equal(u.version, 4) |
| 645 |
|
| 646 |
# Make sure the generated UUIDs are actually unique. |
| 647 |
uuids = {} |
| 648 |
for u in [self.uuid.uuid4() for i in range(1000)]: |
| 649 |
uuids[u] = 1 |
| 650 |
equal(len(uuids.keys()), 1000) |
| 651 |
|
| 652 |
def test_uuid5(self): |
| 653 |
equal = self.assertEqual |
| 654 |
|
| 655 |
# Test some known version-5 UUIDs with names given as byte objects |
| 656 |
for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, b'python.org'), |
| 657 |
'886313e1-3b8a-5372-9b90-0c9aee199e5d'), |
| 658 |
(self.uuid.uuid5(self.uuid.NAMESPACE_URL, b'http://python.org/'), |
| 659 |
'4c565f0d-3f5a-5890-b41b-20cf47701c5e'), |
| 660 |
(self.uuid.uuid5(self.uuid.NAMESPACE_OID, b'1.3.6.1'), |
| 661 |
'1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), |
| 662 |
(self.uuid.uuid5(self.uuid.NAMESPACE_X500, b'c=ca'), |
| 663 |
'cc957dd1-a972-5349-98cd-874190002798'), |
| 664 |
]: |
| 665 |
equal(u.variant, self.uuid.RFC_4122) |
| 666 |
equal(u.version, 5) |
| 667 |
equal(u, self.uuid.UUID(v)) |
| 668 |
equal(str(u), v) |
| 669 |
|
| 670 |
# Test some known version-5 UUIDs with names given as strings |
| 671 |
for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'), |
| 672 |
'886313e1-3b8a-5372-9b90-0c9aee199e5d'), |
| 673 |
(self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'), |
| 674 |
'4c565f0d-3f5a-5890-b41b-20cf47701c5e'), |
| 675 |
(self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'), |
| 676 |
'1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), |
| 677 |
(self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'), |
| 678 |
'cc957dd1-a972-5349-98cd-874190002798'), |
| 679 |
]: |
| 680 |
equal(u.variant, self.uuid.RFC_4122) |
| 681 |
equal(u.version, 5) |
| 682 |
equal(u, self.uuid.UUID(v)) |
| 683 |
equal(str(u), v) |
| 684 |
|
| 685 |
@support.requires_fork() |
| 686 |
def testIssue8621(self): |
| 687 |
# On at least some versions of OSX self.uuid.uuid4 generates |
| 688 |
# the same sequence of UUIDs in the parent and any |
| 689 |
# children started using fork. |
| 690 |
fds = os.pipe() |
| 691 |
pid = os.fork() |
| 692 |
if pid == 0: |
| 693 |
os.close(fds[0]) |
| 694 |
value = self.uuid.uuid4() |
| 695 |
os.write(fds[1], value.hex.encode('latin-1')) |
| 696 |
os._exit(0) |
| 697 |
|
| 698 |
else: |
| 699 |
os.close(fds[1]) |
| 700 |
self.addCleanup(os.close, fds[0]) |
| 701 |
parent_value = self.uuid.uuid4().hex |
| 702 |
support.wait_process(pid, exitcode=0) |
| 703 |
child_value = os.read(fds[0], 100).decode('latin-1') |
| 704 |
|
| 705 |
self.assertNotEqual(parent_value, child_value) |
| 706 |
|
| 707 |
def test_uuid_weakref(self): |
| 708 |
# bpo-35701: check that weak referencing to a UUID object can be created |
| 709 |
strong = self.uuid.uuid4() |
| 710 |
weak = weakref.ref(strong) |
| 711 |
self.assertIs(strong, weak()) |
| 712 |
|
| 713 |
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-n", "@dns"]) |
| 714 |
@mock.patch('sys.stderr', new_callable=io.StringIO) |
| 715 |
def test_cli_namespace_required_for_uuid3(self, mock_err): |
| 716 |
with self.assertRaises(SystemExit) as cm: |
| 717 |
self.uuid.main() |
| 718 |
|
| 719 |
# Check that exception code is the same as argparse.ArgumentParser.error |
| 720 |
self.assertEqual(cm.exception.code, 2) |
| 721 |
self.assertIn("error: Incorrect number of arguments", mock_err.getvalue()) |
| 722 |
|
| 723 |
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-N", "python.org"]) |
| 724 |
@mock.patch('sys.stderr', new_callable=io.StringIO) |
| 725 |
def test_cli_name_required_for_uuid3(self, mock_err): |
| 726 |
with self.assertRaises(SystemExit) as cm: |
| 727 |
self.uuid.main() |
| 728 |
# Check that exception code is the same as argparse.ArgumentParser.error |
| 729 |
self.assertEqual(cm.exception.code, 2) |
| 730 |
self.assertIn("error: Incorrect number of arguments", mock_err.getvalue()) |
| 731 |
|
| 732 |
@mock.patch.object(sys, "argv", [""]) |
| 733 |
def test_cli_uuid4_outputted_with_no_args(self): |
| 734 |
stdout = io.StringIO() |
| 735 |
with contextlib.redirect_stdout(stdout): |
| 736 |
self.uuid.main() |
| 737 |
|
| 738 |
output = stdout.getvalue().strip() |
| 739 |
uuid_output = self.uuid.UUID(output) |
| 740 |
|
| 741 |
# Output uuid should be in the format of uuid4 |
| 742 |
self.assertEqual(output, str(uuid_output)) |
| 743 |
self.assertEqual(uuid_output.version, 4) |
| 744 |
|
| 745 |
@mock.patch.object(sys, "argv", |
| 746 |
["", "-u", "uuid3", "-n", "@dns", "-N", "python.org"]) |
| 747 |
def test_cli_uuid3_ouputted_with_valid_namespace_and_name(self): |
| 748 |
stdout = io.StringIO() |
| 749 |
with contextlib.redirect_stdout(stdout): |
| 750 |
self.uuid.main() |
| 751 |
|
| 752 |
output = stdout.getvalue().strip() |
| 753 |
uuid_output = self.uuid.UUID(output) |
| 754 |
|
| 755 |
# Output should be in the form of uuid5 |
| 756 |
self.assertEqual(output, str(uuid_output)) |
| 757 |
self.assertEqual(uuid_output.version, 3) |
| 758 |
|
| 759 |
@mock.patch.object(sys, "argv", |
| 760 |
["", "-u", "uuid5", "-n", "@dns", "-N", "python.org"]) |
| 761 |
def test_cli_uuid5_ouputted_with_valid_namespace_and_name(self): |
| 762 |
stdout = io.StringIO() |
| 763 |
with contextlib.redirect_stdout(stdout): |
| 764 |
self.uuid.main() |
| 765 |
|
| 766 |
output = stdout.getvalue().strip() |
| 767 |
uuid_output = self.uuid.UUID(output) |
| 768 |
|
| 769 |
# Output should be in the form of uuid5 |
| 770 |
self.assertEqual(output, str(uuid_output)) |
| 771 |
self.assertEqual(uuid_output.version, 5) |
| 772 |
|
| 773 |
|
| 774 |
class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase): |
| 775 |
uuid = py_uuid |
| 776 |
|
| 777 |
|
| 778 |
@unittest.skipUnless(c_uuid, 'requires the C _uuid module') |
| 779 |
class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase): |
| 780 |
uuid = c_uuid |
| 781 |
|
| 782 |
def check_has_stable_libuuid_extractable_node(self): |
| 783 |
if not self.uuid._has_stable_extractable_node: |
| 784 |
self.skipTest("libuuid cannot deduce MAC address") |
| 785 |
|
| 786 |
@unittest.skipUnless(os.name == 'posix', 'POSIX only') |
| 787 |
def test_unix_getnode_from_libuuid(self): |
| 788 |
self.check_has_stable_libuuid_extractable_node() |
| 789 |
script = 'import uuid; print(uuid._unix_getnode())' |
| 790 |
_, n_a, _ = assert_python_ok('-c', script) |
| 791 |
_, n_b, _ = assert_python_ok('-c', script) |
| 792 |
n_a, n_b = n_a.decode().strip(), n_b.decode().strip() |
| 793 |
self.assertTrue(n_a.isdigit()) |
| 794 |
self.assertTrue(n_b.isdigit()) |
| 795 |
self.assertEqual(n_a, n_b) |
| 796 |
|
| 797 |
@unittest.skipUnless(os.name == 'nt', 'Windows only') |
| 798 |
def test_windows_getnode_from_libuuid(self): |
| 799 |
self.check_has_stable_libuuid_extractable_node() |
| 800 |
script = 'import uuid; print(uuid._windll_getnode())' |
| 801 |
_, n_a, _ = assert_python_ok('-c', script) |
| 802 |
_, n_b, _ = assert_python_ok('-c', script) |
| 803 |
n_a, n_b = n_a.decode().strip(), n_b.decode().strip() |
| 804 |
self.assertTrue(n_a.isdigit()) |
| 805 |
self.assertTrue(n_b.isdigit()) |
| 806 |
self.assertEqual(n_a, n_b) |
| 807 |
|
| 808 |
|
| 809 |
class BaseTestInternals: |
| 810 |
_uuid = py_uuid |
| 811 |
|
| 812 |
def check_parse_mac(self, aix): |
| 813 |
if not aix: |
| 814 |
patch = mock.patch.multiple(self.uuid, |
| 815 |
_MAC_DELIM=b':', |
| 816 |
_MAC_OMITS_LEADING_ZEROES=False) |
| 817 |
else: |
| 818 |
patch = mock.patch.multiple(self.uuid, |
| 819 |
_MAC_DELIM=b'.', |
| 820 |
_MAC_OMITS_LEADING_ZEROES=True) |
| 821 |
|
| 822 |
with patch: |
| 823 |
# Valid MAC addresses |
| 824 |
if not aix: |
| 825 |
tests = ( |
| 826 |
(b'52:54:00:9d:0e:67', 0x5254009d0e67), |
| 827 |
(b'12:34:56:78:90:ab', 0x1234567890ab), |
| 828 |
) |
| 829 |
else: |
| 830 |
# AIX format |
| 831 |
tests = ( |
| 832 |
(b'fe.ad.c.1.23.4', 0xfead0c012304), |
| 833 |
) |
| 834 |
for mac, expected in tests: |
| 835 |
self.assertEqual(self.uuid._parse_mac(mac), expected) |
| 836 |
|
| 837 |
# Invalid MAC addresses |
| 838 |
for mac in ( |
| 839 |
b'', |
| 840 |
# IPv6 addresses with same length than valid MAC address |
| 841 |
# (17 characters) |
| 842 |
b'fe80::5054:ff:fe9', |
| 843 |
b'123:2:3:4:5:6:7:8', |
| 844 |
# empty 5rd field |
| 845 |
b'52:54:00:9d::67', |
| 846 |
# only 5 fields instead of 6 |
| 847 |
b'52:54:00:9d:0e' |
| 848 |
# invalid character 'x' |
| 849 |
b'52:54:00:9d:0e:6x' |
| 850 |
# dash separator |
| 851 |
b'52-54-00-9d-0e-67', |
| 852 |
): |
| 853 |
if aix: |
| 854 |
mac = mac.replace(b':', b'.') |
| 855 |
with self.subTest(mac=mac): |
| 856 |
self.assertIsNone(self.uuid._parse_mac(mac)) |
| 857 |
|
| 858 |
def test_parse_mac(self): |
| 859 |
self.check_parse_mac(False) |
| 860 |
|
| 861 |
def test_parse_mac_aix(self): |
| 862 |
self.check_parse_mac(True) |
| 863 |
|
| 864 |
def test_find_under_heading(self): |
| 865 |
data = '''\ |
| 866 |
Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll |
| 867 |
en0 1500 link#2 fe.ad.c.1.23.4 1714807956 0 711348489 0 0 |
| 868 |
01:00:5e:00:00:01 |
| 869 |
en0 1500 192.168.129 x071 1714807956 0 711348489 0 0 |
| 870 |
224.0.0.1 |
| 871 |
en0 1500 192.168.90 x071 1714807956 0 711348489 0 0 |
| 872 |
224.0.0.1 |
| 873 |
''' |
| 874 |
|
| 875 |
# The above data is from AIX - with '.' as _MAC_DELIM and strings |
| 876 |
# shorter than 17 bytes (no leading 0). (_MAC_OMITS_LEADING_ZEROES=True) |
| 877 |
with mock.patch.multiple(self.uuid, |
| 878 |
_MAC_DELIM=b'.', |
| 879 |
_MAC_OMITS_LEADING_ZEROES=True, |
| 880 |
_get_command_stdout=mock_get_command_stdout(data)): |
| 881 |
mac = self.uuid._find_mac_under_heading( |
| 882 |
command='netstat', |
| 883 |
args='-ian', |
| 884 |
heading=b'Address', |
| 885 |
) |
| 886 |
|
| 887 |
self.assertEqual(mac, 0xfead0c012304) |
| 888 |
|
| 889 |
def test_find_under_heading_ipv6(self): |
| 890 |
# bpo-39991: IPv6 address "fe80::5054:ff:fe9" looks like a MAC address |
| 891 |
# (same string length) but must be skipped |
| 892 |
data = '''\ |
| 893 |
Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll |
| 894 |
vtnet 1500 <Link#1> 52:54:00:9d:0e:67 10017 0 0 8174 0 0 |
| 895 |
vtnet - fe80::%vtnet0 fe80::5054:ff:fe9 0 - - 4 - - |
| 896 |
vtnet - 192.168.122.0 192.168.122.45 8844 - - 8171 - - |
| 897 |
lo0 16384 <Link#2> lo0 260148 0 0 260148 0 0 |
| 898 |
lo0 - ::1/128 ::1 193 - - 193 - - |
| 899 |
ff01::1%lo0 |
| 900 |
ff02::2:2eb7:74fa |
| 901 |
ff02::2:ff2e:b774 |
| 902 |
ff02::1%lo0 |
| 903 |
ff02::1:ff00:1%lo |
| 904 |
lo0 - fe80::%lo0/64 fe80::1%lo0 0 - - 0 - - |
| 905 |
ff01::1%lo0 |
| 906 |
ff02::2:2eb7:74fa |
| 907 |
ff02::2:ff2e:b774 |
| 908 |
ff02::1%lo0 |
| 909 |
ff02::1:ff00:1%lo |
| 910 |
lo0 - 127.0.0.0/8 127.0.0.1 259955 - - 259955 - - |
| 911 |
224.0.0.1 |
| 912 |
''' |
| 913 |
|
| 914 |
with mock.patch.multiple(self.uuid, |
| 915 |
_MAC_DELIM=b':', |
| 916 |
_MAC_OMITS_LEADING_ZEROES=False, |
| 917 |
_get_command_stdout=mock_get_command_stdout(data)): |
| 918 |
mac = self.uuid._find_mac_under_heading( |
| 919 |
command='netstat', |
| 920 |
args='-ian', |
| 921 |
heading=b'Address', |
| 922 |
) |
| 923 |
|
| 924 |
self.assertEqual(mac, 0x5254009d0e67) |
| 925 |
|
| 926 |
def test_find_mac_near_keyword(self): |
| 927 |
# key and value are on the same line |
| 928 |
data = ''' |
| 929 |
fake Link encap:UNSPEC hwaddr 00-00 |
| 930 |
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 |
| 931 |
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab |
| 932 |
''' |
| 933 |
|
| 934 |
# The above data will only be parsed properly on non-AIX unixes. |
| 935 |
with mock.patch.multiple(self.uuid, |
| 936 |
_MAC_DELIM=b':', |
| 937 |
_MAC_OMITS_LEADING_ZEROES=False, |
| 938 |
_get_command_stdout=mock_get_command_stdout(data)): |
| 939 |
mac = self.uuid._find_mac_near_keyword( |
| 940 |
command='ifconfig', |
| 941 |
args='', |
| 942 |
keywords=[b'hwaddr'], |
| 943 |
get_word_index=lambda x: x + 1, |
| 944 |
) |
| 945 |
|
| 946 |
self.assertEqual(mac, 0x1234567890ab) |
| 947 |
|
| 948 |
def check_node(self, node, requires=None): |
| 949 |
if requires and node is None: |
| 950 |
self.skipTest('requires ' + requires) |
| 951 |
hex = '%012x' % node |
| 952 |
if support.verbose >= 2: |
| 953 |
print(hex, end=' ') |
| 954 |
self.assertTrue(0 < node < (1 << 48), |
| 955 |
"%s is not an RFC 4122 node ID" % hex) |
| 956 |
|
| 957 |
@unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, |
| 958 |
"ifconfig is not used for introspection on this platform") |
| 959 |
def test_ifconfig_getnode(self): |
| 960 |
node = self.uuid._ifconfig_getnode() |
| 961 |
self.check_node(node, 'ifconfig') |
| 962 |
|
| 963 |
@unittest.skipUnless(_uuid._ip_getnode in _uuid._GETTERS, |
| 964 |
"ip is not used for introspection on this platform") |
| 965 |
def test_ip_getnode(self): |
| 966 |
node = self.uuid._ip_getnode() |
| 967 |
self.check_node(node, 'ip') |
| 968 |
|
| 969 |
@unittest.skipUnless(_uuid._arp_getnode in _uuid._GETTERS, |
| 970 |
"arp is not used for introspection on this platform") |
| 971 |
def test_arp_getnode(self): |
| 972 |
node = self.uuid._arp_getnode() |
| 973 |
self.check_node(node, 'arp') |
| 974 |
|
| 975 |
@unittest.skipUnless(_uuid._lanscan_getnode in _uuid._GETTERS, |
| 976 |
"lanscan is not used for introspection on this platform") |
| 977 |
def test_lanscan_getnode(self): |
| 978 |
node = self.uuid._lanscan_getnode() |
| 979 |
self.check_node(node, 'lanscan') |
| 980 |
|
| 981 |
@unittest.skipUnless(_uuid._netstat_getnode in _uuid._GETTERS, |
| 982 |
"netstat is not used for introspection on this platform") |
| 983 |
def test_netstat_getnode(self): |
| 984 |
node = self.uuid._netstat_getnode() |
| 985 |
self.check_node(node, 'netstat') |
| 986 |
|
| 987 |
def test_random_getnode(self): |
| 988 |
node = self.uuid._random_getnode() |
| 989 |
# The multicast bit, i.e. the least significant bit of first octet, |
| 990 |
# must be set for randomly generated MAC addresses. See RFC 4122, |
| 991 |
# $4.1.6. |
| 992 |
self.assertTrue(node & (1 << 40), '%012x' % node) |
| 993 |
self.check_node(node) |
| 994 |
|
| 995 |
node2 = self.uuid._random_getnode() |
| 996 |
self.assertNotEqual(node2, node, '%012x' % node) |
| 997 |
|
| 998 |
class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase): |
| 999 |
uuid = py_uuid |
| 1000 |
|
| 1001 |
@unittest.skipUnless(c_uuid, 'requires the C _uuid module') |
| 1002 |
class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase): |
| 1003 |
uuid = c_uuid |
| 1004 |
|
| 1005 |
@unittest.skipUnless(os.name == 'posix', 'requires Posix') |
| 1006 |
def test_unix_getnode(self): |
| 1007 |
if not importable('_uuid') and not importable('ctypes'): |
| 1008 |
self.skipTest("neither _uuid extension nor ctypes available") |
| 1009 |
try: # Issues 1481, 3581: _uuid_generate_time() might be None. |
| 1010 |
node = self.uuid._unix_getnode() |
| 1011 |
except TypeError: |
| 1012 |
self.skipTest('requires uuid_generate_time') |
| 1013 |
self.check_node(node, 'unix') |
| 1014 |
|
| 1015 |
@unittest.skipUnless(os.name == 'nt', 'requires Windows') |
| 1016 |
def test_windll_getnode(self): |
| 1017 |
node = self.uuid._windll_getnode() |
| 1018 |
self.check_node(node) |
| 1019 |
|
| 1020 |
|
| 1021 |
if __name__ == '__main__': |
| 1022 |
unittest.main() |