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