Home | History | Annotate | Download | only in protobuf
      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 protobuf_unittest.UnittestProto;
     34 import protobuf_unittest.UnittestProto.ForeignEnum;
     35 import protobuf_unittest.UnittestProto.TestAllExtensions;
     36 import protobuf_unittest.UnittestProto.TestAllTypes;
     37 import protobuf_unittest.UnittestProto.TestEmptyMessage;
     38 import protobuf_unittest.UnittestProto.TestEmptyMessageWithExtensions;
     39 import protobuf_unittest.UnittestProto.TestPackedExtensions;
     40 import protobuf_unittest.UnittestProto.TestPackedTypes;
     41 
     42 import junit.framework.TestCase;
     43 
     44 import java.util.Arrays;
     45 import java.util.Map;
     46 
     47 /**
     48  * Tests related to unknown field handling.
     49  *
     50  * @author kenton (at) google.com (Kenton Varda)
     51  */
     52 public class UnknownFieldSetTest extends TestCase {
     53   @Override
     54   public void setUp() throws Exception {
     55     descriptor = TestAllTypes.getDescriptor();
     56     allFields = TestUtil.getAllSet();
     57     allFieldsData = allFields.toByteString();
     58     emptyMessage = TestEmptyMessage.parseFrom(allFieldsData);
     59     unknownFields = emptyMessage.getUnknownFields();
     60   }
     61 
     62   UnknownFieldSet.Field getField(String name) {
     63     Descriptors.FieldDescriptor field = descriptor.findFieldByName(name);
     64     assertNotNull(field);
     65     return unknownFields.getField(field.getNumber());
     66   }
     67 
     68   // Constructs a protocol buffer which contains fields with all the same
     69   // numbers as allFieldsData except that each field is some other wire
     70   // type.
     71   ByteString getBizarroData() throws Exception {
     72     UnknownFieldSet.Builder bizarroFields = UnknownFieldSet.newBuilder();
     73 
     74     UnknownFieldSet.Field varintField =
     75       UnknownFieldSet.Field.newBuilder().addVarint(1).build();
     76     UnknownFieldSet.Field fixed32Field =
     77       UnknownFieldSet.Field.newBuilder().addFixed32(1).build();
     78 
     79     for (Map.Entry<Integer, UnknownFieldSet.Field> entry :
     80          unknownFields.asMap().entrySet()) {
     81       if (entry.getValue().getVarintList().isEmpty()) {
     82         // Original field is not a varint, so use a varint.
     83         bizarroFields.addField(entry.getKey(), varintField);
     84       } else {
     85         // Original field *is* a varint, so use something else.
     86         bizarroFields.addField(entry.getKey(), fixed32Field);
     87       }
     88     }
     89 
     90     return bizarroFields.build().toByteString();
     91   }
     92 
     93   Descriptors.Descriptor descriptor;
     94   TestAllTypes allFields;
     95   ByteString allFieldsData;
     96 
     97   // An empty message that has been parsed from allFieldsData.  So, it has
     98   // unknown fields of every type.
     99   TestEmptyMessage emptyMessage;
    100   UnknownFieldSet unknownFields;
    101 
    102   // =================================================================
    103 
    104   public void testVarint() throws Exception {
    105     UnknownFieldSet.Field field = getField("optional_int32");
    106     assertEquals(1, field.getVarintList().size());
    107     assertEquals(allFields.getOptionalInt32(),
    108                  (long) field.getVarintList().get(0));
    109   }
    110 
    111   public void testFixed32() throws Exception {
    112     UnknownFieldSet.Field field = getField("optional_fixed32");
    113     assertEquals(1, field.getFixed32List().size());
    114     assertEquals(allFields.getOptionalFixed32(),
    115                  (int) field.getFixed32List().get(0));
    116   }
    117 
    118   public void testFixed64() throws Exception {
    119     UnknownFieldSet.Field field = getField("optional_fixed64");
    120     assertEquals(1, field.getFixed64List().size());
    121     assertEquals(allFields.getOptionalFixed64(),
    122                  (long) field.getFixed64List().get(0));
    123   }
    124 
    125   public void testLengthDelimited() throws Exception {
    126     UnknownFieldSet.Field field = getField("optional_bytes");
    127     assertEquals(1, field.getLengthDelimitedList().size());
    128     assertEquals(allFields.getOptionalBytes(),
    129                  field.getLengthDelimitedList().get(0));
    130   }
    131 
    132   public void testGroup() throws Exception {
    133     Descriptors.FieldDescriptor nestedFieldDescriptor =
    134       TestAllTypes.OptionalGroup.getDescriptor().findFieldByName("a");
    135     assertNotNull(nestedFieldDescriptor);
    136 
    137     UnknownFieldSet.Field field = getField("optionalgroup");
    138     assertEquals(1, field.getGroupList().size());
    139 
    140     UnknownFieldSet group = field.getGroupList().get(0);
    141     assertEquals(1, group.asMap().size());
    142     assertTrue(group.hasField(nestedFieldDescriptor.getNumber()));
    143 
    144     UnknownFieldSet.Field nestedField =
    145       group.getField(nestedFieldDescriptor.getNumber());
    146     assertEquals(1, nestedField.getVarintList().size());
    147     assertEquals(allFields.getOptionalGroup().getA(),
    148                  (long) nestedField.getVarintList().get(0));
    149   }
    150 
    151   public void testSerialize() throws Exception {
    152     // Check that serializing the UnknownFieldSet produces the original data
    153     // again.
    154     ByteString data = emptyMessage.toByteString();
    155     assertEquals(allFieldsData, data);
    156   }
    157 
    158   public void testCopyFrom() throws Exception {
    159     TestEmptyMessage message =
    160       TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).build();
    161 
    162     assertEquals(emptyMessage.toString(), message.toString());
    163   }
    164 
    165   public void testMergeFrom() throws Exception {
    166     TestEmptyMessage source =
    167       TestEmptyMessage.newBuilder()
    168         .setUnknownFields(
    169           UnknownFieldSet.newBuilder()
    170             .addField(2,
    171               UnknownFieldSet.Field.newBuilder()
    172                 .addVarint(2).build())
    173             .addField(3,
    174               UnknownFieldSet.Field.newBuilder()
    175                 .addVarint(4).build())
    176             .build())
    177         .build();
    178     TestEmptyMessage destination =
    179       TestEmptyMessage.newBuilder()
    180         .setUnknownFields(
    181           UnknownFieldSet.newBuilder()
    182             .addField(1,
    183               UnknownFieldSet.Field.newBuilder()
    184                 .addVarint(1).build())
    185             .addField(3,
    186               UnknownFieldSet.Field.newBuilder()
    187                 .addVarint(3).build())
    188             .build())
    189         .mergeFrom(source)
    190         .build();
    191 
    192     assertEquals(
    193       "1: 1\n" +
    194       "2: 2\n" +
    195       "3: 3\n" +
    196       "3: 4\n",
    197       destination.toString());
    198   }
    199 
    200   public void testClear() throws Exception {
    201     UnknownFieldSet fields =
    202       UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clear().build();
    203     assertTrue(fields.asMap().isEmpty());
    204   }
    205 
    206   public void testClearMessage() throws Exception {
    207     TestEmptyMessage message =
    208       TestEmptyMessage.newBuilder().mergeFrom(emptyMessage).clear().build();
    209     assertEquals(0, message.getSerializedSize());
    210   }
    211 
    212   public void testClearField() throws Exception {
    213     int fieldNumber = unknownFields.asMap().keySet().iterator().next();
    214     UnknownFieldSet fields =
    215         UnknownFieldSet.newBuilder().mergeFrom(unknownFields).clearField(fieldNumber).build();
    216     assertFalse(fields.hasField(fieldNumber));
    217   }
    218 
    219   public void testParseKnownAndUnknown() throws Exception {
    220     // Test mixing known and unknown fields when parsing.
    221 
    222     UnknownFieldSet fields =
    223       UnknownFieldSet.newBuilder(unknownFields)
    224         .addField(123456,
    225           UnknownFieldSet.Field.newBuilder().addVarint(654321).build())
    226         .build();
    227 
    228     ByteString data = fields.toByteString();
    229     TestAllTypes destination = TestAllTypes.parseFrom(data);
    230 
    231     TestUtil.assertAllFieldsSet(destination);
    232     assertEquals(1, destination.getUnknownFields().asMap().size());
    233 
    234     UnknownFieldSet.Field field =
    235       destination.getUnknownFields().getField(123456);
    236     assertEquals(1, field.getVarintList().size());
    237     assertEquals(654321, (long) field.getVarintList().get(0));
    238   }
    239 
    240   public void testWrongTypeTreatedAsUnknown() throws Exception {
    241     // Test that fields of the wrong wire type are treated like unknown fields
    242     // when parsing.
    243 
    244     ByteString bizarroData = getBizarroData();
    245     TestAllTypes allTypesMessage = TestAllTypes.parseFrom(bizarroData);
    246     TestEmptyMessage emptyMessage = TestEmptyMessage.parseFrom(bizarroData);
    247 
    248     // All fields should have been interpreted as unknown, so the debug strings
    249     // should be the same.
    250     assertEquals(emptyMessage.toString(), allTypesMessage.toString());
    251   }
    252 
    253   public void testUnknownExtensions() throws Exception {
    254     // Make sure fields are properly parsed to the UnknownFieldSet even when
    255     // they are declared as extension numbers.
    256 
    257     TestEmptyMessageWithExtensions message =
    258       TestEmptyMessageWithExtensions.parseFrom(allFieldsData);
    259 
    260     assertEquals(unknownFields.asMap().size(),
    261                  message.getUnknownFields().asMap().size());
    262     assertEquals(allFieldsData, message.toByteString());
    263   }
    264 
    265   public void testWrongExtensionTypeTreatedAsUnknown() throws Exception {
    266     // Test that fields of the wrong wire type are treated like unknown fields
    267     // when parsing extensions.
    268 
    269     ByteString bizarroData = getBizarroData();
    270     TestAllExtensions allExtensionsMessage =
    271       TestAllExtensions.parseFrom(bizarroData);
    272     TestEmptyMessage emptyMessage = TestEmptyMessage.parseFrom(bizarroData);
    273 
    274     // All fields should have been interpreted as unknown, so the debug strings
    275     // should be the same.
    276     assertEquals(emptyMessage.toString(),
    277                  allExtensionsMessage.toString());
    278   }
    279 
    280   public void testParseUnknownEnumValue() throws Exception {
    281     Descriptors.FieldDescriptor singularField =
    282       TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum");
    283     Descriptors.FieldDescriptor repeatedField =
    284       TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum");
    285     assertNotNull(singularField);
    286     assertNotNull(repeatedField);
    287 
    288     ByteString data =
    289       UnknownFieldSet.newBuilder()
    290         .addField(singularField.getNumber(),
    291           UnknownFieldSet.Field.newBuilder()
    292             .addVarint(TestAllTypes.NestedEnum.BAR.getNumber())
    293             .addVarint(5)   // not valid
    294             .build())
    295         .addField(repeatedField.getNumber(),
    296           UnknownFieldSet.Field.newBuilder()
    297             .addVarint(TestAllTypes.NestedEnum.FOO.getNumber())
    298             .addVarint(4)   // not valid
    299             .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber())
    300             .addVarint(6)   // not valid
    301             .build())
    302         .build()
    303         .toByteString();
    304 
    305     {
    306       TestAllTypes message = TestAllTypes.parseFrom(data);
    307       assertEquals(TestAllTypes.NestedEnum.BAR,
    308                    message.getOptionalNestedEnum());
    309       assertEquals(
    310         Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
    311         message.getRepeatedNestedEnumList());
    312       assertEquals(Arrays.asList(5L),
    313                    message.getUnknownFields()
    314                           .getField(singularField.getNumber())
    315                           .getVarintList());
    316       assertEquals(Arrays.asList(4L, 6L),
    317                    message.getUnknownFields()
    318                           .getField(repeatedField.getNumber())
    319                           .getVarintList());
    320     }
    321 
    322     {
    323       TestAllExtensions message =
    324         TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
    325       assertEquals(TestAllTypes.NestedEnum.BAR,
    326         message.getExtension(UnittestProto.optionalNestedEnumExtension));
    327       assertEquals(
    328         Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
    329         message.getExtension(UnittestProto.repeatedNestedEnumExtension));
    330       assertEquals(Arrays.asList(5L),
    331                    message.getUnknownFields()
    332                           .getField(singularField.getNumber())
    333                           .getVarintList());
    334       assertEquals(Arrays.asList(4L, 6L),
    335                    message.getUnknownFields()
    336                           .getField(repeatedField.getNumber())
    337                           .getVarintList());
    338     }
    339   }
    340 
    341   public void testLargeVarint() throws Exception {
    342     ByteString data =
    343       UnknownFieldSet.newBuilder()
    344         .addField(1,
    345           UnknownFieldSet.Field.newBuilder()
    346             .addVarint(0x7FFFFFFFFFFFFFFFL)
    347             .build())
    348         .build()
    349         .toByteString();
    350     UnknownFieldSet parsed = UnknownFieldSet.parseFrom(data);
    351     UnknownFieldSet.Field field = parsed.getField(1);
    352     assertEquals(1, field.getVarintList().size());
    353     assertEquals(0x7FFFFFFFFFFFFFFFL, (long)field.getVarintList().get(0));
    354   }
    355 
    356   public void testEqualsAndHashCode() {
    357     UnknownFieldSet.Field fixed32Field =
    358         UnknownFieldSet.Field.newBuilder()
    359             .addFixed32(1)
    360             .build();
    361     UnknownFieldSet.Field fixed64Field =
    362         UnknownFieldSet.Field.newBuilder()
    363             .addFixed64(1)
    364             .build();
    365     UnknownFieldSet.Field varIntField =
    366         UnknownFieldSet.Field.newBuilder()
    367             .addVarint(1)
    368             .build();
    369     UnknownFieldSet.Field lengthDelimitedField =
    370         UnknownFieldSet.Field.newBuilder()
    371             .addLengthDelimited(ByteString.EMPTY)
    372             .build();
    373     UnknownFieldSet.Field groupField =
    374         UnknownFieldSet.Field.newBuilder()
    375             .addGroup(unknownFields)
    376             .build();
    377 
    378     UnknownFieldSet a =
    379         UnknownFieldSet.newBuilder()
    380             .addField(1, fixed32Field)
    381             .build();
    382     UnknownFieldSet b =
    383         UnknownFieldSet.newBuilder()
    384             .addField(1, fixed64Field)
    385             .build();
    386     UnknownFieldSet c =
    387         UnknownFieldSet.newBuilder()
    388             .addField(1, varIntField)
    389             .build();
    390     UnknownFieldSet d =
    391         UnknownFieldSet.newBuilder()
    392             .addField(1, lengthDelimitedField)
    393             .build();
    394     UnknownFieldSet e =
    395         UnknownFieldSet.newBuilder()
    396             .addField(1, groupField)
    397             .build();
    398 
    399     checkEqualsIsConsistent(a);
    400     checkEqualsIsConsistent(b);
    401     checkEqualsIsConsistent(c);
    402     checkEqualsIsConsistent(d);
    403     checkEqualsIsConsistent(e);
    404 
    405     checkNotEqual(a, b);
    406     checkNotEqual(a, c);
    407     checkNotEqual(a, d);
    408     checkNotEqual(a, e);
    409     checkNotEqual(b, c);
    410     checkNotEqual(b, d);
    411     checkNotEqual(b, e);
    412     checkNotEqual(c, d);
    413     checkNotEqual(c, e);
    414     checkNotEqual(d, e);
    415   }
    416 
    417   /**
    418    * Asserts that the given field sets are not equal and have different
    419    * hash codes.
    420    *
    421    * @warning It's valid for non-equal objects to have the same hash code, so
    422    *   this test is stricter than it needs to be. However, this should happen
    423    *   relatively rarely.
    424    */
    425   private void checkNotEqual(UnknownFieldSet s1, UnknownFieldSet s2) {
    426     String equalsError = String.format("%s should not be equal to %s", s1, s2);
    427     assertFalse(equalsError, s1.equals(s2));
    428     assertFalse(equalsError, s2.equals(s1));
    429 
    430     assertFalse(
    431         String.format("%s should have a different hash code from %s", s1, s2),
    432         s1.hashCode() == s2.hashCode());
    433   }
    434 
    435   /**
    436    * Asserts that the given field sets are equal and have identical hash codes.
    437    */
    438   private void checkEqualsIsConsistent(UnknownFieldSet set) {
    439     // Object should be equal to itself.
    440     assertEquals(set, set);
    441 
    442     // Object should be equal to a copy of itself.
    443     UnknownFieldSet copy = UnknownFieldSet.newBuilder(set).build();
    444     assertEquals(set, copy);
    445     assertEquals(copy, set);
    446     assertEquals(set.hashCode(), copy.hashCode());
    447   }
    448 
    449   // =================================================================
    450 
    451   public void testSerializeLite() throws Exception {
    452     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    453         UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
    454     assertEquals(allFieldsData.size(), emptyMessageLite.getSerializedSize());
    455     ByteString data = emptyMessageLite.toByteString();
    456     TestAllTypes message = TestAllTypes.parseFrom(data);
    457     TestUtil.assertAllFieldsSet(message);
    458     assertEquals(allFieldsData, data);
    459   }
    460 
    461   public void testAllExtensionsLite() throws Exception {
    462     TestAllExtensions allExtensions = TestUtil.getAllExtensionsSet();
    463     ByteString allExtensionsData = allExtensions.toByteString();
    464     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    465         UnittestLite.TestEmptyMessageLite.parser().parseFrom(allExtensionsData);
    466     ByteString data = emptyMessageLite.toByteString();
    467     TestAllExtensions message =
    468         TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
    469     TestUtil.assertAllExtensionsSet(message);
    470     assertEquals(allExtensionsData, data);
    471   }
    472 
    473   public void testAllPackedFieldsLite() throws Exception {
    474     TestPackedTypes allPackedFields = TestUtil.getPackedSet();
    475     ByteString allPackedData = allPackedFields.toByteString();
    476     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    477         UnittestLite.TestEmptyMessageLite.parseFrom(allPackedData);
    478     ByteString data = emptyMessageLite.toByteString();
    479     TestPackedTypes message =
    480         TestPackedTypes.parseFrom(data, TestUtil.getExtensionRegistry());
    481     TestUtil.assertPackedFieldsSet(message);
    482     assertEquals(allPackedData, data);
    483   }
    484 
    485   public void testAllPackedExtensionsLite() throws Exception {
    486     TestPackedExtensions allPackedExtensions = TestUtil.getPackedExtensionsSet();
    487     ByteString allPackedExtensionsData = allPackedExtensions.toByteString();
    488     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    489         UnittestLite.TestEmptyMessageLite.parseFrom(allPackedExtensionsData);
    490     ByteString data = emptyMessageLite.toByteString();
    491     TestPackedExtensions message =
    492         TestPackedExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
    493     TestUtil.assertPackedExtensionsSet(message);
    494     assertEquals(allPackedExtensionsData, data);
    495   }
    496 
    497   public void testCopyFromLite() throws Exception {
    498     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    499         UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
    500     UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
    501         UnittestLite.TestEmptyMessageLite.newBuilder()
    502         .mergeFrom(emptyMessageLite).build();
    503     assertEquals(emptyMessageLite.toByteString(), emptyMessageLite2.toByteString());
    504   }
    505 
    506   public void testMergeFromLite() throws Exception {
    507     TestAllTypes message1 = TestAllTypes.newBuilder()
    508         .setOptionalInt32(1)
    509         .setOptionalString("foo")
    510         .addRepeatedString("bar")
    511         .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ)
    512         .build();
    513 
    514     TestAllTypes message2 = TestAllTypes.newBuilder()
    515         .setOptionalInt64(2)
    516         .setOptionalString("baz")
    517         .addRepeatedString("qux")
    518         .setOptionalForeignEnum(ForeignEnum.FOREIGN_BAZ)
    519         .build();
    520 
    521     ByteString data1 = message1.toByteString();
    522     UnittestLite.TestEmptyMessageLite emptyMessageLite1 =
    523         UnittestLite.TestEmptyMessageLite.parseFrom(data1);
    524     ByteString data2 = message2.toByteString();
    525     UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
    526         UnittestLite.TestEmptyMessageLite.parseFrom(data2);
    527 
    528     message1 = TestAllTypes.newBuilder(message1).mergeFrom(message2).build();
    529     emptyMessageLite1 = UnittestLite.TestEmptyMessageLite.newBuilder(emptyMessageLite1)
    530                         .mergeFrom(emptyMessageLite2).build();
    531 
    532     data1 = emptyMessageLite1.toByteString();
    533     message2 = TestAllTypes.parseFrom(data1);
    534 
    535     assertEquals(message1, message2);
    536   }
    537 
    538   public void testWrongTypeTreatedAsUnknownLite() throws Exception {
    539     // Test that fields of the wrong wire type are treated like unknown fields
    540     // when parsing.
    541 
    542     ByteString bizarroData = getBizarroData();
    543     TestAllTypes allTypesMessage = TestAllTypes.parseFrom(bizarroData);
    544     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    545         UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData);
    546     ByteString data = emptyMessageLite.toByteString();
    547     TestAllTypes allTypesMessage2 = TestAllTypes.parseFrom(data);
    548 
    549     assertEquals(allTypesMessage.toString(), allTypesMessage2.toString());
    550   }
    551 
    552   public void testUnknownExtensionsLite() throws Exception {
    553     // Make sure fields are properly parsed to the UnknownFieldSet even when
    554     // they are declared as extension numbers.
    555 
    556     UnittestLite.TestEmptyMessageWithExtensionsLite message =
    557       UnittestLite.TestEmptyMessageWithExtensionsLite.parseFrom(allFieldsData);
    558 
    559     assertEquals(allFieldsData, message.toByteString());
    560   }
    561 
    562   public void testWrongExtensionTypeTreatedAsUnknownLite() throws Exception {
    563     // Test that fields of the wrong wire type are treated like unknown fields
    564     // when parsing extensions.
    565 
    566     ByteString bizarroData = getBizarroData();
    567     TestAllExtensions allExtensionsMessage =
    568       TestAllExtensions.parseFrom(bizarroData);
    569     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    570         UnittestLite.TestEmptyMessageLite.parseFrom(bizarroData);
    571 
    572     // All fields should have been interpreted as unknown, so the byte strings
    573     // should be the same.
    574     assertEquals(emptyMessageLite.toByteString(),
    575                  allExtensionsMessage.toByteString());
    576   }
    577 
    578   public void testParseUnknownEnumValueLite() throws Exception {
    579     Descriptors.FieldDescriptor singularField =
    580       TestAllTypes.getDescriptor().findFieldByName("optional_nested_enum");
    581     Descriptors.FieldDescriptor repeatedField =
    582       TestAllTypes.getDescriptor().findFieldByName("repeated_nested_enum");
    583     assertNotNull(singularField);
    584     assertNotNull(repeatedField);
    585 
    586     ByteString data =
    587       UnknownFieldSet.newBuilder()
    588         .addField(singularField.getNumber(),
    589           UnknownFieldSet.Field.newBuilder()
    590             .addVarint(TestAllTypes.NestedEnum.BAR.getNumber())
    591             .addVarint(5)   // not valid
    592             .build())
    593         .addField(repeatedField.getNumber(),
    594           UnknownFieldSet.Field.newBuilder()
    595             .addVarint(TestAllTypes.NestedEnum.FOO.getNumber())
    596             .addVarint(4)   // not valid
    597             .addVarint(TestAllTypes.NestedEnum.BAZ.getNumber())
    598             .addVarint(6)   // not valid
    599             .build())
    600         .build()
    601         .toByteString();
    602 
    603     UnittestLite.TestEmptyMessageLite emptyMessageLite =
    604         UnittestLite.TestEmptyMessageLite.parseFrom(data);
    605     data = emptyMessageLite.toByteString();
    606 
    607     {
    608       TestAllTypes message = TestAllTypes.parseFrom(data);
    609       assertEquals(TestAllTypes.NestedEnum.BAR,
    610                    message.getOptionalNestedEnum());
    611       assertEquals(
    612         Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
    613         message.getRepeatedNestedEnumList());
    614       assertEquals(Arrays.asList(5L),
    615                    message.getUnknownFields()
    616                           .getField(singularField.getNumber())
    617                           .getVarintList());
    618       assertEquals(Arrays.asList(4L, 6L),
    619                    message.getUnknownFields()
    620                           .getField(repeatedField.getNumber())
    621                           .getVarintList());
    622     }
    623 
    624     {
    625       TestAllExtensions message =
    626         TestAllExtensions.parseFrom(data, TestUtil.getExtensionRegistry());
    627       assertEquals(TestAllTypes.NestedEnum.BAR,
    628         message.getExtension(UnittestProto.optionalNestedEnumExtension));
    629       assertEquals(
    630         Arrays.asList(TestAllTypes.NestedEnum.FOO, TestAllTypes.NestedEnum.BAZ),
    631         message.getExtension(UnittestProto.repeatedNestedEnumExtension));
    632       assertEquals(Arrays.asList(5L),
    633                    message.getUnknownFields()
    634                           .getField(singularField.getNumber())
    635                           .getVarintList());
    636       assertEquals(Arrays.asList(4L, 6L),
    637                    message.getUnknownFields()
    638                           .getField(repeatedField.getNumber())
    639                           .getVarintList());
    640     }
    641   }
    642 
    643   public void testClearLite() throws Exception {
    644     UnittestLite.TestEmptyMessageLite emptyMessageLite1 =
    645         UnittestLite.TestEmptyMessageLite.parseFrom(allFieldsData);
    646     UnittestLite.TestEmptyMessageLite emptyMessageLite2 =
    647         UnittestLite.TestEmptyMessageLite.newBuilder()
    648         .mergeFrom(emptyMessageLite1).clear().build();
    649     assertEquals(0, emptyMessageLite2.getSerializedSize());
    650     ByteString data = emptyMessageLite2.toByteString();
    651     assertEquals(0, data.size());
    652   }
    653 
    654 }
    655