• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.google.protobuf;
32 
33 import com.google.protobuf.PackedFieldTestProto.TestAllTypes;
34 import com.google.protobuf.PackedFieldTestProto.TestAllTypes.NestedEnum;
35 import com.google.protobuf.PackedFieldTestProto.TestUnpackedTypes;
36 import junit.framework.TestCase;
37 
38 /** Tests primitive repeated fields in proto3 are packed in wire format. */
39 public class PackedFieldTest extends TestCase {
40   static final ByteString expectedPackedRawBytes =
41       ByteString.copyFrom(
42           new byte[] {
43             (byte) 0xFA,
44             0x01,
45             0x01,
46             0x01, // repeated int32
47             (byte) 0x82,
48             0x02,
49             0x01,
50             0x01, // repeated int64
51             (byte) 0x8A,
52             0x02,
53             0x01,
54             0x01, // repeated uint32
55             (byte) 0x92,
56             0x02,
57             0x01,
58             0x01, // repeated uint64
59             (byte) 0x9A,
60             0x02,
61             0x01,
62             0x02, // repeated sint32
63             (byte) 0xA2,
64             0x02,
65             0x01,
66             0x02, // repeated sint64
67             (byte) 0xAA,
68             0x02,
69             0x04,
70             0x01,
71             0x00,
72             0x00,
73             0x00, // repeated fixed32
74             (byte) 0xB2,
75             0x02,
76             0x08,
77             0x01,
78             0x00,
79             0x00,
80             0x00, // repeated fixed64
81             0x00,
82             0x00,
83             0x00,
84             0x00,
85             (byte) 0xBA,
86             0x02,
87             0x04,
88             0x01,
89             0x00,
90             0x00,
91             0x00, // repeated sfixed32
92             (byte) 0xC2,
93             0x02,
94             0x08,
95             0x01,
96             0x00,
97             0x00,
98             0x00, // repeated sfixed64
99             0x00,
100             0x00,
101             0x00,
102             0x00,
103             (byte) 0xCA,
104             0x02,
105             0x04,
106             0x00,
107             0x00,
108             (byte) 0x80,
109             0x3f, // repeated float
110             (byte) 0xD2,
111             0x02,
112             0x08,
113             0x00,
114             0x00,
115             0x00,
116             0x00, // repeated double
117             0x00,
118             0x00,
119             (byte) 0xf0,
120             0x3f,
121             (byte) 0xDA,
122             0x02,
123             0x01,
124             0x01, // repeated bool
125             (byte) 0x9A,
126             0x03,
127             0x01,
128             0x01 // repeated nested enum
129           });
130 
131   static final ByteString expectedUnpackedRawBytes =
132       ByteString.copyFrom(
133           new byte[] {
134             0x08,
135             0x01, // repeated int32
136             0x10,
137             0x01, // repeated int64
138             0x18,
139             0x01, // repeated uint32
140             0x20,
141             0x01, // repeated uint64
142             0x28,
143             0x02, // repeated sint32
144             0x30,
145             0x02, // repeated sint64
146             0x3D,
147             0x01,
148             0x00,
149             0x00,
150             0x00, // repeated fixed32
151             0x41,
152             0x01,
153             0x00,
154             0x00,
155             0x00, // repeated fixed64
156             0x00,
157             0x00,
158             0x00,
159             0x00,
160             0x4D,
161             0x01,
162             0x00,
163             0x00,
164             0x00, // repeated sfixed32
165             0x51,
166             0x01,
167             0x00,
168             0x00,
169             0x00, // repeated sfixed64
170             0x00,
171             0x00,
172             0x00,
173             0x00,
174             0x5D,
175             0x00,
176             0x00,
177             (byte) 0x80,
178             0x3f, // repeated float
179             0x61,
180             0x00,
181             0x00,
182             0x00,
183             0x00, // repeated double
184             0x00,
185             0x00,
186             (byte) 0xf0,
187             0x3f,
188             0x68,
189             0x01, // repeated bool
190             0x70,
191             0x01, // repeated nested enum
192           });
193 
testPackedGeneratedMessage()194   public void testPackedGeneratedMessage() throws Exception {
195     TestAllTypes message = TestAllTypes.parseFrom(expectedPackedRawBytes);
196     assertEquals(expectedPackedRawBytes, message.toByteString());
197   }
198 
testPackedDynamicMessageSerialize()199   public void testPackedDynamicMessageSerialize() throws Exception {
200     DynamicMessage message =
201         DynamicMessage.parseFrom(TestAllTypes.getDescriptor(), expectedPackedRawBytes);
202     assertEquals(expectedPackedRawBytes, message.toByteString());
203   }
204 
testUnpackedGeneratedMessage()205   public void testUnpackedGeneratedMessage() throws Exception {
206     TestUnpackedTypes message = TestUnpackedTypes.parseFrom(expectedUnpackedRawBytes);
207     assertEquals(expectedUnpackedRawBytes, message.toByteString());
208   }
209 
testUnPackedDynamicMessageSerialize()210   public void testUnPackedDynamicMessageSerialize() throws Exception {
211     DynamicMessage message =
212         DynamicMessage.parseFrom(TestUnpackedTypes.getDescriptor(), expectedUnpackedRawBytes);
213     assertEquals(expectedUnpackedRawBytes, message.toByteString());
214   }
215 
216   // Make sure we haven't screwed up the code generation for packing fields by default.
testPackedSerialization()217   public void testPackedSerialization() throws Exception {
218     TestAllTypes message =
219         TestAllTypes.newBuilder()
220             .addRepeatedInt32(1234)
221             .addRepeatedNestedEnum(NestedEnum.BAR)
222             .build();
223 
224     CodedInputStream in = CodedInputStream.newInstance(message.toByteArray());
225 
226     while (!in.isAtEnd()) {
227       int tag = in.readTag();
228       assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag));
229       in.skipField(tag);
230     }
231   }
232 }
233