summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-02-16 15:48:20 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2023-02-16 15:48:20 +0100
commitf718788442682f87e2fd1b6067f6062bade52d52 (patch)
treee3f2adc39efbdb51233eb45f2897a88e3157def2
parent2a46b5771d15ea55e20a5b52bddb6c04b55326e7 (diff)
Test: Implement util package for starting a TLS server
-rw-r--r--internal/discovery/discovery.go10
-rw-r--r--internal/discovery/discovery_test.go70
-rw-r--r--internal/test/server.go44
3 files changed, 69 insertions, 55 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go
index f4151a7..e561951 100644
--- a/internal/discovery/discovery.go
+++ b/internal/discovery/discovery.go
@@ -35,7 +35,10 @@ func (discovery *Discovery) file(jsonFile string, previousVersion uint64, struct
}
// Get json data
- jsonURL := DiscoURL + jsonFile
+ jsonURL, err := http.JoinURLPath(DiscoURL, jsonFile)
+ if err != nil {
+ return err
+ }
_, body, err := discovery.httpClient.Get(jsonURL)
if err != nil {
return err
@@ -43,7 +46,10 @@ func (discovery *Discovery) file(jsonFile string, previousVersion uint64, struct
// Get signature
sigFile := jsonFile + ".minisig"
- sigURL := DiscoURL + sigFile
+ sigURL, err := http.JoinURLPath(DiscoURL, sigFile)
+ if err != nil {
+ return err
+ }
_, sigBody, err := discovery.httpClient.Get(sigURL)
if err != nil {
return err
diff --git a/internal/discovery/discovery_test.go b/internal/discovery/discovery_test.go
index 0a1ec4d..3422864 100644
--- a/internal/discovery/discovery_test.go
+++ b/internal/discovery/discovery_test.go
@@ -1,67 +1,26 @@
package discovery
import (
- "crypto/tls"
- "crypto/x509"
- "fmt"
- "net"
"net/http"
- "net/http/httptest"
"reflect"
"testing"
"time"
- httpw "github.com/eduvpn/eduvpn-common/internal/http"
"github.com/eduvpn/eduvpn-common/types"
+ "github.com/eduvpn/eduvpn-common/internal/test"
)
-// setupFileServer sets up a file server with a directory
-func setupFileServer(t *testing.T, directory string) *httptest.Server {
- listener, err := net.Listen("tcp", ":0")
- if err != nil {
- t.Fatalf("Failed to setup discovery file server")
- }
- handler := http.FileServer(http.Dir(directory))
- s := httptest.NewUnstartedServer(handler)
- // Close the server listener and use a custom one
- s.Listener.Close()
- s.Listener = listener
- s.StartTLS()
-
- // Override the global disco URL with the local file server
- port := listener.Addr().(*net.TCPAddr).Port
- DiscoURL = fmt.Sprintf("https://127.0.0.1:%d/", port)
- return s
-}
-
-func setupCerts(t *testing.T, discovery *Discovery, server *httptest.Server) {
- // Get the certs from the test server
- certs := x509.NewCertPool()
- for _, c := range server.TLS.Certificates {
- roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
- if err != nil {
- t.Fatalf("failed to parse root certificate with error: %v", err)
- }
- for _, root := range roots {
- certs.AddCert(root)
- }
- }
- // Override the client such that it only trusts the test server cert
- client := httpw.NewClient()
- client.Client.Transport = &http.Transport{
- TLSClientConfig: &tls.Config{
- RootCAs: certs,
- },
- }
- discovery.httpClient = client
-}
-
// TestServers tests whether or not we can obtain discovery servers
// It setups up a file server using the 'test_files' directory
func TestServers(t *testing.T) {
- s := setupFileServer(t, "test_files")
- d := &Discovery{}
- setupCerts(t, d, s)
+ handler := http.FileServer(http.Dir("test_files"))
+ s := test.NewServer(handler)
+ DiscoURL = s.URL
+ c, err := s.Client()
+ if err != nil {
+ t.Fatalf("Failed to get HTTP test client: %v", err)
+ }
+ d := &Discovery{httpClient: c}
// get servers
s1, err := d.Servers()
if err != nil {
@@ -96,9 +55,14 @@ func TestServers(t *testing.T) {
// TestOrganizations tests whether or not we can obtain discovery organizations
// It setups up a file server using the 'test_files' directory
func TestOrganizations(t *testing.T) {
- s := setupFileServer(t, "test_files")
- d := &Discovery{}
- setupCerts(t, d, s)
+ handler := http.FileServer(http.Dir("test_files"))
+ s := test.NewServer(handler)
+ DiscoURL = s.URL
+ c, err := s.Client()
+ if err != nil {
+ t.Fatalf("Failed to get HTTP test client: %v", err)
+ }
+ d := &Discovery{httpClient: c}
// get servers
s1, err := d.Organizations()
if err != nil {
diff --git a/internal/test/server.go b/internal/test/server.go
new file mode 100644
index 0000000..6426db0
--- /dev/null
+++ b/internal/test/server.go
@@ -0,0 +1,44 @@
+// Package test implements utilities for testing
+package test
+
+import (
+ "crypto/tls"
+ "crypto/x509"
+ "net/http"
+ "net/http/httptest"
+ httpw "github.com/eduvpn/eduvpn-common/internal/http"
+ "github.com/go-errors/errors"
+)
+
+type TestServer struct {
+ *httptest.Server
+}
+
+func NewServer(handler http.Handler) *TestServer {
+ s := httptest.NewTLSServer(handler)
+
+ return &TestServer{s}
+}
+
+// Client returns a test client that trusts the HTTPS certificates
+func (srv *TestServer) Client() (*httpw.Client, error) {
+ // Get the certs from the test server
+ certs := x509.NewCertPool()
+ for _, c := range srv.TLS.Certificates {
+ roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
+ if err != nil {
+ return nil, errors.WrapPrefix(err, "failed to parse root certificate", 0)
+ }
+ for _, root := range roots {
+ certs.AddCert(root)
+ }
+ }
+ // Override the client such that it only trusts the test server cert
+ client := httpw.NewClient()
+ client.Client.Transport = &http.Transport{
+ TLSClientConfig: &tls.Config{
+ RootCAs: certs,
+ },
+ }
+ return client, nil
+}