From 6a618949aa639b7007d0fce16f9a7c856488cd18 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 3 Jun 2016 10:24:23 -0700 Subject: Added test for surrogates (valid and invalid). --- python/google/protobuf/internal/json_format_test.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'python/google') diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py index eec1f56f..ba73c68c 100644 --- a/python/google/protobuf/internal/json_format_test.py +++ b/python/google/protobuf/internal/json_format_test.py @@ -247,6 +247,27 @@ class JsonFormatTest(JsonFormatBase): parsed_message = json_format_proto3_pb2.TestOneof() self.CheckParseBack(message, parsed_message) + def testSurrogates(self): + # Test correct surrogate handling. + message = json_format_proto3_pb2.TestMessage() + json_format.Parse('{"stringValue": "\\uD83D\\uDE01"}', message) + self.assertEqual(message.string_value, + b'\xF0\x9F\x98\x81'.decode("utf-8", "strict")) + + # TODO: add test that UTF-8 encoded surrogate code points are rejected. + # UTF-8 does not allow them. + + # Error case: unpaired high surrogate. + self.CheckError( + '{"stringValue": "\\uD83D"}', + r'Invalid \\uXXXX escape|Unpaired surrogate') + + # Unpaired low surrogate. + self.CheckError( + '{"stringValue": "\\uDE01"}', + r'Invalid \\uXXXX escape|Unpaired surrogate') + + def testTimestampMessage(self): message = json_format_proto3_pb2.TestTimestamp() message.value.seconds = 0 -- cgit v1.2.3