| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofileotlp // import "go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp" |
| 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 FuzzRequestUnmarshalJSON(f *testing.F) { |
| 16 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 17 |
er := NewExportRequest() |
| 18 |
err := er.UnmarshalJSON(data) |
| 19 |
if err != nil { |
| 20 |
return |
| 21 |
} |
| 22 |
b1, err := er.MarshalJSON() |
| 23 |
require.NoError(t, err, "failed to marshal valid struct") |
| 24 |
|
| 25 |
er = NewExportRequest() |
| 26 |
require.NoError(t, er.UnmarshalJSON(b1), "failed to unmarshal valid bytes") |
| 27 |
b2, err := er.MarshalJSON() |
| 28 |
require.NoError(t, err, "failed to marshal valid struct") |
| 29 |
|
| 30 |
require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2) |
| 31 |
}) |
| 32 |
} |
| 33 |
|
| 34 |
func FuzzResponseUnmarshalJSON(f *testing.F) { |
| 35 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 36 |
er := NewExportResponse() |
| 37 |
err := er.UnmarshalJSON(data) |
| 38 |
if err != nil { |
| 39 |
return |
| 40 |
} |
| 41 |
b1, err := er.MarshalJSON() |
| 42 |
require.NoError(t, err, "failed to marshal valid struct") |
| 43 |
|
| 44 |
er = NewExportResponse() |
| 45 |
require.NoError(t, er.UnmarshalJSON(b1), "failed to unmarshal valid bytes") |
| 46 |
b2, err := er.MarshalJSON() |
| 47 |
require.NoError(t, err, "failed to marshal valid struct") |
| 48 |
|
| 49 |
require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
func FuzzRequestUnmarshalProto(f *testing.F) { |
| 54 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 55 |
er := NewExportRequest() |
| 56 |
err := er.UnmarshalProto(data) |
| 57 |
if err != nil { |
| 58 |
return |
| 59 |
} |
| 60 |
b1, err := er.MarshalProto() |
| 61 |
require.NoError(t, err, "failed to marshal valid struct") |
| 62 |
|
| 63 |
er = NewExportRequest() |
| 64 |
require.NoError(t, er.UnmarshalProto(b1), "failed to unmarshal valid bytes") |
| 65 |
b2, err := er.MarshalProto() |
| 66 |
require.NoError(t, err, "failed to marshal valid struct") |
| 67 |
|
| 68 |
require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2) |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
func FuzzResponseUnmarshalProto(f *testing.F) { |
| 73 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 74 |
er := NewExportResponse() |
| 75 |
err := er.UnmarshalProto(data) |
| 76 |
if err != nil { |
| 77 |
return |
| 78 |
} |
| 79 |
b1, err := er.MarshalProto() |
| 80 |
require.NoError(t, err, "failed to marshal valid struct") |
| 81 |
|
| 82 |
er = NewExportResponse() |
| 83 |
require.NoError(t, er.UnmarshalProto(b1), "failed to unmarshal valid bytes") |
| 84 |
b2, err := er.MarshalProto() |
| 85 |
require.NoError(t, err, "failed to marshal valid struct") |
| 86 |
|
| 87 |
require.True(t, bytes.Equal(b1, b2), "%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2) |
| 88 |
}) |
| 89 |
} |