|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/ipfs/boxo/routing/http/types" |
| 15 | + "github.com/ipfs/boxo/routing/http/types/iter" |
| 16 | + "github.com/ipfs/go-cid" |
| 17 | + "github.com/libp2p/go-libp2p/core/peer" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +func TestHTTPBlockRouter(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + debug := os.Getenv("DEBUG") == "true" |
| 25 | + |
| 26 | + t.Run("FindProviders", func(t *testing.T) { |
| 27 | + ctx := context.Background() |
| 28 | + // Set up mock HTTP Provider (trustless gateway) that returns HTTP 200 for specific CID |
| 29 | + testData := "Thu 8 May 01:07:03 CEST 2025" |
| 30 | + testCid := cid.MustParse("bafkreie5zycmytdhd5bl4f5jqsayyiwshugf57d4hkd7eif3toh23fsy3i") |
| 31 | + httpBlockGateway := newMockTrustlessGateway(testCid, testData, debug) |
| 32 | + t.Cleanup(func() { httpBlockGateway.Close() }) |
| 33 | + |
| 34 | + // Test args |
| 35 | + endpoint := httpBlockGateway.URL |
| 36 | + peerId, _ := peer.Decode("12D3KooWCjfPiojcCUmv78Wd1NJzi4Mraj1moxigp7AfQVQvGLwH") |
| 37 | + insecureSkipVerify := true |
| 38 | + client := defaultHTTPBlockRouterClient(insecureSkipVerify) |
| 39 | + httpHost, httpPort, err := splitHostPort(endpoint) |
| 40 | + assert.NoError(t, err) |
| 41 | + expectedAddr := fmt.Sprintf("/ip4/%s/tcp/%s/tls/http", httpHost, httpPort) |
| 42 | + |
| 43 | + // Create Router |
| 44 | + httpBlockRouter, err := newHTTPBlockRouter(endpoint, peerId, client) |
| 45 | + assert.NoError(t, err) |
| 46 | + |
| 47 | + t.Run("return gateway as HTTP provider if HTTP HEAD check returned HTTP 200", func(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + // Ask Router for CID present on trustless gateway |
| 51 | + it, err := httpBlockRouter.FindProviders(ctx, testCid, 10) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + results, err := iter.ReadAllResults(it) |
| 55 | + require.NoError(t, err) |
| 56 | + require.Len(t, results, 1) |
| 57 | + |
| 58 | + // Verify returned provider points at http gateway URL |
| 59 | + peerRecord := results[0].(*types.PeerRecord) |
| 60 | + require.Equal(t, peerId, *peerRecord.ID) |
| 61 | + require.Len(t, peerRecord.Addrs, 1) |
| 62 | + assert.NoError(t, err) |
| 63 | + require.Equal(t, expectedAddr, peerRecord.Addrs[0].String()) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("return no results if HTTP HEAD check returned HTTP 404", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + // This CID has no providers |
| 70 | + failCid := cid.MustParse("bafkreie5keu4z5kgutjds5tz3ahdxhcdkn4hl2vr7snenml44ui7y4yfki") |
| 71 | + |
| 72 | + // Ask Router for CID present on trustless gateway |
| 73 | + it, err := httpBlockRouter.FindProviders(ctx, failCid, 10) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + results, err := iter.ReadAllResults(it) |
| 77 | + require.NoError(t, err) |
| 78 | + require.Len(t, results, 0) |
| 79 | + }) |
| 80 | + |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +// newMockTrustlessGateway pretends to be http provider that supports |
| 85 | +// block response https://specs.ipfs.tech/http-gateways/trustless-gateway/#block-responses-application-vnd-ipld-raw |
| 86 | +func newMockTrustlessGateway(c cid.Cid, body string, debug bool) *httptest.Server { |
| 87 | + expectedPathPrefix := "/ipfs/" + c.String() |
| 88 | + handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 89 | + if debug { |
| 90 | + fmt.Printf("mockTrustlessGateway %s %s\n", req.Method, req.URL.Path) |
| 91 | + } |
| 92 | + if strings.HasPrefix(req.URL.Path, expectedPathPrefix) { |
| 93 | + w.Header().Set("Content-Type", "application/vnd.ipld.raw") |
| 94 | + w.WriteHeader(http.StatusOK) |
| 95 | + if req.Method == "GET" { |
| 96 | + _, err := w.Write([]byte(body)) |
| 97 | + if err != nil { |
| 98 | + fmt.Fprintf(os.Stderr, "mockTrustlessGateway %s %s error: %v\n", req.Method, req.URL.Path, err) |
| 99 | + } |
| 100 | + } |
| 101 | + return |
| 102 | + } else if strings.HasPrefix(req.URL.Path, "/ipfs/bafkqaaa") { |
| 103 | + // This is probe from https://specs.ipfs.tech/http-gateways/trustless-gateway/#dedicated-probe-paths |
| 104 | + w.Header().Set("Content-Type", "application/vnd.ipld.raw") |
| 105 | + w.WriteHeader(http.StatusOK) |
| 106 | + return |
| 107 | + } else { |
| 108 | + http.Error(w, "Not Found", http.StatusNotFound) |
| 109 | + return |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + // Make it HTTP/2 with self-signed TLS cert |
| 114 | + srv := httptest.NewUnstartedServer(handler) |
| 115 | + srv.EnableHTTP2 = true |
| 116 | + srv.StartTLS() |
| 117 | + return srv |
| 118 | +} |
| 119 | + |
| 120 | +func splitHostPort(httpUrl string) (ipAddr string, port string, err error) { |
| 121 | + u, err := url.Parse(httpUrl) |
| 122 | + if err != nil { |
| 123 | + return "", "", err |
| 124 | + } |
| 125 | + if u.Scheme == "" || u.Host == "" { |
| 126 | + return "", "", fmt.Errorf("invalid URL format: missing scheme or host") |
| 127 | + } |
| 128 | + ipAddr, port, err = net.SplitHostPort(u.Host) |
| 129 | + if err != nil { |
| 130 | + return "", "", fmt.Errorf("failed to split host and port from %q: %w", u.Host, err) |
| 131 | + } |
| 132 | + return ipAddr, port, nil |
| 133 | +} |
0 commit comments