blob: f21bd6dfa15235667f4b5192be76441abfe8be43 [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
Serhiy Storchaka56507c72013-11-26 22:47:16 +02005import io
Serhiy Storchaka7d15b542013-05-31 22:31:02 +03006import os
Serhiy Storchakacce440f2014-01-10 15:06:59 +02007import shutil
Victor Stinnerb9d01992014-10-21 22:33:10 +02008import subprocess
Antoine Pitroua106aec2017-09-28 23:03:06 +02009
10py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
11c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
12
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013
14def importable(name):
15 try:
16 __import__(name)
17 return True
18 except:
19 return False
20
Antoine Pitroua106aec2017-09-28 23:03:06 +020021
22class BaseTestUUID:
23 uuid = None
24
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 def test_UUID(self):
26 equal = self.assertEqual
27 ascending = []
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000028 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029 time, clock_seq, variant, version) in [
30 ('00000000-0000-0000-0000-000000000000',
31 '{00000000-0000-0000-0000-000000000000}',
32 '00000000000000000000000000000000',
Guido van Rossum65b6a802007-07-09 14:03:08 +000033 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
34 b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035 (0, 0, 0, 0, 0, 0),
36 0,
37 'urn:uuid:00000000-0000-0000-0000-000000000000',
Antoine Pitroua106aec2017-09-28 23:03:06 +020038 0, 0, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039 ('00010203-0405-0607-0809-0a0b0c0d0e0f',
40 '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
41 '000102030405060708090a0b0c0d0e0f',
Guido van Rossum65b6a802007-07-09 14:03:08 +000042 b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
43 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 +000044 (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
45 0x000102030405060708090a0b0c0d0e0f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
Antoine Pitroua106aec2017-09-28 23:03:06 +020047 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
49 '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
50 '02d9e6d59467382e8f9b9300a64ac3cd',
Guido van Rossum65b6a802007-07-09 14:03:08 +000051 b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
52 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 +000053 (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
54 0x02d9e6d59467382e8f9b9300a64ac3cd,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
Antoine Pitroua106aec2017-09-28 23:03:06 +020056 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 ('12345678-1234-5678-1234-567812345678',
58 '{12345678-1234-5678-1234-567812345678}',
59 '12345678123456781234567812345678',
Guido van Rossum65b6a802007-07-09 14:03:08 +000060 b'\x12\x34\x56\x78'*4,
61 b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062 (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
63 0x12345678123456781234567812345678,
64 'urn:uuid:12345678-1234-5678-1234-567812345678',
Antoine Pitroua106aec2017-09-28 23:03:06 +020065 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
67 '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
68 '6ba7b8109dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000069 b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70 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 +000071 (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
72 0x6ba7b8109dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020074 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
76 '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
77 '6ba7b8119dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000078 b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79 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 +000080 (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
81 0x6ba7b8119dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020083 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084 ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
85 '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
86 '6ba7b8129dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000087 b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88 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 +000089 (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
90 0x6ba7b8129dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +020092 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
94 '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
95 '6ba7b8149dad11d180b400c04fd430c8',
Guido van Rossum65b6a802007-07-09 14:03:08 +000096 b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
97 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 +000098 (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
99 0x6ba7b8149dad11d180b400c04fd430c8,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200101 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102 ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
103 '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
104 '7d4448409dc011d1b2455ffdce74fad2',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000105 b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
106 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 +0000107 (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
108 0x7d4448409dc011d1b2455ffdce74fad2,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200110 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111 ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
112 '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
113 'e902893a9d223c7ea7b8d6e313b71d9f',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000114 b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
115 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 +0000116 (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
117 0xe902893a9d223c7ea7b8d6e313b71d9f,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000118 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200119 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
121 '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
122 'eb4240266f544ef8a4d0bb658a1fc6cf',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000123 b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
124 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 +0000125 (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
126 0xeb4240266f544ef8a4d0bb658a1fc6cf,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200128 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129 ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
130 '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
131 'f81d4fae7dec11d0a76500a0c91e6bf6',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000132 b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
133 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 +0000134 (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
135 0xf81d4fae7dec11d0a76500a0c91e6bf6,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000136 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200137 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000138 ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
139 '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
140 'fffefdfcfffefffefffefffefdfcfbfa',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000141 b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
142 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 +0000143 (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
144 0xfffefdfcfffefffefffefffefdfcfbfa,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200146 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 ('ffffffff-ffff-ffff-ffff-ffffffffffff',
148 '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
149 'ffffffffffffffffffffffffffffffff',
Guido van Rossum65b6a802007-07-09 14:03:08 +0000150 b'\xff'*16,
151 b'\xff'*16,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000152 (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
153 0xffffffffffffffffffffffffffffffff,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000154 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
Antoine Pitroua106aec2017-09-28 23:03:06 +0200155 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 ]:
157 equivalents = []
158 # Construct each UUID in several different ways.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200159 for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex),
160 self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le),
161 self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer),
162 self.uuid.UUID(urn)]:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000163 # Test all conversions and properties of the UUID object.
164 equal(str(u), string)
165 equal(int(u), integer)
166 equal(u.bytes, bytes)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000167 equal(u.bytes_le, bytes_le)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000168 equal(u.fields, fields)
169 equal(u.time_low, fields[0])
170 equal(u.time_mid, fields[1])
171 equal(u.time_hi_version, fields[2])
172 equal(u.clock_seq_hi_variant, fields[3])
173 equal(u.clock_seq_low, fields[4])
174 equal(u.node, fields[5])
175 equal(u.hex, hex)
176 equal(u.int, integer)
177 equal(u.urn, urn)
178 equal(u.time, time)
179 equal(u.clock_seq, clock_seq)
180 equal(u.variant, variant)
181 equal(u.version, version)
182 equivalents.append(u)
183
184 # Different construction methods should give the same UUID.
185 for u in equivalents:
186 for v in equivalents:
187 equal(u, v)
Georg Brandl1d523e12009-12-19 18:23:28 +0000188
189 # Bug 7380: "bytes" and "bytes_le" should give the same type.
190 equal(type(u.bytes), builtins.bytes)
191 equal(type(u.bytes_le), builtins.bytes)
192
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000193 ascending.append(u)
194
195 # Test comparison of UUIDs.
196 for i in range(len(ascending)):
197 for j in range(len(ascending)):
Mark Dickinsona56c4672009-01-27 18:17:45 +0000198 equal(i < j, ascending[i] < ascending[j])
199 equal(i <= j, ascending[i] <= ascending[j])
200 equal(i == j, ascending[i] == ascending[j])
201 equal(i > j, ascending[i] > ascending[j])
202 equal(i >= j, ascending[i] >= ascending[j])
203 equal(i != j, ascending[i] != ascending[j])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204
205 # Test sorting of UUIDs (above list is in ascending order).
206 resorted = ascending[:]
207 resorted.reverse()
208 resorted.sort()
209 equal(ascending, resorted)
210
211 def test_exceptions(self):
212 badvalue = lambda f: self.assertRaises(ValueError, f)
213 badtype = lambda f: self.assertRaises(TypeError, f)
214
215 # Badly formed hex strings.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200216 badvalue(lambda: self.uuid.UUID(''))
217 badvalue(lambda: self.uuid.UUID('abc'))
218 badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
219 badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
220 badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221
222 # Badly formed bytes.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200223 badvalue(lambda: self.uuid.UUID(bytes='abc'))
224 badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
225 badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000227 # Badly formed bytes_le.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200228 badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
229 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
230 badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000231
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000232 # Badly formed fields.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200233 badvalue(lambda: self.uuid.UUID(fields=(1,)))
234 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5)))
235 badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236
237 # Field values out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200238 badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
239 badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
240 badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
241 badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
242 badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
243 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
244 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
245 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
246 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
247 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
248 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
249 badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250
251 # Version number out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200252 badvalue(lambda: self.uuid.UUID('00'*16, version=0))
253 badvalue(lambda: self.uuid.UUID('00'*16, version=6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254
255 # Integer value out of range.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200256 badvalue(lambda: self.uuid.UUID(int=-1))
257 badvalue(lambda: self.uuid.UUID(int=1<<128))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258
259 # Must supply exactly one of hex, bytes, fields, int.
Guido van Rossum65b6a802007-07-09 14:03:08 +0000260 h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
Antoine Pitroua106aec2017-09-28 23:03:06 +0200261 self.uuid.UUID(h)
262 self.uuid.UUID(hex=h)
263 self.uuid.UUID(bytes=b)
264 self.uuid.UUID(bytes_le=b)
265 self.uuid.UUID(fields=f)
266 self.uuid.UUID(int=i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267
268 # Wrong number of arguments (positional).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200269 badtype(lambda: self.uuid.UUID())
270 badtype(lambda: self.uuid.UUID(h, b))
271 badtype(lambda: self.uuid.UUID(h, b, b))
272 badtype(lambda: self.uuid.UUID(h, b, b, f))
273 badtype(lambda: self.uuid.UUID(h, b, b, f, i))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000274
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000275 # Duplicate arguments.
276 for hh in [[], [('hex', h)]]:
277 for bb in [[], [('bytes', b)]]:
278 for bble in [[], [('bytes_le', b)]]:
279 for ii in [[], [('int', i)]]:
280 for ff in [[], [('fields', f)]]:
281 args = dict(hh + bb + bble + ii + ff)
282 if len(args) != 0:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200283 badtype(lambda: self.uuid.UUID(h, **args))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000284 if len(args) != 1:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200285 badtype(lambda: self.uuid.UUID(**args))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286
287 # Immutability.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200288 u = self.uuid.UUID(h)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289 badtype(lambda: setattr(u, 'hex', h))
290 badtype(lambda: setattr(u, 'bytes', b))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000291 badtype(lambda: setattr(u, 'bytes_le', b))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292 badtype(lambda: setattr(u, 'fields', f))
293 badtype(lambda: setattr(u, 'int', i))
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000294 badtype(lambda: setattr(u, 'time_low', 0))
295 badtype(lambda: setattr(u, 'time_mid', 0))
296 badtype(lambda: setattr(u, 'time_hi_version', 0))
297 badtype(lambda: setattr(u, 'time_hi_version', 0))
298 badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
299 badtype(lambda: setattr(u, 'clock_seq_low', 0))
300 badtype(lambda: setattr(u, 'node', 0))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301
Berker Peksag6b5e4a82016-12-31 20:08:16 +0300302 # Comparison with a non-UUID object
303 badtype(lambda: u < object())
304 badtype(lambda: u > object())
305
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 def test_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200307 node1 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200308 self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309
310 # Test it again to ensure consistency.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200311 node2 = self.uuid.getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200312 self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
Serhiy Storchaka56507c72013-11-26 22:47:16 +0200313
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314 def test_uuid1(self):
315 equal = self.assertEqual
316
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000317 # Make sure uuid1() generates UUIDs that are actually version 1.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200318 for u in [self.uuid.uuid1() for i in range(10)]:
319 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000320 equal(u.version, 1)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200321 self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe,
322 self.uuid.SafeUUID.unsafe,
323 self.uuid.SafeUUID.unknown})
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000324
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000325 # Make sure the generated UUIDs are actually unique.
326 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200327 for u in [self.uuid.uuid1() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000328 uuids[u] = 1
329 equal(len(uuids.keys()), 1000)
330
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000331 # Make sure the supplied node ID appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200332 u = self.uuid.uuid1(0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000333 equal(u.node, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200334 u = self.uuid.uuid1(0x123456789abc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000335 equal(u.node, 0x123456789abc)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200336 u = self.uuid.uuid1(0xffffffffffff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000337 equal(u.node, 0xffffffffffff)
338
339 # Make sure the supplied clock sequence appears in the UUID.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200340 u = self.uuid.uuid1(0x123456789abc, 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341 equal(u.node, 0x123456789abc)
342 equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200343 u = self.uuid.uuid1(0x123456789abc, 0x1234)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000344 equal(u.node, 0x123456789abc)
345 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
346 u.clock_seq_low, 0x1234)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200347 u = self.uuid.uuid1(0x123456789abc, 0x3fff)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000348 equal(u.node, 0x123456789abc)
349 equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
350 u.clock_seq_low, 0x3fff)
351
Antoine Pitroua106aec2017-09-28 23:03:06 +0200352 # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns
353 # self.uuid.SafeUUID.unknown
Victor Stinnerc209b702017-04-19 13:01:03 +0200354 @support.requires_mac_ver(10, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200355 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500356 def test_uuid1_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200357 if not self.uuid._has_uuid_generate_time_safe:
358 self.skipTest('requires uuid_generate_time_safe(3)')
359
360 u = self.uuid.uuid1()
Barry Warsaw8c130d72017-02-18 15:45:49 -0500361 # uuid_generate_time_safe() may return 0 or -1 but what it returns is
362 # dependent on the underlying platform support. At least it cannot be
363 # unknown (unless I suppose the platform is buggy).
Antoine Pitroua106aec2017-09-28 23:03:06 +0200364 self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500365
Antoine Pitroua106aec2017-09-28 23:03:06 +0200366 @contextlib.contextmanager
367 def mock_generate_time_safe(self, safe_value):
368 """
369 Mock uuid._generate_time_safe() to return a given *safe_value*.
370 """
371 if os.name != 'posix':
372 self.skipTest('POSIX-only test')
373 self.uuid._load_system_functions()
374 f = self.uuid._generate_time_safe
375 if f is None:
376 self.skipTest('need uuid._generate_time_safe')
377 with unittest.mock.patch.object(self.uuid, '_generate_time_safe',
378 lambda: (f()[0], safe_value)):
379 yield
380
381 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500382 def test_uuid1_unknown(self):
383 # Even if the platform has uuid_generate_time_safe(), let's mock it to
384 # be uuid_generate_time() and ensure the safety is unknown.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200385 with self.mock_generate_time_safe(None):
386 u = self.uuid.uuid1()
387 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500388
Antoine Pitroua106aec2017-09-28 23:03:06 +0200389 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500390 def test_uuid1_is_safe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200391 with self.mock_generate_time_safe(0):
392 u = self.uuid.uuid1()
393 self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500394
Antoine Pitroua106aec2017-09-28 23:03:06 +0200395 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500396 def test_uuid1_is_unsafe(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200397 with self.mock_generate_time_safe(-1):
398 u = self.uuid.uuid1()
399 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500400
Antoine Pitroua106aec2017-09-28 23:03:06 +0200401 @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
Barry Warsaw8c130d72017-02-18 15:45:49 -0500402 def test_uuid1_bogus_return_value(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200403 with self.mock_generate_time_safe(3):
404 u = self.uuid.uuid1()
405 self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
Barry Warsaw8c130d72017-02-18 15:45:49 -0500406
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407 def test_uuid3(self):
408 equal = self.assertEqual
409
410 # Test some known version-3 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200411 for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000412 '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200413 (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000414 '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200415 (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200417 (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000418 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
419 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200420 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421 equal(u.version, 3)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200422 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000423 equal(str(u), v)
424
425 def test_uuid4(self):
426 equal = self.assertEqual
427
428 # Make sure uuid4() generates UUIDs that are actually version 4.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200429 for u in [self.uuid.uuid4() for i in range(10)]:
430 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000431 equal(u.version, 4)
432
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000433 # Make sure the generated UUIDs are actually unique.
434 uuids = {}
Antoine Pitroua106aec2017-09-28 23:03:06 +0200435 for u in [self.uuid.uuid4() for i in range(1000)]:
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000436 uuids[u] = 1
437 equal(len(uuids.keys()), 1000)
438
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000439 def test_uuid5(self):
440 equal = self.assertEqual
441
442 # Test some known version-5 UUIDs.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200443 for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444 '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200445 (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446 '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200447 (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000448 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
Antoine Pitroua106aec2017-09-28 23:03:06 +0200449 (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450 'cc957dd1-a972-5349-98cd-874190002798'),
451 ]:
Antoine Pitroua106aec2017-09-28 23:03:06 +0200452 equal(u.variant, self.uuid.RFC_4122)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000453 equal(u.version, 5)
Antoine Pitroua106aec2017-09-28 23:03:06 +0200454 equal(u, self.uuid.UUID(v))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 equal(str(u), v)
456
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300457 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000458 def testIssue8621(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200459 # On at least some versions of OSX self.uuid.uuid4 generates
Ronald Oussorenac764d32010-05-05 15:32:33 +0000460 # the same sequence of UUIDs in the parent and any
461 # children started using fork.
462 fds = os.pipe()
463 pid = os.fork()
464 if pid == 0:
465 os.close(fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200466 value = self.uuid.uuid4()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000467 os.write(fds[1], value.hex.encode('latin-1'))
Ronald Oussorenac764d32010-05-05 15:32:33 +0000468 os._exit(0)
469
470 else:
471 os.close(fds[1])
Richard Oudkerk0e547b62013-06-10 16:29:19 +0100472 self.addCleanup(os.close, fds[0])
Antoine Pitroua106aec2017-09-28 23:03:06 +0200473 parent_value = self.uuid.uuid4().hex
Ronald Oussorenac764d32010-05-05 15:32:33 +0000474 os.waitpid(pid, 0)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000475 child_value = os.read(fds[0], 100).decode('latin-1')
Ronald Oussorenac764d32010-05-05 15:32:33 +0000476
477 self.assertNotEqual(parent_value, child_value)
478
479
Antoine Pitroua106aec2017-09-28 23:03:06 +0200480class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
481 uuid = py_uuid
482
483@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
484class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
485 uuid = c_uuid
486
487
488class BaseTestInternals:
489 uuid = None
490
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200491 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
492 def test_find_mac(self):
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200493 data = '''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200494fake hwaddr
495cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
496eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
497'''
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200498
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200499 popen = unittest.mock.MagicMock()
500 popen.stdout = io.BytesIO(data.encode())
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200501
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200502 with unittest.mock.patch.object(shutil, 'which',
503 return_value='/sbin/ifconfig'):
504 with unittest.mock.patch.object(subprocess, 'Popen',
505 return_value=popen):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200506 mac = self.uuid._find_mac(
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200507 command='ifconfig',
508 args='',
509 hw_identifiers=[b'hwaddr'],
510 get_index=lambda x: x + 1,
511 )
512
513 self.assertEqual(mac, 0x1234567890ab)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200514
Barry Warsaw23df2d12017-11-28 17:26:04 -0500515 def check_node(self, node, requires=None):
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200516 if requires and node is None:
517 self.skipTest('requires ' + requires)
518 hex = '%012x' % node
519 if support.verbose >= 2:
520 print(hex, end=' ')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200521 self.assertTrue(0 < node < (1 << 48),
522 "%s is not an RFC 4122 node ID" % hex)
523
524 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
525 def test_ifconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200526 node = self.uuid._ifconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500527 self.check_node(node, 'ifconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200528
529 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200530 def test_ip_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200531 node = self.uuid._ip_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500532 self.check_node(node, 'ip')
Serhiy Storchaka0e32ea12014-12-15 12:06:22 +0200533
534 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200535 def test_arp_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200536 node = self.uuid._arp_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500537 self.check_node(node, 'arp')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200538
539 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
540 def test_lanscan_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200541 node = self.uuid._lanscan_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500542 self.check_node(node, 'lanscan')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200543
544 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
545 def test_netstat_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200546 node = self.uuid._netstat_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500547 self.check_node(node, 'netstat')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200548
549 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
550 def test_ipconfig_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200551 node = self.uuid._ipconfig_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500552 self.check_node(node, 'ipconfig')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200553
554 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
555 @unittest.skipUnless(importable('netbios'), 'requires netbios')
556 def test_netbios_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200557 node = self.uuid._netbios_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500558 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200559
560 def test_random_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200561 node = self.uuid._random_getnode()
Barry Warsaw23df2d12017-11-28 17:26:04 -0500562 # The multicast bit, i.e. the least significant bit of first octet,
563 # must be set for randomly generated MAC addresses. See RFC 4122,
564 # $4.1.6.
565 self.assertTrue(node & (1 << 40), '%012x' % node)
Victor Stinnerc9409f72017-11-28 00:30:21 +0100566 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200567
Serhiy Storchakae69fbb62017-12-04 11:51:55 +0200568 node2 = self.uuid._random_getnode()
569 self.assertNotEqual(node2, node, '%012x' % node)
570
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200571 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
Antoine Pitroua106aec2017-09-28 23:03:06 +0200572 def test_unix_getnode(self):
573 if not importable('_uuid') and not importable('ctypes'):
574 self.skipTest("neither _uuid extension nor ctypes available")
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200575 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
Antoine Pitroua106aec2017-09-28 23:03:06 +0200576 node = self.uuid._unix_getnode()
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200577 except TypeError:
578 self.skipTest('requires uuid_generate_time')
Victor Stinnerc9409f72017-11-28 00:30:21 +0100579 self.check_node(node, 'unix')
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200580
581 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
582 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
583 def test_windll_getnode(self):
Antoine Pitroua106aec2017-09-28 23:03:06 +0200584 node = self.uuid._windll_getnode()
Victor Stinnerc9409f72017-11-28 00:30:21 +0100585 self.check_node(node)
Serhiy Storchaka79b81732014-12-15 12:03:44 +0200586
587
Antoine Pitroua106aec2017-09-28 23:03:06 +0200588class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase):
589 uuid = py_uuid
590
591@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
592class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase):
593 uuid = c_uuid
594
595
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596if __name__ == '__main__':
Serhiy Storchaka7d15b542013-05-31 22:31:02 +0300597 unittest.main()