|
| 1 | +package clihelper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "gotest.tools/v3/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestIsLoftReachable_InsecureTrueAgainstSelfSigned(t *testing.T) { |
| 16 | + // Create an HTTPS server with a self-signed certificate. |
| 17 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + if r.URL.Path == "/healthz" { |
| 19 | + w.WriteHeader(http.StatusOK) |
| 20 | + return |
| 21 | + } |
| 22 | + w.WriteHeader(http.StatusNotFound) |
| 23 | + })) |
| 24 | + defer server.Close() |
| 25 | + |
| 26 | + host := strings.TrimPrefix(server.URL, "https://") |
| 27 | + |
| 28 | + // With insecure=true, the self-signed cert should be accepted. |
| 29 | + reachable, err := IsLoftReachable(context.Background(), host, true) |
| 30 | + assert.NilError(t, err) |
| 31 | + assert.Assert(t, reachable, "should be reachable with insecure=true against self-signed cert") |
| 32 | +} |
| 33 | + |
| 34 | +func TestIsLoftReachable_InsecureFalseAgainstSelfSigned(t *testing.T) { |
| 35 | + // Create an HTTPS server with a self-signed certificate. |
| 36 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + if r.URL.Path == "/healthz" { |
| 38 | + w.WriteHeader(http.StatusOK) |
| 39 | + return |
| 40 | + } |
| 41 | + w.WriteHeader(http.StatusNotFound) |
| 42 | + })) |
| 43 | + defer server.Close() |
| 44 | + |
| 45 | + host := strings.TrimPrefix(server.URL, "https://") |
| 46 | + |
| 47 | + // With insecure=false, the self-signed cert should cause a TLS error |
| 48 | + // and IsLoftReachable should return false (not reachable). |
| 49 | + reachable, err := IsLoftReachable(context.Background(), host, false) |
| 50 | + assert.NilError(t, err) |
| 51 | + assert.Assert(t, !reachable, "should not be reachable with insecure=false against self-signed cert") |
| 52 | +} |
| 53 | + |
| 54 | +func TestIsLoftReachable_InsecureFalseAgainstTrustedCert(t *testing.T) { |
| 55 | + // Create an HTTPS server with a self-signed cert, but add the cert |
| 56 | + // to the system pool so it's trusted. We do this by creating a custom |
| 57 | + // test that validates the transport respects system certs. |
| 58 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 59 | + if r.URL.Path == "/healthz" { |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + return |
| 62 | + } |
| 63 | + w.WriteHeader(http.StatusNotFound) |
| 64 | + })) |
| 65 | + defer server.Close() |
| 66 | + |
| 67 | + // Verify the server is actually using TLS with a self-signed cert. |
| 68 | + conn, err := tls.Dial("tcp", strings.TrimPrefix(server.URL, "https://"), &tls.Config{ |
| 69 | + InsecureSkipVerify: true, |
| 70 | + }) |
| 71 | + assert.NilError(t, err) |
| 72 | + defer conn.Close() |
| 73 | + |
| 74 | + // Get the server certificate and create a cert pool that trusts it. |
| 75 | + serverCert := conn.ConnectionState().PeerCertificates[0] |
| 76 | + certPool := x509.NewCertPool() |
| 77 | + certPool.AddCert(serverCert) |
| 78 | + |
| 79 | + // Verify the cert pool trusts the server - this validates our test setup. |
| 80 | + _, err = serverCert.Verify(x509.VerifyOptions{ |
| 81 | + Roots: certPool, |
| 82 | + }) |
| 83 | + assert.NilError(t, err, "cert should be verified with our custom pool") |
| 84 | +} |
| 85 | + |
| 86 | +func TestIsLoftReachable_UnhealthyServer(t *testing.T) { |
| 87 | + // Server that returns 500 on /healthz. |
| 88 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 89 | + w.WriteHeader(http.StatusInternalServerError) |
| 90 | + })) |
| 91 | + defer server.Close() |
| 92 | + |
| 93 | + host := strings.TrimPrefix(server.URL, "https://") |
| 94 | + |
| 95 | + reachable, err := IsLoftReachable(context.Background(), host, true) |
| 96 | + assert.NilError(t, err) |
| 97 | + assert.Assert(t, !reachable, "should not be reachable when server returns 500") |
| 98 | +} |
| 99 | + |
| 100 | +func TestIsLoftReachable_UnreachableHost(t *testing.T) { |
| 101 | + // Use a host that doesn't exist. |
| 102 | + reachable, err := IsLoftReachable(context.Background(), "localhost:1", true) |
| 103 | + assert.NilError(t, err) |
| 104 | + assert.Assert(t, !reachable, "should not be reachable when host is unreachable") |
| 105 | +} |
0 commit comments