1# Protocol Buffers - Google's data interchange format 2# Copyright 2008 Google Inc. All rights reserved. 3# https://developers.google.com/protocol-buffers/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Test for google.protobuf.internal.wire_format.""" 32 33__author__ = 'robinson@google.com (Will Robinson)' 34 35import unittest 36 37from google.protobuf import message 38from google.protobuf.internal import wire_format 39 40 41class WireFormatTest(unittest.TestCase): 42 43 def testPackTag(self): 44 field_number = 0xabc 45 tag_type = 2 46 self.assertEqual((field_number << 3) | tag_type, 47 wire_format.PackTag(field_number, tag_type)) 48 PackTag = wire_format.PackTag 49 # Number too high. 50 self.assertRaises(message.EncodeError, PackTag, field_number, 6) 51 # Number too low. 52 self.assertRaises(message.EncodeError, PackTag, field_number, -1) 53 54 def testUnpackTag(self): 55 # Test field numbers that will require various varint sizes. 56 for expected_field_number in (1, 15, 16, 2047, 2048): 57 for expected_wire_type in range(6): # Highest-numbered wiretype is 5. 58 field_number, wire_type = wire_format.UnpackTag( 59 wire_format.PackTag(expected_field_number, expected_wire_type)) 60 self.assertEqual(expected_field_number, field_number) 61 self.assertEqual(expected_wire_type, wire_type) 62 63 self.assertRaises(TypeError, wire_format.UnpackTag, None) 64 self.assertRaises(TypeError, wire_format.UnpackTag, 'abc') 65 self.assertRaises(TypeError, wire_format.UnpackTag, 0.0) 66 self.assertRaises(TypeError, wire_format.UnpackTag, object()) 67 68 def testZigZagEncode(self): 69 Z = wire_format.ZigZagEncode 70 self.assertEqual(0, Z(0)) 71 self.assertEqual(1, Z(-1)) 72 self.assertEqual(2, Z(1)) 73 self.assertEqual(3, Z(-2)) 74 self.assertEqual(4, Z(2)) 75 self.assertEqual(0xfffffffe, Z(0x7fffffff)) 76 self.assertEqual(0xffffffff, Z(-0x80000000)) 77 self.assertEqual(0xfffffffffffffffe, Z(0x7fffffffffffffff)) 78 self.assertEqual(0xffffffffffffffff, Z(-0x8000000000000000)) 79 80 self.assertRaises(TypeError, Z, None) 81 self.assertRaises(TypeError, Z, 'abcd') 82 self.assertRaises(TypeError, Z, 0.0) 83 self.assertRaises(TypeError, Z, object()) 84 85 def testZigZagDecode(self): 86 Z = wire_format.ZigZagDecode 87 self.assertEqual(0, Z(0)) 88 self.assertEqual(-1, Z(1)) 89 self.assertEqual(1, Z(2)) 90 self.assertEqual(-2, Z(3)) 91 self.assertEqual(2, Z(4)) 92 self.assertEqual(0x7fffffff, Z(0xfffffffe)) 93 self.assertEqual(-0x80000000, Z(0xffffffff)) 94 self.assertEqual(0x7fffffffffffffff, Z(0xfffffffffffffffe)) 95 self.assertEqual(-0x8000000000000000, Z(0xffffffffffffffff)) 96 97 self.assertRaises(TypeError, Z, None) 98 self.assertRaises(TypeError, Z, 'abcd') 99 self.assertRaises(TypeError, Z, 0.0) 100 self.assertRaises(TypeError, Z, object()) 101 102 def NumericByteSizeTestHelper(self, byte_size_fn, value, expected_value_size): 103 # Use field numbers that cause various byte sizes for the tag information. 104 for field_number, tag_bytes in ((15, 1), (16, 2), (2047, 2), (2048, 3)): 105 expected_size = expected_value_size + tag_bytes 106 actual_size = byte_size_fn(field_number, value) 107 self.assertEqual(expected_size, actual_size, 108 'byte_size_fn: %s, field_number: %d, value: %r\n' 109 'Expected: %d, Actual: %d'% ( 110 byte_size_fn, field_number, value, expected_size, actual_size)) 111 112 def testByteSizeFunctions(self): 113 # Test all numeric *ByteSize() functions. 114 NUMERIC_ARGS = [ 115 # Int32ByteSize(). 116 [wire_format.Int32ByteSize, 0, 1], 117 [wire_format.Int32ByteSize, 127, 1], 118 [wire_format.Int32ByteSize, 128, 2], 119 [wire_format.Int32ByteSize, -1, 10], 120 # Int64ByteSize(). 121 [wire_format.Int64ByteSize, 0, 1], 122 [wire_format.Int64ByteSize, 127, 1], 123 [wire_format.Int64ByteSize, 128, 2], 124 [wire_format.Int64ByteSize, -1, 10], 125 # UInt32ByteSize(). 126 [wire_format.UInt32ByteSize, 0, 1], 127 [wire_format.UInt32ByteSize, 127, 1], 128 [wire_format.UInt32ByteSize, 128, 2], 129 [wire_format.UInt32ByteSize, wire_format.UINT32_MAX, 5], 130 # UInt64ByteSize(). 131 [wire_format.UInt64ByteSize, 0, 1], 132 [wire_format.UInt64ByteSize, 127, 1], 133 [wire_format.UInt64ByteSize, 128, 2], 134 [wire_format.UInt64ByteSize, wire_format.UINT64_MAX, 10], 135 # SInt32ByteSize(). 136 [wire_format.SInt32ByteSize, 0, 1], 137 [wire_format.SInt32ByteSize, -1, 1], 138 [wire_format.SInt32ByteSize, 1, 1], 139 [wire_format.SInt32ByteSize, -63, 1], 140 [wire_format.SInt32ByteSize, 63, 1], 141 [wire_format.SInt32ByteSize, -64, 1], 142 [wire_format.SInt32ByteSize, 64, 2], 143 # SInt64ByteSize(). 144 [wire_format.SInt64ByteSize, 0, 1], 145 [wire_format.SInt64ByteSize, -1, 1], 146 [wire_format.SInt64ByteSize, 1, 1], 147 [wire_format.SInt64ByteSize, -63, 1], 148 [wire_format.SInt64ByteSize, 63, 1], 149 [wire_format.SInt64ByteSize, -64, 1], 150 [wire_format.SInt64ByteSize, 64, 2], 151 # Fixed32ByteSize(). 152 [wire_format.Fixed32ByteSize, 0, 4], 153 [wire_format.Fixed32ByteSize, wire_format.UINT32_MAX, 4], 154 # Fixed64ByteSize(). 155 [wire_format.Fixed64ByteSize, 0, 8], 156 [wire_format.Fixed64ByteSize, wire_format.UINT64_MAX, 8], 157 # SFixed32ByteSize(). 158 [wire_format.SFixed32ByteSize, 0, 4], 159 [wire_format.SFixed32ByteSize, wire_format.INT32_MIN, 4], 160 [wire_format.SFixed32ByteSize, wire_format.INT32_MAX, 4], 161 # SFixed64ByteSize(). 162 [wire_format.SFixed64ByteSize, 0, 8], 163 [wire_format.SFixed64ByteSize, wire_format.INT64_MIN, 8], 164 [wire_format.SFixed64ByteSize, wire_format.INT64_MAX, 8], 165 # FloatByteSize(). 166 [wire_format.FloatByteSize, 0.0, 4], 167 [wire_format.FloatByteSize, 1000000000.0, 4], 168 [wire_format.FloatByteSize, -1000000000.0, 4], 169 # DoubleByteSize(). 170 [wire_format.DoubleByteSize, 0.0, 8], 171 [wire_format.DoubleByteSize, 1000000000.0, 8], 172 [wire_format.DoubleByteSize, -1000000000.0, 8], 173 # BoolByteSize(). 174 [wire_format.BoolByteSize, False, 1], 175 [wire_format.BoolByteSize, True, 1], 176 # EnumByteSize(). 177 [wire_format.EnumByteSize, 0, 1], 178 [wire_format.EnumByteSize, 127, 1], 179 [wire_format.EnumByteSize, 128, 2], 180 [wire_format.EnumByteSize, wire_format.UINT32_MAX, 5], 181 ] 182 for args in NUMERIC_ARGS: 183 self.NumericByteSizeTestHelper(*args) 184 185 # Test strings and bytes. 186 for byte_size_fn in (wire_format.StringByteSize, wire_format.BytesByteSize): 187 # 1 byte for tag, 1 byte for length, 3 bytes for contents. 188 self.assertEqual(5, byte_size_fn(10, 'abc')) 189 # 2 bytes for tag, 1 byte for length, 3 bytes for contents. 190 self.assertEqual(6, byte_size_fn(16, 'abc')) 191 # 2 bytes for tag, 2 bytes for length, 128 bytes for contents. 192 self.assertEqual(132, byte_size_fn(16, 'a' * 128)) 193 194 # Test UTF-8 string byte size calculation. 195 # 1 byte for tag, 1 byte for length, 8 bytes for content. 196 self.assertEqual(10, wire_format.StringByteSize( 197 5, b'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'.decode('utf-8'))) 198 199 class MockMessage(object): 200 def __init__(self, byte_size): 201 self.byte_size = byte_size 202 def ByteSize(self): 203 return self.byte_size 204 205 message_byte_size = 10 206 mock_message = MockMessage(byte_size=message_byte_size) 207 # Test groups. 208 # (2 * 1) bytes for begin and end tags, plus message_byte_size. 209 self.assertEqual(2 + message_byte_size, 210 wire_format.GroupByteSize(1, mock_message)) 211 # (2 * 2) bytes for begin and end tags, plus message_byte_size. 212 self.assertEqual(4 + message_byte_size, 213 wire_format.GroupByteSize(16, mock_message)) 214 215 # Test messages. 216 # 1 byte for tag, plus 1 byte for length, plus contents. 217 self.assertEqual(2 + mock_message.byte_size, 218 wire_format.MessageByteSize(1, mock_message)) 219 # 2 bytes for tag, plus 1 byte for length, plus contents. 220 self.assertEqual(3 + mock_message.byte_size, 221 wire_format.MessageByteSize(16, mock_message)) 222 # 2 bytes for tag, plus 2 bytes for length, plus contents. 223 mock_message.byte_size = 128 224 self.assertEqual(4 + mock_message.byte_size, 225 wire_format.MessageByteSize(16, mock_message)) 226 227 228 # Test message set item byte size. 229 # 4 bytes for tags, plus 1 byte for length, plus 1 byte for type_id, 230 # plus contents. 231 mock_message.byte_size = 10 232 self.assertEqual(mock_message.byte_size + 6, 233 wire_format.MessageSetItemByteSize(1, mock_message)) 234 235 # 4 bytes for tags, plus 2 bytes for length, plus 1 byte for type_id, 236 # plus contents. 237 mock_message.byte_size = 128 238 self.assertEqual(mock_message.byte_size + 7, 239 wire_format.MessageSetItemByteSize(1, mock_message)) 240 241 # 4 bytes for tags, plus 2 bytes for length, plus 2 byte for type_id, 242 # plus contents. 243 self.assertEqual(mock_message.byte_size + 8, 244 wire_format.MessageSetItemByteSize(128, mock_message)) 245 246 # Too-long varint. 247 self.assertRaises(message.EncodeError, 248 wire_format.UInt64ByteSize, 1, 1 << 128) 249 250 251if __name__ == '__main__': 252 unittest.main() 253