Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
Damien Neil | 0232edc | 2020-02-20 10:30:38 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "reflect" |
| 9 | |
| 10 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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" |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 17 | "google.golang.org/protobuf/testing/protopack" |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 18 | |
| 19 | "google.golang.org/protobuf/types/descriptorpb" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | if flags.ProtoLegacy { |
| 24 | testValidMessages = append(testValidMessages, noEnforceUTF8TestProtos...) |
| 25 | } else { |
| 26 | testInvalidMessages = append(testInvalidMessages, noEnforceUTF8TestProtos...) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | var noEnforceUTF8TestProtos = []testProto{ |
| 31 | { |
| 32 | desc: "invalid UTF-8 in optional string field", |
| 33 | decodeTo: []proto.Message{&TestNoEnforceUTF8{ |
| 34 | OptionalString: string("abc\xff"), |
| 35 | }}, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 36 | wire: protopack.Message{ |
| 37 | protopack.Tag{1, protopack.BytesType}, protopack.String("abc\xff"), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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 | }}, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 45 | wire: protopack.Message{ |
| 46 | protopack.Tag{2, protopack.BytesType}, protopack.String("abc\xff"), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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 | }}, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 54 | wire: protopack.Message{ |
| 55 | protopack.Tag{3, protopack.BytesType}, protopack.String("foo"), |
| 56 | protopack.Tag{3, protopack.BytesType}, protopack.String("abc\xff"), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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 | }}, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 64 | wire: protopack.Message{ |
| 65 | protopack.Tag{4, protopack.BytesType}, protopack.String("foo"), |
| 66 | protopack.Tag{4, protopack.BytesType}, protopack.String("abc\xff"), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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 | }, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 74 | wire: protopack.Message{protopack.Tag{5, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 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 | }, |
Joe Tsai | cfd8049 | 2020-02-14 18:13:14 -0800 | [diff] [blame] | 81 | wire: protopack.Message{protopack.Tag{6, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(), |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 82 | }, |
| 83 | } |
| 84 | |
| 85 | type 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 | |
| 93 | type isOneofField interface{ isOneofField() } |
| 94 | |
| 95 | type TestNoEnforceUTF8_OneofString struct { |
| 96 | OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"` |
| 97 | } |
| 98 | type TestNoEnforceUTF8_OneofBytes struct { |
| 99 | OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"` |
| 100 | } |
| 101 | |
| 102 | func (*TestNoEnforceUTF8_OneofString) isOneofField() {} |
| 103 | func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {} |
| 104 | |
| 105 | func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message { |
| 106 | return messageInfo_TestNoEnforceUTF8.MessageOf(m) |
| 107 | } |
| 108 | |
| 109 | var 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 Folger | 7259b46 | 2024-03-18 15:58:47 +0100 | [diff] [blame] | 137 | md.Fields().Get(i).(*filedesc.Field).L1.EditionFeatures.IsUTF8Validated = false |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 138 | } |
| 139 | return md |
| 140 | }(), |
Michael Stapelberg | cbc3dd6 | 2024-05-15 13:11:52 +0200 | [diff] [blame] | 141 | OneofWrappers: []any{ |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 142 | (*TestNoEnforceUTF8_OneofString)(nil), |
| 143 | (*TestNoEnforceUTF8_OneofBytes)(nil), |
| 144 | }, |
| 145 | } |