Skip to content

Commit a276857

Browse files
committed
add test vectors
1 parent 9bc8090 commit a276857

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

Lib/test/test_uuid.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,11 +690,13 @@ def test_uuid6(self):
690690
fake_nanoseconds = 1545052026752910643
691691
fake_node_value = 93328246233727
692692
fake_clock_seq = 5317
693-
with mock.patch.object(self.uuid, '_generate_time_safe', None), \
694-
mock.patch.object(self.uuid, '_last_timestamp_v6', None), \
695-
mock.patch.object(self.uuid, 'getnode', return_value=fake_node_value), \
696-
mock.patch('time.time_ns', return_value=fake_nanoseconds), \
697-
mock.patch('random.getrandbits', return_value=fake_clock_seq):
693+
with (
694+
mock.patch.object(self.uuid, '_generate_time_safe', None),
695+
mock.patch.object(self.uuid, '_last_timestamp_v6', None),
696+
mock.patch.object(self.uuid, 'getnode', return_value=fake_node_value),
697+
mock.patch('time.time_ns', return_value=fake_nanoseconds),
698+
mock.patch('random.getrandbits', return_value=fake_clock_seq)
699+
):
698700
u = self.uuid.uuid6()
699701
equal(u.variant, self.uuid.RFC_4122)
700702
equal(u.version, 0b0110) # 6
@@ -710,6 +712,30 @@ def test_uuid6(self):
710712
equal(u.fields[4], 0b11000101) # 8 low bits of clock_seq
711713
equal(u.fields[5], fake_node_value)
712714

715+
def test_uuid6_test_vectors(self):
716+
# https://www.rfc-editor.org/rfc/rfc9562#name-test-vectors
717+
fake_nanoseconds = (0x1EC9414C232AB00 - 0x01B21DD213814000) * 100
718+
# https://www.rfc-editor.org/rfc/rfc9562#name-example-of-a-uuidv6-value
719+
node = 0x9F6BDECED846
720+
clock_seq = (0b11 << 12) | 0x3C8
721+
722+
with (
723+
mock.patch.object(self.uuid, '_generate_time_safe', None),
724+
mock.patch.object(self.uuid, '_last_timestamp_v6', None),
725+
mock.patch('time.time_ns', return_value=fake_nanoseconds)
726+
):
727+
u = self.uuid.uuid6(node=node, clock_seq=clock_seq)
728+
self.assertEqual(str(u).upper(), '1EC9414C-232A-6B00-B3C8-9F6BDECED846')
729+
# 32 16 4 12 2 14 48
730+
# time_hi | time_mid | ver | time_lo | var | clock_seq | node
731+
self.assertEqual(u.int & 0xFFFFFFFFFFFF, node)
732+
self.assertEqual((u.int >> 48) & 0x3FFF, clock_seq)
733+
self.assertEqual((u.int >> 62) & 0x3, 0b10)
734+
self.assertEqual((u.int >> 64) & 0xFFF, 0xB00)
735+
self.assertEqual((u.int >> 76) & 0xF, 0x6)
736+
self.assertEqual((u.int >> 80) & 0xFFFF, 0x232A)
737+
self.assertEqual((u.int >> 96) & 0xFFFFFFFF, 0x1EC9414C)
738+
713739
@support.requires_fork()
714740
def testIssue8621(self):
715741
# On at least some versions of OSX self.uuid.uuid4 generates

0 commit comments

Comments
 (0)