blob: 3f56192c70e846cca7366fced14f0dacb039d5d2 [file] [log] [blame]
Michael Felt0bcbfa42019-09-26 20:43:15 +01001import unittest
Serhiy Storchaka56507c72013-11-26 22:47:16 +02002from test import support
Hai Shi883bc632020-07-06 17:12:49 +08003from test.support import import_helper
Georg Brandl1d523e12009-12-19 18:23:28 +00004import builtins
Antoine Pitroua106aec2017-09-28 23:03:06 +02005import contextlib
Tal Einat54752532018-09-10 16:11:04 +03006import copy
Ethan Furmana02cb472021-04-21 10:20:44 -07007import enum
Serhiy Storchaka56507c72013-11-26 22:47:16 +02008import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03009import os
Tal Einat3e2b29d2018-09-06 14:34:25 +030010import pickle
Tal Einat3e2b29d2018-09-06 14:34:25 +030011import sys
David Hf1d8e7c2019-01-17 13:16:51 +010012import weakref
Victor Stinner62a68b72018-12-18 11:45:13 +010013from unittest import mock
Antoine Pitroua106aec2017-09-28 23:03:06 +020014
Hai Shi883bc632020-07-06 17:12:49 +080015py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid'])
16c_uuid = import_helper.import_fresh_module('uuid', fresh=['_uuid'])
Antoine Pitroua106aec2017-09-28 23:03:06 +020017
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018def importable(name):
19 try:
20 __import__(name)
21 return True
22 except:
23 return False
24
Antoine Pitroua106aec2017-09-28 23:03:06 +020025
Victor Stinnereb886db2020-03-17 15:51:42 +010026def mock_get_command_stdout(data):
27 def get_command_stdout(command, args):
28 return io.BytesIO(data.encode())
29 return get_command_stdout
30
31
Antoine Pitroua106aec2017-09-28 23:03:06 +020032class BaseTestUUID:
33 uuid = None
34
Ethan Furmana02cb472021-04-21 10:20:44 -070035 def test_safe_uuid_enum(self):
36 class CheckedSafeUUID(enum.Enum):
37 safe = 0
38 unsafe = -1
39 unknown = None
40 enum._test_simple_enum(CheckedSafeUUID, py_uuid.SafeUUID)
41
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042 def test_UUID(self):
43 equal = self.assertEqual
44 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000045 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046 time, clock_seq, variant, version) in [
47 ('00000000-0000-0000-0000-000000000000',
48 '{00000000-0000-0000-0000-000000000000}',
49 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000050 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
51 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 (0, 0, 0, 0, 0, 0),
53 0,
54 'urn:uuid:00000000-0000-0000-0000-000000000000',
Antoine Pitroua106aec2017-09-28 23:03:06 +020055 0, 0, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
57 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
58 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000059 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
60 b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
Guido van Rossume2a383d2007-01-15 16:59:06 +000061 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
62 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Antoine Pitroua106aec2017-09-28 23:03:06 +020064 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
66 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
67 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000068 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
69 b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
Guido van Rossume2a383d2007-01-15 16:59:06 +000070 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
71 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Antoine Pitroua106aec2017-09-28 23:03:06 +020073 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074 ('12345678-1234-5678-1234-567812345678',
75 '{12345678-1234-5678-1234-567812345678}',
76 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000077 b'\x12\x34\x56\x78'*4,
78 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
80 0x12345678123456781234567812345678,
81 'urn:uuid:12345678-1234-5678-1234-567812345678',
Antoine Pitroua106aec2017-09-28 23:03:06 +020082 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
84 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
85 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000086 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
87 b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000088 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
89 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020091 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
93 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
94 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000095 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
96 b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +000097 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
98 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200100 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
102 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
103 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000104 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
105 b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000106 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
107 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000108 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200109 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
111 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
112 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000113 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
114 b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000115 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
116 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000117 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200118 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
120 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
121 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000122 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
123 b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000124 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
125 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200127 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
129 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
130 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000131 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
132 b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000133 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
134 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000135 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200136 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
138 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
139 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000140 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
141 b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000142 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
143 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200145 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
147 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
148 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000149 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
150 b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000151 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
152 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000153 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200154 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
156 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
157 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000158 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
159 b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
Guido van Rossume2a383d2007-01-15 16:59:06 +0000160 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
161 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200163 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000164 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
165 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
166 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000167 b'\xff'*16,
168 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000169 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
170 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000171 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200172 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173 ]:
174 equivalents = []
175 # Construct each UUID in several different ways.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200176 for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex),
177 self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le),
178 self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer),
179 self.uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180 # Test all conversions and properties of the UUID object.
181 equal(str(u), string)
182 equal(int(u), integer)
183 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000184 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185 equal(u.fields, fields)
186 equal(u.time_low, fields[0])
187 equal(u.time_mid, fields[1])
188 equal(u.time_hi_version, fields[2])
189 equal(u.clock_seq_hi_variant, fields[3])
190 equal(u.clock_seq_low, fields[4])
191 equal(u.node, fields[5])
192 equal(u.hex, hex)
193 equal(u.int, integer)
194 equal(u.urn, urn)
195 equal(u.time, time)
196 equal(u.clock_seq, clock_seq)
197 equal(u.variant, variant)
198 equal(u.version, version)
199 equivalents.append(u)
200
201 # Different construction methods should give the same UUID.
202 for u in equivalents:
203 for v in equivalents:
204 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000205
206 # Bug 7380: "bytes" and "bytes_le" should give the same type.
207 equal(type(u.bytes), builtins.bytes)
208 equal(type(u.bytes_le), builtins.bytes)
209
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210 ascending.append(u)
211
212 # Test comparison of UUIDs.
213 for i in range(len(ascending)):
214 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000215 equal(i < j, ascending[i] < ascending[j])
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])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221
222 # Test sorting of UUIDs (above list is in ascending order).
223 resorted = ascending[:]
224 resorted.reverse()
225 resorted.sort()
226 equal(ascending, resorted)
227
228 def test_exceptions(self):
229 badvalue = lambda f: self.assertRaises(ValueError, f)
230 badtype = lambda f: self.assertRaises(TypeError, f)
231
232 # Badly formed hex strings.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200233 badvalue(lambda: self.uuid.UUID(''))
234 badvalue(lambda: self.uuid.UUID('abc'))
235 badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
236 badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
237 badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238
239 # Badly formed bytes.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200240 badvalue(lambda: self.uuid.UUID(bytes='abc'))
241 badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
242 badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000244 # Badly formed bytes_le.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200245 badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
246 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
247 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000248
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249 # Badly formed fields.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200250 badvalue(lambda: self.uuid.UUID(fields=(1,)))
251 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5)))
252 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253
254 # Field values out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200255 badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
256 badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
257 badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
258 badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
259 badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
260 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
261 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
262 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
263 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
264 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
265 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
266 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267
268 # Version number out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200269 badvalue(lambda: self.uuid.UUID('00'*16, version=0))
270 badvalue(lambda: self.uuid.UUID('00'*16, version=6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271
272 # Integer value out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200273 badvalue(lambda: self.uuid.UUID(int=-1))
274 badvalue(lambda: self.uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275
276 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000277 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Antoine Pitroua106aec2017-09-28 23:03:06 +0200278 self.uuid.UUID(h)
279 self.uuid.UUID(hex=h)
280 self.uuid.UUID(bytes=b)
281 self.uuid.UUID(bytes_le=b)
282 self.uuid.UUID(fields=f)
283 self.uuid.UUID(int=i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284
285 # Wrong number of arguments (positional).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200286 badtype(lambda: self.uuid.UUID())
287 badtype(lambda: self.uuid.UUID(h, b))
288 badtype(lambda: self.uuid.UUID(h, b, b))
289 badtype(lambda: self.uuid.UUID(h, b, b, f))
290 badtype(lambda: self.uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000292 # Duplicate arguments.
293 for hh in [[], [('hex', h)]]:
294 for bb in [[], [('bytes', b)]]:
295 for bble in [[], [('bytes_le', b)]]:
296 for ii in [[], [('int', i)]]:
297 for ff in [[], [('fields', f)]]:
298 args = dict(hh + bb + bble + ii + ff)
299 if len(args) != 0:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200300 badtype(lambda: self.uuid.UUID(h, **args))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000301 if len(args) != 1:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200302 badtype(lambda: self.uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303
304 # Immutability.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200305 u = self.uuid.UUID(h)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 badtype(lambda: setattr(u, 'hex', h))
307 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000308 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309 badtype(lambda: setattr(u, 'fields', f))
310 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000311 badtype(lambda: setattr(u, 'time_low', 0))
312 badtype(lambda: setattr(u, 'time_mid', 0))
313 badtype(lambda: setattr(u, 'time_hi_version', 0))
314 badtype(lambda: setattr(u, 'time_hi_version', 0))
315 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
316 badtype(lambda: setattr(u, 'clock_seq_low', 0))
317 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000318
Berker Peksag6b5e4a82016-12-31 20:08:16 +0300319 # Comparison with a non-UUID object
320 badtype(lambda: u < object())
321 badtype(lambda: u > object())
322
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000323 def test_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200324 node1 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200325 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000326
327 # Test it again to ensure consistency.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200328 node2 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200329 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200330
Tal Einat3e2b29d2018-09-06 14:34:25 +0300331 def test_pickle_roundtrip(self):
Tal Einat54752532018-09-10 16:11:04 +0300332 def check(actual, expected):
333 self.assertEqual(actual, expected)
334 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300335
Tal Einat54752532018-09-10 16:11:04 +0300336 with support.swap_item(sys.modules, 'uuid', self.uuid):
337 for is_safe in self.uuid.SafeUUID:
338 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
339 is_safe=is_safe)
340 check(copy.copy(u), u)
341 check(copy.deepcopy(u), u)
342 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
343 with self.subTest(protocol=proto):
344 check(pickle.loads(pickle.dumps(u, proto)), u)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300345
346 def test_unpickle_previous_python_versions(self):
Tal Einat54752532018-09-10 16:11:04 +0300347 def check(actual, expected):
348 self.assertEqual(actual, expected)
349 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300350
Tal Einat54752532018-09-10 16:11:04 +0300351 pickled_uuids = [
352 # Python 2.7, protocol 0
353 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
354 b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.',
355 # Python 2.7, protocol 1
356 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
357 b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.',
358 # Python 2.7, protocol 2
359 b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}'
360 b'\xde\xa0Bf\xcey%\xd8\x00sb.',
361 # Python 3.6, protocol 0
362 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
363 b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.',
364 # Python 3.6, protocol 1
365 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
366 b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L'
367 b'\nsb.',
368 # Python 3.6, protocol 2
369 b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
370 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
371 # Python 3.6, protocol 3
372 b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
373 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
374 # Python 3.6, protocol 4
375 b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI'
376 b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%'
377 b'\xd8\x00sb.',
378 # Python 3.7, protocol 0
379 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
380 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
381 b'cuuid\nSafeUUID\n(NtRsb.',
382 # Python 3.7, protocol 1
383 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
384 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
385 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.',
386 # Python 3.7, protocol 2
387 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
388 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
389 b'id\nSafeUUID\nN\x85Rub.',
390 # Python 3.7, protocol 3
391 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
392 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
393 b'id\nSafeUUID\nN\x85Rub.',
394 # Python 3.7, protocol 4
395 b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
396 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
397 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub'
398 b'.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300399 ]
Tal Einat54752532018-09-10 16:11:04 +0300400 pickled_uuids_safe = [
401 # Python 3.7, protocol 0
402 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
403 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
404 b'cuuid\nSafeUUID\n(I0\ntRsb.',
405 # Python 3.7, protocol 1
406 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
407 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
408 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.',
409 # Python 3.7, protocol 2
410 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
411 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
412 b'id\nSafeUUID\nK\x00\x85Rub.',
413 # Python 3.7, protocol 3
414 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
415 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
416 b'id\nSafeUUID\nK\x00\x85Rub.',
417 # Python 3.7, protocol 4
418 b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
419 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
420 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00'
421 b'\x85Rub.',
422 ]
423 pickled_uuids_unsafe = [
424 # Python 3.7, protocol 0
425 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
426 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
427 b'cuuid\nSafeUUID\n(I-1\ntRsb.',
428 # Python 3.7, protocol 1
429 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
430 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
431 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR'
432 b'ub.',
433 # Python 3.7, protocol 2
434 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
435 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
436 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
437 # Python 3.7, protocol 3
438 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
439 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
440 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
441 # Python 3.7, protocol 4
442 b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
443 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
444 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff'
445 b'\xff\xff\xff\x85Rub.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300446 ]
447
Tal Einat54752532018-09-10 16:11:04 +0300448 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5')
449 u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
450 is_safe=self.uuid.SafeUUID.safe)
451 u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
452 is_safe=self.uuid.SafeUUID.unsafe)
453
454 with support.swap_item(sys.modules, 'uuid', self.uuid):
455 for pickled in pickled_uuids:
456 # is_safe was added in 3.7. When unpickling values from older
457 # versions, is_safe will be missing, so it should be set to
458 # SafeUUID.unknown.
459 check(pickle.loads(pickled), u)
460 for pickled in pickled_uuids_safe:
461 check(pickle.loads(pickled), u_safe)
462 for pickled in pickled_uuids_unsafe:
463 check(pickle.loads(pickled), u_unsafe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300464
Bo Bayles6b273f72018-01-23 19:11:44 -0600465 # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
466 # need not necessarily be 48 bits (e.g., EUI-64).
467 def test_uuid1_eui64(self):
468 # Confirm that uuid.getnode ignores hardware addresses larger than 48
469 # bits. Mock out each platform's *_getnode helper functions to return
470 # something just larger than 48 bits to test. This will cause
471 # uuid.getnode to fall back on uuid._random_getnode, which will
472 # generate a valid value.
473 too_large_getter = lambda: 1 << 48
Michael Felt0bcbfa42019-09-26 20:43:15 +0100474 with mock.patch.multiple(
Bo Bayles6b273f72018-01-23 19:11:44 -0600475 self.uuid,
476 _node=None, # Ignore any cached node value.
Michael Felt3a1d50e2019-06-15 17:52:29 +0200477 _GETTERS=[too_large_getter],
Bo Bayles6b273f72018-01-23 19:11:44 -0600478 ):
479 node = self.uuid.getnode()
480 self.assertTrue(0 < node < (1 << 48), '%012x' % node)
481
482 # Confirm that uuid1 can use the generated node, i.e., the that
483 # uuid.getnode fell back on uuid._random_getnode() rather than using
484 # the value from too_large_getter above.
485 try:
486 self.uuid.uuid1(node=node)
Pablo Galindo293dd232019-11-19 21:34:03 +0000487 except ValueError:
Bo Bayles6b273f72018-01-23 19:11:44 -0600488 self.fail('uuid1 was given an invalid node ID')
489
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000490 def test_uuid1(self):
491 equal = self.assertEqual
492
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000493 # Make sure uuid1() generates UUIDs that are actually version 1.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200494 for u in [self.uuid.uuid1() for i in range(10)]:
495 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000496 equal(u.version, 1)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200497 self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe,
498 self.uuid.SafeUUID.unsafe,
499 self.uuid.SafeUUID.unknown})
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000501 # Make sure the generated UUIDs are actually unique.
502 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200503 for u in [self.uuid.uuid1() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000504 uuids[u] = 1
505 equal(len(uuids.keys()), 1000)
506
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000507 # Make sure the supplied node ID appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200508 u = self.uuid.uuid1(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000509 equal(u.node, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200510 u = self.uuid.uuid1(0x123456789abc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 equal(u.node, 0x123456789abc)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200512 u = self.uuid.uuid1(0xffffffffffff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 equal(u.node, 0xffffffffffff)
514
515 # Make sure the supplied clock sequence appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200516 u = self.uuid.uuid1(0x123456789abc, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 equal(u.node, 0x123456789abc)
518 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200519 u = self.uuid.uuid1(0x123456789abc, 0x1234)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 equal(u.node, 0x123456789abc)
521 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
522 u.clock_seq_low, 0x1234)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200523 u = self.uuid.uuid1(0x123456789abc, 0x3fff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 equal(u.node, 0x123456789abc)
525 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
526 u.clock_seq_low, 0x3fff)
527
Antoine Pitroua106aec2017-09-28 23:03:06 +0200528 # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns
529 # self.uuid.SafeUUID.unknown
Victor Stinnerc209b702017-04-19 13:01:03 +0200530 @support.requires_mac_ver(10, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200531 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500532 def test_uuid1_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200533 if not self.uuid._has_uuid_generate_time_safe:
534 self.skipTest('requires uuid_generate_time_safe(3)')
535
536 u = self.uuid.uuid1()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500537 # uuid_generate_time_safe() may return 0 or -1 but what it returns is
538 # dependent on the underlying platform support. At least it cannot be
539 # unknown (unless I suppose the platform is buggy).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200540 self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500541
Antoine Pitroua106aec2017-09-28 23:03:06 +0200542 @contextlib.contextmanager
543 def mock_generate_time_safe(self, safe_value):
544 """
545 Mock uuid._generate_time_safe() to return a given *safe_value*.
546 """
547 if os.name != 'posix':
548 self.skipTest('POSIX-only test')
549 self.uuid._load_system_functions()
550 f = self.uuid._generate_time_safe
551 if f is None:
552 self.skipTest('need uuid._generate_time_safe')
Michael Felt0bcbfa42019-09-26 20:43:15 +0100553 with mock.patch.object(self.uuid, '_generate_time_safe',
554 lambda: (f()[0], safe_value)):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200555 yield
556
557 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500558 def test_uuid1_unknown(self):
559 # Even if the platform has uuid_generate_time_safe(), let's mock it to
560 # be uuid_generate_time() and ensure the safety is unknown.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200561 with self.mock_generate_time_safe(None):
562 u = self.uuid.uuid1()
563 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500564
Antoine Pitroua106aec2017-09-28 23:03:06 +0200565 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500566 def test_uuid1_is_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200567 with self.mock_generate_time_safe(0):
568 u = self.uuid.uuid1()
569 self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500570
Antoine Pitroua106aec2017-09-28 23:03:06 +0200571 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500572 def test_uuid1_is_unsafe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200573 with self.mock_generate_time_safe(-1):
574 u = self.uuid.uuid1()
575 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500576
Antoine Pitroua106aec2017-09-28 23:03:06 +0200577 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500578 def test_uuid1_bogus_return_value(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200579 with self.mock_generate_time_safe(3):
580 u = self.uuid.uuid1()
581 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500582
Victor Stinner62a68b72018-12-18 11:45:13 +0100583 def test_uuid1_time(self):
584 with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
585 mock.patch.object(self.uuid, '_generate_time_safe', None), \
586 mock.patch.object(self.uuid, '_last_timestamp', None), \
587 mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \
588 mock.patch('time.time_ns', return_value=1545052026752910643), \
589 mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random
590 u = self.uuid.uuid1()
591 self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))
592
593 with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
594 mock.patch.object(self.uuid, '_generate_time_safe', None), \
595 mock.patch.object(self.uuid, '_last_timestamp', None), \
596 mock.patch('time.time_ns', return_value=1545052026752910643):
597 u = self.uuid.uuid1(node=93328246233727, clock_seq=5317)
598 self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))
599
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600 def test_uuid3(self):
601 equal = self.assertEqual
602
603 # Test some known version-3 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200604 for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000605 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200606 (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000607 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200608 (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000609 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200610 (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000611 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
612 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200613 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000614 equal(u.version, 3)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200615 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616 equal(str(u), v)
617
618 def test_uuid4(self):
619 equal = self.assertEqual
620
621 # Make sure uuid4() generates UUIDs that are actually version 4.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200622 for u in [self.uuid.uuid4() for i in range(10)]:
623 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000624 equal(u.version, 4)
625
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000626 # Make sure the generated UUIDs are actually unique.
627 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200628 for u in [self.uuid.uuid4() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000629 uuids[u] = 1
630 equal(len(uuids.keys()), 1000)
631
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632 def test_uuid5(self):
633 equal = self.assertEqual
634
635 # Test some known version-5 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200636 for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200638 (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200640 (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200642 (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643 'cc957dd1-a972-5349-98cd-874190002798'),
644 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200645 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000646 equal(u.version, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200647 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 equal(str(u), v)
649
pxinwr98a54172020-12-09 07:20:19 +0800650 @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000651 def testIssue8621(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200652 # On at least some versions of OSX self.uuid.uuid4 generates
Ronald Oussorenac764d32010-05-05 15:32:33 +0000653 # the same sequence of UUIDs in the parent and any
654 # children started using fork.
655 fds = os.pipe()
656 pid = os.fork()
657 if pid == 0:
658 os.close(fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200659 value = self.uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000660 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000661 os._exit(0)
662
663 else:
664 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100665 self.addCleanup(os.close, fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200666 parent_value = self.uuid.uuid4().hex
Victor Stinner278c1e12020-03-31 20:08:12 +0200667 support.wait_process(pid, exitcode=0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000668 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000669
670 self.assertNotEqual(parent_value, child_value)
671
David Hf1d8e7c2019-01-17 13:16:51 +0100672 def test_uuid_weakref(self):
673 # bpo-35701: check that weak referencing to a UUID object can be created
674 strong = self.uuid.uuid4()
675 weak = weakref.ref(strong)
676 self.assertIs(strong, weak())
Ronald Oussorenac764d32010-05-05 15:32:33 +0000677
Antoine Pitroua106aec2017-09-28 23:03:06 +0200678class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
679 uuid = py_uuid
680
681@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
682class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
683 uuid = c_uuid
684
685
686class BaseTestInternals:
Michael Felt3a1d50e2019-06-15 17:52:29 +0200687 _uuid = py_uuid
Antoine Pitroua106aec2017-09-28 23:03:06 +0200688
Victor Stinnerebf6bb92020-03-17 18:36:44 +0100689 def check_parse_mac(self, aix):
690 if not aix:
691 patch = mock.patch.multiple(self.uuid,
692 _MAC_DELIM=b':',
693 _MAC_OMITS_LEADING_ZEROES=False)
694 else:
695 patch = mock.patch.multiple(self.uuid,
696 _MAC_DELIM=b'.',
697 _MAC_OMITS_LEADING_ZEROES=True)
698
699 with patch:
700 # Valid MAC addresses
701 if not aix:
702 tests = (
703 (b'52:54:00:9d:0e:67', 0x5254009d0e67),
704 (b'12:34:56:78:90:ab', 0x1234567890ab),
705 )
706 else:
707 # AIX format
708 tests = (
709 (b'fe.ad.c.1.23.4', 0xfead0c012304),
710 )
711 for mac, expected in tests:
712 self.assertEqual(self.uuid._parse_mac(mac), expected)
713
714 # Invalid MAC addresses
715 for mac in (
716 b'',
717 # IPv6 addresses with same length than valid MAC address
718 # (17 characters)
719 b'fe80::5054:ff:fe9',
720 b'123:2:3:4:5:6:7:8',
721 # empty 5rd field
722 b'52:54:00:9d::67',
723 # only 5 fields instead of 6
724 b'52:54:00:9d:0e'
725 # invalid character 'x'
726 b'52:54:00:9d:0e:6x'
727 # dash separator
728 b'52-54-00-9d-0e-67',
729 ):
730 if aix:
731 mac = mac.replace(b':', b'.')
732 with self.subTest(mac=mac):
733 self.assertIsNone(self.uuid._parse_mac(mac))
734
735 def test_parse_mac(self):
736 self.check_parse_mac(False)
737
738 def test_parse_mac_aix(self):
739 self.check_parse_mac(True)
740
Michael Felt0bcbfa42019-09-26 20:43:15 +0100741 def test_find_under_heading(self):
742 data = '''\
743Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll
744en0 1500 link#2 fe.ad.c.1.23.4 1714807956 0 711348489 0 0
745 01:00:5e:00:00:01
746en0 1500 192.168.129 x071 1714807956 0 711348489 0 0
747 224.0.0.1
748en0 1500 192.168.90 x071 1714807956 0 711348489 0 0
749 224.0.0.1
750'''
751
Michael Felt0bcbfa42019-09-26 20:43:15 +0100752 # The above data is from AIX - with '.' as _MAC_DELIM and strings
753 # shorter than 17 bytes (no leading 0). (_MAC_OMITS_LEADING_ZEROES=True)
754 with mock.patch.multiple(self.uuid,
755 _MAC_DELIM=b'.',
756 _MAC_OMITS_LEADING_ZEROES=True,
Victor Stinnereb886db2020-03-17 15:51:42 +0100757 _get_command_stdout=mock_get_command_stdout(data)):
Michael Felt0bcbfa42019-09-26 20:43:15 +0100758 mac = self.uuid._find_mac_under_heading(
759 command='netstat',
760 args='-ian',
761 heading=b'Address',
762 )
763
764 self.assertEqual(mac, 0xfead0c012304)
765
Victor Stinnereb886db2020-03-17 15:51:42 +0100766 def test_find_under_heading_ipv6(self):
767 # bpo-39991: IPv6 address "fe80::5054:ff:fe9" looks like a MAC address
768 # (same string length) but must be skipped
769 data = '''\
770Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll
771vtnet 1500 <Link#1> 52:54:00:9d:0e:67 10017 0 0 8174 0 0
772vtnet - fe80::%vtnet0 fe80::5054:ff:fe9 0 - - 4 - -
773vtnet - 192.168.122.0 192.168.122.45 8844 - - 8171 - -
774lo0 16384 <Link#2> lo0 260148 0 0 260148 0 0
775lo0 - ::1/128 ::1 193 - - 193 - -
776 ff01::1%lo0
777 ff02::2:2eb7:74fa
778 ff02::2:ff2e:b774
779 ff02::1%lo0
780 ff02::1:ff00:1%lo
781lo0 - fe80::%lo0/64 fe80::1%lo0 0 - - 0 - -
782 ff01::1%lo0
783 ff02::2:2eb7:74fa
784 ff02::2:ff2e:b774
785 ff02::1%lo0
786 ff02::1:ff00:1%lo
787lo0 - 127.0.0.0/8 127.0.0.1 259955 - - 259955 - -
788 224.0.0.1
789'''
790
791 with mock.patch.multiple(self.uuid,
792 _MAC_DELIM=b':',
793 _MAC_OMITS_LEADING_ZEROES=False,
794 _get_command_stdout=mock_get_command_stdout(data)):
795 mac = self.uuid._find_mac_under_heading(
796 command='netstat',
797 args='-ian',
798 heading=b'Address',
799 )
800
801 self.assertEqual(mac, 0x5254009d0e67)
802
Michael Felt0bcbfa42019-09-26 20:43:15 +0100803 def test_find_mac_near_keyword(self):
804 # key and value are on the same line
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200805 data = '''
Michael Felt0bcbfa42019-09-26 20:43:15 +0100806fake Link encap:UNSPEC hwaddr 00-00
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200807cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
808eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
809'''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200810
Michael Felt0bcbfa42019-09-26 20:43:15 +0100811 # The above data will only be parsed properly on non-AIX unixes.
812 with mock.patch.multiple(self.uuid,
813 _MAC_DELIM=b':',
814 _MAC_OMITS_LEADING_ZEROES=False,
Victor Stinnereb886db2020-03-17 15:51:42 +0100815 _get_command_stdout=mock_get_command_stdout(data)):
Michael Felt0bcbfa42019-09-26 20:43:15 +0100816 mac = self.uuid._find_mac_near_keyword(
817 command='ifconfig',
818 args='',
819 keywords=[b'hwaddr'],
820 get_word_index=lambda x: x + 1,
821 )
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200822
823 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200824
Barry Warsaw23df2d12017-11-28 17:26:04 -0500825 def check_node(self, node, requires=None):
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200826 if requires and node is None:
827 self.skipTest('requires ' + requires)
828 hex = '%012x' % node
829 if support.verbose >= 2:
830 print(hex, end=' ')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200831 self.assertTrue(0 < node < (1 << 48),
832 "%s is not an RFC 4122 node ID" % hex)
833
Michael Felt3a1d50e2019-06-15 17:52:29 +0200834 @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS,
835 "ifconfig is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200836 def test_ifconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200837 node = self.uuid._ifconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500838 self.check_node(node, 'ifconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200839
Michael Felt3a1d50e2019-06-15 17:52:29 +0200840 @unittest.skipUnless(_uuid._ip_getnode in _uuid._GETTERS,
841 "ip is not used for introspection on this platform")
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200842 def test_ip_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200843 node = self.uuid._ip_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500844 self.check_node(node, 'ip')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200845
Michael Felt3a1d50e2019-06-15 17:52:29 +0200846 @unittest.skipUnless(_uuid._arp_getnode in _uuid._GETTERS,
847 "arp is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200848 def test_arp_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200849 node = self.uuid._arp_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500850 self.check_node(node, 'arp')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200851
Michael Felt3a1d50e2019-06-15 17:52:29 +0200852 @unittest.skipUnless(_uuid._lanscan_getnode in _uuid._GETTERS,
853 "lanscan is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200854 def test_lanscan_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200855 node = self.uuid._lanscan_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500856 self.check_node(node, 'lanscan')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200857
Michael Felt3a1d50e2019-06-15 17:52:29 +0200858 @unittest.skipUnless(_uuid._netstat_getnode in _uuid._GETTERS,
859 "netstat is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200860 def test_netstat_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200861 node = self.uuid._netstat_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500862 self.check_node(node, 'netstat')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200863
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200864 def test_random_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200865 node = self.uuid._random_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500866 # The multicast bit, i.e. the least significant bit of first octet,
867 # must be set for randomly generated MAC addresses. See RFC 4122,
868 # $4.1.6.
869 self.assertTrue(node & (1 << 40), '%012x' % node)
Victor Stinnerc9409f72017-11-28 00:30:21 +0100870 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200871
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200872 node2 = self.uuid._random_getnode()
873 self.assertNotEqual(node2, node, '%012x' % node)
874
Steve Dowerd6b727e2020-05-12 23:32:32 +0100875class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase):
876 uuid = py_uuid
877
878@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
879class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase):
880 uuid = c_uuid
881
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200882 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Antoine Pitroua106aec2017-09-28 23:03:06 +0200883 def test_unix_getnode(self):
884 if not importable('_uuid') and not importable('ctypes'):
885 self.skipTest("neither _uuid extension nor ctypes available")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200886 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200887 node = self.uuid._unix_getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200888 except TypeError:
889 self.skipTest('requires uuid_generate_time')
Victor Stinnerc9409f72017-11-28 00:30:21 +0100890 self.check_node(node, 'unix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200891
892 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200893 def test_windll_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200894 node = self.uuid._windll_getnode()
Victor Stinnerc9409f72017-11-28 00:30:21 +0100895 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200896
897
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000898if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300899 unittest.main()