Debian Codegraph @

pdata/plog/fuzz_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package plog // import "go.opentelemetry.io/collector/pdata/plog"
5
6 import (
7 "bytes"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 )
12
13 var unexpectedBytes = "expected the same bytes from unmarshaling and marshaling."
14
15 func FuzzUnmarshalJsonLogs(f *testing.F) {
16 f.Fuzz(func(t *testing.T, data []byte) {
17 u1 := &JSONUnmarshaler{}
18 ld1, err := u1.UnmarshalLogs(data)
19 if err != nil {
20 return
21 }
22 m1 := &JSONMarshaler{}
23 b1, err := m1.MarshalLogs(ld1)
24 require.NoError(t, err, "failed to marshal valid struct")
25
26 u2 := &JSONUnmarshaler{}
27 ld2, err := u2.UnmarshalLogs(b1)
28 require.NoError(t, err, "failed to unmarshal valid bytes")
29 m2 := &JSONMarshaler{}
30 b2, err := m2.MarshalLogs(ld2)
31 require.NoError(t, err, "failed to marshal valid struct")
32
33 require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
34 })
35 }
36
37 func FuzzUnmarshalPBLogs(f *testing.F) {
38 f.Fuzz(func(t *testing.T, data []byte) {
39 u1 := &ProtoUnmarshaler{}
40 ld1, err := u1.UnmarshalLogs(data)
41 if err != nil {
42 return
43 }
44 m1 := &ProtoMarshaler{}
45 b1, err := m1.MarshalLogs(ld1)
46 require.NoError(t, err, "failed to marshal valid struct")
47
48 u2 := &ProtoUnmarshaler{}
49 ld2, err := u2.UnmarshalLogs(b1)
50 require.NoError(t, err, "failed to unmarshal valid bytes")
51 m2 := &ProtoMarshaler{}
52 b2, err := m2.MarshalLogs(ld2)
53 require.NoError(t, err, "failed to marshal valid struct")
54
55 require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
56 })
57 }