• 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.UnittestLite.TestAllExtensionsLite;
34 import com.google.protobuf.UnittestLite.TestAllTypesLite;
35 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash;
36 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar;
37 import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo;
38 
39 import junit.framework.TestCase;
40 
41 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 
44 /**
45  * Tests for {@link UnknownFieldSetLite}.
46  *
47  * @author dweis@google.com (Daniel Weis)
48  */
49 public class UnknownFieldSetLiteTest extends TestCase {
50 
testDefaultInstance()51   public void testDefaultInstance() {
52     UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance();
53 
54     assertEquals(0, unknownFields.getSerializedSize());
55     assertEquals(ByteString.EMPTY, toByteString(unknownFields));
56   }
57 
testMergeFieldFrom()58   public void testMergeFieldFrom() throws IOException {
59     Foo foo = Foo.newBuilder()
60       .setValue(2)
61       .build();
62 
63     CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray());
64 
65     UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance();
66     instance.mergeFieldFrom(input.readTag(), input);
67 
68     assertEquals(foo.toByteString(), toByteString(instance));
69   }
70 
testSerializedSize()71   public void testSerializedSize() throws IOException {
72     Foo foo = Foo.newBuilder()
73       .setValue(2)
74       .build();
75 
76     CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray());
77 
78     UnknownFieldSetLite instance = UnknownFieldSetLite.newInstance();
79     instance.mergeFieldFrom(input.readTag(), input);
80 
81     assertEquals(foo.toByteString().size(), instance.getSerializedSize());
82   }
83 
testMergeVarintField()84   public void testMergeVarintField() throws IOException {
85     UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance();
86     unknownFields.mergeVarintField(10, 2);
87 
88     CodedInputStream input =
89         CodedInputStream.newInstance(toByteString(unknownFields).toByteArray());
90 
91     int tag = input.readTag();
92     assertEquals(10, WireFormat.getTagFieldNumber(tag));
93     assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag));
94     assertEquals(2, input.readUInt64());
95     assertTrue(input.isAtEnd());
96   }
97 
testMergeVarintField_negative()98   public void testMergeVarintField_negative() throws IOException {
99     UnknownFieldSetLite builder = UnknownFieldSetLite.newInstance();
100     builder.mergeVarintField(10, -6);
101 
102     CodedInputStream input =
103         CodedInputStream.newInstance(toByteString(builder).toByteArray());
104 
105     int tag = input.readTag();
106     assertEquals(10, WireFormat.getTagFieldNumber(tag));
107     assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag));
108     assertEquals(-6, input.readUInt64());
109     assertTrue(input.isAtEnd());
110   }
111 
testEqualsAndHashCode()112   public void testEqualsAndHashCode() {
113     UnknownFieldSetLite unknownFields1 = UnknownFieldSetLite.newInstance();
114     unknownFields1.mergeVarintField(10, 2);
115 
116     UnknownFieldSetLite unknownFields2 = UnknownFieldSetLite.newInstance();
117     unknownFields2.mergeVarintField(10, 2);
118 
119     assertEquals(unknownFields1, unknownFields2);
120     assertEquals(unknownFields1.hashCode(), unknownFields2.hashCode());
121     assertFalse(unknownFields1.equals(UnknownFieldSetLite.getDefaultInstance()));
122     assertFalse(unknownFields1.hashCode() == UnknownFieldSetLite.getDefaultInstance().hashCode());
123   }
124 
testMutableCopyOf()125   public void testMutableCopyOf() throws IOException {
126     UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance();
127     unknownFields.mergeVarintField(10, 2);
128     unknownFields = UnknownFieldSetLite.mutableCopyOf(unknownFields, unknownFields);
129     unknownFields.checkMutable();
130 
131     CodedInputStream input =
132         CodedInputStream.newInstance(toByteString(unknownFields).toByteArray());
133 
134     int tag = input.readTag();
135     assertEquals(10, WireFormat.getTagFieldNumber(tag));
136     assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag));
137     assertEquals(2, input.readUInt64());
138     assertFalse(input.isAtEnd());
139     input.readTag();
140     assertEquals(10, WireFormat.getTagFieldNumber(tag));
141     assertEquals(WireFormat.WIRETYPE_VARINT, WireFormat.getTagWireType(tag));
142     assertEquals(2, input.readUInt64());
143     assertTrue(input.isAtEnd());
144   }
145 
testMutableCopyOf_empty()146   public void testMutableCopyOf_empty() {
147     UnknownFieldSetLite unknownFields = UnknownFieldSetLite.mutableCopyOf(
148         UnknownFieldSetLite.getDefaultInstance(), UnknownFieldSetLite.getDefaultInstance());
149     unknownFields.checkMutable();
150 
151     assertEquals(0, unknownFields.getSerializedSize());
152     assertEquals(ByteString.EMPTY, toByteString(unknownFields));
153   }
154 
testRoundTrips()155   public void testRoundTrips() throws InvalidProtocolBufferException {
156     Foo foo = Foo.newBuilder()
157         .setValue(1)
158         .setExtension(Bar.fooExt, Bar.newBuilder()
159             .setName("name")
160             .build())
161         .setExtension(LiteEqualsAndHash.varint, 22)
162         .setExtension(LiteEqualsAndHash.fixed32, 44)
163         .setExtension(LiteEqualsAndHash.fixed64, 66L)
164         .setExtension(LiteEqualsAndHash.myGroup, LiteEqualsAndHash.MyGroup.newBuilder()
165             .setGroupValue("value")
166             .build())
167         .build();
168 
169     Foo copy = Foo.parseFrom(foo.toByteArray());
170 
171     assertEquals(foo.getSerializedSize(), copy.getSerializedSize());
172     assertFalse(foo.equals(copy));
173 
174     Foo secondCopy = Foo.parseFrom(foo.toByteArray());
175     assertEquals(copy, secondCopy);
176 
177     ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.newInstance();
178     LiteEqualsAndHash.registerAllExtensions(extensionRegistry);
179     Foo copyOfCopy = Foo.parseFrom(copy.toByteArray(), extensionRegistry);
180 
181     assertEquals(foo, copyOfCopy);
182   }
183 
testMalformedBytes()184   public void testMalformedBytes() throws Exception {
185     try {
186       Foo.parseFrom("this is a malformed protocol buffer".getBytes(Internal.UTF_8));
187       fail();
188     } catch (InvalidProtocolBufferException e) {
189       // Expected.
190     }
191   }
192 
testMissingStartGroupTag()193   public void testMissingStartGroupTag() throws IOException {
194     ByteString.Output byteStringOutput = ByteString.newOutput();
195     CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput);
196     output.writeGroupNoTag(Foo.newBuilder().setValue(11).build());
197     output.writeTag(100, WireFormat.WIRETYPE_END_GROUP);
198     output.flush();
199 
200     try {
201       Foo.parseFrom(byteStringOutput.toByteString());
202       fail();
203     } catch (InvalidProtocolBufferException e) {
204       // Expected.
205     }
206   }
207 
testMissingEndGroupTag()208   public void testMissingEndGroupTag() throws IOException {
209     ByteString.Output byteStringOutput = ByteString.newOutput();
210     CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput);
211     output.writeTag(100, WireFormat.WIRETYPE_START_GROUP);
212     output.writeGroupNoTag(Foo.newBuilder().setValue(11).build());
213     output.flush();
214 
215     try {
216       Foo.parseFrom(byteStringOutput.toByteString());
217       fail();
218     } catch (InvalidProtocolBufferException e) {
219       // Expected.
220     }
221   }
222 
testMismatchingGroupTags()223   public void testMismatchingGroupTags() throws IOException {
224     ByteString.Output byteStringOutput = ByteString.newOutput();
225     CodedOutputStream output = CodedOutputStream.newInstance(byteStringOutput);
226     output.writeTag(100, WireFormat.WIRETYPE_START_GROUP);
227     output.writeGroupNoTag(Foo.newBuilder().setValue(11).build());
228     output.writeTag(101, WireFormat.WIRETYPE_END_GROUP);
229     output.flush();
230 
231     try {
232       Foo.parseFrom(byteStringOutput.toByteString());
233       fail();
234     } catch (InvalidProtocolBufferException e) {
235       // Expected.
236     }
237   }
238 
testTruncatedInput()239   public void testTruncatedInput() {
240     Foo foo = Foo.newBuilder()
241         .setValue(1)
242         .setExtension(Bar.fooExt, Bar.newBuilder()
243             .setName("name")
244             .build())
245         .setExtension(LiteEqualsAndHash.varint, 22)
246         .setExtension(LiteEqualsAndHash.myGroup, LiteEqualsAndHash.MyGroup.newBuilder()
247             .setGroupValue("value")
248             .build())
249         .build();
250 
251     try {
252       Foo.parseFrom(foo.toByteString().substring(0, foo.toByteString().size() - 10));
253       fail();
254     } catch (InvalidProtocolBufferException e) {
255       // Expected.
256     }
257   }
258 
testMakeImmutable()259   public void testMakeImmutable() throws Exception {
260     UnknownFieldSetLite unknownFields = UnknownFieldSetLite.newInstance();
261     unknownFields.makeImmutable();
262 
263     try {
264       unknownFields.mergeVarintField(1, 1);
265       fail();
266     } catch (UnsupportedOperationException expected) {}
267 
268     try {
269       unknownFields.mergeLengthDelimitedField(2, ByteString.copyFromUtf8("hello"));
270       fail();
271     } catch (UnsupportedOperationException expected) {}
272 
273     try {
274       unknownFields.mergeFieldFrom(1, CodedInputStream.newInstance(new byte[0]));
275       fail();
276     } catch (UnsupportedOperationException expected) {}
277   }
278 
testEndToEnd()279   public void testEndToEnd() throws Exception {
280     TestAllTypesLite testAllTypes = TestAllTypesLite.getDefaultInstance();
281     try {
282       testAllTypes.unknownFields.checkMutable();
283       fail();
284     } catch (UnsupportedOperationException expected) {}
285 
286     testAllTypes = TestAllTypesLite.parseFrom(new byte[0]);
287     try {
288       testAllTypes.unknownFields.checkMutable();
289       fail();
290     } catch (UnsupportedOperationException expected) {}
291 
292     testAllTypes = TestAllTypesLite.newBuilder().build();
293     try {
294       testAllTypes.unknownFields.checkMutable();
295       fail();
296     } catch (UnsupportedOperationException expected) {}
297 
298     testAllTypes = TestAllTypesLite.newBuilder()
299         .setDefaultBool(true)
300         .build();
301     try {
302       testAllTypes.unknownFields.checkMutable();
303       fail();
304     } catch (UnsupportedOperationException expected) {}
305 
306     TestAllExtensionsLite testAllExtensions = TestAllExtensionsLite.newBuilder()
307         .mergeFrom(TestAllExtensionsLite.newBuilder()
308             .setExtension(UnittestLite.optionalInt32ExtensionLite, 2)
309             .build().toByteArray())
310         .build();
311     try {
312       testAllExtensions.unknownFields.checkMutable();
313       fail();
314     } catch (UnsupportedOperationException expected) {}
315   }
316 
toByteString(UnknownFieldSetLite unknownFields)317   private ByteString toByteString(UnknownFieldSetLite unknownFields) {
318     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
319     CodedOutputStream output = CodedOutputStream.newInstance(byteArrayOutputStream);
320     try {
321       unknownFields.writeTo(output);
322       output.flush();
323     } catch (IOException e) {
324       throw new RuntimeException(e);
325     }
326     return ByteString.copyFrom(byteArrayOutputStream.toByteArray());
327   }
328 }
329