| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | test_description="Test resolve command" |
| 4 | |
| 5 | . lib/test-lib.sh |
| 6 | |
| 7 | test_init_ipfs |
| 8 | |
| 9 | test_expect_success "resolve: prepare files" ' |
| 10 | mkdir -p a/b && |
| 11 | echo "a/b/c" >a/b/c && |
| 12 | a_hash=$(ipfs add -Q -r a) && |
| 13 | b_hash=$(ipfs add -Q -r a/b) && |
| 14 | c_hash=$(ipfs add -Q -r a/b/c) && |
| 15 | a_hash_b32=$(cid-fmt -v 1 -b b %s $a_hash) && |
| 16 | b_hash_b32=$(cid-fmt -v 1 -b b %s $b_hash) && |
| 17 | c_hash_b32=$(cid-fmt -v 1 -b b %s $c_hash) |
| 18 | ' |
| 19 | |
| 20 | test_expect_success "resolve: prepare dag" ' |
| 21 | dag_hash=$(ipfs dag put <<<"{\"i\": {\"j\": {\"k\": \"asdfasdfasdf\"}}}") |
| 22 | ' |
| 23 | |
| 24 | test_expect_success "resolve: prepare keys" ' |
| 25 | self_hash=$(ipfs key list --ipns-base=base36 -l | grep self | cut -d " " -f1) && |
| 26 | alt_hash=$(ipfs key gen --ipns-base=base36 -t rsa alt) |
| 27 | ' |
| 28 | |
| 29 | test_resolve_setup_name() { |
| 30 | local key="$1" |
| 31 | local ref="$2" |
| 32 | |
| 33 | # we pass here --ttl=0s to ensure that it does not get cached by namesys. |
| 34 | # the alternative would be to wait between tests to ensure that the namesys |
| 35 | # cache gets purged in time, but that adds runtime time for the tests. |
| 36 | test_expect_success "resolve: prepare $key" ' |
| 37 | ipfs name publish --key="$key" --ttl=0s --allow-offline "$ref" |
| 38 | ' |
| 39 | } |
| 40 | |
| 41 | test_resolve() { |
| 42 | src=$1 |
| 43 | dst=$2 |
| 44 | extra=$3 |
| 45 | |
| 46 | test_expect_success "resolve succeeds: $src" ' |
| 47 | ipfs resolve $extra "$src" >actual |
| 48 | ' |
| 49 | |
| 50 | test_expect_success "resolved correctly: $src -> $dst" ' |
| 51 | printf "$dst\n" >expected && |
| 52 | test_cmp expected actual |
| 53 | ' |
| 54 | } |
| 55 | |
| 56 | test_resolve_cmd() { |
| 57 | echo '-- starting test_resolve_cmd' |
| 58 | test_resolve "/ipfs/$a_hash" "/ipfs/$a_hash" |
| 59 | test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash" |
| 60 | test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash" |
| 61 | test_resolve "/ipfs/$b_hash/c" "/ipfs/$c_hash" |
| 62 | test_resolve "/ipld/$dag_hash/i/j/k" "/ipld/$dag_hash/i/j/k" |
| 63 | test_resolve "/ipld/$dag_hash/i/j" "/ipld/$dag_hash/i/j" |
| 64 | test_resolve "/ipld/$dag_hash/i" "/ipld/$dag_hash/i" |
| 65 | |
| 66 | test_resolve_setup_name "self" "/ipfs/$a_hash" |
| 67 | test_resolve "/ipns/$self_hash" "/ipfs/$a_hash" |
| 68 | test_resolve "/ipns/$self_hash/b" "/ipfs/$b_hash" |
| 69 | test_resolve "/ipns/$self_hash/b/c" "/ipfs/$c_hash" |
| 70 | |
| 71 | test_resolve_setup_name "self" "/ipfs/$b_hash" |
| 72 | test_resolve "/ipns/$self_hash" "/ipfs/$b_hash" |
| 73 | test_resolve "/ipns/$self_hash/c" "/ipfs/$c_hash" |
| 74 | |
| 75 | test_resolve_setup_name "self" "/ipfs/$c_hash" |
| 76 | test_resolve "/ipns/$self_hash" "/ipfs/$c_hash" |
| 77 | |
| 78 | # simple recursion succeeds |
| 79 | test_resolve_setup_name "alt" "/ipns/$self_hash" |
| 80 | test_resolve "/ipns/$alt_hash" "/ipfs/$c_hash" |
| 81 | |
| 82 | # partial resolve succeeds |
| 83 | test_resolve "/ipns/$alt_hash" "/ipns/$self_hash" -r=false |
| 84 | |
| 85 | # infinite recursion fails |
| 86 | test_resolve_setup_name "self" "/ipns/$self_hash" |
| 87 | test_expect_success "recursive resolve terminates" ' |
| 88 | test_expect_code 1 ipfs resolve /ipns/$self_hash 2>recursion_error && |
| 89 | grep "recursion limit exceeded" recursion_error |
| 90 | ' |
| 91 | } |
| 92 | |
| 93 | test_resolve_cmd_b32() { |
| 94 | echo '-- starting test_resolve_cmd_b32' |
| 95 | # no flags needed, base should be preserved |
| 96 | |
| 97 | test_resolve "/ipfs/$a_hash_b32" "/ipfs/$a_hash_b32" |
| 98 | test_resolve "/ipfs/$a_hash_b32/b" "/ipfs/$b_hash_b32" |
| 99 | test_resolve "/ipfs/$a_hash_b32/b/c" "/ipfs/$c_hash_b32" |
| 100 | test_resolve "/ipfs/$b_hash_b32/c" "/ipfs/$c_hash_b32" |
| 101 | |
| 102 | # flags needed passed in path does not contain cid to derive base |
| 103 | |
| 104 | test_resolve_setup_name "self" "/ipfs/$a_hash_b32" |
| 105 | test_resolve "/ipns/$self_hash" "/ipfs/$a_hash_b32" --cid-base=base32 |
| 106 | test_resolve "/ipns/$self_hash/b" "/ipfs/$b_hash_b32" --cid-base=base32 |
| 107 | test_resolve "/ipns/$self_hash/b/c" "/ipfs/$c_hash_b32" --cid-base=base32 |
| 108 | |
| 109 | test_resolve_setup_name "self" "/ipfs/$b_hash_b32" --cid-base=base32 |
| 110 | test_resolve "/ipns/$self_hash" "/ipfs/$b_hash_b32" --cid-base=base32 |
| 111 | test_resolve "/ipns/$self_hash/c" "/ipfs/$c_hash_b32" --cid-base=base32 |
| 112 | |
| 113 | test_resolve_setup_name "self" "/ipfs/$c_hash_b32" |
| 114 | test_resolve "/ipns/$self_hash" "/ipfs/$c_hash_b32" --cid-base=base32 |
| 115 | |
| 116 | # peer ID represented as CIDv1 require libp2p-key multicodec |
| 117 | # https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md |
| 118 | local self_hash_b32protobuf=$(echo $self_hash | ipfs cid format -v 1 -b b --mc dag-pb) |
| 119 | local self_hash_b32libp2pkey=$(echo $self_hash | ipfs cid format -v 1 -b b --mc libp2p-key) |
| 120 | test_expect_success "resolve of /ipns/{cidv1} with multicodec other than libp2p-key returns a meaningful error" ' |
| 121 | test_expect_code 1 ipfs resolve /ipns/$self_hash_b32protobuf 2>cidcodec_error && |
| 122 | test_should_contain "Error: peer ID represented as CIDv1 require libp2p-key multicodec: retry with /ipns/$self_hash_b32libp2pkey" cidcodec_error |
| 123 | ' |
| 124 | } |
| 125 | |
| 126 | test_resolve_cmd_success() { |
| 127 | test_resolve "/ipfs/$a_hash" "/ipfs/$a_hash" |
| 128 | test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash" |
| 129 | test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash" |
| 130 | test_resolve "/ipfs/$b_hash/c" "/ipfs/$c_hash" |
| 131 | test_resolve "/ipld/$dag_hash" "/ipld/$dag_hash" |
| 132 | test_resolve "/ipld/$dag_hash/i/j/k" "/ipld/$dag_hash/i/j/k" |
| 133 | test_resolve "/ipld/$dag_hash/i/j" "/ipld/$dag_hash/i/j" |
| 134 | test_resolve "/ipld/$dag_hash/i" "/ipld/$dag_hash/i" |
| 135 | |
| 136 | test_resolve_setup_name "self" "/ipfs/$a_hash" |
| 137 | test_resolve "/ipns/$self_hash" "/ipfs/$a_hash" |
| 138 | test_resolve "/ipns/$self_hash/b" "/ipfs/$b_hash" |
| 139 | test_resolve "/ipns/$self_hash/b/c" "/ipfs/$c_hash" |
| 140 | |
| 141 | test_resolve_setup_name "self" "/ipfs/$b_hash" |
| 142 | test_resolve "/ipns/$self_hash" "/ipfs/$b_hash" |
| 143 | test_resolve "/ipns/$self_hash/c" "/ipfs/$c_hash" |
| 144 | |
| 145 | test_resolve_setup_name "self" "/ipfs/$c_hash" |
| 146 | test_resolve "/ipns/$self_hash" "/ipfs/$c_hash" |
| 147 | } |
| 148 | |
| 149 | # should work offline |
| 150 | test_resolve_cmd |
| 151 | test_resolve_cmd_b32 |
| 152 | |
| 153 | # should work online |
| 154 | test_launch_ipfs_daemon |
| 155 | test_resolve_cmd_success |
| 156 | test_kill_ipfs_daemon |
| 157 | |
| 158 | test_done |