Skip to content

Commit

Permalink
add Fuzz testing for ParseTraceHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Nov 23, 2022
1 parent 7870a7d commit 315f944
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions xray/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package xray

import (
"reflect"
"regexp"
"strings"
"testing"
Expand All @@ -23,3 +24,25 @@ func FuzzSanitizeSegmentName(f *testing.F) {
}
})
}

func FuzzParseTraceHeader(f *testing.F) {
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5")
f.Add("ROOT=1-5E645F3E-1DFAD076A177C5CCC5DE12F5")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;Parent=03babb4ba280be51")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;Sampled=1")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;Sampled=0")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;Sampled=?")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;foo=bar;hoge=fuga")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;foo")
f.Add("Root=1-5e645f3e-1dfad076a177c5ccc5de12f5;Sampled=invalid")
f.Add("foo=bar;hoge=fuga;Sampled=1;Parent=03babb4ba280be51;Root=1-5e645f3e-1dfad076a177c5ccc5de12f5")

f.Fuzz(func(t *testing.T, s string) {
h0 := ParseTraceHeader(s)
h1 := ParseTraceHeader(h0.String())
if !reflect.DeepEqual(h0, h1) {
t.Errorf("parsing result not match: %q and %q: input %q", h0, h1, s)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("0; =")
1 change: 1 addition & 0 deletions xray/trace_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func ParseTraceHeader(s string) TraceHeader {
var header TraceHeader
s = strings.TrimSpace(s)
for _, kv := range strings.Split(s, ";") {
kv := strings.TrimSpace(kv)
idx := strings.IndexByte(kv, '=')
if idx < 0 {
// ignore invalid parameter
Expand Down

0 comments on commit 315f944

Please sign in to comment.