blob: 27fc56d226c080bfe61a994849ac426ff9b86666 [file] [log] [blame]
Michael Felt0bcbfa42019-09-26 20:43:15 +01001import unittest
Serhiy Storchaka56507c72013-11-26 22:47:16 +02002from test import support
Georg Brandl1d523e12009-12-19 18:23:28 +00003import builtins
Antoine Pitroua106aec2017-09-28 23:03:06 +02004import contextlib
Tal Einat54752532018-09-10 16:11:04 +03005import copy
Serhiy Storchaka56507c72013-11-26 22:47:16 +02006import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03007import os
Tal Einat3e2b29d2018-09-06 14:34:25 +03008import pickle
Serhiy Storchakacce440f2014-01-10 15:06:59 +02009import shutil
Victor Stinnerb9d01992014-10-21 22:33:10 +020010import subprocess
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
15py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
16c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
17
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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035 def test_UUID(self):
36 equal = self.assertEqual
37 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000038 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039 time, clock_seq, variant, version) in [
40 ('00000000-0000-0000-0000-000000000000',
41 '{00000000-0000-0000-0000-000000000000}',
42 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000043 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
44 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045 (0, 0, 0, 0, 0, 0),
46 0,
47 'urn:uuid:00000000-0000-0000-0000-000000000000',
Antoine Pitroua106aec2017-09-28 23:03:06 +020048 0, 0, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
50 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
51 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000052 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
53 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 +000054 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
55 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Antoine Pitroua106aec2017-09-28 23:03:06 +020057 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
59 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
60 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000061 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
62 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 +000063 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
64 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Antoine Pitroua106aec2017-09-28 23:03:06 +020066 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067 ('12345678-1234-5678-1234-567812345678',
68 '{12345678-1234-5678-1234-567812345678}',
69 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000070 b'\x12\x34\x56\x78'*4,
71 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
73 0x12345678123456781234567812345678,
74 'urn:uuid:12345678-1234-5678-1234-567812345678',
Antoine Pitroua106aec2017-09-28 23:03:06 +020075 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
77 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
78 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000079 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
80 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 +000081 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
82 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020084 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000085 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
86 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
87 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000088 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
89 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 +000090 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
91 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020093 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
95 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
96 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000097 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
98 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 +000099 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
100 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200102 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000103 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
104 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
105 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000106 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
107 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 +0000108 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
109 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200111 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
113 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
114 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000115 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
116 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 +0000117 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
118 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200120 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
122 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
123 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000124 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
125 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 +0000126 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
127 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200129 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
131 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
132 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000133 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
134 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 +0000135 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
136 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200138 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
140 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
141 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000142 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
143 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 +0000144 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
145 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200147 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
149 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
150 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000151 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
152 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 +0000153 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
154 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200156 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
158 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
159 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000160 b'\xff'*16,
161 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000162 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
163 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000164 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200165 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000166 ]:
167 equivalents = []
168 # Construct each UUID in several different ways.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200169 for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex),
170 self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le),
171 self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer),
172 self.uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173 # Test all conversions and properties of the UUID object.
174 equal(str(u), string)
175 equal(int(u), integer)
176 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000177 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 equal(u.fields, fields)
179 equal(u.time_low, fields[0])
180 equal(u.time_mid, fields[1])
181 equal(u.time_hi_version, fields[2])
182 equal(u.clock_seq_hi_variant, fields[3])
183 equal(u.clock_seq_low, fields[4])
184 equal(u.node, fields[5])
185 equal(u.hex, hex)
186 equal(u.int, integer)
187 equal(u.urn, urn)
188 equal(u.time, time)
189 equal(u.clock_seq, clock_seq)
190 equal(u.variant, variant)
191 equal(u.version, version)
192 equivalents.append(u)
193
194 # Different construction methods should give the same UUID.
195 for u in equivalents:
196 for v in equivalents:
197 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000198
199 # Bug 7380: "bytes" and "bytes_le" should give the same type.
200 equal(type(u.bytes), builtins.bytes)
201 equal(type(u.bytes_le), builtins.bytes)
202
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203 ascending.append(u)
204
205 # Test comparison of UUIDs.
206 for i in range(len(ascending)):
207 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000208 equal(i < j, ascending[i] < ascending[j])
209 equal(i <= j, ascending[i] <= ascending[j])
210 equal(i == j, ascending[i] == ascending[j])
211 equal(i > j, ascending[i] > ascending[j])
212 equal(i >= j, ascending[i] >= ascending[j])
213 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000214
215 # Test sorting of UUIDs (above list is in ascending order).
216 resorted = ascending[:]
217 resorted.reverse()
218 resorted.sort()
219 equal(ascending, resorted)
220
221 def test_exceptions(self):
222 badvalue = lambda f: self.assertRaises(ValueError, f)
223 badtype = lambda f: self.assertRaises(TypeError, f)
224
225 # Badly formed hex strings.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200226 badvalue(lambda: self.uuid.UUID(''))
227 badvalue(lambda: self.uuid.UUID('abc'))
228 badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
229 badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
230 badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000231
232 # Badly formed bytes.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200233 badvalue(lambda: self.uuid.UUID(bytes='abc'))
234 badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
235 badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000237 # Badly formed bytes_le.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200238 badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
239 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
240 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000241
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242 # Badly formed fields.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200243 badvalue(lambda: self.uuid.UUID(fields=(1,)))
244 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5)))
245 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246
247 # Field values out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200248 badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
249 badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
250 badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
251 badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
252 badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
253 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
254 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
255 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
256 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
257 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
258 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
259 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000260
261 # Version number out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200262 badvalue(lambda: self.uuid.UUID('00'*16, version=0))
263 badvalue(lambda: self.uuid.UUID('00'*16, version=6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264
265 # Integer value out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200266 badvalue(lambda: self.uuid.UUID(int=-1))
267 badvalue(lambda: self.uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000268
269 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000270 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Antoine Pitroua106aec2017-09-28 23:03:06 +0200271 self.uuid.UUID(h)
272 self.uuid.UUID(hex=h)
273 self.uuid.UUID(bytes=b)
274 self.uuid.UUID(bytes_le=b)
275 self.uuid.UUID(fields=f)
276 self.uuid.UUID(int=i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277
278 # Wrong number of arguments (positional).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200279 badtype(lambda: self.uuid.UUID())
280 badtype(lambda: self.uuid.UUID(h, b))
281 badtype(lambda: self.uuid.UUID(h, b, b))
282 badtype(lambda: self.uuid.UUID(h, b, b, f))
283 badtype(lambda: self.uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000285 # Duplicate arguments.
286 for hh in [[], [('hex', h)]]:
287 for bb in [[], [('bytes', b)]]:
288 for bble in [[], [('bytes_le', b)]]:
289 for ii in [[], [('int', i)]]:
290 for ff in [[], [('fields', f)]]:
291 args = dict(hh + bb + bble + ii + ff)
292 if len(args) != 0:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200293 badtype(lambda: self.uuid.UUID(h, **args))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000294 if len(args) != 1:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200295 badtype(lambda: self.uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296
297 # Immutability.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200298 u = self.uuid.UUID(h)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299 badtype(lambda: setattr(u, 'hex', h))
300 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000301 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302 badtype(lambda: setattr(u, 'fields', f))
303 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000304 badtype(lambda: setattr(u, 'time_low', 0))
305 badtype(lambda: setattr(u, 'time_mid', 0))
306 badtype(lambda: setattr(u, 'time_hi_version', 0))
307 badtype(lambda: setattr(u, 'time_hi_version', 0))
308 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
309 badtype(lambda: setattr(u, 'clock_seq_low', 0))
310 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000311
Berker Peksag6b5e4a82016-12-31 20:08:16 +0300312 # Comparison with a non-UUID object
313 badtype(lambda: u < object())
314 badtype(lambda: u > object())
315
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316 def test_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200317 node1 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200318 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000319
320 # Test it again to ensure consistency.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200321 node2 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200322 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200323
Tal Einat3e2b29d2018-09-06 14:34:25 +0300324 def test_pickle_roundtrip(self):
Tal Einat54752532018-09-10 16:11:04 +0300325 def check(actual, expected):
326 self.assertEqual(actual, expected)
327 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300328
Tal Einat54752532018-09-10 16:11:04 +0300329 with support.swap_item(sys.modules, 'uuid', self.uuid):
330 for is_safe in self.uuid.SafeUUID:
331 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
332 is_safe=is_safe)
333 check(copy.copy(u), u)
334 check(copy.deepcopy(u), u)
335 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
336 with self.subTest(protocol=proto):
337 check(pickle.loads(pickle.dumps(u, proto)), u)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300338
339 def test_unpickle_previous_python_versions(self):
Tal Einat54752532018-09-10 16:11:04 +0300340 def check(actual, expected):
341 self.assertEqual(actual, expected)
342 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300343
Tal Einat54752532018-09-10 16:11:04 +0300344 pickled_uuids = [
345 # Python 2.7, protocol 0
346 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
347 b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.',
348 # Python 2.7, protocol 1
349 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
350 b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.',
351 # Python 2.7, protocol 2
352 b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}'
353 b'\xde\xa0Bf\xcey%\xd8\x00sb.',
354 # Python 3.6, protocol 0
355 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
356 b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.',
357 # Python 3.6, protocol 1
358 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
359 b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L'
360 b'\nsb.',
361 # Python 3.6, protocol 2
362 b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
363 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
364 # Python 3.6, protocol 3
365 b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
366 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
367 # Python 3.6, protocol 4
368 b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI'
369 b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%'
370 b'\xd8\x00sb.',
371 # Python 3.7, protocol 0
372 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
373 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
374 b'cuuid\nSafeUUID\n(NtRsb.',
375 # Python 3.7, protocol 1
376 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
377 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
378 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.',
379 # Python 3.7, protocol 2
380 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
381 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
382 b'id\nSafeUUID\nN\x85Rub.',
383 # Python 3.7, protocol 3
384 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
385 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
386 b'id\nSafeUUID\nN\x85Rub.',
387 # Python 3.7, protocol 4
388 b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
389 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
390 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub'
391 b'.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300392 ]
Tal Einat54752532018-09-10 16:11:04 +0300393 pickled_uuids_safe = [
394 # Python 3.7, protocol 0
395 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
396 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
397 b'cuuid\nSafeUUID\n(I0\ntRsb.',
398 # Python 3.7, protocol 1
399 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
400 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
401 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.',
402 # Python 3.7, protocol 2
403 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
404 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
405 b'id\nSafeUUID\nK\x00\x85Rub.',
406 # Python 3.7, protocol 3
407 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
408 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
409 b'id\nSafeUUID\nK\x00\x85Rub.',
410 # Python 3.7, protocol 4
411 b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
412 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
413 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00'
414 b'\x85Rub.',
415 ]
416 pickled_uuids_unsafe = [
417 # Python 3.7, protocol 0
418 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
419 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
420 b'cuuid\nSafeUUID\n(I-1\ntRsb.',
421 # Python 3.7, protocol 1
422 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
423 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
424 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR'
425 b'ub.',
426 # Python 3.7, protocol 2
427 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
428 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
429 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
430 # Python 3.7, protocol 3
431 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
432 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
433 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
434 # Python 3.7, protocol 4
435 b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
436 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
437 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff'
438 b'\xff\xff\xff\x85Rub.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300439 ]
440
Tal Einat54752532018-09-10 16:11:04 +0300441 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5')
442 u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
443 is_safe=self.uuid.SafeUUID.safe)
444 u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
445 is_safe=self.uuid.SafeUUID.unsafe)
446
447 with support.swap_item(sys.modules, 'uuid', self.uuid):
448 for pickled in pickled_uuids:
449 # is_safe was added in 3.7. When unpickling values from older
450 # versions, is_safe will be missing, so it should be set to
451 # SafeUUID.unknown.
452 check(pickle.loads(pickled), u)
453 for pickled in pickled_uuids_safe:
454 check(pickle.loads(pickled), u_safe)
455 for pickled in pickled_uuids_unsafe:
456 check(pickle.loads(pickled), u_unsafe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300457
Bo Bayles6b273f72018-01-23 19:11:44 -0600458 # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
459 # need not necessarily be 48 bits (e.g., EUI-64).
460 def test_uuid1_eui64(self):
461 # Confirm that uuid.getnode ignores hardware addresses larger than 48
462 # bits. Mock out each platform's *_getnode helper functions to return
463 # something just larger than 48 bits to test. This will cause
464 # uuid.getnode to fall back on uuid._random_getnode, which will
465 # generate a valid value.
466 too_large_getter = lambda: 1 << 48
Michael Felt0bcbfa42019-09-26 20:43:15 +0100467 with mock.patch.multiple(
Bo Bayles6b273f72018-01-23 19:11:44 -0600468 self.uuid,
469 _node=None, # Ignore any cached node value.
Michael Felt3a1d50e2019-06-15 17:52:29 +0200470 _GETTERS=[too_large_getter],
Bo Bayles6b273f72018-01-23 19:11:44 -0600471 ):
472 node = self.uuid.getnode()
473 self.assertTrue(0 < node < (1 << 48), '%012x' % node)
474
475 # Confirm that uuid1 can use the generated node, i.e., the that
476 # uuid.getnode fell back on uuid._random_getnode() rather than using
477 # the value from too_large_getter above.
478 try:
479 self.uuid.uuid1(node=node)
Pablo Galindo293dd232019-11-19 21:34:03 +0000480 except ValueError:
Bo Bayles6b273f72018-01-23 19:11:44 -0600481 self.fail('uuid1 was given an invalid node ID')
482
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 def test_uuid1(self):
484 equal = self.assertEqual
485
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000486 # Make sure uuid1() generates UUIDs that are actually version 1.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200487 for u in [self.uuid.uuid1() for i in range(10)]:
488 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 equal(u.version, 1)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200490 self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe,
491 self.uuid.SafeUUID.unsafe,
492 self.uuid.SafeUUID.unknown})
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000494 # Make sure the generated UUIDs are actually unique.
495 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200496 for u in [self.uuid.uuid1() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000497 uuids[u] = 1
498 equal(len(uuids.keys()), 1000)
499
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500 # Make sure the supplied node ID appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200501 u = self.uuid.uuid1(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000502 equal(u.node, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200503 u = self.uuid.uuid1(0x123456789abc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 equal(u.node, 0x123456789abc)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200505 u = self.uuid.uuid1(0xffffffffffff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506 equal(u.node, 0xffffffffffff)
507
508 # Make sure the supplied clock sequence appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200509 u = self.uuid.uuid1(0x123456789abc, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 equal(u.node, 0x123456789abc)
511 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200512 u = self.uuid.uuid1(0x123456789abc, 0x1234)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 equal(u.node, 0x123456789abc)
514 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
515 u.clock_seq_low, 0x1234)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200516 u = self.uuid.uuid1(0x123456789abc, 0x3fff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 equal(u.node, 0x123456789abc)
518 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
519 u.clock_seq_low, 0x3fff)
520
Antoine Pitroua106aec2017-09-28 23:03:06 +0200521 # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns
522 # self.uuid.SafeUUID.unknown
Victor Stinnerc209b702017-04-19 13:01:03 +0200523 @support.requires_mac_ver(10, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200524 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500525 def test_uuid1_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200526 if not self.uuid._has_uuid_generate_time_safe:
527 self.skipTest('requires uuid_generate_time_safe(3)')
528
529 u = self.uuid.uuid1()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500530 # uuid_generate_time_safe() may return 0 or -1 but what it returns is
531 # dependent on the underlying platform support. At least it cannot be
532 # unknown (unless I suppose the platform is buggy).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200533 self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500534
Antoine Pitroua106aec2017-09-28 23:03:06 +0200535 @contextlib.contextmanager
536 def mock_generate_time_safe(self, safe_value):
537 """
538 Mock uuid._generate_time_safe() to return a given *safe_value*.
539 """
540 if os.name != 'posix':
541 self.skipTest('POSIX-only test')
542 self.uuid._load_system_functions()
543 f = self.uuid._generate_time_safe
544 if f is None:
545 self.skipTest('need uuid._generate_time_safe')
Michael Felt0bcbfa42019-09-26 20:43:15 +0100546 with mock.patch.object(self.uuid, '_generate_time_safe',
547 lambda: (f()[0], safe_value)):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200548 yield
549
550 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500551 def test_uuid1_unknown(self):
552 # Even if the platform has uuid_generate_time_safe(), let's mock it to
553 # be uuid_generate_time() and ensure the safety is unknown.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200554 with self.mock_generate_time_safe(None):
555 u = self.uuid.uuid1()
556 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500557
Antoine Pitroua106aec2017-09-28 23:03:06 +0200558 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500559 def test_uuid1_is_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200560 with self.mock_generate_time_safe(0):
561 u = self.uuid.uuid1()
562 self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500563
Antoine Pitroua106aec2017-09-28 23:03:06 +0200564 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500565 def test_uuid1_is_unsafe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200566 with self.mock_generate_time_safe(-1):
567 u = self.uuid.uuid1()
568 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500569
Antoine Pitroua106aec2017-09-28 23:03:06 +0200570 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500571 def test_uuid1_bogus_return_value(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200572 with self.mock_generate_time_safe(3):
573 u = self.uuid.uuid1()
574 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500575
Victor Stinner62a68b72018-12-18 11:45:13 +0100576 def test_uuid1_time(self):
577 with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
578 mock.patch.object(self.uuid, '_generate_time_safe', None), \
579 mock.patch.object(self.uuid, '_last_timestamp', None), \
580 mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \
581 mock.patch('time.time_ns', return_value=1545052026752910643), \
582 mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random
583 u = self.uuid.uuid1()
584 self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))
585
586 with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
587 mock.patch.object(self.uuid, '_generate_time_safe', None), \
588 mock.patch.object(self.uuid, '_last_timestamp', None), \
589 mock.patch('time.time_ns', return_value=1545052026752910643):
590 u = self.uuid.uuid1(node=93328246233727, clock_seq=5317)
591 self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))
592
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 def test_uuid3(self):
594 equal = self.assertEqual
595
596 # Test some known version-3 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200597 for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200599 (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200601 (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000602 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200603 (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000604 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
605 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200606 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000607 equal(u.version, 3)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200608 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000609 equal(str(u), v)
610
611 def test_uuid4(self):
612 equal = self.assertEqual
613
614 # Make sure uuid4() generates UUIDs that are actually version 4.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200615 for u in [self.uuid.uuid4() for i in range(10)]:
616 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 equal(u.version, 4)
618
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000619 # Make sure the generated UUIDs are actually unique.
620 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200621 for u in [self.uuid.uuid4() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000622 uuids[u] = 1
623 equal(len(uuids.keys()), 1000)
624
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 def test_uuid5(self):
626 equal = self.assertEqual
627
628 # Test some known version-5 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200629 for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200631 (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200633 (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200635 (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 'cc957dd1-a972-5349-98cd-874190002798'),
637 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200638 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639 equal(u.version, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200640 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 equal(str(u), v)
642
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300643 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000644 def testIssue8621(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200645 # On at least some versions of OSX self.uuid.uuid4 generates
Ronald Oussorenac764d32010-05-05 15:32:33 +0000646 # the same sequence of UUIDs in the parent and any
647 # children started using fork.
648 fds = os.pipe()
649 pid = os.fork()
650 if pid == 0:
651 os.close(fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200652 value = self.uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000653 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000654 os._exit(0)
655
656 else:
657 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100658 self.addCleanup(os.close, fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200659 parent_value = self.uuid.uuid4().hex
Ronald Oussorenac764d32010-05-05 15:32:33 +0000660 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000661 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000662
663 self.assertNotEqual(parent_value, child_value)
664
David Hf1d8e7c2019-01-17 13:16:51 +0100665 def test_uuid_weakref(self):
666 # bpo-35701: check that weak referencing to a UUID object can be created
667 strong = self.uuid.uuid4()
668 weak = weakref.ref(strong)
669 self.assertIs(strong, weak())
Ronald Oussorenac764d32010-05-05 15:32:33 +0000670
Antoine Pitroua106aec2017-09-28 23:03:06 +0200671class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
672 uuid = py_uuid
673
674@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
675class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
676 uuid = c_uuid
677
678
679class BaseTestInternals:
Michael Felt3a1d50e2019-06-15 17:52:29 +0200680 _uuid = py_uuid
Antoine Pitroua106aec2017-09-28 23:03:06 +0200681
Victor Stinnerebf6bb92020-03-17 18:36:44 +0100682 def check_parse_mac(self, aix):
683 if not aix:
684 patch = mock.patch.multiple(self.uuid,
685 _MAC_DELIM=b':',
686 _MAC_OMITS_LEADING_ZEROES=False)
687 else:
688 patch = mock.patch.multiple(self.uuid,
689 _MAC_DELIM=b'.',
690 _MAC_OMITS_LEADING_ZEROES=True)
691
692 with patch:
693 # Valid MAC addresses
694 if not aix:
695 tests = (
696 (b'52:54:00:9d:0e:67', 0x5254009d0e67),
697 (b'12:34:56:78:90:ab', 0x1234567890ab),
698 )
699 else:
700 # AIX format
701 tests = (
702 (b'fe.ad.c.1.23.4', 0xfead0c012304),
703 )
704 for mac, expected in tests:
705 self.assertEqual(self.uuid._parse_mac(mac), expected)
706
707 # Invalid MAC addresses
708 for mac in (
709 b'',
710 # IPv6 addresses with same length than valid MAC address
711 # (17 characters)
712 b'fe80::5054:ff:fe9',
713 b'123:2:3:4:5:6:7:8',
714 # empty 5rd field
715 b'52:54:00:9d::67',
716 # only 5 fields instead of 6
717 b'52:54:00:9d:0e'
718 # invalid character 'x'
719 b'52:54:00:9d:0e:6x'
720 # dash separator
721 b'52-54-00-9d-0e-67',
722 ):
723 if aix:
724 mac = mac.replace(b':', b'.')
725 with self.subTest(mac=mac):
726 self.assertIsNone(self.uuid._parse_mac(mac))
727
728 def test_parse_mac(self):
729 self.check_parse_mac(False)
730
731 def test_parse_mac_aix(self):
732 self.check_parse_mac(True)
733
Michael Felt0bcbfa42019-09-26 20:43:15 +0100734 def test_find_under_heading(self):
735 data = '''\
736Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll
737en0 1500 link#2 fe.ad.c.1.23.4 1714807956 0 711348489 0 0
738 01:00:5e:00:00:01
739en0 1500 192.168.129 x071 1714807956 0 711348489 0 0
740 224.0.0.1
741en0 1500 192.168.90 x071 1714807956 0 711348489 0 0
742 224.0.0.1
743'''
744
Michael Felt0bcbfa42019-09-26 20:43:15 +0100745 # The above data is from AIX - with '.' as _MAC_DELIM and strings
746 # shorter than 17 bytes (no leading 0). (_MAC_OMITS_LEADING_ZEROES=True)
747 with mock.patch.multiple(self.uuid,
748 _MAC_DELIM=b'.',
749 _MAC_OMITS_LEADING_ZEROES=True,
Victor Stinnereb886db2020-03-17 15:51:42 +0100750 _get_command_stdout=mock_get_command_stdout(data)):
Michael Felt0bcbfa42019-09-26 20:43:15 +0100751 mac = self.uuid._find_mac_under_heading(
752 command='netstat',
753 args='-ian',
754 heading=b'Address',
755 )
756
757 self.assertEqual(mac, 0xfead0c012304)
758
Victor Stinnereb886db2020-03-17 15:51:42 +0100759 def test_find_under_heading_ipv6(self):
760 # bpo-39991: IPv6 address "fe80::5054:ff:fe9" looks like a MAC address
761 # (same string length) but must be skipped
762 data = '''\
763Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll
764vtnet 1500 <Link#1> 52:54:00:9d:0e:67 10017 0 0 8174 0 0
765vtnet - fe80::%vtnet0 fe80::5054:ff:fe9 0 - - 4 - -
766vtnet - 192.168.122.0 192.168.122.45 8844 - - 8171 - -
767lo0 16384 <Link#2> lo0 260148 0 0 260148 0 0
768lo0 - ::1/128 ::1 193 - - 193 - -
769 ff01::1%lo0
770 ff02::2:2eb7:74fa
771 ff02::2:ff2e:b774
772 ff02::1%lo0
773 ff02::1:ff00:1%lo
774lo0 - fe80::%lo0/64 fe80::1%lo0 0 - - 0 - -
775 ff01::1%lo0
776 ff02::2:2eb7:74fa
777 ff02::2:ff2e:b774
778 ff02::1%lo0
779 ff02::1:ff00:1%lo
780lo0 - 127.0.0.0/8 127.0.0.1 259955 - - 259955 - -
781 224.0.0.1
782'''
783
784 with mock.patch.multiple(self.uuid,
785 _MAC_DELIM=b':',
786 _MAC_OMITS_LEADING_ZEROES=False,
787 _get_command_stdout=mock_get_command_stdout(data)):
788 mac = self.uuid._find_mac_under_heading(
789 command='netstat',
790 args='-ian',
791 heading=b'Address',
792 )
793
794 self.assertEqual(mac, 0x5254009d0e67)
795
Michael Felt0bcbfa42019-09-26 20:43:15 +0100796 def test_find_mac_near_keyword(self):
797 # key and value are on the same line
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200798 data = '''
Michael Felt0bcbfa42019-09-26 20:43:15 +0100799fake Link encap:UNSPEC hwaddr 00-00
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200800cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
801eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
802'''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200803
Michael Felt0bcbfa42019-09-26 20:43:15 +0100804 # The above data will only be parsed properly on non-AIX unixes.
805 with mock.patch.multiple(self.uuid,
806 _MAC_DELIM=b':',
807 _MAC_OMITS_LEADING_ZEROES=False,
Victor Stinnereb886db2020-03-17 15:51:42 +0100808 _get_command_stdout=mock_get_command_stdout(data)):
Michael Felt0bcbfa42019-09-26 20:43:15 +0100809 mac = self.uuid._find_mac_near_keyword(
810 command='ifconfig',
811 args='',
812 keywords=[b'hwaddr'],
813 get_word_index=lambda x: x + 1,
814 )
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200815
816 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200817
Barry Warsaw23df2d12017-11-28 17:26:04 -0500818 def check_node(self, node, requires=None):
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200819 if requires and node is None:
820 self.skipTest('requires ' + requires)
821 hex = '%012x' % node
822 if support.verbose >= 2:
823 print(hex, end=' ')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200824 self.assertTrue(0 < node < (1 << 48),
825 "%s is not an RFC 4122 node ID" % hex)
826
Michael Felt3a1d50e2019-06-15 17:52:29 +0200827 @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS,
828 "ifconfig is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200829 def test_ifconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200830 node = self.uuid._ifconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500831 self.check_node(node, 'ifconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200832
Michael Felt3a1d50e2019-06-15 17:52:29 +0200833 @unittest.skipUnless(_uuid._ip_getnode in _uuid._GETTERS,
834 "ip is not used for introspection on this platform")
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200835 def test_ip_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200836 node = self.uuid._ip_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500837 self.check_node(node, 'ip')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200838
Michael Felt3a1d50e2019-06-15 17:52:29 +0200839 @unittest.skipUnless(_uuid._arp_getnode in _uuid._GETTERS,
840 "arp is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200841 def test_arp_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200842 node = self.uuid._arp_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500843 self.check_node(node, 'arp')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200844
Michael Felt3a1d50e2019-06-15 17:52:29 +0200845 @unittest.skipUnless(_uuid._lanscan_getnode in _uuid._GETTERS,
846 "lanscan is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200847 def test_lanscan_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200848 node = self.uuid._lanscan_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500849 self.check_node(node, 'lanscan')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200850
Michael Felt3a1d50e2019-06-15 17:52:29 +0200851 @unittest.skipUnless(_uuid._netstat_getnode in _uuid._GETTERS,
852 "netstat is not used for introspection on this platform")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200853 def test_netstat_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200854 node = self.uuid._netstat_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500855 self.check_node(node, 'netstat')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200856
857 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
858 def test_ipconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200859 node = self.uuid._ipconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500860 self.check_node(node, 'ipconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200861
862 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
863 @unittest.skipUnless(importable('netbios'), 'requires netbios')
864 def test_netbios_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200865 node = self.uuid._netbios_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500866 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200867
868 def test_random_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200869 node = self.uuid._random_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500870 # The multicast bit, i.e. the least significant bit of first octet,
871 # must be set for randomly generated MAC addresses. See RFC 4122,
872 # $4.1.6.
873 self.assertTrue(node & (1 << 40), '%012x' % node)
Victor Stinnerc9409f72017-11-28 00:30:21 +0100874 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200875
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200876 node2 = self.uuid._random_getnode()
877 self.assertNotEqual(node2, node, '%012x' % node)
878
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200879 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Antoine Pitroua106aec2017-09-28 23:03:06 +0200880 def test_unix_getnode(self):
881 if not importable('_uuid') and not importable('ctypes'):
882 self.skipTest("neither _uuid extension nor ctypes available")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200883 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200884 node = self.uuid._unix_getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200885 except TypeError:
886 self.skipTest('requires uuid_generate_time')
Victor Stinnerc9409f72017-11-28 00:30:21 +0100887 self.check_node(node, 'unix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200888
889 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
890 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
891 def test_windll_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200892 node = self.uuid._windll_getnode()
Victor Stinnerc9409f72017-11-28 00:30:21 +0100893 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200894
895
Antoine Pitroua106aec2017-09-28 23:03:06 +0200896class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase):
897 uuid = py_uuid
898
899@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
900class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase):
901 uuid = c_uuid
902
903
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000904if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300905 unittest.main()