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