Skip to content

Commit f896aba

Browse files
committed
fix: honor quiet for result output
1 parent 8915314 commit f896aba

5 files changed

Lines changed: 85 additions & 5 deletions

File tree

core/e2e_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@ func TestE2E_MultiTargetCheck(t *testing.T) {
263263
}
264264
}
265265

266+
func TestE2E_QuietSuppressesCheckOutput(t *testing.T) {
267+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268+
w.Header().Set("Content-Type", "text/html")
269+
w.WriteHeader(400)
270+
fmt.Fprint(w, "<html><body>bad request</body></html>")
271+
}))
272+
defer server.Close()
273+
274+
urlFile := writeTempFile(t, server.URL+"\n")
275+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
276+
defer cancel()
277+
278+
out, err := runSpray(t, ctx, []string{
279+
"-l", urlFile,
280+
"-q",
281+
"--no-bar",
282+
"--no-stat",
283+
})
284+
if err != nil {
285+
t.Fatalf("RunWithArgs: %v", err)
286+
}
287+
if strings.TrimSpace(out) != "" {
288+
t.Fatalf("quiet check mode output = %q, want empty", out)
289+
}
290+
}
291+
266292
// ---------------------------------------------------------------------------
267293
// E2E: mixed status codes with valid findings
268294
// ---------------------------------------------------------------------------

core/pool/pool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ func (pool *BasePool) putToFuzzy(bl *baseline.Baseline) {
118118
pool.Outwg.Add(1)
119119
bl.IsFuzzy = true
120120
out := bl.Snapshot()
121+
out.IsValid = false
122+
out.IsFuzzy = true
121123
select {
122124
case pool.FuzzyCh <- out:
123125
case <-pool.ctx.Done():

core/pool/pool_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ func TestPutToFuzzy_Normal(t *testing.T) {
559559
if !bl.IsFuzzy {
560560
t.Fatal("IsFuzzy should be true")
561561
}
562+
if bl.IsValid {
563+
t.Fatal("fuzzy output snapshot should not stay valid")
564+
}
562565
case <-time.After(time.Second):
563566
t.Fatal("putToFuzzy: receive timed out")
564567
}

core/runner.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,13 @@ func (r *Runner) Output(bl *baseline.Baseline) {
498498
out = bl.String()
499499
}
500500

501-
if bl.IsValid {
502-
logs.Log.Console(out + "\n")
503-
} else if r.Fuzzy && bl.IsFuzzy {
504-
logs.Log.Console("[fuzzy] " + out + "\n")
501+
quiet := r.Option != nil && r.Option.Quiet
502+
if !quiet {
503+
if bl.IsValid {
504+
logs.Log.Console(out + "\n")
505+
} else if r.Fuzzy && bl.IsFuzzy {
506+
logs.Log.Console("[fuzzy] " + out + "\n")
507+
}
505508
}
506509

507510
if r.OutputFile != nil {
@@ -555,7 +558,9 @@ func (r *Runner) OutputHandler() {
555558
if !ok {
556559
return
557560
}
558-
r.Output(bl)
561+
if r.Fuzzy {
562+
r.Output(bl)
563+
}
559564
r.OutWg.Done()
560565
}
561566
}

core/runner_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package core
22

33
import (
4+
"bytes"
45
"sync"
56
"testing"
67
"time"
78

9+
"github.com/chainreactors/logs"
10+
"github.com/chainreactors/parsers"
11+
"github.com/chainreactors/spray/core/baseline"
812
"github.com/chainreactors/spray/pkg"
913
)
1014

@@ -55,3 +59,43 @@ func TestRecordStat_NilStat(t *testing.T) {
5559
t.Fatalf("stats.Requests = %d, want 0 for nil stat", stats.Requests)
5660
}
5761
}
62+
63+
func TestOutputHandlerSuppressesFuzzyChannelWithoutFuzzyFlag(t *testing.T) {
64+
var out bytes.Buffer
65+
oldLog := logs.Log
66+
logs.Log = logs.NewLogger(oldLog.Level)
67+
logs.Log.SetOutput(&out)
68+
t.Cleanup(func() {
69+
logs.Log = oldLog
70+
})
71+
72+
r := &Runner{
73+
Option: &Option{},
74+
OutputCh: make(chan *baseline.Baseline, 1),
75+
FuzzyCh: make(chan *baseline.Baseline, 1),
76+
OutWg: &sync.WaitGroup{},
77+
}
78+
r.OutputHandler()
79+
80+
r.OutWg.Add(1)
81+
r.FuzzyCh <- &baseline.Baseline{
82+
SprayResult: &parsers.SprayResult{
83+
UrlString: "http://example.com/fuzzy",
84+
Status: 400,
85+
BodyLength: 100,
86+
IsValid: true,
87+
IsFuzzy: true,
88+
Reason: pkg.ErrFuzzyCompareFailed.Error(),
89+
Source: parsers.CommonFileSource,
90+
},
91+
}
92+
mustFinishCore(t, 2*time.Second, "fuzzy output was not drained", func() {
93+
r.OutWg.Wait()
94+
})
95+
close(r.OutputCh)
96+
close(r.FuzzyCh)
97+
98+
if got := out.String(); got != "" {
99+
t.Fatalf("fuzzy output without --fuzzy = %q, want empty", got)
100+
}
101+
}

0 commit comments

Comments
 (0)