Skip to content

Commit

Permalink
Add marshal calls to fuzzers
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Korczynski <adam@adalogics.com>
  • Loading branch information
AdamKorcz committed Sep 18, 2024
1 parent 59c083f commit 6701a45
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 6701a45

Please sign in to comment.