Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add marshal calls to fuzzers #11119

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions pdata/plog/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,70 @@
package plog // import "go.opentelemetry.io/collector/pdata/plog"

import (
"bytes"
"testing"
)

var (
unexpectedBytes = "expected the same bytes from unmarshaling and marshaling."
)

func FuzzUnmarshalJsonLogs(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
u := &JSONUnmarshaler{}
//nolint: errcheck
_, _ = u.UnmarshalLogs(data)
f.Fuzz(func(t *testing.T, data []byte) {
u1 := &JSONUnmarshaler{}
ld1, err := u1.UnmarshalLogs(data)
if err != nil {
return
}
m1 := &JSONMarshaler{}
b1, err := m1.MarshalLogs(ld1)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

u2 := &JSONUnmarshaler{}
ld2, err := u2.UnmarshalLogs(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
m2 := &JSONMarshaler{}
b2, err := m2.MarshalLogs(ld2)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzUnmarshalPBLogs(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
u := &ProtoUnmarshaler{}
//nolint: errcheck
_, _ = u.UnmarshalLogs(data)
f.Fuzz(func(t *testing.T, data []byte) {
u1 := &ProtoUnmarshaler{}
ld1, err := u1.UnmarshalLogs(data)
if err != nil {
return
}
m1 := &ProtoMarshaler{}
b1, err := m1.MarshalLogs(ld1)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

u2 := &ProtoUnmarshaler{}
ld2, err := u2.UnmarshalLogs(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
m2 := &ProtoMarshaler{}
b2, err := m2.MarshalLogs(ld2)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}
108 changes: 96 additions & 12 deletions pdata/plog/plogotlp/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,121 @@
package plogotlp // import "go.opentelemetry.io/collector/pdata/plog/plogotlp"

import (
"bytes"
"testing"
)

var (
unexpectedBytes = "expected the same bytes from unmarshaling and marshaling."
)

func FuzzRequestUnmarshalJSON(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportRequest()
//nolint: errcheck
er.UnmarshalJSON(data)
err := er.UnmarshalJSON(data)
if err != nil {
return
}
b1, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportRequest()
err = er.UnmarshalJSON(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzResponseUnmarshalJSON(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportResponse()
//nolint: errcheck
er.UnmarshalJSON(data)
err := er.UnmarshalJSON(data)
if err != nil {
return
}
b1, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportResponse()
err = er.UnmarshalJSON(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzRequestUnmarshalProto(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportRequest()
//nolint: errcheck
er.UnmarshalProto(data)
err := er.UnmarshalProto(data)
if err != nil {
return
}
b1, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}
er = NewExportRequest()
err = er.UnmarshalProto(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzResponseUnmarshalProto(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportResponse()
//nolint: errcheck
er.UnmarshalProto(data)
err := er.UnmarshalProto(data)
if err != nil {
return
}
b1, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportResponse()
err = er.UnmarshalProto(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}
35 changes: 31 additions & 4 deletions pdata/pprofile/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,40 @@
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

import (
"bytes"
"testing"
)

var (
unexpectedBytes = "expected the same bytes from unmarshaling and marshaling."
)

func FuzzUnmarshalProfiles(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
u := &JSONUnmarshaler{}
//nolint: errcheck
_, _ = u.UnmarshalProfiles(data)
f.Fuzz(func(t *testing.T, data []byte) {
u1 := &JSONUnmarshaler{}
ld1, err := u1.UnmarshalProfiles(data)
if err != nil {
return
}
m1 := &JSONMarshaler{}
b1, err := m1.MarshalProfiles(ld1)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

u2 := &JSONUnmarshaler{}
ld2, err := u2.UnmarshalProfiles(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
m2 := &JSONMarshaler{}
b2, err := m2.MarshalProfiles(ld2)
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}
109 changes: 97 additions & 12 deletions pdata/pprofile/pprofileotlp/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,122 @@
package pprofileotlp // import "go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp"

import (
"bytes"
"testing"
)

var (
unexpectedBytes = "expected the same bytes from unmarshaling and marshaling."
)

func FuzzRequestUnmarshalJSON(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportRequest()
//nolint: errcheck
er.UnmarshalJSON(data)
err := er.UnmarshalJSON(data)
if err != nil {
return
}
b1, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportRequest()
err = er.UnmarshalJSON(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzResponseUnmarshalJSON(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportResponse()
//nolint: errcheck
er.UnmarshalJSON(data)
err := er.UnmarshalJSON(data)
if err != nil {
return
}
b1, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportResponse()
err = er.UnmarshalJSON(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzRequestUnmarshalProto(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportRequest()
//nolint: errcheck
er.UnmarshalProto(data)
err := er.UnmarshalProto(data)
if err != nil {
return
}
b1, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportRequest()
err = er.UnmarshalProto(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}

func FuzzResponseUnmarshalProto(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte) {
f.Fuzz(func(t *testing.T, data []byte) {
er := NewExportResponse()
//nolint: errcheck
er.UnmarshalProto(data)
err := er.UnmarshalProto(data)
if err != nil {
return
}
b1, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

er = NewExportResponse()
err = er.UnmarshalProto(b1)
if err != nil {
t.Fatalf("failed to unmarshal valid bytes: %v", err)
}
b2, err := er.MarshalProto()
if err != nil {
t.Fatalf("failed to marshal valid struct: %v", err)
}

if !bytes.Equal(b1, b2) {
t.Fatalf("%s. \nexpected %d but got %d\n", unexpectedBytes, b1, b2)
}
})
}
Loading
Loading