Skip to content

Commit ab8619a

Browse files
committed
Add additional cases
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent adfab6d commit ab8619a

4 files changed

Lines changed: 67 additions & 14 deletions

File tree

.github/workflows/go-tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ jobs:
116116
- name: Trust repository
117117
run: git config --global --add safe.directory "${GITHUB_WORKSPACE}"
118118

119+
- name: Clone malcontent samples required for Fuzz tests
120+
run: |
121+
make samples
122+
119123
- name: Fuzz tests
120124
run: |
121125
make fuzz

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ out/$(YARA_X_REPO)/.git/commit-$(YARA_X_COMMIT):
123123
git -C out/$(YARA_X_REPO) checkout $(YARA_X_COMMIT)
124124
touch out/$(YARA_X_REPO)/.git/commit-$(YARA_X_COMMIT)
125125

126+
samples: out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT)
127+
126128
.PHONY: install-yara-x
127129
install-yara-x: out/$(YARA_X_REPO)/.git/commit-$(YARA_X_COMMIT)
128130
mkdir -p out/lib
@@ -142,7 +144,7 @@ fuzz:
142144
go test -fuzz=FuzzExtractZip -fuzztime=10s ./pkg/archive/
143145
go test -fuzz=FuzzExtractArchive -fuzztime=10s ./pkg/archive/
144146
go test -fuzz=FuzzIsValidPath -fuzztime=10s ./pkg/archive/
145-
go test -fuzz=FuzzFile -fuzztime=10s ./pkg/programkind/
147+
go test -fuzz=FuzzFile -fuzztime=30s ./pkg/programkind/
146148
go test -fuzz=FuzzPath -fuzztime=10s ./pkg/programkind/
147149
go test -fuzz=FuzzGetExt -fuzztime=10s ./pkg/programkind/
148150
go test -fuzz=FuzzLongestUnique -fuzztime=10s ./pkg/report/

pkg/programkind/fuzz_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@ import (
55
"os"
66
"path/filepath"
77
"testing"
8+
"time"
89
)
910

1011
// FuzzFile tests file type detection with random inputs.
1112
func FuzzFile(f *testing.F) {
12-
testFiles := []string{
13-
"../../tests/linux/clean/ls",
14-
"../../tests/linux/clean/busybox",
15-
}
13+
samplesDir := "../../out/chainguard-dev/malcontent-samples"
14+
err := filepath.WalkDir(samplesDir, func(path string, d os.DirEntry, _ error) error {
15+
if d == nil || d.IsDir() {
16+
return nil
17+
}
18+
if filepath.Base(path)[0] == '.' {
19+
return nil
20+
}
1621

17-
for _, tf := range testFiles {
18-
if data, err := os.ReadFile(tf); err == nil {
19-
f.Add(data, filepath.Base(tf))
22+
if data, readErr := os.ReadFile(path); readErr == nil {
23+
if len(data) <= 10*1024*1024 { // 10MB max
24+
f.Add(data, filepath.Base(path))
25+
}
2026
}
27+
return nil
28+
})
29+
if err != nil {
30+
f.Logf("Could not walk samples directory: %v", err)
2131
}
2232

2333
f.Add([]byte{0x7f, 0x45, 0x4c, 0x46}, "test.elf") // ELF magic
@@ -51,7 +61,9 @@ func FuzzFile(f *testing.F) {
5161
}
5262
tmpFile.Close()
5363

54-
ctx := context.Background()
64+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
65+
defer cancel()
66+
5567
ft, err := File(ctx, tmpFile.Name())
5668

5769
_ = ft

pkg/report/fuzz_test.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ import (
77

88
// FuzzLongestUnique tests the longestUnique function with random string inputs.
99
func FuzzLongestUnique(f *testing.F) {
10-
// Seed with test cases from the unit test
1110
f.Add("apple,banana,cherry,applecherry,bananaapple,cherrybanana")
1211
f.Add("test,testing,tester,testest")
1312
f.Add(",a,aa,aaa")
1413
f.Add("abc,def,ghi")
1514
f.Add("abc,abcabc,abcabcabc")
16-
17-
// Add edge cases
1815
f.Add("") // empty input
1916
f.Add("single") // single string
2017
f.Add("a,a,a,a") // all duplicates
2118
f.Add("very_long_string_" + strings.Repeat("x", 1000)) // long strings
2219
f.Add(strings.Repeat("a,", 100)) // many strings
2320
f.Add("a,b,c,d,e,f,g,h,i,j,k,l,m") // many different strings
21+
f.Add("test\x00null,normal") // null byte
22+
f.Add("test\nnewline,test\rcarriage,test\ttab") // whitespace control chars
23+
f.Add("test\x01\x02\x03,normal") // low control characters
24+
f.Add("test\x7f\x80\x9f,normal") // high control characters
25+
f.Add("test\u200b,normal") // zero-width space
26+
f.Add("test\u200c,normal") // zero-width non-joiner
27+
f.Add("test\u200d,normal") // zero-width joiner
28+
f.Add("test\ufeff,normal") // zero-width no-break space (BOM)
29+
f.Add("test\u202a\u202b\u202c,normal") // bidirectional text marks
30+
f.Add("test\u2060,normal") // word joiner
31+
f.Add("hello\u200bworld,helloworld") // same word with/without zero-width
32+
f.Add("test\u034f,normal") // combining grapheme joiner
33+
f.Add("\u200b\u200c\u200d,visible") // only invisible characters
34+
f.Add("a\u0300\u0301\u0302,a") // combining diacritical marks
35+
f.Add("test\u00ad,test") // soft hyphen
36+
f.Add("fi\ufb01,fi") // ligature vs normal chars
37+
f.Add("test\u180e,normal") // mongolian vowel separator
38+
f.Add("\u061c\u2066\u2067\u2068\u2069,normal") // directional formatting
2439

2540
f.Fuzz(func(t *testing.T, input string) {
2641
var strs []string
@@ -76,12 +91,32 @@ func FuzzLongestUnique(f *testing.F) {
7691
func FuzzTrimPrefixes(f *testing.F) {
7792
f.Add("/tmp/extract/path/to/file", "/tmp/extract")
7893
f.Add("/home/user/file", "/home/user,/tmp")
79-
f.Add("./relative/path", "./relative")
8094
f.Add("/absolute/path", "/absolute,./relative")
95+
f.Add("/path/to/file", "/path/to")
96+
f.Add("./relative/path", "./relative")
97+
f.Add("./path/to/file", "./path")
98+
f.Add("./a/b/c/d/e", "./a/b")
99+
f.Add("../path/to/file", "../path/to")
100+
f.Add("../../parent/path", "../../parent")
101+
f.Add("../../../deeply/nested", "../../../deeply")
102+
f.Add("./././path", "./")
103+
f.Add("path/../other/file", "path/..")
104+
f.Add("relative/path", "/absolute,./relative")
105+
f.Add("/abs/path", "./relative,/abs")
81106
f.Add("", "")
82107
f.Add("path", "")
83108
f.Add("path/to/file", "path")
84-
f.Add("/path/to/file", "/path/to")
109+
f.Add(".", ".")
110+
f.Add("..", "..")
111+
f.Add("../..", "../..")
112+
f.Add("./path/./to/./file", "./path")
113+
f.Add("path/./to/file", "path/.")
114+
f.Add("path/../to/file", "path/..")
115+
f.Add("path/to/link/../real", "path/to")
116+
f.Add("./path/to/../../other", "./path")
117+
f.Add("path/to/file/", "path/to/")
118+
f.Add("/path/to/file/", "/path/to/")
119+
f.Add("./path/to/file/", "./path/to/")
85120

86121
f.Fuzz(func(t *testing.T, path, prefixesStr string) {
87122
var prefixes []string

0 commit comments

Comments
 (0)