Skip to content

Commit

Permalink
[Fuzzing] Fix oss-fuzz build and use a different file for benchmark t…
Browse files Browse the repository at this point in the history
…esting

Signed-off-by: Arjun Singh <ajsinghyadav00@gmail.com>
  • Loading branch information
0x34d authored Jul 25, 2023
1 parent ee38291 commit 7f8a11a
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 129 deletions.
29 changes: 29 additions & 0 deletions message/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package message

import "testing"

func BenchmarkPathOption(b *testing.B) {
buf := make([]byte, 256)
b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
options := make(Options, 0, 10)
path := "a/b/c"

options, bufLen, err := options.SetPath(buf, path)
if err != nil {
b.Fatalf("unexpected error %d", err)
}
if bufLen != 3 {
b.Fatalf("unexpected length %d", bufLen)
}

v := make([]string, 3)
n, err := options.GetStrings(URIPath, v)
if n != 3 {
b.Fatalf("bad length")
}
if err != nil {
b.Fatalf("unexpected code")
}
}
}
28 changes: 1 addition & 27 deletions message/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,33 +351,7 @@ func TestQueryOption(t *testing.T) {
require.True(t, opts.HasOption(URIQuery))
}

func BenchmarkPathOption(b *testing.B) {
buf := make([]byte, 256)
b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
options := make(Options, 0, 10)
path := "a/b/c"

options, bufLen, err := options.SetPath(buf, path)
if err != nil {
b.Fatalf("unexpected error %d", err)
}
if bufLen != 3 {
b.Fatalf("unexpected length %d", bufLen)
}

v := make([]string, 3)
n, err := options.GetStrings(URIPath, v)
if n != 3 {
b.Fatalf("bad length")
}
if err != nil {
b.Fatalf("unexpected code")
}
}
}

func FuzzUnmarshal(f *testing.F) {
func FuzzUnmarshalData(f *testing.F) {
f.Fuzz(func(t *testing.T, input_data []byte) {
uoptions := make(Options, 0, 10)
_, _ = uoptions.Unmarshal(input_data, CoapOptionDefs)
Expand Down
53 changes: 53 additions & 0 deletions tcp/coder/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package coder

import (
"testing"

"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
)

func BenchmarkMarshalMessage(b *testing.B) {
options := make(message.Options, 0, 32)
bufOptions := make([]byte, 1024)
bufOptionsUsed := bufOptions

enc := 0

options, enc, _ = options.SetPath(bufOptionsUsed, "/a/b/c/d/e")
bufOptionsUsed = bufOptionsUsed[enc:]

options, _, _ = options.SetContentFormat(bufOptionsUsed, message.TextPlain)
msg := message.Message{
Code: codes.GET,
Payload: []byte{0x1},
Token: []byte{0x1, 0x2, 0x3},
Options: options,
}
buffer := make([]byte, 1024)

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
_, err := DefaultCoder.Encode(msg, buffer)
if err != nil {
b.Fatalf("cannot marshal")
}
}
}

func BenchmarkUnmarshalMessage(b *testing.B) {
buffer := []byte{211, 0, 1, 1, 2, 3, 177, 97, 1, 98, 1, 99, 1, 100, 1, 101, 16, 255, 1}
options := make(message.Options, 0, 32)
msg := message.Message{
Options: options,
}

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
msg.Options = options
_, err := DefaultCoder.Decode(buffer, &msg)
if err != nil {
b.Fatalf("cannot unmarshal: %v", err)
}
}
}
45 changes: 0 additions & 45 deletions tcp/coder/coder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,6 @@ func TestUnmarshalMessage(t *testing.T) {
})
}

func BenchmarkMarshalMessage(b *testing.B) {
options := make(message.Options, 0, 32)
bufOptions := make([]byte, 1024)
bufOptionsUsed := bufOptions

enc := 0

options, enc, _ = options.SetPath(bufOptionsUsed, "/a/b/c/d/e")
bufOptionsUsed = bufOptionsUsed[enc:]

options, _, _ = options.SetContentFormat(bufOptionsUsed, message.TextPlain)
msg := message.Message{
Code: codes.GET,
Payload: []byte{0x1},
Token: []byte{0x1, 0x2, 0x3},
Options: options,
}
buffer := make([]byte, 1024)

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
_, err := DefaultCoder.Encode(msg, buffer)
if err != nil {
b.Fatalf("cannot marshal")
}
}
}

func BenchmarkUnmarshalMessage(b *testing.B) {
buffer := []byte{211, 0, 1, 1, 2, 3, 177, 97, 1, 98, 1, 99, 1, 100, 1, 101, 16, 255, 1}
options := make(message.Options, 0, 32)
msg := message.Message{
Options: options,
}

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
msg.Options = options
_, err := DefaultCoder.Decode(buffer, &msg)
if err != nil {
b.Fatalf("cannot unmarshal: %v", err)
}
}
}

func FuzzDecode(f *testing.F) {
f.Add([]byte{211, 0, 1, 1, 2, 3, 177, 97, 1, 98, 1, 99, 1, 100, 1, 101, 16, 255, 1})

Expand Down
12 changes: 12 additions & 0 deletions udp/client/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

import "testing"

func BenchmarkM(b *testing.B) {
m := NewMutexMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// run uncontended lock/unlock - should be quite fast
m.Lock(i).Unlock()
}
}
9 changes: 0 additions & 9 deletions udp/client/mutexmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,3 @@ func TestMutexMap(t *testing.T) {
t.Logf("Failed, outLists: %#v", outLists)
}
}

func BenchmarkM(b *testing.B) {
m := NewMutexMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// run uncontended lock/unlock - should be quite fast
m.Lock(i).Unlock()
}
}
56 changes: 56 additions & 0 deletions udp/coder/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package coder

import (
"testing"

"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
)

func BenchmarkMarshalMessage(b *testing.B) {
options := make(message.Options, 0, 32)
bufOptions := make([]byte, 1024)
bufOptionsUsed := bufOptions

enc := 0
options, enc, _ = options.SetPath(bufOptionsUsed, "/a/b/c/d/e")
bufOptionsUsed = bufOptionsUsed[enc:]

options, _, _ = options.SetContentFormat(bufOptionsUsed, message.TextPlain)
msg := message.Message{
Code: codes.GET,
Payload: []byte{0x1},
Token: []byte{0x1, 0x2, 0x3},
Options: options,
}
buffer := make([]byte, 1024)

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
_, err := DefaultCoder.Encode(msg, buffer)
if err != nil {
b.Fatalf("cannot marshal")
}
}
}

func BenchmarkUnmarshalMessage(b *testing.B) {
buffer := []byte{
0x40, 0x1, 0x30, 0x39, 0x46, 0x77,
0x65, 0x65, 0x74, 0x61, 0x67, 0xa1, 0x3,
0xff, 'h', 'i',
}
options := make(message.Options, 0, 32)
msg := message.Message{
Options: options,
}

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
msg.Options = options
_, err := DefaultCoder.Decode(buffer, &msg)
if err != nil {
b.Fatalf("cannot unmarshal: %v", err)
}
}
}
48 changes: 0 additions & 48 deletions udp/coder/coder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,54 +195,6 @@ func TestUnmarshalMessage(t *testing.T) {
})
}

func BenchmarkMarshalMessage(b *testing.B) {
options := make(message.Options, 0, 32)
bufOptions := make([]byte, 1024)
bufOptionsUsed := bufOptions

enc := 0
options, enc, _ = options.SetPath(bufOptionsUsed, "/a/b/c/d/e")
bufOptionsUsed = bufOptionsUsed[enc:]

options, _, _ = options.SetContentFormat(bufOptionsUsed, message.TextPlain)
msg := message.Message{
Code: codes.GET,
Payload: []byte{0x1},
Token: []byte{0x1, 0x2, 0x3},
Options: options,
}
buffer := make([]byte, 1024)

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
_, err := DefaultCoder.Encode(msg, buffer)
if err != nil {
b.Fatalf("cannot marshal")
}
}
}

func BenchmarkUnmarshalMessage(b *testing.B) {
buffer := []byte{
0x40, 0x1, 0x30, 0x39, 0x46, 0x77,
0x65, 0x65, 0x74, 0x61, 0x67, 0xa1, 0x3,
0xff, 'h', 'i',
}
options := make(message.Options, 0, 32)
msg := message.Message{
Options: options,
}

b.ResetTimer()
for i := uint32(0); i < uint32(b.N); i++ {
msg.Options = options
_, err := DefaultCoder.Decode(buffer, &msg)
if err != nil {
b.Fatalf("cannot unmarshal: %v", err)
}
}
}

func FuzzDecode(f *testing.F) {
f.Add([]byte{0x40, 0x1, 0x30, 0x39, 0x46, 0x77, 0x65, 0x65, 0x74, 0x61, 0x67, 0xa1, 0x3, 0xff, 'h', 'i'})
f.Add([]byte{88, 128, 107, 170, 134, 237, 158, 132, 150, 19, 19, 159, 72, 20, 210, 14, 23, 231, 160, 183, 145, 128, 177, 14, 82, 20, 210, 255})
Expand Down

0 comments on commit 7f8a11a

Please sign in to comment.