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
5package proto_test
6
7import (
8	"reflect"
9
10	"google.golang.org/protobuf/encoding/prototext"
11	"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"
17	"google.golang.org/protobuf/testing/protopack"
18
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		}},
36		wire: protopack.Message{
37			protopack.Tag{1, protopack.BytesType}, protopack.String("abc\xff"),
38		}.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		}},
45		wire: protopack.Message{
46			protopack.Tag{2, protopack.BytesType}, protopack.String("abc\xff"),
47		}.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		}},
54		wire: protopack.Message{
55			protopack.Tag{3, protopack.BytesType}, protopack.String("foo"),
56			protopack.Tag{3, protopack.BytesType}, protopack.String("abc\xff"),
57		}.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		}},
64		wire: protopack.Message{
65			protopack.Tag{4, protopack.BytesType}, protopack.String("foo"),
66			protopack.Tag{4, protopack.BytesType}, protopack.String("abc\xff"),
67		}.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		},
74		wire: protopack.Message{protopack.Tag{5, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
75	},
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		},
81		wire: protopack.Message{protopack.Tag{6, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
82	},
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++ {
137			md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
138			md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
139		}
140		return md
141	}(),
142	OneofWrappers: []interface{}{
143		(*TestNoEnforceUTF8_OneofString)(nil),
144		(*TestNoEnforceUTF8_OneofBytes)(nil),
145	},
146}
147