1 | import unittest
|
---|
2 | from test import test_support
|
---|
3 | import os
|
---|
4 | import uuid
|
---|
5 |
|
---|
6 | def importable(name):
|
---|
7 | try:
|
---|
8 | __import__(name)
|
---|
9 | return True
|
---|
10 | except:
|
---|
11 | return False
|
---|
12 |
|
---|
13 | class TestUUID(unittest.TestCase):
|
---|
14 | last_node = None
|
---|
15 | source2node = {}
|
---|
16 |
|
---|
17 | def test_UUID(self):
|
---|
18 | equal = self.assertEqual
|
---|
19 | ascending = []
|
---|
20 | for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
|
---|
21 | time, clock_seq, variant, version) in [
|
---|
22 | ('00000000-0000-0000-0000-000000000000',
|
---|
23 | '{00000000-0000-0000-0000-000000000000}',
|
---|
24 | '00000000000000000000000000000000',
|
---|
25 | '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
|
---|
26 | '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
|
---|
27 | (0, 0, 0, 0, 0, 0),
|
---|
28 | 0,
|
---|
29 | 'urn:uuid:00000000-0000-0000-0000-000000000000',
|
---|
30 | 0, 0, uuid.RESERVED_NCS, None),
|
---|
31 | ('00010203-0405-0607-0809-0a0b0c0d0e0f',
|
---|
32 | '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
|
---|
33 | '000102030405060708090a0b0c0d0e0f',
|
---|
34 | '\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
|
---|
35 | '\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
|
---|
36 | (0x00010203L, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0fL),
|
---|
37 | 0x000102030405060708090a0b0c0d0e0fL,
|
---|
38 | 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
|
---|
39 | 0x607040500010203L, 0x809, uuid.RESERVED_NCS, None),
|
---|
40 | ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
|
---|
41 | '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
|
---|
42 | '02d9e6d59467382e8f9b9300a64ac3cd',
|
---|
43 | '\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
|
---|
44 | '\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
|
---|
45 | (0x02d9e6d5L, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cdL),
|
---|
46 | 0x02d9e6d59467382e8f9b9300a64ac3cdL,
|
---|
47 | 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
|
---|
48 | 0x82e946702d9e6d5L, 0xf9b, uuid.RFC_4122, 3),
|
---|
49 | ('12345678-1234-5678-1234-567812345678',
|
---|
50 | '{12345678-1234-5678-1234-567812345678}',
|
---|
51 | '12345678123456781234567812345678',
|
---|
52 | '\x12\x34\x56\x78'*4,
|
---|
53 | '\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
|
---|
54 | (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
|
---|
55 | 0x12345678123456781234567812345678,
|
---|
56 | 'urn:uuid:12345678-1234-5678-1234-567812345678',
|
---|
57 | 0x678123412345678L, 0x1234, uuid.RESERVED_NCS, None),
|
---|
58 | ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
|
---|
59 | '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
|
---|
60 | '6ba7b8109dad11d180b400c04fd430c8',
|
---|
61 | '\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
62 | '\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
63 | (0x6ba7b810L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
|
---|
64 | 0x6ba7b8109dad11d180b400c04fd430c8L,
|
---|
65 | 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
|
---|
66 | 0x1d19dad6ba7b810L, 0xb4, uuid.RFC_4122, 1),
|
---|
67 | ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
|
---|
68 | '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
|
---|
69 | '6ba7b8119dad11d180b400c04fd430c8',
|
---|
70 | '\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
71 | '\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
72 | (0x6ba7b811L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
|
---|
73 | 0x6ba7b8119dad11d180b400c04fd430c8L,
|
---|
74 | 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
|
---|
75 | 0x1d19dad6ba7b811L, 0xb4, uuid.RFC_4122, 1),
|
---|
76 | ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
|
---|
77 | '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
|
---|
78 | '6ba7b8129dad11d180b400c04fd430c8',
|
---|
79 | '\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
80 | '\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
81 | (0x6ba7b812L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
|
---|
82 | 0x6ba7b8129dad11d180b400c04fd430c8L,
|
---|
83 | 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
|
---|
84 | 0x1d19dad6ba7b812L, 0xb4, uuid.RFC_4122, 1),
|
---|
85 | ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
|
---|
86 | '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
|
---|
87 | '6ba7b8149dad11d180b400c04fd430c8',
|
---|
88 | '\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
89 | '\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
|
---|
90 | (0x6ba7b814L, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8L),
|
---|
91 | 0x6ba7b8149dad11d180b400c04fd430c8L,
|
---|
92 | 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
|
---|
93 | 0x1d19dad6ba7b814L, 0xb4, uuid.RFC_4122, 1),
|
---|
94 | ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
|
---|
95 | '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
|
---|
96 | '7d4448409dc011d1b2455ffdce74fad2',
|
---|
97 | '\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
|
---|
98 | '\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
|
---|
99 | (0x7d444840L, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2L),
|
---|
100 | 0x7d4448409dc011d1b2455ffdce74fad2L,
|
---|
101 | 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
|
---|
102 | 0x1d19dc07d444840L, 0x3245, uuid.RFC_4122, 1),
|
---|
103 | ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
|
---|
104 | '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
|
---|
105 | 'e902893a9d223c7ea7b8d6e313b71d9f',
|
---|
106 | '\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
|
---|
107 | '\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
|
---|
108 | (0xe902893aL, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9fL),
|
---|
109 | 0xe902893a9d223c7ea7b8d6e313b71d9fL,
|
---|
110 | 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
|
---|
111 | 0xc7e9d22e902893aL, 0x27b8, uuid.RFC_4122, 3),
|
---|
112 | ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
|
---|
113 | '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
|
---|
114 | 'eb4240266f544ef8a4d0bb658a1fc6cf',
|
---|
115 | '\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
|
---|
116 | '\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
|
---|
117 | (0xeb424026L, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cfL),
|
---|
118 | 0xeb4240266f544ef8a4d0bb658a1fc6cfL,
|
---|
119 | 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
|
---|
120 | 0xef86f54eb424026L, 0x24d0, uuid.RFC_4122, 4),
|
---|
121 | ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
|
---|
122 | '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
|
---|
123 | 'f81d4fae7dec11d0a76500a0c91e6bf6',
|
---|
124 | '\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
|
---|
125 | '\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
|
---|
126 | (0xf81d4faeL, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6L),
|
---|
127 | 0xf81d4fae7dec11d0a76500a0c91e6bf6L,
|
---|
128 | 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
|
---|
129 | 0x1d07decf81d4faeL, 0x2765, uuid.RFC_4122, 1),
|
---|
130 | ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
|
---|
131 | '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
|
---|
132 | 'fffefdfcfffefffefffefffefdfcfbfa',
|
---|
133 | '\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
|
---|
134 | '\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
|
---|
135 | (0xfffefdfcL, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfaL),
|
---|
136 | 0xfffefdfcfffefffefffefffefdfcfbfaL,
|
---|
137 | 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
|
---|
138 | 0xffefffefffefdfcL, 0x3ffe, uuid.RESERVED_FUTURE, None),
|
---|
139 | ('ffffffff-ffff-ffff-ffff-ffffffffffff',
|
---|
140 | '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
|
---|
141 | 'ffffffffffffffffffffffffffffffff',
|
---|
142 | '\xff'*16,
|
---|
143 | '\xff'*16,
|
---|
144 | (0xffffffffL, 0xffffL, 0xffffL, 0xff, 0xff, 0xffffffffffffL),
|
---|
145 | 0xffffffffffffffffffffffffffffffffL,
|
---|
146 | 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
|
---|
147 | 0xfffffffffffffffL, 0x3fff, uuid.RESERVED_FUTURE, None),
|
---|
148 | ]:
|
---|
149 | equivalents = []
|
---|
150 | # Construct each UUID in several different ways.
|
---|
151 | for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
|
---|
152 | uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
|
---|
153 | uuid.UUID(fields=fields), uuid.UUID(int=integer),
|
---|
154 | uuid.UUID(urn)]:
|
---|
155 | # Test all conversions and properties of the UUID object.
|
---|
156 | equal(str(u), string)
|
---|
157 | equal(int(u), integer)
|
---|
158 | equal(u.bytes, bytes)
|
---|
159 | equal(u.bytes_le, bytes_le)
|
---|
160 | equal(u.fields, fields)
|
---|
161 | equal(u.time_low, fields[0])
|
---|
162 | equal(u.time_mid, fields[1])
|
---|
163 | equal(u.time_hi_version, fields[2])
|
---|
164 | equal(u.clock_seq_hi_variant, fields[3])
|
---|
165 | equal(u.clock_seq_low, fields[4])
|
---|
166 | equal(u.node, fields[5])
|
---|
167 | equal(u.hex, hex)
|
---|
168 | equal(u.int, integer)
|
---|
169 | equal(u.urn, urn)
|
---|
170 | equal(u.time, time)
|
---|
171 | equal(u.clock_seq, clock_seq)
|
---|
172 | equal(u.variant, variant)
|
---|
173 | equal(u.version, version)
|
---|
174 | equivalents.append(u)
|
---|
175 |
|
---|
176 | # Different construction methods should give the same UUID.
|
---|
177 | for u in equivalents:
|
---|
178 | for v in equivalents:
|
---|
179 | equal(u, v)
|
---|
180 | ascending.append(u)
|
---|
181 |
|
---|
182 | # Test comparison of UUIDs.
|
---|
183 | for i in range(len(ascending)):
|
---|
184 | for j in range(len(ascending)):
|
---|
185 | equal(cmp(i, j), cmp(ascending[i], ascending[j]))
|
---|
186 |
|
---|
187 | # Test sorting of UUIDs (above list is in ascending order).
|
---|
188 | resorted = ascending[:]
|
---|
189 | resorted.reverse()
|
---|
190 | resorted.sort()
|
---|
191 | equal(ascending, resorted)
|
---|
192 |
|
---|
193 | def test_exceptions(self):
|
---|
194 | badvalue = lambda f: self.assertRaises(ValueError, f)
|
---|
195 | badtype = lambda f: self.assertRaises(TypeError, f)
|
---|
196 |
|
---|
197 | # Badly formed hex strings.
|
---|
198 | badvalue(lambda: uuid.UUID(''))
|
---|
199 | badvalue(lambda: uuid.UUID('abc'))
|
---|
200 | badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
|
---|
201 | badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
|
---|
202 | badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
|
---|
203 |
|
---|
204 | # Badly formed bytes.
|
---|
205 | badvalue(lambda: uuid.UUID(bytes='abc'))
|
---|
206 | badvalue(lambda: uuid.UUID(bytes='\0'*15))
|
---|
207 | badvalue(lambda: uuid.UUID(bytes='\0'*17))
|
---|
208 |
|
---|
209 | # Badly formed bytes_le.
|
---|
210 | badvalue(lambda: uuid.UUID(bytes_le='abc'))
|
---|
211 | badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
|
---|
212 | badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
|
---|
213 |
|
---|
214 | # Badly formed fields.
|
---|
215 | badvalue(lambda: uuid.UUID(fields=(1,)))
|
---|
216 | badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
|
---|
217 | badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
|
---|
218 |
|
---|
219 | # Field values out of range.
|
---|
220 | badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
|
---|
221 | badvalue(lambda: uuid.UUID(fields=(0x100000000L, 0, 0, 0, 0, 0)))
|
---|
222 | badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
|
---|
223 | badvalue(lambda: uuid.UUID(fields=(0, 0x10000L, 0, 0, 0, 0)))
|
---|
224 | badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
|
---|
225 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000L, 0, 0, 0)))
|
---|
226 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
|
---|
227 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100L, 0, 0)))
|
---|
228 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
|
---|
229 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100L, 0)))
|
---|
230 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
|
---|
231 | badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000L)))
|
---|
232 |
|
---|
233 | # Version number out of range.
|
---|
234 | badvalue(lambda: uuid.UUID('00'*16, version=0))
|
---|
235 | badvalue(lambda: uuid.UUID('00'*16, version=6))
|
---|
236 |
|
---|
237 | # Integer value out of range.
|
---|
238 | badvalue(lambda: uuid.UUID(int=-1))
|
---|
239 | badvalue(lambda: uuid.UUID(int=1<<128L))
|
---|
240 |
|
---|
241 | # Must supply exactly one of hex, bytes, fields, int.
|
---|
242 | h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
|
---|
243 | uuid.UUID(h)
|
---|
244 | uuid.UUID(hex=h)
|
---|
245 | uuid.UUID(bytes=b)
|
---|
246 | uuid.UUID(bytes_le=b)
|
---|
247 | uuid.UUID(fields=f)
|
---|
248 | uuid.UUID(int=i)
|
---|
249 |
|
---|
250 | # Wrong number of arguments (positional).
|
---|
251 | badtype(lambda: uuid.UUID())
|
---|
252 | badtype(lambda: uuid.UUID(h, b))
|
---|
253 | badtype(lambda: uuid.UUID(h, b, b))
|
---|
254 | badtype(lambda: uuid.UUID(h, b, b, f))
|
---|
255 | badtype(lambda: uuid.UUID(h, b, b, f, i))
|
---|
256 |
|
---|
257 | # Duplicate arguments.
|
---|
258 | for hh in [[], [('hex', h)]]:
|
---|
259 | for bb in [[], [('bytes', b)]]:
|
---|
260 | for bble in [[], [('bytes_le', b)]]:
|
---|
261 | for ii in [[], [('int', i)]]:
|
---|
262 | for ff in [[], [('fields', f)]]:
|
---|
263 | args = dict(hh + bb + bble + ii + ff)
|
---|
264 | if len(args) != 0:
|
---|
265 | badtype(lambda: uuid.UUID(h, **args))
|
---|
266 | if len(args) != 1:
|
---|
267 | badtype(lambda: uuid.UUID(**args))
|
---|
268 |
|
---|
269 | # Immutability.
|
---|
270 | u = uuid.UUID(h)
|
---|
271 | badtype(lambda: setattr(u, 'hex', h))
|
---|
272 | badtype(lambda: setattr(u, 'bytes', b))
|
---|
273 | badtype(lambda: setattr(u, 'bytes_le', b))
|
---|
274 | badtype(lambda: setattr(u, 'fields', f))
|
---|
275 | badtype(lambda: setattr(u, 'int', i))
|
---|
276 | badtype(lambda: setattr(u, 'time_low', 0))
|
---|
277 | badtype(lambda: setattr(u, 'time_mid', 0))
|
---|
278 | badtype(lambda: setattr(u, 'time_hi_version', 0))
|
---|
279 | badtype(lambda: setattr(u, 'time_hi_version', 0))
|
---|
280 | badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
|
---|
281 | badtype(lambda: setattr(u, 'clock_seq_low', 0))
|
---|
282 | badtype(lambda: setattr(u, 'node', 0))
|
---|
283 |
|
---|
284 | def check_node(self, node, source):
|
---|
285 | message = "%012x is not an RFC 4122 node ID" % node
|
---|
286 | self.assertTrue(0 < node, message)
|
---|
287 | self.assertTrue(node < (1L << 48), message)
|
---|
288 |
|
---|
289 | TestUUID.source2node[source] = node
|
---|
290 | if TestUUID.last_node:
|
---|
291 | if TestUUID.last_node != node:
|
---|
292 | msg = "different sources disagree on node:\n"
|
---|
293 | for s, n in TestUUID.source2node.iteritems():
|
---|
294 | msg += " from source %r, node was %012x\n" % (s, n)
|
---|
295 | # There's actually no reason to expect the MAC addresses
|
---|
296 | # to agree across various methods -- e.g., a box may have
|
---|
297 | # multiple network interfaces, and different ways of getting
|
---|
298 | # a MAC address may favor different HW.
|
---|
299 | ##self.fail(msg)
|
---|
300 | else:
|
---|
301 | TestUUID.last_node = node
|
---|
302 |
|
---|
303 | @unittest.skipUnless(os.name == 'posix', 'requires Posix')
|
---|
304 | def test_ifconfig_getnode(self):
|
---|
305 | node = uuid._ifconfig_getnode()
|
---|
306 | if node is not None:
|
---|
307 | self.check_node(node, 'ifconfig')
|
---|
308 |
|
---|
309 | @unittest.skipUnless(os.name == 'nt', 'requires Windows')
|
---|
310 | def test_ipconfig_getnode(self):
|
---|
311 | node = uuid._ipconfig_getnode()
|
---|
312 | if node is not None:
|
---|
313 | self.check_node(node, 'ipconfig')
|
---|
314 |
|
---|
315 | @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
|
---|
316 | @unittest.skipUnless(importable('netbios'), 'requires netbios')
|
---|
317 | def test_netbios_getnode(self):
|
---|
318 | self.check_node(uuid._netbios_getnode(), 'netbios')
|
---|
319 |
|
---|
320 | def test_random_getnode(self):
|
---|
321 | node = uuid._random_getnode()
|
---|
322 | # Least significant bit of first octet must be set.
|
---|
323 | self.assertTrue(node & 0x010000000000)
|
---|
324 | self.assertTrue(node < (1L << 48))
|
---|
325 |
|
---|
326 | @unittest.skipUnless(os.name == 'posix', 'requires Posix')
|
---|
327 | @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
|
---|
328 | def test_unixdll_getnode(self):
|
---|
329 | try: # Issues 1481, 3581: _uuid_generate_time() might be None.
|
---|
330 | self.check_node(uuid._unixdll_getnode(), 'unixdll')
|
---|
331 | except TypeError:
|
---|
332 | pass
|
---|
333 |
|
---|
334 | @unittest.skipUnless(os.name == 'nt', 'requires Windows')
|
---|
335 | @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
|
---|
336 | def test_windll_getnode(self):
|
---|
337 | self.check_node(uuid._windll_getnode(), 'windll')
|
---|
338 |
|
---|
339 | def test_getnode(self):
|
---|
340 | node1 = uuid.getnode()
|
---|
341 | self.check_node(node1, "getnode1")
|
---|
342 |
|
---|
343 | # Test it again to ensure consistency.
|
---|
344 | node2 = uuid.getnode()
|
---|
345 | self.check_node(node2, "getnode2")
|
---|
346 |
|
---|
347 | self.assertEqual(node1, node2)
|
---|
348 |
|
---|
349 | @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
|
---|
350 | def test_uuid1(self):
|
---|
351 | equal = self.assertEqual
|
---|
352 |
|
---|
353 | # Make sure uuid1() generates UUIDs that are actually version 1.
|
---|
354 | for u in [uuid.uuid1() for i in range(10)]:
|
---|
355 | equal(u.variant, uuid.RFC_4122)
|
---|
356 | equal(u.version, 1)
|
---|
357 |
|
---|
358 | # Make sure the generated UUIDs are actually unique.
|
---|
359 | uuids = {}
|
---|
360 | for u in [uuid.uuid1() for i in range(1000)]:
|
---|
361 | uuids[u] = 1
|
---|
362 | equal(len(uuids.keys()), 1000)
|
---|
363 |
|
---|
364 | # Make sure the supplied node ID appears in the UUID.
|
---|
365 | u = uuid.uuid1(0)
|
---|
366 | equal(u.node, 0)
|
---|
367 | u = uuid.uuid1(0x123456789abc)
|
---|
368 | equal(u.node, 0x123456789abc)
|
---|
369 | u = uuid.uuid1(0xffffffffffff)
|
---|
370 | equal(u.node, 0xffffffffffff)
|
---|
371 |
|
---|
372 | # Make sure the supplied clock sequence appears in the UUID.
|
---|
373 | u = uuid.uuid1(0x123456789abc, 0)
|
---|
374 | equal(u.node, 0x123456789abc)
|
---|
375 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
|
---|
376 | u = uuid.uuid1(0x123456789abc, 0x1234)
|
---|
377 | equal(u.node, 0x123456789abc)
|
---|
378 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
|
---|
379 | u.clock_seq_low, 0x1234)
|
---|
380 | u = uuid.uuid1(0x123456789abc, 0x3fff)
|
---|
381 | equal(u.node, 0x123456789abc)
|
---|
382 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
|
---|
383 | u.clock_seq_low, 0x3fff)
|
---|
384 |
|
---|
385 | def test_uuid3(self):
|
---|
386 | equal = self.assertEqual
|
---|
387 |
|
---|
388 | # Test some known version-3 UUIDs.
|
---|
389 | for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
|
---|
390 | '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
|
---|
391 | (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
|
---|
392 | '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
|
---|
393 | (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
|
---|
394 | 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
|
---|
395 | (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
|
---|
396 | '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
|
---|
397 | ]:
|
---|
398 | equal(u.variant, uuid.RFC_4122)
|
---|
399 | equal(u.version, 3)
|
---|
400 | equal(u, uuid.UUID(v))
|
---|
401 | equal(str(u), v)
|
---|
402 |
|
---|
403 | @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
|
---|
404 | def test_uuid4(self):
|
---|
405 | equal = self.assertEqual
|
---|
406 |
|
---|
407 | # Make sure uuid4() generates UUIDs that are actually version 4.
|
---|
408 | for u in [uuid.uuid4() for i in range(10)]:
|
---|
409 | equal(u.variant, uuid.RFC_4122)
|
---|
410 | equal(u.version, 4)
|
---|
411 |
|
---|
412 | # Make sure the generated UUIDs are actually unique.
|
---|
413 | uuids = {}
|
---|
414 | for u in [uuid.uuid4() for i in range(1000)]:
|
---|
415 | uuids[u] = 1
|
---|
416 | equal(len(uuids.keys()), 1000)
|
---|
417 |
|
---|
418 | def test_uuid5(self):
|
---|
419 | equal = self.assertEqual
|
---|
420 |
|
---|
421 | # Test some known version-5 UUIDs.
|
---|
422 | for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
|
---|
423 | '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
|
---|
424 | (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
|
---|
425 | '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
|
---|
426 | (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
|
---|
427 | '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
|
---|
428 | (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
|
---|
429 | 'cc957dd1-a972-5349-98cd-874190002798'),
|
---|
430 | ]:
|
---|
431 | equal(u.variant, uuid.RFC_4122)
|
---|
432 | equal(u.version, 5)
|
---|
433 | equal(u, uuid.UUID(v))
|
---|
434 | equal(str(u), v)
|
---|
435 |
|
---|
436 | @unittest.skipUnless(os.name == 'posix', 'requires Posix')
|
---|
437 | def testIssue8621(self):
|
---|
438 | # On at least some versions of OSX uuid.uuid4 generates
|
---|
439 | # the same sequence of UUIDs in the parent and any
|
---|
440 | # children started using fork.
|
---|
441 | fds = os.pipe()
|
---|
442 | pid = os.fork()
|
---|
443 | if pid == 0:
|
---|
444 | os.close(fds[0])
|
---|
445 | value = uuid.uuid4()
|
---|
446 | os.write(fds[1], value.hex)
|
---|
447 | os._exit(0)
|
---|
448 |
|
---|
449 | else:
|
---|
450 | os.close(fds[1])
|
---|
451 | self.addCleanup(os.close, fds[0])
|
---|
452 | parent_value = uuid.uuid4().hex
|
---|
453 | os.waitpid(pid, 0)
|
---|
454 | child_value = os.read(fds[0], 100)
|
---|
455 |
|
---|
456 | self.assertNotEqual(parent_value, child_value)
|
---|
457 |
|
---|
458 |
|
---|
459 |
|
---|
460 |
|
---|
461 |
|
---|
462 | def test_main():
|
---|
463 | test_support.run_unittest(TestUUID)
|
---|
464 |
|
---|
465 | if __name__ == '__main__':
|
---|
466 | test_main()
|
---|