| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/ipfs/kubo/test/cli/harness" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestCLIWithRemoteHTTPS(t *testing.T) { |
| 16 | tests := []struct{ addrSuffix string }{{"https"}, {"tls/http"}} |
| 17 | for _, tt := range tests { |
| 18 | t.Run("with "+tt.addrSuffix+" multiaddr", func(t *testing.T) { |
| 19 | |
| 20 | // Create HTTPS test server |
| 21 | server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | if r.TLS == nil { |
| 23 | t.Error("Mocked Kubo RPC received plain HTTP request instead of HTTPS TLS Handshake") |
| 24 | } |
| 25 | _, _ = w.Write([]byte("OK")) |
| 26 | })) |
| 27 | defer server.Close() |
| 28 | |
| 29 | serverURL, _ := url.Parse(server.URL) |
| 30 | _, port, _ := net.SplitHostPort(serverURL.Host) |
| 31 | |
| 32 | // Create Kubo repo |
| 33 | node := harness.NewT(t).NewNode().Init() |
| 34 | |
| 35 | // Attempt to talk to remote Kubo RPC endpoint over HTTPS |
| 36 | resp := node.RunIPFS("id", "--api", fmt.Sprintf("/ip4/127.0.0.1/tcp/%s/%s", port, tt.addrSuffix)) |
| 37 | |
| 38 | // Expect HTTPS error (confirming TLS and https:// were used, and not Cleartext HTTP) |
| 39 | require.Error(t, resp.Err) |
| 40 | require.Contains(t, resp.Stderr.String(), "Error: tls: failed to verify certificate: x509: certificate signed by unknown authority") |
| 41 | |
| 42 | node.StopDaemon() |
| 43 | |
| 44 | }) |
| 45 | } |
| 46 | } |