noenforceutf8_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "reflect"
  7. "google.golang.org/protobuf/encoding/prototext"
  8. "google.golang.org/protobuf/internal/filedesc"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/proto"
  11. "google.golang.org/protobuf/reflect/protodesc"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. "google.golang.org/protobuf/runtime/protoimpl"
  14. "google.golang.org/protobuf/testing/protopack"
  15. "google.golang.org/protobuf/types/descriptorpb"
  16. )
  17. func init() {
  18. if flags.ProtoLegacy {
  19. testValidMessages = append(testValidMessages, noEnforceUTF8TestProtos...)
  20. } else {
  21. testInvalidMessages = append(testInvalidMessages, noEnforceUTF8TestProtos...)
  22. }
  23. }
  24. var noEnforceUTF8TestProtos = []testProto{
  25. {
  26. desc: "invalid UTF-8 in optional string field",
  27. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  28. OptionalString: string("abc\xff"),
  29. }},
  30. wire: protopack.Message{
  31. protopack.Tag{1, protopack.BytesType}, protopack.String("abc\xff"),
  32. }.Marshal(),
  33. },
  34. {
  35. desc: "invalid UTF-8 in optional string field of Go bytes",
  36. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  37. OptionalBytes: []byte("abc\xff"),
  38. }},
  39. wire: protopack.Message{
  40. protopack.Tag{2, protopack.BytesType}, protopack.String("abc\xff"),
  41. }.Marshal(),
  42. },
  43. {
  44. desc: "invalid UTF-8 in repeated string field",
  45. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  46. RepeatedString: []string{string("foo"), string("abc\xff")},
  47. }},
  48. wire: protopack.Message{
  49. protopack.Tag{3, protopack.BytesType}, protopack.String("foo"),
  50. protopack.Tag{3, protopack.BytesType}, protopack.String("abc\xff"),
  51. }.Marshal(),
  52. },
  53. {
  54. desc: "invalid UTF-8 in repeated string field of Go bytes",
  55. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  56. RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
  57. }},
  58. wire: protopack.Message{
  59. protopack.Tag{4, protopack.BytesType}, protopack.String("foo"),
  60. protopack.Tag{4, protopack.BytesType}, protopack.String("abc\xff"),
  61. }.Marshal(),
  62. },
  63. {
  64. desc: "invalid UTF-8 in oneof string field",
  65. decodeTo: []proto.Message{
  66. &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
  67. },
  68. wire: protopack.Message{protopack.Tag{5, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
  69. },
  70. {
  71. desc: "invalid UTF-8 in oneof string field of Go bytes",
  72. decodeTo: []proto.Message{
  73. &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
  74. },
  75. wire: protopack.Message{protopack.Tag{6, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
  76. },
  77. }
  78. type TestNoEnforceUTF8 struct {
  79. OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
  80. OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
  81. RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
  82. RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
  83. OneofField isOneofField `protobuf_oneof:"oneof_field"`
  84. }
  85. type isOneofField interface{ isOneofField() }
  86. type TestNoEnforceUTF8_OneofString struct {
  87. OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
  88. }
  89. type TestNoEnforceUTF8_OneofBytes struct {
  90. OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
  91. }
  92. func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
  93. func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
  94. func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
  95. return messageInfo_TestNoEnforceUTF8.MessageOf(m)
  96. }
  97. var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
  98. GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
  99. Desc: func() protoreflect.MessageDescriptor {
  100. pb := new(descriptorpb.FileDescriptorProto)
  101. if err := prototext.Unmarshal([]byte(`
  102. syntax: "proto3"
  103. name: "test.proto"
  104. message_type: [{
  105. name: "TestNoEnforceUTF8"
  106. field: [
  107. {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
  108. {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
  109. {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
  110. {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
  111. {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
  112. {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
  113. ]
  114. oneof_decl: [{name:"oneof_field"}]
  115. }]
  116. `), pb); err != nil {
  117. panic(err)
  118. }
  119. fd, err := protodesc.NewFile(pb, nil)
  120. if err != nil {
  121. panic(err)
  122. }
  123. md := fd.Messages().Get(0)
  124. for i := 0; i < md.Fields().Len(); i++ {
  125. md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
  126. md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
  127. }
  128. return md
  129. }(),
  130. OneofWrappers: []interface{}{
  131. (*TestNoEnforceUTF8_OneofString)(nil),
  132. (*TestNoEnforceUTF8_OneofBytes)(nil),
  133. },
  134. }