Switch to new test harness
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -17,6 +16,32 @@ func TestDetectHTTPRequest(t *testing.T) {
|
||||
// An invalid HTTP request
|
||||
sshBanner := []byte("SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.4\r\n")
|
||||
|
||||
tests := []*testCase{
|
||||
{
|
||||
Name: "HTTP/1.0 GET",
|
||||
Direction: Client,
|
||||
Data: http10Request,
|
||||
DstPort: 80,
|
||||
WantProto: ProtocolHTTP,
|
||||
WantConfidence: .95,
|
||||
},
|
||||
{
|
||||
Name: "HTTP/1.1 GET",
|
||||
Direction: Client,
|
||||
Data: getRequest,
|
||||
DstPort: 80,
|
||||
WantProto: ProtocolHTTP,
|
||||
WantConfidence: .95,
|
||||
},
|
||||
{
|
||||
Name: "Invalid SSH",
|
||||
Direction: Client,
|
||||
Data: sshBanner,
|
||||
DstPort: 80,
|
||||
WantError: ErrUnknown,
|
||||
},
|
||||
}
|
||||
|
||||
defer func() { Strict = false }()
|
||||
for _, strict := range []bool{false, true} {
|
||||
Strict = strict
|
||||
@@ -27,40 +52,7 @@ func TestDetectHTTPRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Run("HTTP/1.0 GET", func(t *testing.T) {
|
||||
p, c, err := Detect(Client, http10Request, 1234, 80)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
|
||||
if p.Name != ProtocolHTTP {
|
||||
t.Fatalf("expected http protocol, got %s", p.Name)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HTTP/1.1 GET", func(t *testing.T) {
|
||||
p, c, err := Detect(Client, getRequest, 1234, 80)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
|
||||
if p.Name != ProtocolHTTP {
|
||||
t.Fatalf("expected http protocol, got %s", p.Name)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Invalid SSH", func(t *testing.T) {
|
||||
_, _, err := Detect(Server, sshBanner, 1234, 22)
|
||||
if !errors.Is(err, ErrUnknown) {
|
||||
t.Fatalf("expected unknown format, got error %T: %q", err, err)
|
||||
} else {
|
||||
t.Logf("error %q, as expected", err)
|
||||
}
|
||||
})
|
||||
testRunner(t, tests)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -83,72 +75,67 @@ func TestDetectHTTPResponse(t *testing.T) {
|
||||
// An invalid banner (SSH)
|
||||
sshBanner := []byte("SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.4\r\n")
|
||||
|
||||
tests := []*testCase{
|
||||
{
|
||||
Name: "HTTP/1.0 403",
|
||||
Direction: Server,
|
||||
Data: http10Response,
|
||||
SrcPort: 80,
|
||||
WantProto: ProtocolHTTP,
|
||||
},
|
||||
{
|
||||
Name: "HTTP/1.1 200",
|
||||
Direction: Server,
|
||||
Data: responseOK,
|
||||
SrcPort: 80,
|
||||
WantProto: ProtocolHTTP,
|
||||
},
|
||||
{
|
||||
Name: "HTTP/1.1 404",
|
||||
Direction: Server,
|
||||
Data: responseNotFound,
|
||||
SrcPort: 80,
|
||||
WantProto: ProtocolHTTP,
|
||||
},
|
||||
{
|
||||
Name: "Invalid HTTP/1.1 GET",
|
||||
Direction: Server,
|
||||
Data: getRequest,
|
||||
SrcPort: 80,
|
||||
WantError: ErrUnknown,
|
||||
},
|
||||
{
|
||||
Name: "Invalid SSH",
|
||||
Direction: Server,
|
||||
Data: sshBanner,
|
||||
SrcPort: 80,
|
||||
WantError: ErrUnknown,
|
||||
},
|
||||
}
|
||||
|
||||
defer func() { Strict = false }()
|
||||
for _, strict := range []bool{false, true} {
|
||||
Strict = strict
|
||||
|
||||
name := "loose"
|
||||
var name string
|
||||
if strict {
|
||||
name = "strict"
|
||||
for _, test := range tests {
|
||||
if test.WantError == nil {
|
||||
test.WantConfidence = .95
|
||||
}
|
||||
}
|
||||
} else {
|
||||
name = "loose"
|
||||
for _, test := range tests {
|
||||
if test.WantError == nil {
|
||||
test.WantConfidence = .85
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Run("HTTP/1.0 403", func(t *testing.T) {
|
||||
p, c, err := Detect(Server, http10Response, 80, 1234)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
|
||||
if p.Name != ProtocolHTTP {
|
||||
t.Fatalf("expected http protocol, got %s", p.Name)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HTTP/1.1 200", func(t *testing.T) {
|
||||
p, c, err := Detect(Server, responseOK, 80, 1234)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
|
||||
if p.Name != ProtocolHTTP {
|
||||
t.Fatalf("expected http protocol, got %s", p.Name)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HTTP/1.1 404", func(t *testing.T) {
|
||||
p, c, err := Detect(Server, responseNotFound, 80, 1234)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
t.Logf("detected %s version %s confidence %g%%", p.Name, p.Version, c*100)
|
||||
if p.Name != ProtocolHTTP {
|
||||
t.Fatalf("expected http protocol, got %s", p.Name)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Invalid HTTP/1.1 GET", func(t *testing.T) {
|
||||
_, _, err := Detect(Server, getRequest, 1234, 80)
|
||||
if !errors.Is(err, ErrUnknown) {
|
||||
t.Fatalf("expected unknown format, got error %T: %q", err, err)
|
||||
} else {
|
||||
t.Logf("error %q, as expected", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Invalid SSH", func(t *testing.T) {
|
||||
_, _, err := Detect(Server, sshBanner, 22, 1234)
|
||||
if !errors.Is(err, ErrUnknown) {
|
||||
t.Fatalf("expected unknown format, got error %T: %q", err, err)
|
||||
} else {
|
||||
t.Logf("error %q, as expected", err)
|
||||
}
|
||||
})
|
||||
testRunner(t, tests)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user