• 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 static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertNull;
36 
37 import com.google.protobuf.testing.Proto2Testing;
38 import com.google.protobuf.testing.Proto2Testing.Proto2Message;
39 import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum;
40 import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions;
41 import java.util.List;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.JUnit4;
46 
47 @RunWith(JUnit4.class)
48 public class Proto2ExtensionLookupSchemaTest {
49   private byte[] data;
50   private ExtensionRegistry extensionRegistry;
51 
52   @Before
setup()53   public void setup() {
54     TestSchemas.registerGenericProto2Schemas();
55 
56     Protobuf.getInstance().schemaFor(Proto2MessageWithExtensions.class);
57     data = new Proto2MessageFactory(10, 20, 1, 1).newMessage().toByteArray();
58     extensionRegistry = ExtensionRegistry.newInstance();
59     Proto2Testing.registerAllExtensions(extensionRegistry);
60   }
61 
62   @Test
testExtensions()63   public void testExtensions() throws Exception {
64     Proto2MessageWithExtensions base =
65         Proto2MessageWithExtensions.parseFrom(data, extensionRegistry);
66 
67     Proto2MessageWithExtensions message =
68         ExperimentalSerializationUtil.fromByteArray(
69             data, Proto2MessageWithExtensions.class, extensionRegistry);
70     assertEquals(base, message);
71 
72     Proto2MessageWithExtensions roundtripMessage =
73         ExperimentalSerializationUtil.fromByteArray(
74             ExperimentalSerializationUtil.toByteArray(message),
75             Proto2MessageWithExtensions.class,
76             extensionRegistry);
77     assertEquals(base, roundtripMessage);
78   }
79 
80   @Test
testUnknownEnum()81   public void testUnknownEnum() throws Exception {
82     // Use unknown fields to hold invalid enum values.
83     UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance();
84     final int outOfRange = 1000;
85     assertNull(TestEnum.forNumber(outOfRange));
86     unknowns.storeField(
87         WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
88         (long) outOfRange);
89     unknowns.storeField(
90         WireFormat.makeTag(
91             Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
92         (long) TestEnum.ONE_VALUE);
93     unknowns.storeField(
94         WireFormat.makeTag(
95             Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
96         (long) outOfRange);
97     unknowns.storeField(
98         WireFormat.makeTag(
99             Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
100         (long) TestEnum.TWO_VALUE);
101 
102     {
103       // Construct a packed enum list.
104       int packedSize =
105           CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE)
106               + CodedOutputStream.computeUInt32SizeNoTag(outOfRange)
107               + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE);
108       ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize);
109       CodedOutputStream packedOut = packedBuilder.getCodedOutput();
110       packedOut.writeEnumNoTag(TestEnum.ONE_VALUE);
111       packedOut.writeEnumNoTag(outOfRange);
112       packedOut.writeEnumNoTag(TestEnum.TWO_VALUE);
113       unknowns.storeField(
114           WireFormat.makeTag(
115               Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER,
116               WireFormat.WIRETYPE_LENGTH_DELIMITED),
117           packedBuilder.build());
118     }
119     int size = unknowns.getSerializedSize();
120     byte[] output = new byte[size];
121     CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
122     unknowns.writeTo(codedOutput);
123     codedOutput.flush();
124 
125     Proto2MessageWithExtensions parsed =
126         ExperimentalSerializationUtil.fromByteArray(
127             output, Proto2MessageWithExtensions.class, extensionRegistry);
128     assertFalse(
129         "out-of-range singular enum should not be in message",
130         parsed.hasExtension(Proto2Testing.fieldEnum13));
131     {
132       List<Long> singularEnum =
133           parsed
134               .getUnknownFields()
135               .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER)
136               .getVarintList();
137       assertEquals(1, singularEnum.size());
138       assertEquals((Long) (long) outOfRange, singularEnum.get(0));
139     }
140     {
141       List<Long> repeatedEnum =
142           parsed
143               .getUnknownFields()
144               .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER)
145               .getVarintList();
146       assertEquals(1, repeatedEnum.size());
147       assertEquals((Long) (long) outOfRange, repeatedEnum.get(0));
148     }
149     {
150       List<Long> packedRepeatedEnum =
151           parsed
152               .getUnknownFields()
153               .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER)
154               .getVarintList();
155       assertEquals(1, packedRepeatedEnum.size());
156       assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0));
157     }
158     assertEquals(
159         "out-of-range repeated enum should not be in message",
160         2,
161         parsed.getExtension(Proto2Testing.fieldEnumList30).size());
162     assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumList30, 0));
163     assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumList30, 1));
164     assertEquals(
165         "out-of-range packed repeated enum should not be in message",
166         2,
167         parsed.getExtension(Proto2Testing.fieldEnumListPacked44).size());
168     assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 0));
169     assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 1));
170   }
171 }
172