From 852a696f40c0d50e058632cf0ecdbb6fd68717fc Mon Sep 17 00:00:00 2001 From: AdamKorcz <44787359+AdamKorcz@users.noreply.github.com> Date: Fri, 26 Jul 2024 09:31:09 +0100 Subject: [PATCH] Add fuzz tests for unmarshaling routines (#10697) #### Description Adds fuzz tests for various unmarshaling routines. This can be tested with `go test -fuzz=` This is ongoing work for https://github.com/open-telemetry/sig-security/issues/55 #### Link to tracking issue Fixes # #### Testing #### Documentation Signed-off-by: Adam Korczynski --- pdata/plog/fuzz_test.go | 24 ++++++++++++++++ pdata/plog/plogotlp/fuzz_test.go | 40 ++++++++++++++++++++++++++ pdata/pmetric/fuzz_test.go | 16 +++++++++++ pdata/pmetric/pmetricotlp/fuzz_test.go | 40 ++++++++++++++++++++++++++ pdata/ptrace/fuzz_test.go | 16 +++++++++++ pdata/ptrace/ptraceotlp/fuzz_test.go | 40 ++++++++++++++++++++++++++ 6 files changed, 176 insertions(+) create mode 100644 pdata/plog/fuzz_test.go create mode 100644 pdata/plog/plogotlp/fuzz_test.go create mode 100644 pdata/pmetric/fuzz_test.go create mode 100644 pdata/pmetric/pmetricotlp/fuzz_test.go create mode 100644 pdata/ptrace/fuzz_test.go create mode 100644 pdata/ptrace/ptraceotlp/fuzz_test.go diff --git a/pdata/plog/fuzz_test.go b/pdata/plog/fuzz_test.go new file mode 100644 index 00000000000..aed808a2ef4 --- /dev/null +++ b/pdata/plog/fuzz_test.go @@ -0,0 +1,24 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package plog // import "go.opentelemetry.io/collector/pdata/plog" + +import ( + "testing" +) + +func FuzzUnmarshalJsonLogs(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + u := &JSONUnmarshaler{} + //nolint: errcheck + _, _ = u.UnmarshalLogs(data) + }) +} + +func FuzzUnmarshalPBLogs(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + u := &ProtoUnmarshaler{} + //nolint: errcheck + _, _ = u.UnmarshalLogs(data) + }) +} diff --git a/pdata/plog/plogotlp/fuzz_test.go b/pdata/plog/plogotlp/fuzz_test.go new file mode 100644 index 00000000000..af73d07e970 --- /dev/null +++ b/pdata/plog/plogotlp/fuzz_test.go @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package plogotlp // import "go.opentelemetry.io/collector/pdata/plog/plogotlp" + +import ( + "testing" +) + +func FuzzRequestUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzResponseUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzRequestUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalProto(data) + }) +} + +func FuzzResponseUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalProto(data) + }) +} diff --git a/pdata/pmetric/fuzz_test.go b/pdata/pmetric/fuzz_test.go new file mode 100644 index 00000000000..f6e5f3a043b --- /dev/null +++ b/pdata/pmetric/fuzz_test.go @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" + +import ( + "testing" +) + +func FuzzUnmarshalMetrics(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + u := &JSONUnmarshaler{} + //nolint: errcheck + _, _ = u.UnmarshalMetrics(data) + }) +} diff --git a/pdata/pmetric/pmetricotlp/fuzz_test.go b/pdata/pmetric/pmetricotlp/fuzz_test.go new file mode 100644 index 00000000000..137b98b61b2 --- /dev/null +++ b/pdata/pmetric/pmetricotlp/fuzz_test.go @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package pmetricotlp // import "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" + +import ( + "testing" +) + +func FuzzRequestUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzResponseUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzRequestUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalProto(data) + }) +} + +func FuzzResponseUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalProto(data) + }) +} diff --git a/pdata/ptrace/fuzz_test.go b/pdata/ptrace/fuzz_test.go new file mode 100644 index 00000000000..7047c781eff --- /dev/null +++ b/pdata/ptrace/fuzz_test.go @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" + +import ( + "testing" +) + +func FuzzUnmarshalTraces(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + u := &JSONUnmarshaler{} + //nolint: errcheck + _, _ = u.UnmarshalTraces(data) + }) +} diff --git a/pdata/ptrace/ptraceotlp/fuzz_test.go b/pdata/ptrace/ptraceotlp/fuzz_test.go new file mode 100644 index 00000000000..473eb39b2cd --- /dev/null +++ b/pdata/ptrace/ptraceotlp/fuzz_test.go @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ptraceotlp // import "go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" + +import ( + "testing" +) + +func FuzzRequestUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzResponseUnmarshalJSON(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalJSON(data) + }) +} + +func FuzzRequestUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportRequest() + //nolint: errcheck + er.UnmarshalProto(data) + }) +} + +func FuzzResponseUnmarshalProto(f *testing.F) { + f.Fuzz(func(_ *testing.T, data []byte) { + er := NewExportResponse() + //nolint: errcheck + er.UnmarshalProto(data) + }) +}