Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 1 | import unittest |
Serhiy Storchaka | 56507c7 | 2013-11-26 22:47:16 +0200 | [diff] [blame] | 2 | from test import support |
Hai Shi | 883bc63 | 2020-07-06 17:12:49 +0800 | [diff] [blame] | 3 | from test.support import import_helper |
Georg Brandl | 1d523e1 | 2009-12-19 18:23:28 +0000 | [diff] [blame] | 4 | import builtins |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 5 | import contextlib |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 6 | import copy |
Serhiy Storchaka | 56507c7 | 2013-11-26 22:47:16 +0200 | [diff] [blame] | 7 | import io |
Serhiy Storchaka | 7d15b54 | 2013-05-31 22:31:02 +0300 | [diff] [blame] | 8 | import os |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 9 | import pickle |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 10 | import sys |
David H | f1d8e7c | 2019-01-17 13:16:51 +0100 | [diff] [blame] | 11 | import weakref |
Victor Stinner | 62a68b7 | 2018-12-18 11:45:13 +0100 | [diff] [blame] | 12 | from unittest import mock |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 13 | |
Hai Shi | 883bc63 | 2020-07-06 17:12:49 +0800 | [diff] [blame] | 14 | py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid']) |
| 15 | c_uuid = import_helper.import_fresh_module('uuid', fresh=['_uuid']) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 16 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 17 | def importable(name): |
| 18 | try: |
| 19 | __import__(name) |
| 20 | return True |
| 21 | except: |
| 22 | return False |
| 23 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 24 | |
Victor Stinner | eb886db | 2020-03-17 15:51:42 +0100 | [diff] [blame] | 25 | def mock_get_command_stdout(data): |
| 26 | def get_command_stdout(command, args): |
| 27 | return io.BytesIO(data.encode()) |
| 28 | return get_command_stdout |
| 29 | |
| 30 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 31 | class BaseTestUUID: |
| 32 | uuid = None |
| 33 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 34 | def test_UUID(self): |
| 35 | equal = self.assertEqual |
| 36 | ascending = [] |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 37 | for (string, curly, hex, bytes, bytes_le, fields, integer, urn, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 38 | time, clock_seq, variant, version) in [ |
| 39 | ('00000000-0000-0000-0000-000000000000', |
| 40 | '{00000000-0000-0000-0000-000000000000}', |
| 41 | '00000000000000000000000000000000', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 42 | b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
| 43 | b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | (0, 0, 0, 0, 0, 0), |
| 45 | 0, |
| 46 | 'urn:uuid:00000000-0000-0000-0000-000000000000', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 47 | 0, 0, self.uuid.RESERVED_NCS, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 48 | ('00010203-0405-0607-0809-0a0b0c0d0e0f', |
| 49 | '{00010203-0405-0607-0809-0a0b0c0d0e0f}', |
| 50 | '000102030405060708090a0b0c0d0e0f', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 51 | b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
| 52 | b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 53 | (0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f), |
| 54 | 0x000102030405060708090a0b0c0d0e0f, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 55 | 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 56 | 0x607040500010203, 0x809, self.uuid.RESERVED_NCS, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | ('02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
| 58 | '{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}', |
| 59 | '02d9e6d59467382e8f9b9300a64ac3cd', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 60 | b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
| 61 | b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 62 | (0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd), |
| 63 | 0x02d9e6d59467382e8f9b9300a64ac3cd, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 65 | 0x82e946702d9e6d5, 0xf9b, self.uuid.RFC_4122, 3), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | ('12345678-1234-5678-1234-567812345678', |
| 67 | '{12345678-1234-5678-1234-567812345678}', |
| 68 | '12345678123456781234567812345678', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 69 | b'\x12\x34\x56\x78'*4, |
| 70 | b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 71 | (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678), |
| 72 | 0x12345678123456781234567812345678, |
| 73 | 'urn:uuid:12345678-1234-5678-1234-567812345678', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 74 | 0x678123412345678, 0x1234, self.uuid.RESERVED_NCS, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | ('6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
| 76 | '{6ba7b810-9dad-11d1-80b4-00c04fd430c8}', |
| 77 | '6ba7b8109dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 78 | b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 79 | b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 80 | (0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 81 | 0x6ba7b8109dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 83 | 0x1d19dad6ba7b810, 0xb4, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 84 | ('6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
| 85 | '{6ba7b811-9dad-11d1-80b4-00c04fd430c8}', |
| 86 | '6ba7b8119dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 87 | b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 88 | b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 89 | (0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 90 | 0x6ba7b8119dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 92 | 0x1d19dad6ba7b811, 0xb4, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | ('6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
| 94 | '{6ba7b812-9dad-11d1-80b4-00c04fd430c8}', |
| 95 | '6ba7b8129dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 96 | b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 97 | b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 98 | (0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 99 | 0x6ba7b8129dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 101 | 0x1d19dad6ba7b812, 0xb4, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 102 | ('6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
| 103 | '{6ba7b814-9dad-11d1-80b4-00c04fd430c8}', |
| 104 | '6ba7b8149dad11d180b400c04fd430c8', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 105 | b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
| 106 | b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 107 | (0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8), |
| 108 | 0x6ba7b8149dad11d180b400c04fd430c8, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 110 | 0x1d19dad6ba7b814, 0xb4, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | ('7d444840-9dc0-11d1-b245-5ffdce74fad2', |
| 112 | '{7d444840-9dc0-11d1-b245-5ffdce74fad2}', |
| 113 | '7d4448409dc011d1b2455ffdce74fad2', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 114 | b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
| 115 | b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 116 | (0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2), |
| 117 | 0x7d4448409dc011d1b2455ffdce74fad2, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 119 | 0x1d19dc07d444840, 0x3245, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | ('e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
| 121 | '{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}', |
| 122 | 'e902893a9d223c7ea7b8d6e313b71d9f', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 123 | b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
| 124 | b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 125 | (0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f), |
| 126 | 0xe902893a9d223c7ea7b8d6e313b71d9f, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 128 | 0xc7e9d22e902893a, 0x27b8, self.uuid.RFC_4122, 3), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 129 | ('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
| 130 | '{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}', |
| 131 | 'eb4240266f544ef8a4d0bb658a1fc6cf', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 132 | b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
| 133 | b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 134 | (0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf), |
| 135 | 0xeb4240266f544ef8a4d0bb658a1fc6cf, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 136 | 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 137 | 0xef86f54eb424026, 0x24d0, self.uuid.RFC_4122, 4), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 138 | ('f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
| 139 | '{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}', |
| 140 | 'f81d4fae7dec11d0a76500a0c91e6bf6', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 141 | b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
| 142 | b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 143 | (0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6), |
| 144 | 0xf81d4fae7dec11d0a76500a0c91e6bf6, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 145 | 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 146 | 0x1d07decf81d4fae, 0x2765, self.uuid.RFC_4122, 1), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 147 | ('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
| 148 | '{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}', |
| 149 | 'fffefdfcfffefffefffefffefdfcfbfa', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 150 | b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
| 151 | b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa', |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 152 | (0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa), |
| 153 | 0xfffefdfcfffefffefffefffefdfcfbfa, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 154 | 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 155 | 0xffefffefffefdfc, 0x3ffe, self.uuid.RESERVED_FUTURE, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 156 | ('ffffffff-ffff-ffff-ffff-ffffffffffff', |
| 157 | '{ffffffff-ffff-ffff-ffff-ffffffffffff}', |
| 158 | 'ffffffffffffffffffffffffffffffff', |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 159 | b'\xff'*16, |
| 160 | b'\xff'*16, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 161 | (0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff), |
| 162 | 0xffffffffffffffffffffffffffffffff, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 163 | 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 164 | 0xfffffffffffffff, 0x3fff, self.uuid.RESERVED_FUTURE, None), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 165 | ]: |
| 166 | equivalents = [] |
| 167 | # Construct each UUID in several different ways. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 168 | for u in [self.uuid.UUID(string), self.uuid.UUID(curly), self.uuid.UUID(hex), |
| 169 | self.uuid.UUID(bytes=bytes), self.uuid.UUID(bytes_le=bytes_le), |
| 170 | self.uuid.UUID(fields=fields), self.uuid.UUID(int=integer), |
| 171 | self.uuid.UUID(urn)]: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | # Test all conversions and properties of the UUID object. |
| 173 | equal(str(u), string) |
| 174 | equal(int(u), integer) |
| 175 | equal(u.bytes, bytes) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 176 | equal(u.bytes_le, bytes_le) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 177 | equal(u.fields, fields) |
| 178 | equal(u.time_low, fields[0]) |
| 179 | equal(u.time_mid, fields[1]) |
| 180 | equal(u.time_hi_version, fields[2]) |
| 181 | equal(u.clock_seq_hi_variant, fields[3]) |
| 182 | equal(u.clock_seq_low, fields[4]) |
| 183 | equal(u.node, fields[5]) |
| 184 | equal(u.hex, hex) |
| 185 | equal(u.int, integer) |
| 186 | equal(u.urn, urn) |
| 187 | equal(u.time, time) |
| 188 | equal(u.clock_seq, clock_seq) |
| 189 | equal(u.variant, variant) |
| 190 | equal(u.version, version) |
| 191 | equivalents.append(u) |
| 192 | |
| 193 | # Different construction methods should give the same UUID. |
| 194 | for u in equivalents: |
| 195 | for v in equivalents: |
| 196 | equal(u, v) |
Georg Brandl | 1d523e1 | 2009-12-19 18:23:28 +0000 | [diff] [blame] | 197 | |
| 198 | # Bug 7380: "bytes" and "bytes_le" should give the same type. |
| 199 | equal(type(u.bytes), builtins.bytes) |
| 200 | equal(type(u.bytes_le), builtins.bytes) |
| 201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | ascending.append(u) |
| 203 | |
| 204 | # Test comparison of UUIDs. |
| 205 | for i in range(len(ascending)): |
| 206 | for j in range(len(ascending)): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 207 | equal(i < j, ascending[i] < ascending[j]) |
| 208 | equal(i <= j, ascending[i] <= ascending[j]) |
| 209 | equal(i == j, ascending[i] == ascending[j]) |
| 210 | equal(i > j, ascending[i] > ascending[j]) |
| 211 | equal(i >= j, ascending[i] >= ascending[j]) |
| 212 | equal(i != j, ascending[i] != ascending[j]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 213 | |
| 214 | # Test sorting of UUIDs (above list is in ascending order). |
| 215 | resorted = ascending[:] |
| 216 | resorted.reverse() |
| 217 | resorted.sort() |
| 218 | equal(ascending, resorted) |
| 219 | |
| 220 | def test_exceptions(self): |
| 221 | badvalue = lambda f: self.assertRaises(ValueError, f) |
| 222 | badtype = lambda f: self.assertRaises(TypeError, f) |
| 223 | |
| 224 | # Badly formed hex strings. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 225 | badvalue(lambda: self.uuid.UUID('')) |
| 226 | badvalue(lambda: self.uuid.UUID('abc')) |
| 227 | badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567')) |
| 228 | badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789')) |
| 229 | badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678')) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 230 | |
| 231 | # Badly formed bytes. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 232 | badvalue(lambda: self.uuid.UUID(bytes='abc')) |
| 233 | badvalue(lambda: self.uuid.UUID(bytes='\0'*15)) |
| 234 | badvalue(lambda: self.uuid.UUID(bytes='\0'*17)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 235 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 236 | # Badly formed bytes_le. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 237 | badvalue(lambda: self.uuid.UUID(bytes_le='abc')) |
| 238 | badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15)) |
| 239 | badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 240 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 241 | # Badly formed fields. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 242 | badvalue(lambda: self.uuid.UUID(fields=(1,))) |
| 243 | badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5))) |
| 244 | badvalue(lambda: self.uuid.UUID(fields=(1, 2, 3, 4, 5, 6, 7))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 245 | |
| 246 | # Field values out of range. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 247 | badvalue(lambda: self.uuid.UUID(fields=(-1, 0, 0, 0, 0, 0))) |
| 248 | badvalue(lambda: self.uuid.UUID(fields=(0x100000000, 0, 0, 0, 0, 0))) |
| 249 | badvalue(lambda: self.uuid.UUID(fields=(0, -1, 0, 0, 0, 0))) |
| 250 | badvalue(lambda: self.uuid.UUID(fields=(0, 0x10000, 0, 0, 0, 0))) |
| 251 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, -1, 0, 0, 0))) |
| 252 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0x10000, 0, 0, 0))) |
| 253 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, -1, 0, 0))) |
| 254 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0x100, 0, 0))) |
| 255 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, -1, 0))) |
| 256 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0x100, 0))) |
| 257 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, -1))) |
| 258 | badvalue(lambda: self.uuid.UUID(fields=(0, 0, 0, 0, 0, 0x1000000000000))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 259 | |
| 260 | # Version number out of range. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 261 | badvalue(lambda: self.uuid.UUID('00'*16, version=0)) |
| 262 | badvalue(lambda: self.uuid.UUID('00'*16, version=6)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 263 | |
| 264 | # Integer value out of range. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 265 | badvalue(lambda: self.uuid.UUID(int=-1)) |
| 266 | badvalue(lambda: self.uuid.UUID(int=1<<128)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 267 | |
| 268 | # Must supply exactly one of hex, bytes, fields, int. |
Guido van Rossum | 65b6a80 | 2007-07-09 14:03:08 +0000 | [diff] [blame] | 269 | h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0 |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 270 | self.uuid.UUID(h) |
| 271 | self.uuid.UUID(hex=h) |
| 272 | self.uuid.UUID(bytes=b) |
| 273 | self.uuid.UUID(bytes_le=b) |
| 274 | self.uuid.UUID(fields=f) |
| 275 | self.uuid.UUID(int=i) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 276 | |
| 277 | # Wrong number of arguments (positional). |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 278 | badtype(lambda: self.uuid.UUID()) |
| 279 | badtype(lambda: self.uuid.UUID(h, b)) |
| 280 | badtype(lambda: self.uuid.UUID(h, b, b)) |
| 281 | badtype(lambda: self.uuid.UUID(h, b, b, f)) |
| 282 | badtype(lambda: self.uuid.UUID(h, b, b, f, i)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 284 | # Duplicate arguments. |
| 285 | for hh in [[], [('hex', h)]]: |
| 286 | for bb in [[], [('bytes', b)]]: |
| 287 | for bble in [[], [('bytes_le', b)]]: |
| 288 | for ii in [[], [('int', i)]]: |
| 289 | for ff in [[], [('fields', f)]]: |
| 290 | args = dict(hh + bb + bble + ii + ff) |
| 291 | if len(args) != 0: |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 292 | badtype(lambda: self.uuid.UUID(h, **args)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 293 | if len(args) != 1: |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 294 | badtype(lambda: self.uuid.UUID(**args)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | |
| 296 | # Immutability. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 297 | u = self.uuid.UUID(h) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 298 | badtype(lambda: setattr(u, 'hex', h)) |
| 299 | badtype(lambda: setattr(u, 'bytes', b)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 300 | badtype(lambda: setattr(u, 'bytes_le', b)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | badtype(lambda: setattr(u, 'fields', f)) |
| 302 | badtype(lambda: setattr(u, 'int', i)) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 303 | badtype(lambda: setattr(u, 'time_low', 0)) |
| 304 | badtype(lambda: setattr(u, 'time_mid', 0)) |
| 305 | badtype(lambda: setattr(u, 'time_hi_version', 0)) |
| 306 | badtype(lambda: setattr(u, 'time_hi_version', 0)) |
| 307 | badtype(lambda: setattr(u, 'clock_seq_hi_variant', 0)) |
| 308 | badtype(lambda: setattr(u, 'clock_seq_low', 0)) |
| 309 | badtype(lambda: setattr(u, 'node', 0)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 310 | |
Berker Peksag | 6b5e4a8 | 2016-12-31 20:08:16 +0300 | [diff] [blame] | 311 | # Comparison with a non-UUID object |
| 312 | badtype(lambda: u < object()) |
| 313 | badtype(lambda: u > object()) |
| 314 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 315 | def test_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 316 | node1 = self.uuid.getnode() |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 317 | self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 318 | |
| 319 | # Test it again to ensure consistency. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 320 | node2 = self.uuid.getnode() |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 321 | self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2)) |
Serhiy Storchaka | 56507c7 | 2013-11-26 22:47:16 +0200 | [diff] [blame] | 322 | |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 323 | def test_pickle_roundtrip(self): |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 324 | def check(actual, expected): |
| 325 | self.assertEqual(actual, expected) |
| 326 | self.assertEqual(actual.is_safe, expected.is_safe) |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 327 | |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 328 | with support.swap_item(sys.modules, 'uuid', self.uuid): |
| 329 | for is_safe in self.uuid.SafeUUID: |
| 330 | u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 331 | is_safe=is_safe) |
| 332 | check(copy.copy(u), u) |
| 333 | check(copy.deepcopy(u), u) |
| 334 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 335 | with self.subTest(protocol=proto): |
| 336 | check(pickle.loads(pickle.dumps(u, proto)), u) |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 337 | |
| 338 | def test_unpickle_previous_python_versions(self): |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 339 | def check(actual, expected): |
| 340 | self.assertEqual(actual, expected) |
| 341 | self.assertEqual(actual.is_safe, expected.is_safe) |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 342 | |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 343 | pickled_uuids = [ |
| 344 | # Python 2.7, protocol 0 |
| 345 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 346 | b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.', |
| 347 | # Python 2.7, protocol 1 |
| 348 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 349 | b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.', |
| 350 | # Python 2.7, protocol 2 |
| 351 | b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}' |
| 352 | b'\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 353 | # Python 3.6, protocol 0 |
| 354 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 355 | b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.', |
| 356 | # Python 3.6, protocol 1 |
| 357 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 358 | b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L' |
| 359 | b'\nsb.', |
| 360 | # Python 3.6, protocol 2 |
| 361 | b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 362 | b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 363 | # Python 3.6, protocol 3 |
| 364 | b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 365 | b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 366 | # Python 3.6, protocol 4 |
| 367 | b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI' |
| 368 | b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%' |
| 369 | b'\xd8\x00sb.', |
| 370 | # Python 3.7, protocol 0 |
| 371 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 372 | b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 373 | b'cuuid\nSafeUUID\n(NtRsb.', |
| 374 | # Python 3.7, protocol 1 |
| 375 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 376 | b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 377 | b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.', |
| 378 | # Python 3.7, protocol 2 |
| 379 | b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 380 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 381 | b'id\nSafeUUID\nN\x85Rub.', |
| 382 | # Python 3.7, protocol 3 |
| 383 | b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 384 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 385 | b'id\nSafeUUID\nN\x85Rub.', |
| 386 | # Python 3.7, protocol 4 |
| 387 | b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 388 | b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 389 | b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub' |
| 390 | b'.', |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 391 | ] |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 392 | pickled_uuids_safe = [ |
| 393 | # Python 3.7, protocol 0 |
| 394 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 395 | b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 396 | b'cuuid\nSafeUUID\n(I0\ntRsb.', |
| 397 | # Python 3.7, protocol 1 |
| 398 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 399 | b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 400 | b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.', |
| 401 | # Python 3.7, protocol 2 |
| 402 | b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 403 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 404 | b'id\nSafeUUID\nK\x00\x85Rub.', |
| 405 | # Python 3.7, protocol 3 |
| 406 | b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 407 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 408 | b'id\nSafeUUID\nK\x00\x85Rub.', |
| 409 | # Python 3.7, protocol 4 |
| 410 | b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 411 | b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 412 | b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00' |
| 413 | b'\x85Rub.', |
| 414 | ] |
| 415 | pickled_uuids_unsafe = [ |
| 416 | # Python 3.7, protocol 0 |
| 417 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 418 | b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 419 | b'cuuid\nSafeUUID\n(I-1\ntRsb.', |
| 420 | # Python 3.7, protocol 1 |
| 421 | b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 422 | b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 423 | b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR' |
| 424 | b'ub.', |
| 425 | # Python 3.7, protocol 2 |
| 426 | b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 427 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 428 | b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 429 | # Python 3.7, protocol 3 |
| 430 | b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 431 | b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 432 | b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 433 | # Python 3.7, protocol 4 |
| 434 | b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 435 | b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 436 | b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff' |
| 437 | b'\xff\xff\xff\x85Rub.', |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 438 | ] |
| 439 | |
Tal Einat | 5475253 | 2018-09-10 16:11:04 +0300 | [diff] [blame] | 440 | u = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5') |
| 441 | u_safe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 442 | is_safe=self.uuid.SafeUUID.safe) |
| 443 | u_unsafe = self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 444 | is_safe=self.uuid.SafeUUID.unsafe) |
| 445 | |
| 446 | with support.swap_item(sys.modules, 'uuid', self.uuid): |
| 447 | for pickled in pickled_uuids: |
| 448 | # is_safe was added in 3.7. When unpickling values from older |
| 449 | # versions, is_safe will be missing, so it should be set to |
| 450 | # SafeUUID.unknown. |
| 451 | check(pickle.loads(pickled), u) |
| 452 | for pickled in pickled_uuids_safe: |
| 453 | check(pickle.loads(pickled), u_safe) |
| 454 | for pickled in pickled_uuids_unsafe: |
| 455 | check(pickle.loads(pickled), u_unsafe) |
Tal Einat | 3e2b29d | 2018-09-06 14:34:25 +0300 | [diff] [blame] | 456 | |
Bo Bayles | 6b273f7 | 2018-01-23 19:11:44 -0600 | [diff] [blame] | 457 | # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers |
| 458 | # need not necessarily be 48 bits (e.g., EUI-64). |
| 459 | def test_uuid1_eui64(self): |
| 460 | # Confirm that uuid.getnode ignores hardware addresses larger than 48 |
| 461 | # bits. Mock out each platform's *_getnode helper functions to return |
| 462 | # something just larger than 48 bits to test. This will cause |
| 463 | # uuid.getnode to fall back on uuid._random_getnode, which will |
| 464 | # generate a valid value. |
| 465 | too_large_getter = lambda: 1 << 48 |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 466 | with mock.patch.multiple( |
Bo Bayles | 6b273f7 | 2018-01-23 19:11:44 -0600 | [diff] [blame] | 467 | self.uuid, |
| 468 | _node=None, # Ignore any cached node value. |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 469 | _GETTERS=[too_large_getter], |
Bo Bayles | 6b273f7 | 2018-01-23 19:11:44 -0600 | [diff] [blame] | 470 | ): |
| 471 | node = self.uuid.getnode() |
| 472 | self.assertTrue(0 < node < (1 << 48), '%012x' % node) |
| 473 | |
| 474 | # Confirm that uuid1 can use the generated node, i.e., the that |
| 475 | # uuid.getnode fell back on uuid._random_getnode() rather than using |
| 476 | # the value from too_large_getter above. |
| 477 | try: |
| 478 | self.uuid.uuid1(node=node) |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 479 | except ValueError: |
Bo Bayles | 6b273f7 | 2018-01-23 19:11:44 -0600 | [diff] [blame] | 480 | self.fail('uuid1 was given an invalid node ID') |
| 481 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 482 | def test_uuid1(self): |
| 483 | equal = self.assertEqual |
| 484 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 485 | # Make sure uuid1() generates UUIDs that are actually version 1. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 486 | for u in [self.uuid.uuid1() for i in range(10)]: |
| 487 | equal(u.variant, self.uuid.RFC_4122) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 488 | equal(u.version, 1) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 489 | self.assertIn(u.is_safe, {self.uuid.SafeUUID.safe, |
| 490 | self.uuid.SafeUUID.unsafe, |
| 491 | self.uuid.SafeUUID.unknown}) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 492 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 493 | # Make sure the generated UUIDs are actually unique. |
| 494 | uuids = {} |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 495 | for u in [self.uuid.uuid1() for i in range(1000)]: |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 496 | uuids[u] = 1 |
| 497 | equal(len(uuids.keys()), 1000) |
| 498 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 499 | # Make sure the supplied node ID appears in the UUID. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 500 | u = self.uuid.uuid1(0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 501 | equal(u.node, 0) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 502 | u = self.uuid.uuid1(0x123456789abc) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 503 | equal(u.node, 0x123456789abc) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 504 | u = self.uuid.uuid1(0xffffffffffff) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 505 | equal(u.node, 0xffffffffffff) |
| 506 | |
| 507 | # Make sure the supplied clock sequence appears in the UUID. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 508 | u = self.uuid.uuid1(0x123456789abc, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 509 | equal(u.node, 0x123456789abc) |
| 510 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | u.clock_seq_low, 0) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 511 | u = self.uuid.uuid1(0x123456789abc, 0x1234) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | equal(u.node, 0x123456789abc) |
| 513 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 514 | u.clock_seq_low, 0x1234) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 515 | u = self.uuid.uuid1(0x123456789abc, 0x3fff) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 516 | equal(u.node, 0x123456789abc) |
| 517 | equal(((u.clock_seq_hi_variant & 0x3f) << 8) | |
| 518 | u.clock_seq_low, 0x3fff) |
| 519 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 520 | # bpo-29925: On Mac OS X Tiger, self.uuid.uuid1().is_safe returns |
| 521 | # self.uuid.SafeUUID.unknown |
Victor Stinner | c209b70 | 2017-04-19 13:01:03 +0200 | [diff] [blame] | 522 | @support.requires_mac_ver(10, 5) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 523 | @unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 524 | def test_uuid1_safe(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 525 | if not self.uuid._has_uuid_generate_time_safe: |
| 526 | self.skipTest('requires uuid_generate_time_safe(3)') |
| 527 | |
| 528 | u = self.uuid.uuid1() |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 529 | # uuid_generate_time_safe() may return 0 or -1 but what it returns is |
| 530 | # dependent on the underlying platform support. At least it cannot be |
| 531 | # unknown (unless I suppose the platform is buggy). |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 532 | self.assertNotEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 533 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 534 | @contextlib.contextmanager |
| 535 | def mock_generate_time_safe(self, safe_value): |
| 536 | """ |
| 537 | Mock uuid._generate_time_safe() to return a given *safe_value*. |
| 538 | """ |
| 539 | if os.name != 'posix': |
| 540 | self.skipTest('POSIX-only test') |
| 541 | self.uuid._load_system_functions() |
| 542 | f = self.uuid._generate_time_safe |
| 543 | if f is None: |
| 544 | self.skipTest('need uuid._generate_time_safe') |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 545 | with mock.patch.object(self.uuid, '_generate_time_safe', |
| 546 | lambda: (f()[0], safe_value)): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 547 | yield |
| 548 | |
| 549 | @unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 550 | def test_uuid1_unknown(self): |
| 551 | # Even if the platform has uuid_generate_time_safe(), let's mock it to |
| 552 | # be uuid_generate_time() and ensure the safety is unknown. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 553 | with self.mock_generate_time_safe(None): |
| 554 | u = self.uuid.uuid1() |
| 555 | self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 556 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 557 | @unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 558 | def test_uuid1_is_safe(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 559 | with self.mock_generate_time_safe(0): |
| 560 | u = self.uuid.uuid1() |
| 561 | self.assertEqual(u.is_safe, self.uuid.SafeUUID.safe) |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 562 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 563 | @unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 564 | def test_uuid1_is_unsafe(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 565 | with self.mock_generate_time_safe(-1): |
| 566 | u = self.uuid.uuid1() |
| 567 | self.assertEqual(u.is_safe, self.uuid.SafeUUID.unsafe) |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 568 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 569 | @unittest.skipUnless(os.name == 'posix', 'POSIX-only test') |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 570 | def test_uuid1_bogus_return_value(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 571 | with self.mock_generate_time_safe(3): |
| 572 | u = self.uuid.uuid1() |
| 573 | self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown) |
Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 574 | |
Victor Stinner | 62a68b7 | 2018-12-18 11:45:13 +0100 | [diff] [blame] | 575 | def test_uuid1_time(self): |
| 576 | with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \ |
| 577 | mock.patch.object(self.uuid, '_generate_time_safe', None), \ |
| 578 | mock.patch.object(self.uuid, '_last_timestamp', None), \ |
| 579 | mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \ |
| 580 | mock.patch('time.time_ns', return_value=1545052026752910643), \ |
| 581 | mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random |
| 582 | u = self.uuid.uuid1() |
| 583 | self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) |
| 584 | |
| 585 | with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \ |
| 586 | mock.patch.object(self.uuid, '_generate_time_safe', None), \ |
| 587 | mock.patch.object(self.uuid, '_last_timestamp', None), \ |
| 588 | mock.patch('time.time_ns', return_value=1545052026752910643): |
| 589 | u = self.uuid.uuid1(node=93328246233727, clock_seq=5317) |
| 590 | self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) |
| 591 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 592 | def test_uuid3(self): |
| 593 | equal = self.assertEqual |
| 594 | |
| 595 | # Test some known version-3 UUIDs. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 596 | for u, v in [(self.uuid.uuid3(self.uuid.NAMESPACE_DNS, 'python.org'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 597 | '6fa459ea-ee8a-3ca4-894e-db77e160355e'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 598 | (self.uuid.uuid3(self.uuid.NAMESPACE_URL, 'http://python.org/'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 599 | '9fe8e8c4-aaa8-32a9-a55c-4535a88b748d'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 600 | (self.uuid.uuid3(self.uuid.NAMESPACE_OID, '1.3.6.1'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 601 | 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 602 | (self.uuid.uuid3(self.uuid.NAMESPACE_X500, 'c=ca'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 603 | '658d3002-db6b-3040-a1d1-8ddd7d189a4d'), |
| 604 | ]: |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 605 | equal(u.variant, self.uuid.RFC_4122) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 606 | equal(u.version, 3) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 607 | equal(u, self.uuid.UUID(v)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 608 | equal(str(u), v) |
| 609 | |
| 610 | def test_uuid4(self): |
| 611 | equal = self.assertEqual |
| 612 | |
| 613 | # Make sure uuid4() generates UUIDs that are actually version 4. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 614 | for u in [self.uuid.uuid4() for i in range(10)]: |
| 615 | equal(u.variant, self.uuid.RFC_4122) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 616 | equal(u.version, 4) |
| 617 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 618 | # Make sure the generated UUIDs are actually unique. |
| 619 | uuids = {} |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 620 | for u in [self.uuid.uuid4() for i in range(1000)]: |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 621 | uuids[u] = 1 |
| 622 | equal(len(uuids.keys()), 1000) |
| 623 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 624 | def test_uuid5(self): |
| 625 | equal = self.assertEqual |
| 626 | |
| 627 | # Test some known version-5 UUIDs. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 628 | for u, v in [(self.uuid.uuid5(self.uuid.NAMESPACE_DNS, 'python.org'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 629 | '886313e1-3b8a-5372-9b90-0c9aee199e5d'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 630 | (self.uuid.uuid5(self.uuid.NAMESPACE_URL, 'http://python.org/'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 631 | '4c565f0d-3f5a-5890-b41b-20cf47701c5e'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 632 | (self.uuid.uuid5(self.uuid.NAMESPACE_OID, '1.3.6.1'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 634 | (self.uuid.uuid5(self.uuid.NAMESPACE_X500, 'c=ca'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 635 | 'cc957dd1-a972-5349-98cd-874190002798'), |
| 636 | ]: |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 637 | equal(u.variant, self.uuid.RFC_4122) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 638 | equal(u.version, 5) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 639 | equal(u, self.uuid.UUID(v)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 640 | equal(str(u), v) |
| 641 | |
pxinwr | 98a5417 | 2020-12-09 07:20:19 +0800 | [diff] [blame] | 642 | @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork') |
Ronald Oussoren | ac764d3 | 2010-05-05 15:32:33 +0000 | [diff] [blame] | 643 | def testIssue8621(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 644 | # On at least some versions of OSX self.uuid.uuid4 generates |
Ronald Oussoren | ac764d3 | 2010-05-05 15:32:33 +0000 | [diff] [blame] | 645 | # the same sequence of UUIDs in the parent and any |
| 646 | # children started using fork. |
| 647 | fds = os.pipe() |
| 648 | pid = os.fork() |
| 649 | if pid == 0: |
| 650 | os.close(fds[0]) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 651 | value = self.uuid.uuid4() |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 652 | os.write(fds[1], value.hex.encode('latin-1')) |
Ronald Oussoren | ac764d3 | 2010-05-05 15:32:33 +0000 | [diff] [blame] | 653 | os._exit(0) |
| 654 | |
| 655 | else: |
| 656 | os.close(fds[1]) |
Richard Oudkerk | 0e547b6 | 2013-06-10 16:29:19 +0100 | [diff] [blame] | 657 | self.addCleanup(os.close, fds[0]) |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 658 | parent_value = self.uuid.uuid4().hex |
Victor Stinner | 278c1e1 | 2020-03-31 20:08:12 +0200 | [diff] [blame] | 659 | support.wait_process(pid, exitcode=0) |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 660 | child_value = os.read(fds[0], 100).decode('latin-1') |
Ronald Oussoren | ac764d3 | 2010-05-05 15:32:33 +0000 | [diff] [blame] | 661 | |
| 662 | self.assertNotEqual(parent_value, child_value) |
| 663 | |
David H | f1d8e7c | 2019-01-17 13:16:51 +0100 | [diff] [blame] | 664 | def test_uuid_weakref(self): |
| 665 | # bpo-35701: check that weak referencing to a UUID object can be created |
| 666 | strong = self.uuid.uuid4() |
| 667 | weak = weakref.ref(strong) |
| 668 | self.assertIs(strong, weak()) |
Ronald Oussoren | ac764d3 | 2010-05-05 15:32:33 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 670 | class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase): |
| 671 | uuid = py_uuid |
| 672 | |
| 673 | @unittest.skipUnless(c_uuid, 'requires the C _uuid module') |
| 674 | class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase): |
| 675 | uuid = c_uuid |
| 676 | |
| 677 | |
| 678 | class BaseTestInternals: |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 679 | _uuid = py_uuid |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 680 | |
Victor Stinner | ebf6bb9 | 2020-03-17 18:36:44 +0100 | [diff] [blame] | 681 | def check_parse_mac(self, aix): |
| 682 | if not aix: |
| 683 | patch = mock.patch.multiple(self.uuid, |
| 684 | _MAC_DELIM=b':', |
| 685 | _MAC_OMITS_LEADING_ZEROES=False) |
| 686 | else: |
| 687 | patch = mock.patch.multiple(self.uuid, |
| 688 | _MAC_DELIM=b'.', |
| 689 | _MAC_OMITS_LEADING_ZEROES=True) |
| 690 | |
| 691 | with patch: |
| 692 | # Valid MAC addresses |
| 693 | if not aix: |
| 694 | tests = ( |
| 695 | (b'52:54:00:9d:0e:67', 0x5254009d0e67), |
| 696 | (b'12:34:56:78:90:ab', 0x1234567890ab), |
| 697 | ) |
| 698 | else: |
| 699 | # AIX format |
| 700 | tests = ( |
| 701 | (b'fe.ad.c.1.23.4', 0xfead0c012304), |
| 702 | ) |
| 703 | for mac, expected in tests: |
| 704 | self.assertEqual(self.uuid._parse_mac(mac), expected) |
| 705 | |
| 706 | # Invalid MAC addresses |
| 707 | for mac in ( |
| 708 | b'', |
| 709 | # IPv6 addresses with same length than valid MAC address |
| 710 | # (17 characters) |
| 711 | b'fe80::5054:ff:fe9', |
| 712 | b'123:2:3:4:5:6:7:8', |
| 713 | # empty 5rd field |
| 714 | b'52:54:00:9d::67', |
| 715 | # only 5 fields instead of 6 |
| 716 | b'52:54:00:9d:0e' |
| 717 | # invalid character 'x' |
| 718 | b'52:54:00:9d:0e:6x' |
| 719 | # dash separator |
| 720 | b'52-54-00-9d-0e-67', |
| 721 | ): |
| 722 | if aix: |
| 723 | mac = mac.replace(b':', b'.') |
| 724 | with self.subTest(mac=mac): |
| 725 | self.assertIsNone(self.uuid._parse_mac(mac)) |
| 726 | |
| 727 | def test_parse_mac(self): |
| 728 | self.check_parse_mac(False) |
| 729 | |
| 730 | def test_parse_mac_aix(self): |
| 731 | self.check_parse_mac(True) |
| 732 | |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 733 | def test_find_under_heading(self): |
| 734 | data = '''\ |
| 735 | Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll |
| 736 | en0 1500 link#2 fe.ad.c.1.23.4 1714807956 0 711348489 0 0 |
| 737 | 01:00:5e:00:00:01 |
| 738 | en0 1500 192.168.129 x071 1714807956 0 711348489 0 0 |
| 739 | 224.0.0.1 |
| 740 | en0 1500 192.168.90 x071 1714807956 0 711348489 0 0 |
| 741 | 224.0.0.1 |
| 742 | ''' |
| 743 | |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 744 | # The above data is from AIX - with '.' as _MAC_DELIM and strings |
| 745 | # shorter than 17 bytes (no leading 0). (_MAC_OMITS_LEADING_ZEROES=True) |
| 746 | with mock.patch.multiple(self.uuid, |
| 747 | _MAC_DELIM=b'.', |
| 748 | _MAC_OMITS_LEADING_ZEROES=True, |
Victor Stinner | eb886db | 2020-03-17 15:51:42 +0100 | [diff] [blame] | 749 | _get_command_stdout=mock_get_command_stdout(data)): |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 750 | mac = self.uuid._find_mac_under_heading( |
| 751 | command='netstat', |
| 752 | args='-ian', |
| 753 | heading=b'Address', |
| 754 | ) |
| 755 | |
| 756 | self.assertEqual(mac, 0xfead0c012304) |
| 757 | |
Victor Stinner | eb886db | 2020-03-17 15:51:42 +0100 | [diff] [blame] | 758 | def test_find_under_heading_ipv6(self): |
| 759 | # bpo-39991: IPv6 address "fe80::5054:ff:fe9" looks like a MAC address |
| 760 | # (same string length) but must be skipped |
| 761 | data = '''\ |
| 762 | Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll |
| 763 | vtnet 1500 <Link#1> 52:54:00:9d:0e:67 10017 0 0 8174 0 0 |
| 764 | vtnet - fe80::%vtnet0 fe80::5054:ff:fe9 0 - - 4 - - |
| 765 | vtnet - 192.168.122.0 192.168.122.45 8844 - - 8171 - - |
| 766 | lo0 16384 <Link#2> lo0 260148 0 0 260148 0 0 |
| 767 | lo0 - ::1/128 ::1 193 - - 193 - - |
| 768 | ff01::1%lo0 |
| 769 | ff02::2:2eb7:74fa |
| 770 | ff02::2:ff2e:b774 |
| 771 | ff02::1%lo0 |
| 772 | ff02::1:ff00:1%lo |
| 773 | lo0 - fe80::%lo0/64 fe80::1%lo0 0 - - 0 - - |
| 774 | ff01::1%lo0 |
| 775 | ff02::2:2eb7:74fa |
| 776 | ff02::2:ff2e:b774 |
| 777 | ff02::1%lo0 |
| 778 | ff02::1:ff00:1%lo |
| 779 | lo0 - 127.0.0.0/8 127.0.0.1 259955 - - 259955 - - |
| 780 | 224.0.0.1 |
| 781 | ''' |
| 782 | |
| 783 | with mock.patch.multiple(self.uuid, |
| 784 | _MAC_DELIM=b':', |
| 785 | _MAC_OMITS_LEADING_ZEROES=False, |
| 786 | _get_command_stdout=mock_get_command_stdout(data)): |
| 787 | mac = self.uuid._find_mac_under_heading( |
| 788 | command='netstat', |
| 789 | args='-ian', |
| 790 | heading=b'Address', |
| 791 | ) |
| 792 | |
| 793 | self.assertEqual(mac, 0x5254009d0e67) |
| 794 | |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 795 | def test_find_mac_near_keyword(self): |
| 796 | # key and value are on the same line |
Serhiy Storchaka | 0e32ea1 | 2014-12-15 12:06:22 +0200 | [diff] [blame] | 797 | data = ''' |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 798 | fake Link encap:UNSPEC hwaddr 00-00 |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 799 | cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 |
| 800 | eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab |
| 801 | ''' |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 802 | |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 803 | # The above data will only be parsed properly on non-AIX unixes. |
| 804 | with mock.patch.multiple(self.uuid, |
| 805 | _MAC_DELIM=b':', |
| 806 | _MAC_OMITS_LEADING_ZEROES=False, |
Victor Stinner | eb886db | 2020-03-17 15:51:42 +0100 | [diff] [blame] | 807 | _get_command_stdout=mock_get_command_stdout(data)): |
Michael Felt | 0bcbfa4 | 2019-09-26 20:43:15 +0100 | [diff] [blame] | 808 | mac = self.uuid._find_mac_near_keyword( |
| 809 | command='ifconfig', |
| 810 | args='', |
| 811 | keywords=[b'hwaddr'], |
| 812 | get_word_index=lambda x: x + 1, |
| 813 | ) |
Serhiy Storchaka | 0e32ea1 | 2014-12-15 12:06:22 +0200 | [diff] [blame] | 814 | |
| 815 | self.assertEqual(mac, 0x1234567890ab) |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 816 | |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 817 | def check_node(self, node, requires=None): |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 818 | if requires and node is None: |
| 819 | self.skipTest('requires ' + requires) |
| 820 | hex = '%012x' % node |
| 821 | if support.verbose >= 2: |
| 822 | print(hex, end=' ') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 823 | self.assertTrue(0 < node < (1 << 48), |
| 824 | "%s is not an RFC 4122 node ID" % hex) |
| 825 | |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 826 | @unittest.skipUnless(_uuid._ifconfig_getnode in _uuid._GETTERS, |
| 827 | "ifconfig is not used for introspection on this platform") |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 828 | def test_ifconfig_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 829 | node = self.uuid._ifconfig_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 830 | self.check_node(node, 'ifconfig') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 831 | |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 832 | @unittest.skipUnless(_uuid._ip_getnode in _uuid._GETTERS, |
| 833 | "ip is not used for introspection on this platform") |
Serhiy Storchaka | 0e32ea1 | 2014-12-15 12:06:22 +0200 | [diff] [blame] | 834 | def test_ip_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 835 | node = self.uuid._ip_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 836 | self.check_node(node, 'ip') |
Serhiy Storchaka | 0e32ea1 | 2014-12-15 12:06:22 +0200 | [diff] [blame] | 837 | |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 838 | @unittest.skipUnless(_uuid._arp_getnode in _uuid._GETTERS, |
| 839 | "arp is not used for introspection on this platform") |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 840 | def test_arp_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 841 | node = self.uuid._arp_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 842 | self.check_node(node, 'arp') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 843 | |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 844 | @unittest.skipUnless(_uuid._lanscan_getnode in _uuid._GETTERS, |
| 845 | "lanscan is not used for introspection on this platform") |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 846 | def test_lanscan_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 847 | node = self.uuid._lanscan_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 848 | self.check_node(node, 'lanscan') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 849 | |
Michael Felt | 3a1d50e | 2019-06-15 17:52:29 +0200 | [diff] [blame] | 850 | @unittest.skipUnless(_uuid._netstat_getnode in _uuid._GETTERS, |
| 851 | "netstat is not used for introspection on this platform") |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 852 | def test_netstat_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 853 | node = self.uuid._netstat_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 854 | self.check_node(node, 'netstat') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 855 | |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 856 | def test_random_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 857 | node = self.uuid._random_getnode() |
Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame] | 858 | # The multicast bit, i.e. the least significant bit of first octet, |
| 859 | # must be set for randomly generated MAC addresses. See RFC 4122, |
| 860 | # $4.1.6. |
| 861 | self.assertTrue(node & (1 << 40), '%012x' % node) |
Victor Stinner | c9409f7 | 2017-11-28 00:30:21 +0100 | [diff] [blame] | 862 | self.check_node(node) |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 863 | |
Serhiy Storchaka | e69fbb6 | 2017-12-04 11:51:55 +0200 | [diff] [blame] | 864 | node2 = self.uuid._random_getnode() |
| 865 | self.assertNotEqual(node2, node, '%012x' % node) |
| 866 | |
Steve Dower | d6b727e | 2020-05-12 23:32:32 +0100 | [diff] [blame] | 867 | class TestInternalsWithoutExtModule(BaseTestInternals, unittest.TestCase): |
| 868 | uuid = py_uuid |
| 869 | |
| 870 | @unittest.skipUnless(c_uuid, 'requires the C _uuid module') |
| 871 | class TestInternalsWithExtModule(BaseTestInternals, unittest.TestCase): |
| 872 | uuid = c_uuid |
| 873 | |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 874 | @unittest.skipUnless(os.name == 'posix', 'requires Posix') |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 875 | def test_unix_getnode(self): |
| 876 | if not importable('_uuid') and not importable('ctypes'): |
| 877 | self.skipTest("neither _uuid extension nor ctypes available") |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 878 | try: # Issues 1481, 3581: _uuid_generate_time() might be None. |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 879 | node = self.uuid._unix_getnode() |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 880 | except TypeError: |
| 881 | self.skipTest('requires uuid_generate_time') |
Victor Stinner | c9409f7 | 2017-11-28 00:30:21 +0100 | [diff] [blame] | 882 | self.check_node(node, 'unix') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 883 | |
| 884 | @unittest.skipUnless(os.name == 'nt', 'requires Windows') |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 885 | def test_windll_getnode(self): |
Antoine Pitrou | a106aec | 2017-09-28 23:03:06 +0200 | [diff] [blame] | 886 | node = self.uuid._windll_getnode() |
Victor Stinner | c9409f7 | 2017-11-28 00:30:21 +0100 | [diff] [blame] | 887 | self.check_node(node) |
Serhiy Storchaka | 79b8173 | 2014-12-15 12:03:44 +0200 | [diff] [blame] | 888 | |
| 889 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 890 | if __name__ == '__main__': |
Serhiy Storchaka | 7d15b54 | 2013-05-31 22:31:02 +0300 | [diff] [blame] | 891 | unittest.main() |