1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 import static com.google.common.truth.Truth.assertThat; 11 12 import com.google.protobuf.PackedFieldTestProto.TestAllTypes; 13 import com.google.protobuf.PackedFieldTestProto.TestAllTypes.NestedEnum; 14 import com.google.protobuf.PackedFieldTestProto.TestUnpackedTypes; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 /** Tests primitive repeated fields in proto3 are packed in wire format. */ 20 @RunWith(JUnit4.class) 21 public class PackedFieldTest { 22 static final ByteString expectedPackedRawBytes = 23 ByteString.copyFrom( 24 new byte[] { 25 (byte) 0xFA, 26 0x01, 27 0x01, 28 0x01, // repeated int32 29 (byte) 0x82, 30 0x02, 31 0x01, 32 0x01, // repeated int64 33 (byte) 0x8A, 34 0x02, 35 0x01, 36 0x01, // repeated uint32 37 (byte) 0x92, 38 0x02, 39 0x01, 40 0x01, // repeated uint64 41 (byte) 0x9A, 42 0x02, 43 0x01, 44 0x02, // repeated sint32 45 (byte) 0xA2, 46 0x02, 47 0x01, 48 0x02, // repeated sint64 49 (byte) 0xAA, 50 0x02, 51 0x04, 52 0x01, 53 0x00, 54 0x00, 55 0x00, // repeated fixed32 56 (byte) 0xB2, 57 0x02, 58 0x08, 59 0x01, 60 0x00, 61 0x00, 62 0x00, // repeated fixed64 63 0x00, 64 0x00, 65 0x00, 66 0x00, 67 (byte) 0xBA, 68 0x02, 69 0x04, 70 0x01, 71 0x00, 72 0x00, 73 0x00, // repeated sfixed32 74 (byte) 0xC2, 75 0x02, 76 0x08, 77 0x01, 78 0x00, 79 0x00, 80 0x00, // repeated sfixed64 81 0x00, 82 0x00, 83 0x00, 84 0x00, 85 (byte) 0xCA, 86 0x02, 87 0x04, 88 0x00, 89 0x00, 90 (byte) 0x80, 91 0x3f, // repeated float 92 (byte) 0xD2, 93 0x02, 94 0x08, 95 0x00, 96 0x00, 97 0x00, 98 0x00, // repeated double 99 0x00, 100 0x00, 101 (byte) 0xf0, 102 0x3f, 103 (byte) 0xDA, 104 0x02, 105 0x01, 106 0x01, // repeated bool 107 (byte) 0x9A, 108 0x03, 109 0x01, 110 0x01 // repeated nested enum 111 }); 112 113 static final ByteString expectedUnpackedRawBytes = 114 ByteString.copyFrom( 115 new byte[] { 116 0x08, 117 0x01, // repeated int32 118 0x10, 119 0x01, // repeated int64 120 0x18, 121 0x01, // repeated uint32 122 0x20, 123 0x01, // repeated uint64 124 0x28, 125 0x02, // repeated sint32 126 0x30, 127 0x02, // repeated sint64 128 0x3D, 129 0x01, 130 0x00, 131 0x00, 132 0x00, // repeated fixed32 133 0x41, 134 0x01, 135 0x00, 136 0x00, 137 0x00, // repeated fixed64 138 0x00, 139 0x00, 140 0x00, 141 0x00, 142 0x4D, 143 0x01, 144 0x00, 145 0x00, 146 0x00, // repeated sfixed32 147 0x51, 148 0x01, 149 0x00, 150 0x00, 151 0x00, // repeated sfixed64 152 0x00, 153 0x00, 154 0x00, 155 0x00, 156 0x5D, 157 0x00, 158 0x00, 159 (byte) 0x80, 160 0x3f, // repeated float 161 0x61, 162 0x00, 163 0x00, 164 0x00, 165 0x00, // repeated double 166 0x00, 167 0x00, 168 (byte) 0xf0, 169 0x3f, 170 0x68, 171 0x01, // repeated bool 172 0x70, 173 0x01, // repeated nested enum 174 }); 175 176 @Test testPackedGeneratedMessage()177 public void testPackedGeneratedMessage() throws Exception { 178 TestAllTypes message = TestAllTypes.parseFrom(expectedPackedRawBytes); 179 assertThat(message.toByteString()).isEqualTo(expectedPackedRawBytes); 180 } 181 182 @Test testPackedDynamicMessageSerialize()183 public void testPackedDynamicMessageSerialize() throws Exception { 184 DynamicMessage message = 185 DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), expectedPackedRawBytes); 186 assertThat(message.toByteString()).isEqualTo(expectedPackedRawBytes); 187 } 188 189 @Test testUnpackedGeneratedMessage()190 public void testUnpackedGeneratedMessage() throws Exception { 191 TestUnpackedTypes message = TestUnpackedTypes.parseFrom(expectedUnpackedRawBytes); 192 assertThat(message.toByteString()).isEqualTo(expectedUnpackedRawBytes); 193 } 194 195 @Test testUnPackedDynamicMessageSerialize()196 public void testUnPackedDynamicMessageSerialize() throws Exception { 197 DynamicMessage message = 198 DynamicMessage.parseFrom(TestUnpackedTypes.getDescriptor(), expectedUnpackedRawBytes); 199 assertThat(message.toByteString()).isEqualTo(expectedUnpackedRawBytes); 200 } 201 202 // Make sure we haven't screwed up the code generation for packing fields by default. 203 @Test testPackedSerialization()204 public void testPackedSerialization() throws Exception { 205 TestAllTypes message = 206 TestAllTypes.newBuilder() 207 .addRepeatedInt32(1234) 208 .addRepeatedNestedEnum(NestedEnum.BAR) 209 .build(); 210 211 CodedInputStream in = CodedInputStream.newInstance(message.toByteArray()); 212 213 while (!in.isAtEnd()) { 214 int tag = in.readTag(); 215 assertThat(WireFormat.getTagWireType(tag)).isEqualTo(WireFormat.WIRETYPE_LENGTH_DELIMITED); 216 in.skipField(tag); 217 } 218 } 219 } 220