| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) Protocol Labs |
| 4 | |
| 5 | test_description="Test directory listing (dir-index-html) on the HTTP gateway" |
| 6 | |
| 7 | |
| 8 | . lib/test-lib.sh |
| 9 | |
| 10 | ## ============================================================================ |
| 11 | ## Start IPFS Node and prepare test CIDs |
| 12 | ## ============================================================================ |
| 13 | |
| 14 | test_expect_success "ipfs init" ' |
| 15 | export IPFS_PATH="$(pwd)/.ipfs" && |
| 16 | ipfs init --profile=test > /dev/null |
| 17 | ' |
| 18 | |
| 19 | test_launch_ipfs_daemon_without_network |
| 20 | |
| 21 | # Import test case |
| 22 | # See the static fixtures in ./t0115-gateway-dir-listing/ |
| 23 | test_expect_success "Add the test directory" ' |
| 24 | ipfs dag import --pin-roots ../t0115-gateway-dir-listing/fixtures.car |
| 25 | ' |
| 26 | DIR_CID=bafybeig6ka5mlwkl4subqhaiatalkcleo4jgnr3hqwvpmsqfca27cijp3i # ./rootDir/ |
| 27 | FILE_CID=bafkreialihlqnf5uwo4byh4n3cmwlntwqzxxs2fg5vanqdi3d7tb2l5xkm # ./rootDir/ą/ę/file-źł.txt |
| 28 | FILE_SIZE=34 |
| 29 | |
| 30 | ## ============================================================================ |
| 31 | ## Test dir listing on path gateway (eg. 127.0.0.1:8080/ipfs/) |
| 32 | ## ============================================================================ |
| 33 | |
| 34 | test_expect_success "path gw: backlink on root CID should be hidden" ' |
| 35 | curl -sD - http://127.0.0.1:$GWAY_PORT/ipfs/${DIR_CID}/ > list_response && |
| 36 | test_should_contain "Index of" list_response && |
| 37 | test_should_not_contain "<a href=\"/ipfs/$DIR_CID/\">..</a>" list_response |
| 38 | ' |
| 39 | |
| 40 | test_expect_success "path gw: redirect dir listing to URL with trailing slash" ' |
| 41 | curl -sD - http://127.0.0.1:$GWAY_PORT/ipfs/${DIR_CID}/ą/ę > list_response && |
| 42 | test_should_contain "HTTP/1.1 301 Moved Permanently" list_response && |
| 43 | test_should_contain "Location: /ipfs/${DIR_CID}/%C4%85/%C4%99/" list_response |
| 44 | ' |
| 45 | |
| 46 | test_expect_success "path gw: Etag should be present" ' |
| 47 | curl -sD - http://127.0.0.1:$GWAY_PORT/ipfs/${DIR_CID}/ą/ę/ > list_response && |
| 48 | test_should_contain "Index of" list_response && |
| 49 | test_should_contain "Etag: \"DirIndex-" list_response |
| 50 | ' |
| 51 | |
| 52 | test_expect_success "path gw: breadcrumbs should point at /ipfs namespace mounted at Origin root" ' |
| 53 | test_should_contain "/ipfs/<a href=\"/ipfs/$DIR_CID\">$DIR_CID</a>/<a href=\"/ipfs/$DIR_CID/%C4%85\">ą</a>/<a href=\"/ipfs/$DIR_CID/%C4%85/%C4%99\">ę</a>" list_response |
| 54 | ' |
| 55 | |
| 56 | test_expect_success "path gw: backlink on subdirectory should point at parent directory" ' |
| 57 | test_should_contain "<a href=\"/ipfs/$DIR_CID/%C4%85/%C4%99/..\">..</a>" list_response |
| 58 | ' |
| 59 | |
| 60 | test_expect_success "path gw: name column should be a link to its content path" ' |
| 61 | test_should_contain "<a href=\"/ipfs/$DIR_CID/%C4%85/%C4%99/file-%C5%BA%C5%82.txt\">file-źł.txt</a>" list_response |
| 62 | ' |
| 63 | |
| 64 | test_expect_success "path gw: hash column should be a CID link with filename param" ' |
| 65 | test_should_contain "<a class=\"ipfs-hash\" translate=\"no\" href=\"/ipfs/$FILE_CID?filename=file-%25C5%25BA%25C5%2582.txt\">" list_response |
| 66 | ' |
| 67 | |
| 68 | ## ============================================================================ |
| 69 | ## Test dir listing on subdomain gateway (eg. <cid>.ipfs.localhost:8080) |
| 70 | ## ============================================================================ |
| 71 | |
| 72 | DIR_HOSTNAME="${DIR_CID}.ipfs.localhost" |
| 73 | # note: we skip DNS lookup by running curl with --resolve $DIR_HOSTNAME:127.0.0.1 |
| 74 | |
| 75 | test_expect_success "subdomain gw: backlink on root CID should be hidden" ' |
| 76 | curl -sD - --resolve $DIR_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DIR_HOSTNAME:$GWAY_PORT/ > list_response && |
| 77 | test_should_contain "Index of" list_response && |
| 78 | test_should_not_contain "<a href=\"/\">..</a>" list_response |
| 79 | ' |
| 80 | |
| 81 | test_expect_success "subdomain gw: redirect dir listing to URL with trailing slash" ' |
| 82 | curl -sD - --resolve $DIR_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DIR_HOSTNAME:$GWAY_PORT/ą/ę > list_response && |
| 83 | test_should_contain "HTTP/1.1 301 Moved Permanently" list_response && |
| 84 | test_should_contain "Location: /%C4%85/%C4%99/" list_response |
| 85 | ' |
| 86 | |
| 87 | test_expect_success "subdomain gw: Etag should be present" ' |
| 88 | curl -sD - --resolve $DIR_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DIR_HOSTNAME:$GWAY_PORT/ą/ę/ > list_response && |
| 89 | test_should_contain "Index of" list_response && |
| 90 | test_should_contain "Etag: \"DirIndex-" list_response |
| 91 | ' |
| 92 | |
| 93 | test_expect_success "subdomain gw: backlink on subdirectory should point at parent directory" ' |
| 94 | test_should_contain "<a href=\"/%C4%85/%C4%99/..\">..</a>" list_response |
| 95 | ' |
| 96 | |
| 97 | test_expect_success "subdomain gw: breadcrumbs should leverage path-based router mounted on the parent domain" ' |
| 98 | test_should_contain "/ipfs/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID\">$DIR_CID</a>/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID/%C4%85\">ą</a>/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID/%C4%85/%C4%99\">ę</a>" list_response |
| 99 | ' |
| 100 | |
| 101 | test_expect_success "subdomain gw: name column should be a link to content root mounted at subdomain origin" ' |
| 102 | test_should_contain "<a href=\"/%C4%85/%C4%99/file-%C5%BA%C5%82.txt\">file-źł.txt</a>" list_response |
| 103 | ' |
| 104 | |
| 105 | test_expect_success "subdomain gw: hash column should be a CID link to path router with filename param" ' |
| 106 | test_should_contain "<a class=\"ipfs-hash\" translate=\"no\" href=\"//localhost:$GWAY_PORT/ipfs/$FILE_CID?filename=file-%25C5%25BA%25C5%2582.txt\">" list_response |
| 107 | ' |
| 108 | |
| 109 | ## ============================================================================ |
| 110 | ## Test dir listing on DNSLink gateway (eg. example.com) |
| 111 | ## ============================================================================ |
| 112 | |
| 113 | # DNSLink test requires a daemon in online mode with precached /ipns/ mapping |
| 114 | test_kill_ipfs_daemon |
| 115 | DNSLINK_HOSTNAME="website.example.com" |
| 116 | export IPFS_NS_MAP="$DNSLINK_HOSTNAME:/ipfs/$DIR_CID" |
| 117 | test_launch_ipfs_daemon |
| 118 | |
| 119 | # Note that: |
| 120 | # - this type of gateway is also tested in gateway_test.go#TestIPNSHostnameBacklinks |
| 121 | # (go tests and sharness tests should be kept in sync) |
| 122 | # - we skip DNS lookup by running curl with --resolve $DNSLINK_HOSTNAME:127.0.0.1 |
| 123 | |
| 124 | test_expect_success "dnslink gw: backlink on root CID should be hidden" ' |
| 125 | curl -v -sD - --resolve $DNSLINK_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DNSLINK_HOSTNAME:$GWAY_PORT/ > list_response && |
| 126 | test_should_contain "Index of" list_response && |
| 127 | test_should_not_contain "<a href=\"/\">..</a>" list_response |
| 128 | ' |
| 129 | |
| 130 | test_expect_success "dnslink gw: redirect dir listing to URL with trailing slash" ' |
| 131 | curl -sD - --resolve $DNSLINK_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DNSLINK_HOSTNAME:$GWAY_PORT/ą/ę > list_response && |
| 132 | test_should_contain "HTTP/1.1 301 Moved Permanently" list_response && |
| 133 | test_should_contain "Location: /%C4%85/%C4%99/" list_response |
| 134 | ' |
| 135 | |
| 136 | test_expect_success "dnslink gw: Etag should be present" ' |
| 137 | curl -sD - --resolve $DNSLINK_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DNSLINK_HOSTNAME:$GWAY_PORT/ą/ę/ > list_response && |
| 138 | test_should_contain "Index of" list_response && |
| 139 | test_should_contain "Etag: \"DirIndex-" list_response |
| 140 | ' |
| 141 | |
| 142 | test_expect_success "dnslink gw: backlink on subdirectory should point at parent directory" ' |
| 143 | test_should_contain "<a href=\"/%C4%85/%C4%99/..\">..</a>" list_response |
| 144 | ' |
| 145 | |
| 146 | test_expect_success "dnslink gw: breadcrumbs should point at content root mounted at dnslink origin" ' |
| 147 | test_should_contain "/ipns/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/\">website.example.com</a>/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/%C4%85\">ą</a>/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/%C4%85/%C4%99\">ę</a>" list_response |
| 148 | ' |
| 149 | |
| 150 | test_expect_success "dnslink gw: name column should be a link to content root mounted at dnslink origin" ' |
| 151 | test_should_contain "<a href=\"/%C4%85/%C4%99/file-%C5%BA%C5%82.txt\">file-źł.txt</a>" list_response |
| 152 | ' |
| 153 | |
| 154 | # DNSLink websites don't have public gateway mounted by default |
| 155 | # See: https://github.com/ipfs/dir-index-html/issues/42 |
| 156 | test_expect_success "dnslink gw: hash column should be a CID link to cid.ipfs.tech" ' |
| 157 | test_should_contain "<a class=\"ipfs-hash\" translate=\"no\" href=\"https://cid.ipfs.tech/#$FILE_CID\" target=\"_blank\" rel=\"noreferrer noopener\">" list_response |
| 158 | ' |
| 159 | |
| 160 | ## ============================================================================ |
| 161 | ## End of tests, cleanup |
| 162 | ## ============================================================================ |
| 163 | |
| 164 | test_kill_ipfs_daemon |
| 165 | test_expect_success "clean up ipfs dir" ' |
| 166 | rm -rf "$IPFS_PATH" |
| 167 | ' |
| 168 | test_done |