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 static org.junit.Assert.assertEquals;
34 
35 import protobuf_unittest.UnittestProto;
36 import proto3_unittest.UnittestProto3;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.JUnit4;
40 
41 /** Unit tests for discard or preserve unknown fields. */
42 @RunWith(JUnit4.class)
43 public class DiscardUnknownFieldsTest {
44   @Test
testProto2()45   public void testProto2() throws Exception {
46     testProto2Message(UnittestProto.TestEmptyMessage.getDefaultInstance());
47     testProto2Message(UnittestProto.TestEmptyMessageWithExtensions.getDefaultInstance());
48     testProto2Message(
49         DynamicMessage.getDefaultInstance(UnittestProto.TestEmptyMessage.getDescriptor()));
50     testProto2Message(
51         DynamicMessage.getDefaultInstance(
52             UnittestProto.TestEmptyMessageWithExtensions.getDescriptor()));
53   }
54 
55   @Test
testProto3()56   public void testProto3() throws Exception {
57     testProto3Message(UnittestProto3.TestEmptyMessage.getDefaultInstance());
58     testProto3Message(
59         DynamicMessage.getDefaultInstance(UnittestProto3.TestEmptyMessage.getDescriptor()));
60   }
61 
testProto2Message(Message message)62   private static void testProto2Message(Message message) throws Exception {
63     assertUnknownFieldsPreserved(message);
64     assertUnknownFieldsExplicitlyDiscarded(message);
65     assertReuseCodedInputStreamPreserve(message);
66     assertUnknownFieldsInUnknownFieldSetArePreserve(message);
67   }
68 
testProto3Message(Message message)69   private static void testProto3Message(Message message) throws Exception {
70     assertUnknownFieldsPreserved(message);
71     assertUnknownFieldsExplicitlyDiscarded(message);
72     assertReuseCodedInputStreamPreserve(message);
73     assertUnknownFieldsInUnknownFieldSetArePreserve(message);
74   }
75 
assertReuseCodedInputStreamPreserve(Message message)76   private static void assertReuseCodedInputStreamPreserve(Message message) throws Exception {
77     final int messageSize = payload.size();
78     byte[] copied = new byte[messageSize * 2];
79     payload.copyTo(copied, 0);
80     payload.copyTo(copied, messageSize);
81     CodedInputStream input = CodedInputStream.newInstance(copied);
82 
83     // Use DiscardUnknownFieldsParser to parse the first payload.
84     int oldLimit = input.pushLimit(messageSize);
85     Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(input);
86     assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize());
87     input.popLimit(oldLimit);
88 
89     // Use the normal parser to parse the remaining payload should have unknown fields preserved.
90     parsed = message.getParserForType().parseFrom(input);
91     assertEquals(message.getClass().getName(), payload, parsed.toByteString());
92   }
93 
94   /**
95    * {@link Message.Builder#setUnknownFields(UnknownFieldSet)} and {@link
96    * Message.Builder#mergeUnknownFields(UnknownFieldSet)} should preserve the unknown fields.
97    */
assertUnknownFieldsInUnknownFieldSetArePreserve(Message message)98   private static void assertUnknownFieldsInUnknownFieldSetArePreserve(Message message)
99       throws Exception {
100     UnknownFieldSet unknownFields = UnknownFieldSet.newBuilder().mergeFrom(payload).build();
101     Message built = message.newBuilderForType().setUnknownFields(unknownFields).build();
102     assertEquals(message.getClass().getName(), payload, built.toByteString());
103   }
104 
assertUnknownFieldsPreserved(MessageLite message)105   private static void assertUnknownFieldsPreserved(MessageLite message) throws Exception {
106     MessageLite parsed = message.getParserForType().parseFrom(payload);
107     assertEquals(message.getClass().getName(), payload, parsed.toByteString());
108 
109     parsed = message.newBuilderForType().mergeFrom(payload).build();
110     assertEquals(message.getClass().getName(), payload, parsed.toByteString());
111   }
112 
assertUnknownFieldsExplicitlyDiscarded(Message message)113   private static void assertUnknownFieldsExplicitlyDiscarded(Message message) throws Exception {
114     Message parsed = DiscardUnknownFieldsParser.wrap(message.getParserForType()).parseFrom(payload);
115     assertEquals(message.getClass().getName(), 0, parsed.getSerializedSize());
116   }
117 
118   private static final ByteString payload =
119       TestUtilLite.getAllLiteSetBuilder().build().toByteString();
120 }
121