diff --git a/test/sharness/t0123-gateway-json-cbor.sh b/test/sharness/t0123-gateway-json-cbor.sh index ac446c3df7f4..831b5f82eadb 100755 --- a/test/sharness/t0123-gateway-json-cbor.sh +++ b/test/sharness/t0123-gateway-json-cbor.sh @@ -22,57 +22,65 @@ test_expect_success "Add the test directory" ' echo "$FILE_CID / $FILE_SIZE" ' -test_codec_unixfs () { +test_headers () { name=$1 format=$2 - test_expect_success "GET UnixFS $name with format=dag-$format has expected Content-Type" ' + test_expect_success "GET $name with format=dag-$format has expected Content-Type" ' curl -sD - "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID?format=dag-$format" > curl_output 2>&1 && test_should_contain "Content-Type: application/vnd.ipld.dag-$format" curl_output && test_should_not_contain "Content-Type: application/$format" curl_output ' - test_expect_success "GET UnixFS $name with 'Accept: application/vnd.ipld.dag-$format' has expected Content-Type" ' + test_expect_success "GET $name with 'Accept: application/vnd.ipld.dag-$format' has expected Content-Type" ' curl -sD - -H "Accept: application/vnd.ipld.dag-$format" "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID" > curl_output 2>&1 && test_should_contain "Content-Type: application/vnd.ipld.dag-$format" curl_output && test_should_not_contain "Content-Type: application/$format" curl_output ' - test_expect_success "GET UnixFS $name with format=$format has expected Content-Type" ' + test_expect_success "GET $name with format=$format has expected Content-Type" ' curl -sD - "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID?format=$format" > curl_output 2>&1 && test_should_contain "Content-Type: application/$format" curl_output && test_should_not_contain "Content-Type: application/vnd.ipld.dag-$format" curl_output ' - test_expect_success "GET UnixFS $name with 'Accept: application/$format' has expected Content-Type" ' + test_expect_success "GET $name with 'Accept: application/$format' has expected Content-Type" ' curl -sD - -H "Accept: application/$format" "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID" > curl_output 2>&1 && test_should_contain "Content-Type: application/$format" curl_output && test_should_not_contain "Content-Type: application/vnd.ipld.dag-$format" curl_output ' +} + +test_headers "DAG-JSON" "json" +test_headers "DAG-CBOR" "cbor" - test_expect_success "GET UnixFS $name has expected output for file" ' +test_dag_pb () { + name=$1 + format=$2 + + test_expect_success "GET DAG-PB $name has expected output for file" ' curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$FILE_CID?format=dag-$format" > curl_output 2>&1 && ipfs dag get --output-codec dag-$format $FILE_CID > ipfs_dag_get_output 2>&1 && test_cmp ipfs_dag_get_output curl_output ' - test_expect_success "GET UnixFS $name has expected output for directory" ' + test_expect_success "GET DAG-PB $name has expected output for directory" ' curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DIR_CID?format=dag-$format" > curl_output 2>&1 && ipfs dag get --output-codec dag-$format $DIR_CID > ipfs_dag_get_output 2>&1 && test_cmp ipfs_dag_get_output curl_output ' - test_expect_success "GET UnixFS $name with format=dag-$format and format=$format produce same output" ' + test_expect_success "GET DAG-PB $name with format=dag-$format and format=$format produce same output" ' curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DIR_CID?format=dag-$format" > curl_output_1 2>&1 && curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DIR_CID?format=$format" > curl_output_2 2>&1 && test_cmp curl_output_1 curl_output_2 ' } -test_codec_unixfs "DAG-JSON" "json" -test_codec_unixfs "DAG-CBOR" "cbor" +test_dag_pb "DAG-JSON" "json" +test_dag_pb "DAG-CBOR" "cbor" -test_codec () { +test_cmp_dag_get () { name=$1 format=$2 @@ -91,8 +99,8 @@ test_codec () { ' } -test_codec "JSON" "json" -test_codec "CBOR" "cbor" +test_cmp_dag_get "JSON" "json" +test_cmp_dag_get "CBOR" "cbor" test_expect_success "GET JSON as CBOR produces DAG-CBOR output" ' CID=$(echo "{ \"test\": \"json\" }" | ipfs dag put --input-codec json --store-codec json) && @@ -108,6 +116,39 @@ test_expect_success "GET CBOR as JSON produces DAG-JSON output" ' test_cmp ipfs_dag_get_output curl_output ' +DAG_CBOR_TRAVERSAL_CID="bafyreiehxu373cu3v5gyxyxfsfjryscs7sq6fh3unqcqgqhdfn3n43vrgu" +DAG_JSON_TRAVERSAL_CID="baguqeeraoaeabj5hdfcmpkzfeiwtfwb3qbvfwzbiknqn7itcwsb2fdtu7eta" +DAG_PB_CID="bafybeiegxwlgmoh2cny7qlolykdf7aq7g6dlommarldrbm7c4hbckhfcke" + +test_expect_success "Add CARs for path traversal and DAG-PB representation tests" ' + ipfs dag import ../t0123-gateway-json-cbor/dag-cbor-traversal.car > import_output && + test_should_contain $DAG_CBOR_TRAVERSAL_CID import_output && + ipfs dag import ../t0123-gateway-json-cbor/dag-json-traversal.car > import_output && + test_should_contain $DAG_JSON_TRAVERSAL_CID import_output && + ipfs dag import ../t0123-gateway-json-cbor/dag-pb.car > import_output && + test_should_contain $DAG_PB_CID import_output +' + +test_expect_success "GET DAG-JSON traverses multiple links" ' + curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DAG_CBOR_TRAVERSAL_CID/foo/bar?format=dag-json" > curl_output 2>&1 && + jq --sort-keys . curl_output > actual && + echo "{ \"hello\": \"this is not a link\" }" | jq --sort-keys . > expected && + test_cmp expected actual +' + +test_expect_success "GET DAG-CBOR traverses multiple links" ' + curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DAG_CBOR_TRAVERSAL_CID/foo/bar?format=dag-json" > curl_output 2>&1 && + jq --sort-keys . curl_output > actual && + echo "{ \"hello\": \"this is not a link\" }" | jq --sort-keys . > expected && + test_cmp expected actual +' + +test_expect_success "GET DAG-PB has expected output" ' + curl -s "http://127.0.0.1:$GWAY_PORT/ipfs/$DAG_PB_CID?format=dag-json" > curl_output 2>&1 && + jq --sort-keys . curl_output > actual && + test_cmp ../t0123-gateway-json-cbor/dag-pb.json actual +' + test_kill_ipfs_daemon test_done \ No newline at end of file diff --git a/test/sharness/t0123-gateway-json-cbor/dag-cbor-traversal.car b/test/sharness/t0123-gateway-json-cbor/dag-cbor-traversal.car new file mode 100644 index 000000000000..40a0cd4013ae Binary files /dev/null and b/test/sharness/t0123-gateway-json-cbor/dag-cbor-traversal.car differ diff --git a/test/sharness/t0123-gateway-json-cbor/dag-json-traversal.car b/test/sharness/t0123-gateway-json-cbor/dag-json-traversal.car new file mode 100644 index 000000000000..fa56006d5f4e Binary files /dev/null and b/test/sharness/t0123-gateway-json-cbor/dag-json-traversal.car differ diff --git a/test/sharness/t0123-gateway-json-cbor/dag-pb.car b/test/sharness/t0123-gateway-json-cbor/dag-pb.car new file mode 100644 index 000000000000..a6bb076c7e32 Binary files /dev/null and b/test/sharness/t0123-gateway-json-cbor/dag-pb.car differ diff --git a/test/sharness/t0123-gateway-json-cbor/dag-pb.json b/test/sharness/t0123-gateway-json-cbor/dag-pb.json new file mode 100644 index 000000000000..ab4f9f011a60 --- /dev/null +++ b/test/sharness/t0123-gateway-json-cbor/dag-pb.json @@ -0,0 +1,23 @@ +{ + "Data": { + "/": { + "bytes": "CAE" + } + }, + "Links": [ + { + "Hash": { + "/": "bafybeidryarwh34ygbtyypbu7qjkl4euiwxby6cql6uvosonohkq2kwnkm" + }, + "Name": "foo", + "Tsize": 69 + }, + { + "Hash": { + "/": "bafkreic3ondyhizrzeoufvoodehinugpj3ecruwokaygl7elezhn2khqfa" + }, + "Name": "foo.txt", + "Tsize": 13 + } + ] +}