MCPcopy
hub / github.com/gogo/protobuf / TestInvalidUTF8

Function TestInvalidUTF8

proto/all_test.go:2255–2327  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2253}
2254
2255func TestInvalidUTF8(t *testing.T) {
2256 const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"
2257 tests := []struct {
2258 label string
2259 proto2 Message
2260 proto3 Message
2261 want []byte
2262 }{{
2263 label: "Scalar",
2264 proto2: &TestUTF8{Scalar: String(invalidUTF8)},
2265 proto3: &pb3.TestUTF8{Scalar: invalidUTF8},
2266 want: []byte{0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2267 }, {
2268 label: "Vector",
2269 proto2: &TestUTF8{Vector: []string{invalidUTF8}},
2270 proto3: &pb3.TestUTF8{Vector: []string{invalidUTF8}},
2271 want: []byte{0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2272 }, {
2273 label: "Oneof",
2274 proto2: &TestUTF8{Oneof: &TestUTF8_Field{Field: invalidUTF8}},
2275 proto3: &pb3.TestUTF8{Oneof: &pb3.TestUTF8_Field{Field: invalidUTF8}},
2276 want: []byte{0x1a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2277 }, {
2278 label: "MapKey",
2279 proto2: &TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2280 proto3: &pb3.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2281 want: []byte{0x22, 0x0b, 0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff, 0x10, 0x00},
2282 }, {
2283 label: "MapValue",
2284 proto2: &TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2285 proto3: &pb3.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2286 want: []byte{0x2a, 0x0b, 0x08, 0x00, 0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2287 }}
2288
2289 for _, tt := range tests {
2290 // Proto2 should not validate UTF-8.
2291 b, err := Marshal(tt.proto2)
2292 if err != nil {
2293 t.Errorf("Marshal(proto2.%s) = %v, want nil", tt.label, err)
2294 }
2295 if !bytes.Equal(b, tt.want) {
2296 t.Errorf("Marshal(proto2.%s) = %x, want %x", tt.label, b, tt.want)
2297 }
2298
2299 m := Clone(tt.proto2)
2300 m.Reset()
2301 if err = Unmarshal(tt.want, m); err != nil {
2302 t.Errorf("Unmarshal(proto2.%s) = %v, want nil", tt.label, err)
2303 }
2304 if !Equal(m, tt.proto2) {
2305 t.Errorf("proto2.%s: output mismatch:\ngot %v\nwant %v", tt.label, m, tt.proto2)
2306 }
2307
2308 // Proto3 should validate UTF-8.
2309 b, err = Marshal(tt.proto3)
2310 if err == nil {
2311 t.Errorf("Marshal(proto3.%s) = %v, want non-nil", tt.label, err)
2312 }

Callers

nothing calls this directly

Calls 7

StringFunction · 0.85
MarshalFunction · 0.85
CloneFunction · 0.85
EqualFunction · 0.85
UnmarshalFunction · 0.70
EqualMethod · 0.65
ResetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…