blob: c1b35c4a7f6c1b3762beac68352cdaff66657711 [file] [log] [blame]
Victor Stinnerb9d01992014-10-21 22:33:10 +02001import unittest.mock
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
6from functools import partial
Serhiy Storchaka56507c72013-11-26 22:47:16 +02007import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03008import os
Tal Einat3e2b29d2018-09-06 14:34:25 +03009import pickle
Serhiy Storchakacce440f2014-01-10 15:06:59 +020010import shutil
Victor Stinnerb9d01992014-10-21 22:33:10 +020011import subprocess
Tal Einat3e2b29d2018-09-06 14:34:25 +030012import sys
Antoine Pitroua106aec2017-09-28 23:03:06 +020013
14py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
15c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
16
Thomas Wouters0e3f5912006-08-11 14:57:12 +000017
18def importable(name):
19 try:
20 __import__(name)
21 return True
22 except:
23 return False
24
Antoine Pitroua106aec2017-09-28 23:03:06 +020025
26class BaseTestUUID:
27 uuid = None
28
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029 def test_UUID(self):
30 equal = self.assertEqual
31 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000032 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033 time, clock_seq, variant, version) in [
34 ('00000000-0000-0000-0000-000000000000',
35 '{00000000-0000-0000-0000-000000000000}',
36 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000037 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
38 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039 (0, 0, 0, 0, 0, 0),
40 0,
41 'urn:uuid:00000000-0000-0000-0000-000000000000',
Antoine Pitroua106aec2017-09-28 23:03:06 +020042 0, 0, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
44 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
45 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000046 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
47 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 +000048 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
49 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Antoine Pitroua106aec2017-09-28 23:03:06 +020051 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
53 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
54 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000055 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
56 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 +000057 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
58 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Antoine Pitroua106aec2017-09-28 23:03:06 +020060 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 ('12345678-1234-5678-1234-567812345678',
62 '{12345678-1234-5678-1234-567812345678}',
63 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000064 b'\x12\x34\x56\x78'*4,
65 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
67 0x12345678123456781234567812345678,
68 'urn:uuid:12345678-1234-5678-1234-567812345678',
Antoine Pitroua106aec2017-09-28 23:03:06 +020069 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
71 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
72 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000073 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
74 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 +000075 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
76 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020078 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
80 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
81 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000082 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
83 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 +000084 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
85 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020087 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
89 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
90 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000091 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
92 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 +000093 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
94 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020096 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
98 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
99 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000100 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
101 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 +0000102 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
103 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200105 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
107 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
108 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000109 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
110 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 +0000111 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
112 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000113 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200114 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000115 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
116 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
117 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000118 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
119 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 +0000120 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
121 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000122 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200123 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
125 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
126 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000127 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
128 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 +0000129 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
130 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000131 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200132 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
134 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
135 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000136 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
137 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 +0000138 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
139 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000140 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200141 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
143 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
144 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000145 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
146 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 +0000147 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
148 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200150 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
152 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
153 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000154 b'\xff'*16,
155 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000156 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
157 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000158 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200159 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000160 ]:
161 equivalents = []
162 # Construct each UUID in several different ways.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200163 for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex),
164 self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le),
165 self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer),
166 self.uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167 # Test all conversions and properties of the UUID object.
168 equal(str(u), string)
169 equal(int(u), integer)
170 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000171 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172 equal(u.fields, fields)
173 equal(u.time_low, fields[0])
174 equal(u.time_mid, fields[1])
175 equal(u.time_hi_version, fields[2])
176 equal(u.clock_seq_hi_variant, fields[3])
177 equal(u.clock_seq_low, fields[4])
178 equal(u.node, fields[5])
179 equal(u.hex, hex)
180 equal(u.int, integer)
181 equal(u.urn, urn)
182 equal(u.time, time)
183 equal(u.clock_seq, clock_seq)
184 equal(u.variant, variant)
185 equal(u.version, version)
186 equivalents.append(u)
187
188 # Different construction methods should give the same UUID.
189 for u in equivalents:
190 for v in equivalents:
191 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000192
193 # Bug 7380: "bytes" and "bytes_le" should give the same type.
194 equal(type(u.bytes), builtins.bytes)
195 equal(type(u.bytes_le), builtins.bytes)
196
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000197 ascending.append(u)
198
199 # Test comparison of UUIDs.
200 for i in range(len(ascending)):
201 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000202 equal(i < j, ascending[i] < ascending[j])
203 equal(i <= j, ascending[i] <= ascending[j])
204 equal(i == j, ascending[i] == ascending[j])
205 equal(i > j, ascending[i] > ascending[j])
206 equal(i >= j, ascending[i] >= ascending[j])
207 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208
209 # Test sorting of UUIDs (above list is in ascending order).
210 resorted = ascending[:]
211 resorted.reverse()
212 resorted.sort()
213 equal(ascending, resorted)
214
215 def test_exceptions(self):
216 badvalue = lambda f: self.assertRaises(ValueError, f)
217 badtype = lambda f: self.assertRaises(TypeError, f)
218
219 # Badly formed hex strings.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200220 badvalue(lambda: self.uuid.UUID(''))
221 badvalue(lambda: self.uuid.UUID('abc'))
222 badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
223 badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
224 badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000225
226 # Badly formed bytes.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200227 badvalue(lambda: self.uuid.UUID(bytes='abc'))
228 badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
229 badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000230
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000231 # Badly formed bytes_le.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200232 badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
233 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
234 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000235
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236 # Badly formed fields.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200237 badvalue(lambda: self.uuid.UUID(fields=(1,)))
238 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5)))
239 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240
241 # Field values out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200242 badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
243 badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
244 badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
245 badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
246 badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
247 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
248 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
249 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
250 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
251 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
252 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
253 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254
255 # Version number out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200256 badvalue(lambda: self.uuid.UUID('00'*16, version=0))
257 badvalue(lambda: self.uuid.UUID('00'*16, version=6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258
259 # Integer value out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200260 badvalue(lambda: self.uuid.UUID(int=-1))
261 badvalue(lambda: self.uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262
263 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000264 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Antoine Pitroua106aec2017-09-28 23:03:06 +0200265 self.uuid.UUID(h)
266 self.uuid.UUID(hex=h)
267 self.uuid.UUID(bytes=b)
268 self.uuid.UUID(bytes_le=b)
269 self.uuid.UUID(fields=f)
270 self.uuid.UUID(int=i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271
272 # Wrong number of arguments (positional).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200273 badtype(lambda: self.uuid.UUID())
274 badtype(lambda: self.uuid.UUID(h, b))
275 badtype(lambda: self.uuid.UUID(h, b, b))
276 badtype(lambda: self.uuid.UUID(h, b, b, f))
277 badtype(lambda: self.uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000279 # Duplicate arguments.
280 for hh in [[], [('hex', h)]]:
281 for bb in [[], [('bytes', b)]]:
282 for bble in [[], [('bytes_le', b)]]:
283 for ii in [[], [('int', i)]]:
284 for ff in [[], [('fields', f)]]:
285 args = dict(hh + bb + bble + ii + ff)
286 if len(args) != 0:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200287 badtype(lambda: self.uuid.UUID(h, **args))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000288 if len(args) != 1:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200289 badtype(lambda: self.uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000290
291 # Immutability.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200292 u = self.uuid.UUID(h)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293 badtype(lambda: setattr(u, 'hex', h))
294 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000295 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296 badtype(lambda: setattr(u, 'fields', f))
297 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000298 badtype(lambda: setattr(u, 'time_low', 0))
299 badtype(lambda: setattr(u, 'time_mid', 0))
300 badtype(lambda: setattr(u, 'time_hi_version', 0))
301 badtype(lambda: setattr(u, 'time_hi_version', 0))
302 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
303 badtype(lambda: setattr(u, 'clock_seq_low', 0))
304 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305
Berker Peksag6b5e4a82016-12-31 20:08:16 +0300306 # Comparison with a non-UUID object
307 badtype(lambda: u < object())
308 badtype(lambda: u > object())
309
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000310 def test_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200311 node1 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200312 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000313
314 # Test it again to ensure consistency.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200315 node2 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200316 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200317
Tal Einat3e2b29d2018-09-06 14:34:25 +0300318 def test_pickle_roundtrip(self):
Tal Einat54752532018-09-10 16:11:04 +0300319 def check(actual, expected):
320 self.assertEqual(actual, expected)
321 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300322
Tal Einat54752532018-09-10 16:11:04 +0300323 with support.swap_item(sys.modules, 'uuid', self.uuid):
324 for is_safe in self.uuid.SafeUUID:
325 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
326 is_safe=is_safe)
327 check(copy.copy(u), u)
328 check(copy.deepcopy(u), u)
329 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
330 with self.subTest(protocol=proto):
331 check(pickle.loads(pickle.dumps(u, proto)), u)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300332
333 def test_unpickle_previous_python_versions(self):
Tal Einat54752532018-09-10 16:11:04 +0300334 def check(actual, expected):
335 self.assertEqual(actual, expected)
336 self.assertEqual(actual.is_safe, expected.is_safe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300337
Tal Einat54752532018-09-10 16:11:04 +0300338 pickled_uuids = [
339 # Python 2.7, protocol 0
340 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
341 b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.',
342 # Python 2.7, protocol 1
343 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
344 b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.',
345 # Python 2.7, protocol 2
346 b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}'
347 b'\xde\xa0Bf\xcey%\xd8\x00sb.',
348 # Python 3.6, protocol 0
349 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
350 b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.',
351 # Python 3.6, protocol 1
352 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
353 b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L'
354 b'\nsb.',
355 # Python 3.6, protocol 2
356 b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
357 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
358 # Python 3.6, protocol 3
359 b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec'
360 b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.',
361 # Python 3.6, protocol 4
362 b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI'
363 b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%'
364 b'\xd8\x00sb.',
365 # Python 3.7, protocol 0
366 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
367 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
368 b'cuuid\nSafeUUID\n(NtRsb.',
369 # Python 3.7, protocol 1
370 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
371 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
372 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.',
373 # Python 3.7, protocol 2
374 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
375 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
376 b'id\nSafeUUID\nN\x85Rub.',
377 # Python 3.7, protocol 3
378 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
379 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
380 b'id\nSafeUUID\nN\x85Rub.',
381 # Python 3.7, protocol 4
382 b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
383 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
384 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub'
385 b'.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300386 ]
Tal Einat54752532018-09-10 16:11:04 +0300387 pickled_uuids_safe = [
388 # Python 3.7, protocol 0
389 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
390 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
391 b'cuuid\nSafeUUID\n(I0\ntRsb.',
392 # Python 3.7, protocol 1
393 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
394 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
395 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.',
396 # Python 3.7, protocol 2
397 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
398 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
399 b'id\nSafeUUID\nK\x00\x85Rub.',
400 # Python 3.7, protocol 3
401 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
402 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
403 b'id\nSafeUUID\nK\x00\x85Rub.',
404 # Python 3.7, protocol 4
405 b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
406 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
407 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00'
408 b'\x85Rub.',
409 ]
410 pickled_uuids_unsafe = [
411 # Python 3.7, protocol 0
412 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
413 b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n'
414 b'cuuid\nSafeUUID\n(I-1\ntRsb.',
415 # Python 3.7, protocol 1
416 b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN'
417 b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701'
418 b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR'
419 b'ub.',
420 # Python 3.7, protocol 2
421 b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
422 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
423 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
424 # Python 3.7, protocol 3
425 b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z'
426 b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu'
427 b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.',
428 # Python 3.7, protocol 4
429 b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c'
430 b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0'
431 b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff'
432 b'\xff\xff\xff\x85Rub.',
Tal Einat3e2b29d2018-09-06 14:34:25 +0300433 ]
434
Tal Einat54752532018-09-10 16:11:04 +0300435 u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5')
436 u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
437 is_safe=self.uuid.SafeUUID.safe)
438 u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5',
439 is_safe=self.uuid.SafeUUID.unsafe)
440
441 with support.swap_item(sys.modules, 'uuid', self.uuid):
442 for pickled in pickled_uuids:
443 # is_safe was added in 3.7. When unpickling values from older
444 # versions, is_safe will be missing, so it should be set to
445 # SafeUUID.unknown.
446 check(pickle.loads(pickled), u)
447 for pickled in pickled_uuids_safe:
448 check(pickle.loads(pickled), u_safe)
449 for pickled in pickled_uuids_unsafe:
450 check(pickle.loads(pickled), u_unsafe)
Tal Einat3e2b29d2018-09-06 14:34:25 +0300451
Bo Bayles6b273f72018-01-23 19:11:44 -0600452 # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
453 # need not necessarily be 48 bits (e.g., EUI-64).
454 def test_uuid1_eui64(self):
455 # Confirm that uuid.getnode ignores hardware addresses larger than 48
456 # bits. Mock out each platform's *_getnode helper functions to return
457 # something just larger than 48 bits to test. This will cause
458 # uuid.getnode to fall back on uuid._random_getnode, which will
459 # generate a valid value.
460 too_large_getter = lambda: 1 << 48
461 with unittest.mock.patch.multiple(
462 self.uuid,
463 _node=None, # Ignore any cached node value.
464 _NODE_GETTERS_WIN32=[too_large_getter],
465 _NODE_GETTERS_UNIX=[too_large_getter],
466 ):
467 node = self.uuid.getnode()
468 self.assertTrue(0 < node < (1 << 48), '%012x' % node)
469
470 # Confirm that uuid1 can use the generated node, i.e., the that
471 # uuid.getnode fell back on uuid._random_getnode() rather than using
472 # the value from too_large_getter above.
473 try:
474 self.uuid.uuid1(node=node)
475 except ValueError as e:
476 self.fail('uuid1 was given an invalid node ID')
477
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 def test_uuid1(self):
479 equal = self.assertEqual
480
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000481 # Make sure uuid1() generates UUIDs that are actually version 1.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200482 for u in [self.uuid.uuid1() for i in range(10)]:
483 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484 equal(u.version, 1)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200485 self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe,
486 self.uuid.SafeUUID.unsafe,
487 self.uuid.SafeUUID.unknown})
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000489 # Make sure the generated UUIDs are actually unique.
490 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200491 for u in [self.uuid.uuid1() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000492 uuids[u] = 1
493 equal(len(uuids.keys()), 1000)
494
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000495 # Make sure the supplied node ID appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200496 u = self.uuid.uuid1(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497 equal(u.node, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200498 u = self.uuid.uuid1(0x123456789abc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000499 equal(u.node, 0x123456789abc)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200500 u = self.uuid.uuid1(0xffffffffffff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501 equal(u.node, 0xffffffffffff)
502
503 # Make sure the supplied clock sequence appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200504 u = self.uuid.uuid1(0x123456789abc, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505 equal(u.node, 0x123456789abc)
506 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200507 u = self.uuid.uuid1(0x123456789abc, 0x1234)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508 equal(u.node, 0x123456789abc)
509 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
510 u.clock_seq_low, 0x1234)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200511 u = self.uuid.uuid1(0x123456789abc, 0x3fff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 equal(u.node, 0x123456789abc)
513 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
514 u.clock_seq_low, 0x3fff)
515
Antoine Pitroua106aec2017-09-28 23:03:06 +0200516 # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns
517 # self.uuid.SafeUUID.unknown
Victor Stinnerc209b702017-04-19 13:01:03 +0200518 @support.requires_mac_ver(10, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200519 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500520 def test_uuid1_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200521 if not self.uuid._has_uuid_generate_time_safe:
522 self.skipTest('requires uuid_generate_time_safe(3)')
523
524 u = self.uuid.uuid1()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500525 # uuid_generate_time_safe() may return 0 or -1 but what it returns is
526 # dependent on the underlying platform support. At least it cannot be
527 # unknown (unless I suppose the platform is buggy).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200528 self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500529
Antoine Pitroua106aec2017-09-28 23:03:06 +0200530 @contextlib.contextmanager
531 def mock_generate_time_safe(self, safe_value):
532 """
533 Mock uuid._generate_time_safe() to return a given *safe_value*.
534 """
535 if os.name != 'posix':
536 self.skipTest('POSIX-only test')
537 self.uuid._load_system_functions()
538 f = self.uuid._generate_time_safe
539 if f is None:
540 self.skipTest('need uuid._generate_time_safe')
541 with unittest.mock.patch.object(self.uuid, '_generate_time_safe',
542 lambda: (f()[0], safe_value)):
543 yield
544
545 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500546 def test_uuid1_unknown(self):
547 # Even if the platform has uuid_generate_time_safe(), let's mock it to
548 # be uuid_generate_time() and ensure the safety is unknown.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200549 with self.mock_generate_time_safe(None):
550 u = self.uuid.uuid1()
551 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500552
Antoine Pitroua106aec2017-09-28 23:03:06 +0200553 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500554 def test_uuid1_is_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200555 with self.mock_generate_time_safe(0):
556 u = self.uuid.uuid1()
557 self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500558
Antoine Pitroua106aec2017-09-28 23:03:06 +0200559 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500560 def test_uuid1_is_unsafe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200561 with self.mock_generate_time_safe(-1):
562 u = self.uuid.uuid1()
563 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe)
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_bogus_return_value(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200567 with self.mock_generate_time_safe(3):
568 u = self.uuid.uuid1()
569 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500570
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000571 def test_uuid3(self):
572 equal = self.assertEqual
573
574 # Test some known version-3 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200575 for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200577 (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000578 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200579 (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200581 (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
583 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200584 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585 equal(u.version, 3)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200586 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000587 equal(str(u), v)
588
589 def test_uuid4(self):
590 equal = self.assertEqual
591
592 # Make sure uuid4() generates UUIDs that are actually version 4.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200593 for u in [self.uuid.uuid4() for i in range(10)]:
594 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595 equal(u.version, 4)
596
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000597 # Make sure the generated UUIDs are actually unique.
598 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200599 for u in [self.uuid.uuid4() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000600 uuids[u] = 1
601 equal(len(uuids.keys()), 1000)
602
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000603 def test_uuid5(self):
604 equal = self.assertEqual
605
606 # Test some known version-5 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200607 for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000608 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200609 (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200611 (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200613 (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000614 'cc957dd1-a972-5349-98cd-874190002798'),
615 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200616 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 equal(u.version, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200618 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619 equal(str(u), v)
620
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300621 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000622 def testIssue8621(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200623 # On at least some versions of OSX self.uuid.uuid4 generates
Ronald Oussorenac764d32010-05-05 15:32:33 +0000624 # the same sequence of UUIDs in the parent and any
625 # children started using fork.
626 fds = os.pipe()
627 pid = os.fork()
628 if pid == 0:
629 os.close(fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200630 value = self.uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000631 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000632 os._exit(0)
633
634 else:
635 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100636 self.addCleanup(os.close, fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200637 parent_value = self.uuid.uuid4().hex
Ronald Oussorenac764d32010-05-05 15:32:33 +0000638 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000639 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000640
641 self.assertNotEqual(parent_value, child_value)
642
643
Antoine Pitroua106aec2017-09-28 23:03:06 +0200644class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
645 uuid = py_uuid
646
647@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
648class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
649 uuid = c_uuid
650
651
652class BaseTestInternals:
653 uuid = None
654
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200655 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
656 def test_find_mac(self):
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200657 data = '''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200658fake hwaddr
659cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
660eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
661'''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200662
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200663 popen = unittest.mock.MagicMock()
664 popen.stdout = io.BytesIO(data.encode())
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200665
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200666 with unittest.mock.patch.object(shutil, 'which',
667 return_value='/sbin/ifconfig'):
668 with unittest.mock.patch.object(subprocess, 'Popen',
669 return_value=popen):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200670 mac = self.uuid._find_mac(
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200671 command='ifconfig',
672 args='',
673 hw_identifiers=[b'hwaddr'],
674 get_index=lambda x: x + 1,
675 )
676
677 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200678
Barry Warsaw23df2d12017-11-28 17:26:04 -0500679 def check_node(self, node, requires=None):
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200680 if requires and node is None:
681 self.skipTest('requires ' + requires)
682 hex = '%012x' % node
683 if support.verbose >= 2:
684 print(hex, end=' ')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200685 self.assertTrue(0 < node < (1 << 48),
686 "%s is not an RFC 4122 node ID" % hex)
687
688 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
689 def test_ifconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200690 node = self.uuid._ifconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500691 self.check_node(node, 'ifconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200692
693 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200694 def test_ip_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200695 node = self.uuid._ip_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500696 self.check_node(node, 'ip')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200697
698 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200699 def test_arp_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200700 node = self.uuid._arp_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500701 self.check_node(node, 'arp')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200702
703 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
704 def test_lanscan_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200705 node = self.uuid._lanscan_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500706 self.check_node(node, 'lanscan')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200707
708 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
709 def test_netstat_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200710 node = self.uuid._netstat_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500711 self.check_node(node, 'netstat')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200712
713 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
714 def test_ipconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200715 node = self.uuid._ipconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500716 self.check_node(node, 'ipconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200717
718 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
719 @unittest.skipUnless(importable('netbios'), 'requires netbios')
720 def test_netbios_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200721 node = self.uuid._netbios_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500722 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200723
724 def test_random_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200725 node = self.uuid._random_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500726 # The multicast bit, i.e. the least significant bit of first octet,
727 # must be set for randomly generated MAC addresses. See RFC 4122,
728 # $4.1.6.
729 self.assertTrue(node & (1 << 40), '%012x' % node)
Victor Stinnerc9409f72017-11-28 00:30:21 +0100730 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200731
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200732 node2 = self.uuid._random_getnode()
733 self.assertNotEqual(node2, node, '%012x' % node)
734
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200735 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Antoine Pitroua106aec2017-09-28 23:03:06 +0200736 def test_unix_getnode(self):
737 if not importable('_uuid') and not importable('ctypes'):
738 self.skipTest("neither _uuid extension nor ctypes available")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200739 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200740 node = self.uuid._unix_getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200741 except TypeError:
742 self.skipTest('requires uuid_generate_time')
Victor Stinnerc9409f72017-11-28 00:30:21 +0100743 self.check_node(node, 'unix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200744
745 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
746 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
747 def test_windll_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200748 node = self.uuid._windll_getnode()
Victor Stinnerc9409f72017-11-28 00:30:21 +0100749 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200750
751
Antoine Pitroua106aec2017-09-28 23:03:06 +0200752class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase):
753 uuid = py_uuid
754
755@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
756class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase):
757 uuid = c_uuid
758
759
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000760if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300761 unittest.main()