-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from shamaton/case_crash
Return error when strange data is received
- Loading branch information
Showing
118 changed files
with
595 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package msgpack_test | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/shamaton/msgpack/v2" | ||
) | ||
|
||
var crashDir = filepath.Join("testdata", "crashers") | ||
|
||
func TestCrashBinary(t *testing.T) { | ||
entries, err := os.ReadDir(crashDir) | ||
if err != nil { | ||
t.Fatalf("os.ReadDir error. err: %+v", err) | ||
} | ||
|
||
ch := make(chan string, len(entries)) | ||
|
||
// worker | ||
wg := sync.WaitGroup{} | ||
for i := 0; i < runtime.NumCPU(); i++ { | ||
wg.Add(1) | ||
go check(t, &wg, ch) | ||
} | ||
|
||
for _, entry := range entries { | ||
ch <- filepath.Join(crashDir, entry.Name()) | ||
} | ||
close(ch) | ||
wg.Wait() | ||
} | ||
|
||
func check(t *testing.T, wg *sync.WaitGroup, ch <-chan string) { | ||
var ( | ||
path string | ||
ok bool | ||
data []byte | ||
) | ||
t.Helper() | ||
defer wg.Done() | ||
defer func() { | ||
if e := recover(); e != nil { | ||
t.Logf("panic occurred.\nfile: %s\nlen: %d\nbin: % x\nerr: %+v", | ||
path, len(data), data, e, | ||
) | ||
} | ||
}() | ||
|
||
for { | ||
path, ok = <-ch // closeされると ok が false になる | ||
if !ok { | ||
return | ||
} | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
t.Logf("%s file open error. err: %+v", path, err) | ||
t.Fail() | ||
return | ||
} | ||
|
||
data, err = io.ReadAll(file) | ||
if err != nil { | ||
t.Logf("%s io.ReadAll error. err: %+v", path, err) | ||
t.Fail() | ||
return | ||
} | ||
|
||
var r interface{} | ||
err = msgpack.Unmarshal(data, &r) | ||
if err == nil { | ||
t.Logf("err should be occurred.\nname: %s\nlen: %d\nbin: % x", | ||
file.Name(), len(data), data, | ||
) | ||
t.Fail() | ||
return | ||
} | ||
|
||
path = "" | ||
data = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.