2021-11-10 19:06:12 +00:00
|
|
|
package integrationtests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2023-12-31 11:52:08 +00:00
|
|
|
"fmt"
|
2022-08-11 18:00:25 +00:00
|
|
|
"io"
|
2021-11-10 19:06:12 +00:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2023-03-18 19:38:18 +00:00
|
|
|
"strings"
|
2021-11-10 19:06:12 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/dcarrillo/whatismyip/router"
|
2023-03-18 19:38:18 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
|
|
|
"github.com/quic-go/quic-go/http3"
|
2021-11-10 19:06:12 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
)
|
|
|
|
|
|
|
|
func buildContainer() testcontainers.ContainerRequest {
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
2021-11-30 15:19:45 +00:00
|
|
|
dir := filepath.Dir(filename)
|
2021-11-10 19:06:12 +00:00
|
|
|
|
|
|
|
req := testcontainers.ContainerRequest{
|
|
|
|
FromDockerfile: testcontainers.FromDockerfile{
|
|
|
|
Context: "../",
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
},
|
|
|
|
Cmd: []string{
|
2023-12-31 11:52:08 +00:00
|
|
|
"-geoip2-city", "/GeoIP2-City-Test.mmdb",
|
|
|
|
"-geoip2-asn", "/GeoLite2-ASN-Test.mmdb",
|
2021-11-10 19:06:12 +00:00
|
|
|
"-bind", ":8000",
|
|
|
|
"-tls-bind", ":8001",
|
2023-12-31 11:52:08 +00:00
|
|
|
"-tls-crt", "/server.pem",
|
|
|
|
"-tls-key", "/server.key",
|
2021-11-10 19:06:12 +00:00
|
|
|
"-trusted-header", "X-Real-IP",
|
2022-04-02 16:10:48 +00:00
|
|
|
"-enable-secure-headers",
|
2023-03-18 19:38:18 +00:00
|
|
|
"-enable-http3",
|
2021-11-10 19:06:12 +00:00
|
|
|
},
|
2023-03-18 19:38:18 +00:00
|
|
|
ExposedPorts: []string{"8000:8000", "8001:8001", "8001:8001/udp"},
|
|
|
|
WaitingFor: wait.ForHTTP("/geo").
|
|
|
|
WithTLS(true, &tls.Config{InsecureSkipVerify: true}).
|
|
|
|
WithPort("8001"),
|
2023-12-31 11:52:08 +00:00
|
|
|
Files: []testcontainers.ContainerFile{
|
|
|
|
{
|
|
|
|
HostFilePath: filepath.Join(dir, "/../test/GeoIP2-City-Test.mmdb"),
|
|
|
|
ContainerFilePath: "/GeoIP2-City-Test.mmdb",
|
|
|
|
FileMode: 0644,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HostFilePath: filepath.Join(dir, "/../test/GeoLite2-ASN-Test.mmdb"),
|
|
|
|
ContainerFilePath: "/GeoLite2-ASN-Test.mmdb",
|
|
|
|
FileMode: 0644,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HostFilePath: filepath.Join(dir, "/../test/server.pem"),
|
|
|
|
ContainerFilePath: "/server.pem",
|
|
|
|
FileMode: 0644,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HostFilePath: filepath.Join(dir, "/../test/server.key"),
|
|
|
|
ContainerFilePath: "/server.key",
|
|
|
|
FileMode: 0644,
|
|
|
|
},
|
|
|
|
},
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2023-12-31 11:52:08 +00:00
|
|
|
func initContainer(t assert.TestingT, request testcontainers.ContainerRequest) func() {
|
2021-11-10 19:06:12 +00:00
|
|
|
ctx := context.Background()
|
2023-12-31 11:52:08 +00:00
|
|
|
|
2021-11-10 19:06:12 +00:00
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
2023-12-31 11:52:08 +00:00
|
|
|
ContainerRequest: request,
|
2021-11-10 19:06:12 +00:00
|
|
|
Started: true,
|
|
|
|
})
|
2023-12-31 11:52:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
assert.NoError(t, container.Terminate(ctx))
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
2023-12-31 11:52:08 +00:00
|
|
|
}
|
2021-11-10 19:06:12 +00:00
|
|
|
|
2023-12-31 11:52:08 +00:00
|
|
|
func TestContainerIntegration(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skiping integration tests")
|
|
|
|
}
|
2021-11-10 19:06:12 +00:00
|
|
|
|
2023-12-31 11:52:08 +00:00
|
|
|
t.Cleanup(initContainer(t, buildContainer()))
|
|
|
|
|
|
|
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
2023-03-18 19:38:18 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
url string
|
|
|
|
quic bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "RequestOverHTTP",
|
|
|
|
url: "http://localhost:8000",
|
|
|
|
quic: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RequestOverHTTPs",
|
|
|
|
url: "https://localhost:8001",
|
|
|
|
quic: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RequestOverUDPWithQuic",
|
|
|
|
url: "https://localhost:8001",
|
|
|
|
quic: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-31 11:52:08 +00:00
|
|
|
testsPortScan := []struct {
|
|
|
|
name string
|
|
|
|
port int
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "RequestOpenPortScan",
|
|
|
|
port: 8000,
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RequestClosedPortScan",
|
|
|
|
port: 65533,
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-18 19:38:18 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-12-31 11:52:08 +00:00
|
|
|
req, err := http.NewRequest("GET", tt.url, nil)
|
|
|
|
assert.NoError(t, err)
|
2023-03-18 19:38:18 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
var body []byte
|
|
|
|
if tt.quic {
|
|
|
|
resp, body, err = doQuicRequest(req)
|
|
|
|
} else {
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, _ = client.Do(req)
|
|
|
|
body, err = io.ReadAll(resp.Body)
|
|
|
|
if strings.Contains(tt.url, "https://") {
|
2023-07-22 11:20:17 +00:00
|
|
|
assert.Equal(t, `h3=":8001"; ma=2592000`, resp.Header.Get("Alt-Svc"))
|
2023-03-18 19:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 200, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.NoError(t, json.Unmarshal(body, &router.JSONResponse{}))
|
|
|
|
assert.Equal(t, "DENY", resp.Header.Get("X-Frame-Options"))
|
|
|
|
assert.Equal(t, "nosniff", resp.Header.Get("X-Content-Type-Options"))
|
|
|
|
assert.Equal(t, "1; mode=block", resp.Header.Get("X-Xss-Protection"))
|
|
|
|
})
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
2023-12-31 11:52:08 +00:00
|
|
|
|
|
|
|
for _, tt := range testsPortScan {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:8000/scan/tcp/%d", tt.port), nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("X-Real-IP", "127.0.0.1")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 200, resp.StatusCode)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
j := router.JSONScanResponse{}
|
|
|
|
assert.NoError(t, json.Unmarshal(body, &j))
|
|
|
|
assert.Equal(t, tt.want, j.Reachable)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
2023-03-18 19:38:18 +00:00
|
|
|
|
|
|
|
func doQuicRequest(req *http.Request) (*http.Response, []byte, error) {
|
|
|
|
roundTripper := &http3.RoundTripper{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
QuicConfig: &quic.Config{},
|
|
|
|
}
|
|
|
|
defer roundTripper.Close()
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: roundTripper,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
return resp, body, nil
|
|
|
|
}
|