ยปCore Development>Code coverage>Lib/test/test_uuid.py

Python code coverage for Lib/test/test_uuid.py

#countcontent
1n/aimport unittest.mock
2n/afrom test import support
3n/aimport builtins
4n/aimport io
5n/aimport os
6n/aimport shutil
7n/aimport subprocess
8n/aimport uuid
9n/a
10n/adef importable(name):
11n/a try:
12n/a __import__(name)
13n/a return True
14n/a except:
15n/a return False
16n/a
17n/aclass TestUUID(unittest.TestCase):
18n/a def test_UUID(self):
19n/a equal = self.assertEqual
20n/a ascending = []
21n/a for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
22n/a time, clock_seq, variant, version) in [
23n/a ('00000000-0000-0000-0000-000000000000',
24n/a '{00000000-0000-0000-0000-000000000000}',
25n/a '00000000000000000000000000000000',
26n/a b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
27n/a b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
28n/a (0, 0, 0, 0, 0, 0),
29n/a 0,
30n/a 'urn:uuid:00000000-0000-0000-0000-000000000000',
31n/a 0, 0, uuid.RESERVED_NCS, None),
32n/a ('00010203-0405-0607-0809-0a0b0c0d0e0f',
33n/a '{00010203-0405-0607-0809-0a0b0c0d0e0f}',
34n/a '000102030405060708090a0b0c0d0e0f',
35n/a b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
36n/a b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
37n/a (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
38n/a 0x000102030405060708090a0b0c0d0e0f,
39n/a 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
40n/a 0x607040500010203, 0x809, uuid.RESERVED_NCS, None),
41n/a ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
42n/a '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
43n/a '02d9e6d59467382e8f9b9300a64ac3cd',
44n/a b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
45n/a b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
46n/a (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
47n/a 0x02d9e6d59467382e8f9b9300a64ac3cd,
48n/a 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
49n/a 0x82e946702d9e6d5, 0xf9b, uuid.RFC_4122, 3),
50n/a ('12345678-1234-5678-1234-567812345678',
51n/a '{12345678-1234-5678-1234-567812345678}',
52n/a '12345678123456781234567812345678',
53n/a b'\x12\x34\x56\x78'*4,
54n/a b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
55n/a (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
56n/a 0x12345678123456781234567812345678,
57n/a 'urn:uuid:12345678-1234-5678-1234-567812345678',
58n/a 0x678123412345678, 0x1234, uuid.RESERVED_NCS, None),
59n/a ('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
60n/a '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
61n/a '6ba7b8109dad11d180b400c04fd430c8',
62n/a b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
63n/a b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
64n/a (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
65n/a 0x6ba7b8109dad11d180b400c04fd430c8,
66n/a 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
67n/a 0x1d19dad6ba7b810, 0xb4, uuid.RFC_4122, 1),
68n/a ('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
69n/a '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
70n/a '6ba7b8119dad11d180b400c04fd430c8',
71n/a b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
72n/a b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
73n/a (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
74n/a 0x6ba7b8119dad11d180b400c04fd430c8,
75n/a 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
76n/a 0x1d19dad6ba7b811, 0xb4, uuid.RFC_4122, 1),
77n/a ('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
78n/a '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
79n/a '6ba7b8129dad11d180b400c04fd430c8',
80n/a b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
81n/a b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
82n/a (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
83n/a 0x6ba7b8129dad11d180b400c04fd430c8,
84n/a 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
85n/a 0x1d19dad6ba7b812, 0xb4, uuid.RFC_4122, 1),
86n/a ('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
87n/a '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
88n/a '6ba7b8149dad11d180b400c04fd430c8',
89n/a b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
90n/a b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
91n/a (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
92n/a 0x6ba7b8149dad11d180b400c04fd430c8,
93n/a 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
94n/a 0x1d19dad6ba7b814, 0xb4, uuid.RFC_4122, 1),
95n/a ('7d444840-9dc0-11d1-b245-5ffdce74fad2',
96n/a '{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
97n/a '7d4448409dc011d1b2455ffdce74fad2',
98n/a b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
99n/a b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
100n/a (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
101n/a 0x7d4448409dc011d1b2455ffdce74fad2,
102n/a 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
103n/a 0x1d19dc07d444840, 0x3245, uuid.RFC_4122, 1),
104n/a ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
105n/a '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
106n/a 'e902893a9d223c7ea7b8d6e313b71d9f',
107n/a b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
108n/a b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
109n/a (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
110n/a 0xe902893a9d223c7ea7b8d6e313b71d9f,
111n/a 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
112n/a 0xc7e9d22e902893a, 0x27b8, uuid.RFC_4122, 3),
113n/a ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
114n/a '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
115n/a 'eb4240266f544ef8a4d0bb658a1fc6cf',
116n/a b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
117n/a b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
118n/a (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
119n/a 0xeb4240266f544ef8a4d0bb658a1fc6cf,
120n/a 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
121n/a 0xef86f54eb424026, 0x24d0, uuid.RFC_4122, 4),
122n/a ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
123n/a '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
124n/a 'f81d4fae7dec11d0a76500a0c91e6bf6',
125n/a b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
126n/a b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
127n/a (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
128n/a 0xf81d4fae7dec11d0a76500a0c91e6bf6,
129n/a 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
130n/a 0x1d07decf81d4fae, 0x2765, uuid.RFC_4122, 1),
131n/a ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
132n/a '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
133n/a 'fffefdfcfffefffefffefffefdfcfbfa',
134n/a b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
135n/a b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
136n/a (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
137n/a 0xfffefdfcfffefffefffefffefdfcfbfa,
138n/a 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
139n/a 0xffefffefffefdfc, 0x3ffe, uuid.RESERVED_FUTURE, None),
140n/a ('ffffffff-ffff-ffff-ffff-ffffffffffff',
141n/a '{ffffffff-ffff-ffff-ffff-ffffffffffff}',
142n/a 'ffffffffffffffffffffffffffffffff',
143n/a b'\xff'*16,
144n/a b'\xff'*16,
145n/a (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
146n/a 0xffffffffffffffffffffffffffffffff,
147n/a 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
148n/a 0xfffffffffffffff, 0x3fff, uuid.RESERVED_FUTURE, None),
149n/a ]:
150n/a equivalents = []
151n/a # Construct each UUID in several different ways.
152n/a for u in [uuid.UUID(string), uuid.UUID(curly), uuid.UUID(hex),
153n/a uuid.UUID(bytes=bytes), uuid.UUID(bytes_le=bytes_le),
154n/a uuid.UUID(fields=fields), uuid.UUID(int=integer),
155n/a uuid.UUID(urn)]:
156n/a # Test all conversions and properties of the UUID object.
157n/a equal(str(u), string)
158n/a equal(int(u), integer)
159n/a equal(u.bytes, bytes)
160n/a equal(u.bytes_le, bytes_le)
161n/a equal(u.fields, fields)
162n/a equal(u.time_low, fields[0])
163n/a equal(u.time_mid, fields[1])
164n/a equal(u.time_hi_version, fields[2])
165n/a equal(u.clock_seq_hi_variant, fields[3])
166n/a equal(u.clock_seq_low, fields[4])
167n/a equal(u.node, fields[5])
168n/a equal(u.hex, hex)
169n/a equal(u.int, integer)
170n/a equal(u.urn, urn)
171n/a equal(u.time, time)
172n/a equal(u.clock_seq, clock_seq)
173n/a equal(u.variant, variant)
174n/a equal(u.version, version)
175n/a equivalents.append(u)
176n/a
177n/a # Different construction methods should give the same UUID.
178n/a for u in equivalents:
179n/a for v in equivalents:
180n/a equal(u, v)
181n/a
182n/a # Bug 7380: "bytes" and "bytes_le" should give the same type.
183n/a equal(type(u.bytes), builtins.bytes)
184n/a equal(type(u.bytes_le), builtins.bytes)
185n/a
186n/a ascending.append(u)
187n/a
188n/a # Test comparison of UUIDs.
189n/a for i in range(len(ascending)):
190n/a for j in range(len(ascending)):
191n/a equal(i < j, ascending[i] < ascending[j])
192n/a equal(i <= j, ascending[i] <= ascending[j])
193n/a equal(i == j, ascending[i] == ascending[j])
194n/a equal(i > j, ascending[i] > ascending[j])
195n/a equal(i >= j, ascending[i] >= ascending[j])
196n/a equal(i != j, ascending[i] != ascending[j])
197n/a
198n/a # Test sorting of UUIDs (above list is in ascending order).
199n/a resorted = ascending[:]
200n/a resorted.reverse()
201n/a resorted.sort()
202n/a equal(ascending, resorted)
203n/a
204n/a def test_exceptions(self):
205n/a badvalue = lambda f: self.assertRaises(ValueError, f)
206n/a badtype = lambda f: self.assertRaises(TypeError, f)
207n/a
208n/a # Badly formed hex strings.
209n/a badvalue(lambda: uuid.UUID(''))
210n/a badvalue(lambda: uuid.UUID('abc'))
211n/a badvalue(lambda: uuid.UUID('1234567812345678123456781234567'))
212n/a badvalue(lambda: uuid.UUID('123456781234567812345678123456789'))
213n/a badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
214n/a
215n/a # Badly formed bytes.
216n/a badvalue(lambda: uuid.UUID(bytes='abc'))
217n/a badvalue(lambda: uuid.UUID(bytes='\0'*15))
218n/a badvalue(lambda: uuid.UUID(bytes='\0'*17))
219n/a
220n/a # Badly formed bytes_le.
221n/a badvalue(lambda: uuid.UUID(bytes_le='abc'))
222n/a badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
223n/a badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
224n/a
225n/a # Badly formed fields.
226n/a badvalue(lambda: uuid.UUID(fields=(1,)))
227n/a badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5)))
228n/a badvalue(lambda: uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7)))
229n/a
230n/a # Field values out of range.
231n/a badvalue(lambda: uuid.UUID(fields=(-1, 0, 0, 0, 0, 0)))
232n/a badvalue(lambda: uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0)))
233n/a badvalue(lambda: uuid.UUID(fields=(0, -1, 0, 0, 0, 0)))
234n/a badvalue(lambda: uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0)))
235n/a badvalue(lambda: uuid.UUID(fields=(0, 0, -1, 0, 0, 0)))
236n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0)))
237n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, -1, 0, 0)))
238n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0)))
239n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, -1, 0)))
240n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0)))
241n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, -1)))
242n/a badvalue(lambda: uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000)))
243n/a
244n/a # Version number out of range.
245n/a badvalue(lambda: uuid.UUID('00'*16, version=0))
246n/a badvalue(lambda: uuid.UUID('00'*16, version=6))
247n/a
248n/a # Integer value out of range.
249n/a badvalue(lambda: uuid.UUID(int=-1))
250n/a badvalue(lambda: uuid.UUID(int=1<<128))
251n/a
252n/a # Must supply exactly one of hex, bytes, fields, int.
253n/a h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
254n/a uuid.UUID(h)
255n/a uuid.UUID(hex=h)
256n/a uuid.UUID(bytes=b)
257n/a uuid.UUID(bytes_le=b)
258n/a uuid.UUID(fields=f)
259n/a uuid.UUID(int=i)
260n/a
261n/a # Wrong number of arguments (positional).
262n/a badtype(lambda: uuid.UUID())
263n/a badtype(lambda: uuid.UUID(h, b))
264n/a badtype(lambda: uuid.UUID(h, b, b))
265n/a badtype(lambda: uuid.UUID(h, b, b, f))
266n/a badtype(lambda: uuid.UUID(h, b, b, f, i))
267n/a
268n/a # Duplicate arguments.
269n/a for hh in [[], [('hex', h)]]:
270n/a for bb in [[], [('bytes', b)]]:
271n/a for bble in [[], [('bytes_le', b)]]:
272n/a for ii in [[], [('int', i)]]:
273n/a for ff in [[], [('fields', f)]]:
274n/a args = dict(hh + bb + bble + ii + ff)
275n/a if len(args) != 0:
276n/a badtype(lambda: uuid.UUID(h, **args))
277n/a if len(args) != 1:
278n/a badtype(lambda: uuid.UUID(**args))
279n/a
280n/a # Immutability.
281n/a u = uuid.UUID(h)
282n/a badtype(lambda: setattr(u, 'hex', h))
283n/a badtype(lambda: setattr(u, 'bytes', b))
284n/a badtype(lambda: setattr(u, 'bytes_le', b))
285n/a badtype(lambda: setattr(u, 'fields', f))
286n/a badtype(lambda: setattr(u, 'int', i))
287n/a badtype(lambda: setattr(u, 'time_low', 0))
288n/a badtype(lambda: setattr(u, 'time_mid', 0))
289n/a badtype(lambda: setattr(u, 'time_hi_version', 0))
290n/a badtype(lambda: setattr(u, 'time_hi_version', 0))
291n/a badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0))
292n/a badtype(lambda: setattr(u, 'clock_seq_low', 0))
293n/a badtype(lambda: setattr(u, 'node', 0))
294n/a
295n/a # Comparison with a non-UUID object
296n/a badtype(lambda: u < object())
297n/a badtype(lambda: u > object())
298n/a
299n/a def test_getnode(self):
300n/a node1 = uuid.getnode()
301n/a self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
302n/a
303n/a # Test it again to ensure consistency.
304n/a node2 = uuid.getnode()
305n/a self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
306n/a
307n/a @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
308n/a def test_uuid1(self):
309n/a equal = self.assertEqual
310n/a
311n/a # Make sure uuid1() generates UUIDs that are actually version 1.
312n/a for u in [uuid.uuid1() for i in range(10)]:
313n/a equal(u.variant, uuid.RFC_4122)
314n/a equal(u.version, 1)
315n/a
316n/a # Make sure the generated UUIDs are actually unique.
317n/a uuids = {}
318n/a for u in [uuid.uuid1() for i in range(1000)]:
319n/a uuids[u] = 1
320n/a equal(len(uuids.keys()), 1000)
321n/a
322n/a # Make sure the supplied node ID appears in the UUID.
323n/a u = uuid.uuid1(0)
324n/a equal(u.node, 0)
325n/a u = uuid.uuid1(0x123456789abc)
326n/a equal(u.node, 0x123456789abc)
327n/a u = uuid.uuid1(0xffffffffffff)
328n/a equal(u.node, 0xffffffffffff)
329n/a
330n/a # Make sure the supplied clock sequence appears in the UUID.
331n/a u = uuid.uuid1(0x123456789abc, 0)
332n/a equal(u.node, 0x123456789abc)
333n/a equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0)
334n/a u = uuid.uuid1(0x123456789abc, 0x1234)
335n/a equal(u.node, 0x123456789abc)
336n/a equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
337n/a u.clock_seq_low, 0x1234)
338n/a u = uuid.uuid1(0x123456789abc, 0x3fff)
339n/a equal(u.node, 0x123456789abc)
340n/a equal(((u.clock_seq_hi_variant & 0x3f) << 8) |
341n/a u.clock_seq_low, 0x3fff)
342n/a
343n/a def test_uuid3(self):
344n/a equal = self.assertEqual
345n/a
346n/a # Test some known version-3 UUIDs.
347n/a for u, v in [(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'),
348n/a '6fa459ea-ee8a-3ca4-894e-db77e160355e'),
349n/a (uuid.uuid3(uuid.NAMESPACE_URL, 'http://python.org/'),
350n/a '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'),
351n/a (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
352n/a 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
353n/a (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
354n/a '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
355n/a ]:
356n/a equal(u.variant, uuid.RFC_4122)
357n/a equal(u.version, 3)
358n/a equal(u, uuid.UUID(v))
359n/a equal(str(u), v)
360n/a
361n/a def test_uuid4(self):
362n/a equal = self.assertEqual
363n/a
364n/a # Make sure uuid4() generates UUIDs that are actually version 4.
365n/a for u in [uuid.uuid4() for i in range(10)]:
366n/a equal(u.variant, uuid.RFC_4122)
367n/a equal(u.version, 4)
368n/a
369n/a # Make sure the generated UUIDs are actually unique.
370n/a uuids = {}
371n/a for u in [uuid.uuid4() for i in range(1000)]:
372n/a uuids[u] = 1
373n/a equal(len(uuids.keys()), 1000)
374n/a
375n/a def test_uuid5(self):
376n/a equal = self.assertEqual
377n/a
378n/a # Test some known version-5 UUIDs.
379n/a for u, v in [(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'),
380n/a '886313e1-3b8a-5372-9b90-0c9aee199e5d'),
381n/a (uuid.uuid5(uuid.NAMESPACE_URL, 'http://python.org/'),
382n/a '4c565f0d-3f5a-5890-b41b-20cf47701c5e'),
383n/a (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
384n/a '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
385n/a (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
386n/a 'cc957dd1-a972-5349-98cd-874190002798'),
387n/a ]:
388n/a equal(u.variant, uuid.RFC_4122)
389n/a equal(u.version, 5)
390n/a equal(u, uuid.UUID(v))
391n/a equal(str(u), v)
392n/a
393n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
394n/a def testIssue8621(self):
395n/a # On at least some versions of OSX uuid.uuid4 generates
396n/a # the same sequence of UUIDs in the parent and any
397n/a # children started using fork.
398n/a fds = os.pipe()
399n/a pid = os.fork()
400n/a if pid == 0:
401n/a os.close(fds[0])
402n/a value = uuid.uuid4()
403n/a os.write(fds[1], value.hex.encode('latin-1'))
404n/a os._exit(0)
405n/a
406n/a else:
407n/a os.close(fds[1])
408n/a self.addCleanup(os.close, fds[0])
409n/a parent_value = uuid.uuid4().hex
410n/a os.waitpid(pid, 0)
411n/a child_value = os.read(fds[0], 100).decode('latin-1')
412n/a
413n/a self.assertNotEqual(parent_value, child_value)
414n/a
415n/a
416n/aclass TestInternals(unittest.TestCase):
417n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
418n/a def test_find_mac(self):
419n/a data = '''
420n/afake hwaddr
421n/acscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
422n/aeth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
423n/a'''
424n/a
425n/a popen = unittest.mock.MagicMock()
426n/a popen.stdout = io.BytesIO(data.encode())
427n/a
428n/a with unittest.mock.patch.object(shutil, 'which',
429n/a return_value='/sbin/ifconfig'):
430n/a with unittest.mock.patch.object(subprocess, 'Popen',
431n/a return_value=popen):
432n/a mac = uuid._find_mac(
433n/a command='ifconfig',
434n/a args='',
435n/a hw_identifiers=[b'hwaddr'],
436n/a get_index=lambda x: x + 1,
437n/a )
438n/a
439n/a self.assertEqual(mac, 0x1234567890ab)
440n/a
441n/a def check_node(self, node, requires=None, network=False):
442n/a if requires and node is None:
443n/a self.skipTest('requires ' + requires)
444n/a hex = '%012x' % node
445n/a if support.verbose >= 2:
446n/a print(hex, end=' ')
447n/a if network:
448n/a # 47 bit will never be set in IEEE 802 addresses obtained
449n/a # from network cards.
450n/a self.assertFalse(node & 0x010000000000, hex)
451n/a self.assertTrue(0 < node < (1 << 48),
452n/a "%s is not an RFC 4122 node ID" % hex)
453n/a
454n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
455n/a def test_ifconfig_getnode(self):
456n/a node = uuid._ifconfig_getnode()
457n/a self.check_node(node, 'ifconfig', True)
458n/a
459n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
460n/a def test_ip_getnode(self):
461n/a node = uuid._ip_getnode()
462n/a self.check_node(node, 'ip', True)
463n/a
464n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
465n/a def test_arp_getnode(self):
466n/a node = uuid._arp_getnode()
467n/a self.check_node(node, 'arp', True)
468n/a
469n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
470n/a def test_lanscan_getnode(self):
471n/a node = uuid._lanscan_getnode()
472n/a self.check_node(node, 'lanscan', True)
473n/a
474n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
475n/a def test_netstat_getnode(self):
476n/a node = uuid._netstat_getnode()
477n/a self.check_node(node, 'netstat', True)
478n/a
479n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
480n/a def test_ipconfig_getnode(self):
481n/a node = uuid._ipconfig_getnode()
482n/a self.check_node(node, 'ipconfig', True)
483n/a
484n/a @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
485n/a @unittest.skipUnless(importable('netbios'), 'requires netbios')
486n/a def test_netbios_getnode(self):
487n/a node = uuid._netbios_getnode()
488n/a self.check_node(node, network=True)
489n/a
490n/a def test_random_getnode(self):
491n/a node = uuid._random_getnode()
492n/a # Least significant bit of first octet must be set.
493n/a self.assertTrue(node & 0x010000000000, '%012x' % node)
494n/a self.check_node(node)
495n/a
496n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
497n/a @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
498n/a def test_unixdll_getnode(self):
499n/a try: # Issues 1481, 3581: _uuid_generate_time() might be None.
500n/a node = uuid._unixdll_getnode()
501n/a except TypeError:
502n/a self.skipTest('requires uuid_generate_time')
503n/a self.check_node(node)
504n/a
505n/a @unittest.skipUnless(os.name == 'nt', 'requires Windows')
506n/a @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
507n/a def test_windll_getnode(self):
508n/a node = uuid._windll_getnode()
509n/a self.check_node(node)
510n/a
511n/a
512n/aif __name__ == '__main__':
513n/a unittest.main()