Skip to content

Commit

Permalink
test: path traversal and dag-pb output
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 19, 2022
1 parent 9eba2a9 commit 2e5a05d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
67 changes: 54 additions & 13 deletions test/sharness/t0123-gateway-json-cbor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) &&
Expand All @@ -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
Binary file not shown.
Binary file not shown.
Binary file added test/sharness/t0123-gateway-json-cbor/dag-pb.car
Binary file not shown.
23 changes: 23 additions & 0 deletions test/sharness/t0123-gateway-json-cbor/dag-pb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Data": {
"/": {
"bytes": "CAE"
}
},
"Links": [
{
"Hash": {
"/": "bafybeidryarwh34ygbtyypbu7qjkl4euiwxby6cql6uvosonohkq2kwnkm"
},
"Name": "foo",
"Tsize": 69
},
{
"Hash": {
"/": "bafkreic3ondyhizrzeoufvoodehinugpj3ecruwokaygl7elezhn2khqfa"
},
"Name": "foo.txt",
"Tsize": 13
}
]
}

0 comments on commit 2e5a05d

Please sign in to comment.