// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Proto file used for unit testing, a simplified version of
// https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/unittest.proto
//
// LINT: ALLOW_GROUPS, LEGACY_NAMES

syntax = "proto2";

import "unittest_import.proto";

package protobuf_unittest;

option java_outer_classname = "UnittestProto";
option java_package = "protobuf_unittest";

// This proto includes every type of field in both singular and repeated
// forms.
message TestAllTypes {
  message NestedMessage {
    // The field name "b" fails to compile in proto1 because it conflicts with
    // a local variable named "b" in one of the generated methods.  Doh.
    // This file needs to compile in proto1 to test backwards-compatibility.
    optional int32 bb = 1;
  }

  enum NestedEnum {
    FOO = 1;
    BAR = 2;
    BAZ = 3;
    NEG = -1;  // Intentionally negative.
  }

  // Singular
  optional    int32 optional_int32    =  1;
  optional    int64 optional_int64    =  2;
  optional   uint32 optional_uint32   =  3;
  optional   uint64 optional_uint64   =  4;
  optional   sint32 optional_sint32   =  5;
  optional   sint64 optional_sint64   =  6;
  optional  fixed32 optional_fixed32  =  7;
  optional  fixed64 optional_fixed64  =  8;
  optional sfixed32 optional_sfixed32 =  9;
  optional sfixed64 optional_sfixed64 = 10;
  optional    float optional_float    = 11;
  optional   double optional_double   = 12;
  optional     bool optional_bool     = 13;
  optional   string optional_string   = 14;
  optional    bytes optional_bytes    = 15;

  optional group OptionalGroup = 16 {
    optional int32 a = 17;
  }

  optional NestedMessage                        optional_nested_message  = 18;
  optional ForeignMessage                       optional_foreign_message = 19;
  optional protobuf_unittest_import.ImportMessage optional_import_message  = 20;

  optional NestedEnum                           optional_nested_enum     = 21;
  optional ForeignEnum                          optional_foreign_enum    = 22;
  optional protobuf_unittest_import.ImportEnum    optional_import_enum     = 23;

  optional string optional_string_piece = 24 [ctype=STRING_PIECE];
  optional string optional_cord = 25 [ctype=CORD];

  // Defined in unittest_import_public.proto
  optional protobuf_unittest_import.PublicImportMessage
      optional_public_import_message = 26;

  optional NestedMessage optional_lazy_message = 27 [lazy=true];

  // Repeated
  repeated    int32 repeated_int32    = 31;
  repeated    int64 repeated_int64    = 32;
  repeated   uint32 repeated_uint32   = 33;
  repeated   uint64 repeated_uint64   = 34;
  repeated   sint32 repeated_sint32   = 35;
  repeated   sint64 repeated_sint64   = 36;
  repeated  fixed32 repeated_fixed32  = 37;
  repeated  fixed64 repeated_fixed64  = 38;
  repeated sfixed32 repeated_sfixed32 = 39;
  repeated sfixed64 repeated_sfixed64 = 40;
  repeated    float repeated_float    = 41;
  repeated   double repeated_double   = 42;
  repeated     bool repeated_bool     = 43;
  repeated   string repeated_string   = 44;
  repeated    bytes repeated_bytes    = 45;

  repeated group RepeatedGroup = 46 {
    optional int32 a = 47;
  }

  repeated NestedMessage                        repeated_nested_message  = 48;
  repeated ForeignMessage                       repeated_foreign_message = 49;
  repeated protobuf_unittest_import.ImportMessage repeated_import_message  = 50;

  repeated NestedEnum                           repeated_nested_enum     = 51;
  repeated ForeignEnum                          repeated_foreign_enum    = 52;
  repeated protobuf_unittest_import.ImportEnum    repeated_import_enum     = 53;

  repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
  repeated string repeated_cord = 55 [ctype=CORD];

  repeated NestedMessage repeated_lazy_message = 57 ;

  // Singular with defaults
  optional    int32 default_int32    = 61 [default =  41    ];
  optional    int64 default_int64    = 62 [default =  42    ];
  optional   uint32 default_uint32   = 63 [default =  43    ];
  optional   uint64 default_uint64   = 64 [default =  44    ];
  optional   sint32 default_sint32   = 65 [default = -45    ];
  optional   sint64 default_sint64   = 66 [default =  46    ];
  optional  fixed32 default_fixed32  = 67 [default =  47    ];
  optional  fixed64 default_fixed64  = 68 [default =  48    ];
  optional sfixed32 default_sfixed32 = 69 [default =  49    ];
  optional sfixed64 default_sfixed64 = 70 [default = -50    ];
  optional    float default_float    = 71 [default =  51.5  ];
  optional   double default_double   = 72 [default =  52e3  ];
  optional     bool default_bool     = 73 [default = true   ];
  optional   string default_string   = 74 [default = "hello"];
  optional    bytes default_bytes    = 75 [default = "world"];

  optional NestedEnum  default_nested_enum  = 81 [default = BAR        ];
  optional ForeignEnum default_foreign_enum = 82 [default = FOREIGN_BAR];
  optional protobuf_unittest_import.ImportEnum
      default_import_enum = 83 [default = IMPORT_BAR];

  optional string default_string_piece = 84 [ctype=STRING_PIECE,default="abc"];
  optional string default_cord = 85 [ctype=CORD,default="123"];

  // For oneof test
  oneof oneof_field {
    uint32 oneof_uint32 = 111;
    NestedMessage oneof_nested_message = 112;
    string oneof_string = 113;
    bytes oneof_bytes = 114;
  }
}

// Define these after TestAllTypes to make sure the compiler can handle
// that.
message ForeignMessage {
  optional int32 c = 1;
  optional int32 d = 2;
}

enum ForeignEnum {
  FOREIGN_FOO = 4;
  FOREIGN_BAR = 5;
  FOREIGN_BAZ = 6;
}

message TestAllExtensions {
  extensions 1 to max;
}

extend TestAllExtensions {
  // Singular
  optional    int32 optional_int32_extension    =  1;
  optional    int64 optional_int64_extension    =  2;
  optional   uint32 optional_uint32_extension   =  3;
  optional   uint64 optional_uint64_extension   =  4;
  optional   sint32 optional_sint32_extension   =  5;
  optional   sint64 optional_sint64_extension   =  6;
  optional  fixed32 optional_fixed32_extension  =  7;
  optional  fixed64 optional_fixed64_extension  =  8;
  optional sfixed32 optional_sfixed32_extension =  9;
  optional sfixed64 optional_sfixed64_extension = 10;
  optional    float optional_float_extension    = 11;
  optional   double optional_double_extension   = 12;
  optional     bool optional_bool_extension     = 13;
  optional   string optional_string_extension   = 14;
  optional    bytes optional_bytes_extension    = 15;

  optional group OptionalGroup_extension = 16 {
    optional int32 a = 17;
  }

  optional TestAllTypes.NestedMessage optional_nested_message_extension = 18;
  optional ForeignMessage optional_foreign_message_extension = 19;
  optional protobuf_unittest_import.ImportMessage
    optional_import_message_extension = 20;

  optional TestAllTypes.NestedEnum optional_nested_enum_extension = 21;
  optional ForeignEnum optional_foreign_enum_extension = 22;
  optional protobuf_unittest_import.ImportEnum
    optional_import_enum_extension = 23;

  optional string optional_string_piece_extension = 24 [ctype=STRING_PIECE];
  // TODO(b/273349148): ctype=CORD is not supported for extension. Add
  // ctype=CORD option back after it is supported.
  optional string optional_cord_extension = 25;

  optional protobuf_unittest_import.PublicImportMessage
    optional_public_import_message_extension = 26;

  optional TestAllTypes.NestedMessage
    optional_lazy_message_extension = 27 [lazy=true];

  // Repeated
  repeated    int32 repeated_int32_extension    = 31;
  repeated    int64 repeated_int64_extension    = 32;
  repeated   uint32 repeated_uint32_extension   = 33;
  repeated   uint64 repeated_uint64_extension   = 34;
  repeated   sint32 repeated_sint32_extension   = 35;
  repeated   sint64 repeated_sint64_extension   = 36;
  repeated  fixed32 repeated_fixed32_extension  = 37;
  repeated  fixed64 repeated_fixed64_extension  = 38;
  repeated sfixed32 repeated_sfixed32_extension = 39;
  repeated sfixed64 repeated_sfixed64_extension = 40;
  repeated    float repeated_float_extension    = 41;
  repeated   double repeated_double_extension   = 42;
  repeated     bool repeated_bool_extension     = 43;
  repeated   string repeated_string_extension   = 44;
  repeated    bytes repeated_bytes_extension    = 45;

  repeated group RepeatedGroup_extension = 46 {
    optional int32 a = 47;
  }

  repeated TestAllTypes.NestedMessage repeated_nested_message_extension = 48;
  repeated ForeignMessage repeated_foreign_message_extension = 49;
  repeated protobuf_unittest_import.ImportMessage
    repeated_import_message_extension = 50;

  repeated TestAllTypes.NestedEnum repeated_nested_enum_extension = 51;
  repeated ForeignEnum repeated_foreign_enum_extension = 52;
  repeated protobuf_unittest_import.ImportEnum
    repeated_import_enum_extension = 53;

  repeated string repeated_string_piece_extension = 54 [ctype=STRING_PIECE];
  // TODO(b/273349148): ctype=CORD is not supported for extension. Add
  // ctype=CORD option back after it is supported.
  repeated string repeated_cord_extension = 55;

  repeated TestAllTypes.NestedMessage
    repeated_lazy_message_extension = 57 [lazy=true];

  // Singular with defaults
  optional    int32 default_int32_extension    = 61 [default =  41    ];
  optional    int64 default_int64_extension    = 62 [default =  42    ];
  optional   uint32 default_uint32_extension   = 63 [default =  43    ];
  optional   uint64 default_uint64_extension   = 64 [default =  44    ];
  optional   sint32 default_sint32_extension   = 65 [default = -45    ];
  optional   sint64 default_sint64_extension   = 66 [default =  46    ];
  optional  fixed32 default_fixed32_extension  = 67 [default =  47    ];
  optional  fixed64 default_fixed64_extension  = 68 [default =  48    ];
  optional sfixed32 default_sfixed32_extension = 69 [default =  49    ];
  optional sfixed64 default_sfixed64_extension = 70 [default = -50    ];
  optional    float default_float_extension    = 71 [default =  51.5  ];
  optional   double default_double_extension   = 72 [default =  52e3  ];
  optional     bool default_bool_extension     = 73 [default = true   ];
  optional   string default_string_extension   = 74 [default = "hello"];
  optional    bytes default_bytes_extension    = 75 [default = "world"];

  optional TestAllTypes.NestedEnum
    default_nested_enum_extension = 81 [default = BAR];
  optional ForeignEnum
    default_foreign_enum_extension = 82 [default = FOREIGN_BAR];
  optional protobuf_unittest_import.ImportEnum
    default_import_enum_extension = 83 [default = IMPORT_BAR];

  optional string default_string_piece_extension = 84 [ctype=STRING_PIECE,
                                                       default="abc"];
  // TODO(b/273349148): ctype=CORD is not supported for extension. Add
  // ctype=CORD option back after it is supported.
  optional string default_cord_extension = 85 [default="123"];

  // For oneof test
  optional uint32 oneof_uint32_extension = 111;
  optional TestAllTypes.NestedMessage oneof_nested_message_extension = 112;
  optional string oneof_string_extension = 113;
  optional bytes oneof_bytes_extension = 114;
}

message TestGroup {
  optional group OptionalGroup = 16 {
    optional int32 a = 17;
  }
  optional ForeignEnum optional_foreign_enum = 22;
}

message TestGroupExtension {
  extensions 1 to max;
}

message TestNestedExtension {
  extend TestAllExtensions {
    // Check for bug where string extensions declared in tested scope did not
    // compile.
    optional string test = 1002 [default="test"];
    // Used to test if generated extension name is correct when there are
    // underscores.
    optional string nested_string_extension = 1003;
  }

  extend TestGroupExtension {
    optional group OptionalGroup_extension = 16 {
      optional int32 a = 17;
    }
    optional ForeignEnum optional_foreign_enum_extension = 22;
  }
}

message TestChildExtension {
  optional string a = 1;
  optional string b = 2;
  optional TestAllExtensions optional_extension = 3;
}

// We have separate messages for testing required fields because it's
// annoying to have to fill in required fields in TestProto in order to
// do anything with it.  Note that we don't need to test every type of
// required filed because the code output is basically identical to
// optional fields for all types.
message TestRequired {
  required int32 a = 1;
  optional int32 dummy2 = 2;
  required int32 b = 3;

  extend TestAllExtensions {
    optional TestRequired single = 1000;
    repeated TestRequired multi  = 1001;
  }

  // Pad the field count to 32 so that we can test that IsInitialized()
  // properly checks multiple elements of has_bits_.
  optional int32 dummy4  =  4;
  optional int32 dummy5  =  5;
  optional int32 dummy6  =  6;
  optional int32 dummy7  =  7;
  optional int32 dummy8  =  8;
  optional int32 dummy9  =  9;
  optional int32 dummy10 = 10;
  optional int32 dummy11 = 11;
  optional int32 dummy12 = 12;
  optional int32 dummy13 = 13;
  optional int32 dummy14 = 14;
  optional int32 dummy15 = 15;
  optional int32 dummy16 = 16;
  optional int32 dummy17 = 17;
  optional int32 dummy18 = 18;
  optional int32 dummy19 = 19;
  optional int32 dummy20 = 20;
  optional int32 dummy21 = 21;
  optional int32 dummy22 = 22;
  optional int32 dummy23 = 23;
  optional int32 dummy24 = 24;
  optional int32 dummy25 = 25;
  optional int32 dummy26 = 26;
  optional int32 dummy27 = 27;
  optional int32 dummy28 = 28;
  optional int32 dummy29 = 29;
  optional int32 dummy30 = 30;
  optional int32 dummy31 = 31;
  optional int32 dummy32 = 32;

  required int32 c = 33;
}

// TestEmptyMessage is used to test unknown field support.
message TestEmptyMessage {
}

// Like above, but declare all field numbers as potential extensions.  No
// actual extensions should ever be defined for this type.
message TestEmptyMessageWithExtensions {
  extensions 1 to max;
}


// We list fields out of order, to ensure that we're using field number and not
// field index to determine serialization order.
message TestFieldOrderings {
  optional string my_string = 11;
  extensions 2 to 10;
  optional int64 my_int = 1;
  extensions 12 to 100;
  optional float my_float = 101;
  message NestedMessage {
    optional int64 oo = 2;
    // The field name "b" fails to compile in proto1 because it conflicts with
    // a local variable named "b" in one of the generated methods.  Doh.
    // This file needs to compile in proto1 to test backwards-compatibility.
    optional int32 bb = 1;
  }

  optional NestedMessage optional_nested_message  = 200;
}

extend TestFieldOrderings {
  optional string my_extension_string = 50;
  optional int32 my_extension_int = 5;
}

// Test oneofs.
message TestOneofBackwardsCompatible {
  optional int32 foo_int = 1;
  optional string foo_string = 2;
  optional TestAllTypes foo_message = 3;
  optional group FooGroup = 4 {
    optional int32 a = 5;
    optional string b = 6;
  }
}

message TestOneof2 {
  oneof foo {
    int32 foo_int = 1;
    string foo_string = 2;
    string foo_cord = 3 [ctype=CORD];
    string foo_string_piece = 4 [ctype=STRING_PIECE];
    bytes foo_bytes = 5;
    NestedEnum foo_enum = 6;
    NestedMessage foo_message = 7;
    group FooGroup = 8 {
      optional int32 a = 9;
      optional string b = 10;
    }
    NestedMessage foo_lazy_message = 11 [lazy=true];
  }

  oneof bar {
    int32 bar_int = 12 [default = 5];
    string bar_string = 13 [default = "STRING"];
    string bar_cord = 14 [ctype=CORD, default = "CORD"];
    string bar_string_piece = 15 [ctype=STRING_PIECE, default = "SPIECE"];
    bytes bar_bytes = 16 [default = "BYTES"];
    NestedEnum bar_enum = 17 [default = BAR];
  }

  optional int32 baz_int = 18;
  optional string baz_string = 19 [default = "BAZ"];

  message NestedMessage {
    optional int64 qux_int = 1;
    repeated int32 corge_int = 2;
  }

  enum NestedEnum {
    FOO = 1;
    BAR = 2;
    BAZ = 3;
  }
}

// Test messages for packed fields

message TestPackedTypes {
  repeated    int32 packed_int32    =  90 [packed = true];
  repeated    int64 packed_int64    =  91 [packed = true];
  repeated   uint32 packed_uint32   =  92 [packed = true];
  repeated   uint64 packed_uint64   =  93 [packed = true];
  repeated   sint32 packed_sint32   =  94 [packed = true];
  repeated   sint64 packed_sint64   =  95 [packed = true];
  repeated  fixed32 packed_fixed32  =  96 [packed = true];
  repeated  fixed64 packed_fixed64  =  97 [packed = true];
  repeated sfixed32 packed_sfixed32 =  98 [packed = true];
  repeated sfixed64 packed_sfixed64 =  99 [packed = true];
  repeated    float packed_float    = 100 [packed = true];
  repeated   double packed_double   = 101 [packed = true];
  repeated     bool packed_bool     = 102 [packed = true];
  repeated ForeignEnum packed_enum  = 103 [packed = true];
}

// A message with the same fields as TestPackedTypes, but without packing. Used
// to test packed <-> unpacked wire compatibility.
message TestUnpackedTypes {
  repeated    int32 unpacked_int32    =  90 [packed = false];
  repeated    int64 unpacked_int64    =  91 [packed = false];
  repeated   uint32 unpacked_uint32   =  92 [packed = false];
  repeated   uint64 unpacked_uint64   =  93 [packed = false];
  repeated   sint32 unpacked_sint32   =  94 [packed = false];
  repeated   sint64 unpacked_sint64   =  95 [packed = false];
  repeated  fixed32 unpacked_fixed32  =  96 [packed = false];
  repeated  fixed64 unpacked_fixed64  =  97 [packed = false];
  repeated sfixed32 unpacked_sfixed32 =  98 [packed = false];
  repeated sfixed64 unpacked_sfixed64 =  99 [packed = false];
  repeated    float unpacked_float    = 100 [packed = false];
  repeated   double unpacked_double   = 101 [packed = false];
  repeated     bool unpacked_bool     = 102 [packed = false];
  repeated ForeignEnum unpacked_enum  = 103 [packed = false];
}

message TestExtensionInsideTable {
  optional int32 field1 = 1;
  optional int32 field2 = 2;
  optional int32 field3 = 3;
  optional int32 field4 = 4;
  extensions 5 to 5;
  optional int32 field6 = 6;
  optional int32 field7 = 7;
  optional int32 field8 = 8;
  optional int32 field9 = 9;
  optional int32 field10 = 10;
}

extend TestExtensionInsideTable {
  optional int32 test_extension_inside_table_extension = 5;
}

message TestPackedExtensions {
  extensions 1 to max;
}

extend TestPackedExtensions {
  repeated    int32 packed_int32_extension    =  90 [packed = true];
  repeated    int64 packed_int64_extension    =  91 [packed = true];
  repeated   uint32 packed_uint32_extension   =  92 [packed = true];
  repeated   uint64 packed_uint64_extension   =  93 [packed = true];
  repeated   sint32 packed_sint32_extension   =  94 [packed = true];
  repeated   sint64 packed_sint64_extension   =  95 [packed = true];
  repeated  fixed32 packed_fixed32_extension  =  96 [packed = true];
  repeated  fixed64 packed_fixed64_extension  =  97 [packed = true];
  repeated sfixed32 packed_sfixed32_extension =  98 [packed = true];
  repeated sfixed64 packed_sfixed64_extension =  99 [packed = true];
  repeated    float packed_float_extension    = 100 [packed = true];
  repeated   double packed_double_extension   = 101 [packed = true];
  repeated     bool packed_bool_extension     = 102 [packed = true];
  repeated ForeignEnum packed_enum_extension  = 103 [packed = true];
}
