Skip to content

Commit

Permalink
Add marshal calls to fuzzers (#11119)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes #

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

Adds both a marshal call and another unmarshal -> marshal round to the
fuzzers to test for more potential bugs.

<!--Please delete paragraphs that you did not use before submitting.-->

Signed-off-by: Adam Korczynski <[email protected]>
  • Loading branch information
AdamKorcz authored Sep 18, 2024
1 parent fbffbb0 commit a0d5966
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 53 deletions.
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

0 comments on commit a0d5966

Please sign in to comment.