unittest_proto3.proto 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // A proto file we will use for unit testing.
  35. syntax = "proto3";
  36. // Some generic_services option(s) added automatically.
  37. // See: http://go/proto2-generic-services-default
  38. option cc_generic_services = true; // auto-added
  39. option java_generic_services = true; // auto-added
  40. option py_generic_services = true; // auto-added
  41. option cc_enable_arenas = true;
  42. option csharp_namespace = "Google.Protobuf.TestProtos";
  43. import "google/protobuf/unittest_import_proto3.proto";
  44. // We don't put this in a package within proto2 because we need to make sure
  45. // that the generated code doesn't depend on being in the proto2 namespace.
  46. // In test_util.h we do "using namespace unittest = protobuf_unittest".
  47. package protobuf_unittest;
  48. // Protos optimized for SPEED use a strict superset of the generated code
  49. // of equivalent ones optimized for CODE_SIZE, so we should optimize all our
  50. // tests for speed unless explicitly testing code size optimization.
  51. option optimize_for = SPEED;
  52. option java_outer_classname = "UnittestProto";
  53. // Disambiguate generated code from unittest.proto
  54. option swift_prefix = "Proto3";
  55. // This proto includes every type of field in both singular and repeated
  56. // forms.
  57. message TestAllTypes {
  58. message NestedMessage {
  59. // The field name "b" fails to compile in proto1 because it conflicts with
  60. // a local variable named "b" in one of the generated methods. Doh.
  61. // This file needs to compile in proto1 to test backwards-compatibility.
  62. int32 bb = 1;
  63. }
  64. enum NestedEnum {
  65. NESTED_ENUM_UNSPECIFIED = 0;
  66. FOO = 1;
  67. BAR = 2;
  68. BAZ = 3;
  69. NEG = -1; // Intentionally negative.
  70. }
  71. // Singular
  72. int32 single_int32 = 1;
  73. int64 single_int64 = 2;
  74. uint32 single_uint32 = 3;
  75. uint64 single_uint64 = 4;
  76. sint32 single_sint32 = 5;
  77. sint64 single_sint64 = 6;
  78. fixed32 single_fixed32 = 7;
  79. fixed64 single_fixed64 = 8;
  80. sfixed32 single_sfixed32 = 9;
  81. sfixed64 single_sfixed64 = 10;
  82. float single_float = 11;
  83. double single_double = 12;
  84. bool single_bool = 13;
  85. string single_string = 14;
  86. bytes single_bytes = 15;
  87. NestedMessage single_nested_message = 18;
  88. ForeignMessage single_foreign_message = 19;
  89. protobuf_unittest_import.ImportMessage single_import_message = 20;
  90. NestedEnum single_nested_enum = 21;
  91. ForeignEnum single_foreign_enum = 22;
  92. protobuf_unittest_import.ImportEnum single_import_enum = 23;
  93. // Defined in unittest_import_public.proto
  94. protobuf_unittest_import.PublicImportMessage
  95. single_public_import_message = 26;
  96. // Repeated
  97. repeated int32 repeated_int32 = 31;
  98. repeated int64 repeated_int64 = 32;
  99. repeated uint32 repeated_uint32 = 33;
  100. repeated uint64 repeated_uint64 = 34;
  101. repeated sint32 repeated_sint32 = 35;
  102. repeated sint64 repeated_sint64 = 36;
  103. repeated fixed32 repeated_fixed32 = 37;
  104. repeated fixed64 repeated_fixed64 = 38;
  105. repeated sfixed32 repeated_sfixed32 = 39;
  106. repeated sfixed64 repeated_sfixed64 = 40;
  107. repeated float repeated_float = 41;
  108. repeated double repeated_double = 42;
  109. repeated bool repeated_bool = 43;
  110. repeated string repeated_string = 44;
  111. repeated bytes repeated_bytes = 45;
  112. repeated NestedMessage repeated_nested_message = 48;
  113. repeated ForeignMessage repeated_foreign_message = 49;
  114. repeated protobuf_unittest_import.ImportMessage repeated_import_message = 50;
  115. repeated NestedEnum repeated_nested_enum = 51;
  116. repeated ForeignEnum repeated_foreign_enum = 52;
  117. repeated protobuf_unittest_import.ImportEnum repeated_import_enum = 53;
  118. // Defined in unittest_import_public.proto
  119. repeated protobuf_unittest_import.PublicImportMessage
  120. repeated_public_import_message = 54;
  121. // For oneof test
  122. oneof oneof_field {
  123. uint32 oneof_uint32 = 111;
  124. NestedMessage oneof_nested_message = 112;
  125. string oneof_string = 113;
  126. bytes oneof_bytes = 114;
  127. }
  128. }
  129. // This proto includes a recusively nested message.
  130. message NestedTestAllTypes {
  131. NestedTestAllTypes child = 1;
  132. TestAllTypes payload = 2;
  133. repeated NestedTestAllTypes repeated_child = 3;
  134. }
  135. message TestDeprecatedFields {
  136. int32 deprecated_int32 = 1 [deprecated=true];
  137. }
  138. // Define these after TestAllTypes to make sure the compiler can handle
  139. // that.
  140. message ForeignMessage {
  141. int32 c = 1;
  142. }
  143. enum ForeignEnum {
  144. FOREIGN_UNSPECIFIED = 0;
  145. FOREIGN_FOO = 4;
  146. FOREIGN_BAR = 5;
  147. FOREIGN_BAZ = 6;
  148. }
  149. message TestReservedFields {
  150. reserved 2, 15, 9 to 11;
  151. reserved "bar", "baz";
  152. }
  153. // Test that we can use NestedMessage from outside TestAllTypes.
  154. message TestForeignNested {
  155. TestAllTypes.NestedMessage foreign_nested = 1;
  156. }
  157. // Test that really large tag numbers don't break anything.
  158. message TestReallyLargeTagNumber {
  159. // The largest possible tag number is 2^28 - 1, since the wire format uses
  160. // three bits to communicate wire type.
  161. int32 a = 1;
  162. int32 bb = 268435455;
  163. }
  164. message TestRecursiveMessage {
  165. TestRecursiveMessage a = 1;
  166. int32 i = 2;
  167. }
  168. // Test that mutual recursion works.
  169. message TestMutualRecursionA {
  170. TestMutualRecursionB bb = 1;
  171. }
  172. message TestMutualRecursionB {
  173. TestMutualRecursionA a = 1;
  174. int32 optional_int32 = 2;
  175. }
  176. // Test an enum that has multiple values with the same number.
  177. enum TestEnumWithDupValue {
  178. TEST_ENUM_WITH_DUP_VALUE_UNSPECIFIED = 0;
  179. option allow_alias = true;
  180. FOO1 = 1;
  181. BAR1 = 2;
  182. BAZ = 3;
  183. FOO2 = 1;
  184. BAR2 = 2;
  185. }
  186. // Test an enum with large, unordered values.
  187. enum TestSparseEnum {
  188. TEST_SPARSE_ENUM_UNSPECIFIED = 0;
  189. SPARSE_A = 123;
  190. SPARSE_B = 62374;
  191. SPARSE_C = 12589234;
  192. SPARSE_D = -15;
  193. SPARSE_E = -53452;
  194. // In proto3, value 0 must be the first one specified
  195. // SPARSE_F = 0;
  196. SPARSE_G = 2;
  197. }
  198. // Test message with CamelCase field names. This violates Protocol Buffer
  199. // standard style.
  200. message TestCamelCaseFieldNames {
  201. int32 PrimitiveField = 1;
  202. string StringField = 2;
  203. ForeignEnum EnumField = 3;
  204. ForeignMessage MessageField = 4;
  205. repeated int32 RepeatedPrimitiveField = 7;
  206. repeated string RepeatedStringField = 8;
  207. repeated ForeignEnum RepeatedEnumField = 9;
  208. repeated ForeignMessage RepeatedMessageField = 10;
  209. }
  210. // We list fields out of order, to ensure that we're using field number and not
  211. // field index to determine serialization order.
  212. message TestFieldOrderings {
  213. string my_string = 11;
  214. int64 my_int = 1;
  215. float my_float = 101;
  216. message NestedMessage {
  217. int64 oo = 2;
  218. // The field name "b" fails to compile in proto1 because it conflicts with
  219. // a local variable named "b" in one of the generated methods. Doh.
  220. // This file needs to compile in proto1 to test backwards-compatibility.
  221. int32 bb = 1;
  222. }
  223. NestedMessage single_nested_message = 200;
  224. }
  225. message SparseEnumMessage {
  226. TestSparseEnum sparse_enum = 1;
  227. }
  228. // Test String and Bytes: string is for valid UTF-8 strings
  229. message OneString {
  230. string data = 1;
  231. }
  232. message MoreString {
  233. repeated string data = 1;
  234. }
  235. message OneBytes {
  236. bytes data = 1;
  237. }
  238. message MoreBytes {
  239. bytes data = 1;
  240. }
  241. // Test int32, uint32, int64, uint64, and bool are all compatible
  242. message Int32Message {
  243. int32 data = 1;
  244. }
  245. message Uint32Message {
  246. uint32 data = 1;
  247. }
  248. message Int64Message {
  249. int64 data = 1;
  250. }
  251. message Uint64Message {
  252. uint64 data = 1;
  253. }
  254. message BoolMessage {
  255. bool data = 1;
  256. }
  257. // Test oneofs.
  258. message TestOneof {
  259. oneof foo {
  260. int32 foo_int = 1;
  261. string foo_string = 2;
  262. TestAllTypes foo_message = 3;
  263. }
  264. }
  265. // Test messages for packed fields
  266. message TestPackedTypes {
  267. repeated int32 packed_int32 = 90 [packed = true];
  268. repeated int64 packed_int64 = 91 [packed = true];
  269. repeated uint32 packed_uint32 = 92 [packed = true];
  270. repeated uint64 packed_uint64 = 93 [packed = true];
  271. repeated sint32 packed_sint32 = 94 [packed = true];
  272. repeated sint64 packed_sint64 = 95 [packed = true];
  273. repeated fixed32 packed_fixed32 = 96 [packed = true];
  274. repeated fixed64 packed_fixed64 = 97 [packed = true];
  275. repeated sfixed32 packed_sfixed32 = 98 [packed = true];
  276. repeated sfixed64 packed_sfixed64 = 99 [packed = true];
  277. repeated float packed_float = 100 [packed = true];
  278. repeated double packed_double = 101 [packed = true];
  279. repeated bool packed_bool = 102 [packed = true];
  280. repeated ForeignEnum packed_enum = 103 [packed = true];
  281. }
  282. // A message with the same fields as TestPackedTypes, but without packing. Used
  283. // to test packed <-> unpacked wire compatibility.
  284. message TestUnpackedTypes {
  285. repeated int32 unpacked_int32 = 90 [packed = false];
  286. repeated int64 unpacked_int64 = 91 [packed = false];
  287. repeated uint32 unpacked_uint32 = 92 [packed = false];
  288. repeated uint64 unpacked_uint64 = 93 [packed = false];
  289. repeated sint32 unpacked_sint32 = 94 [packed = false];
  290. repeated sint64 unpacked_sint64 = 95 [packed = false];
  291. repeated fixed32 unpacked_fixed32 = 96 [packed = false];
  292. repeated fixed64 unpacked_fixed64 = 97 [packed = false];
  293. repeated sfixed32 unpacked_sfixed32 = 98 [packed = false];
  294. repeated sfixed64 unpacked_sfixed64 = 99 [packed = false];
  295. repeated float unpacked_float = 100 [packed = false];
  296. repeated double unpacked_double = 101 [packed = false];
  297. repeated bool unpacked_bool = 102 [packed = false];
  298. repeated ForeignEnum unpacked_enum = 103 [packed = false];
  299. }
  300. message TestRepeatedScalarDifferentTagSizes {
  301. // Parsing repeated fixed size values used to fail. This message needs to be
  302. // used in order to get a tag of the right size; all of the repeated fields
  303. // in TestAllTypes didn't trigger the check.
  304. repeated fixed32 repeated_fixed32 = 12;
  305. // Check for a varint type, just for good measure.
  306. repeated int32 repeated_int32 = 13;
  307. // These have two-byte tags.
  308. repeated fixed64 repeated_fixed64 = 2046;
  309. repeated int64 repeated_int64 = 2047;
  310. // Three byte tags.
  311. repeated float repeated_float = 262142;
  312. repeated uint64 repeated_uint64 = 262143;
  313. }
  314. message TestCommentInjectionMessage {
  315. // */ <- This should not close the generated doc comment
  316. string a = 1;
  317. }
  318. // Test that RPC services work.
  319. message FooRequest {}
  320. message FooResponse {}
  321. message FooClientMessage {}
  322. message FooServerMessage{}
  323. service TestService {
  324. rpc Foo(FooRequest) returns (FooResponse);
  325. rpc Bar(BarRequest) returns (BarResponse);
  326. }
  327. message BarRequest {}
  328. message BarResponse {}