| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2014 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test ipfs repo operations" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | export DEBUG=true |
| 12 | |
| 13 | setup_iptb() { |
| 14 | num_nodes="$1" |
| 15 | bound=$(expr "$num_nodes" - 1) |
| 16 | |
| 17 | test_expect_success "iptb init" ' |
| 18 | iptb testbed create -type localipfs -count $num_nodes -init |
| 19 | ' |
| 20 | |
| 21 | for i in $(test_seq 0 "$bound") |
| 22 | do |
| 23 | test_expect_success "set configs up for node $i" ' |
| 24 | ipfsi "$i" config Ipns.RepublishPeriod 40s && |
| 25 | ipfsi "$i" config --json Ipns.ResolveCacheSize 0 |
| 26 | ' |
| 27 | done |
| 28 | |
| 29 | startup_cluster "$num_nodes" |
| 30 | } |
| 31 | |
| 32 | teardown_iptb() { |
| 33 | test_expect_success "shut down nodes" ' |
| 34 | iptb stop |
| 35 | ' |
| 36 | } |
| 37 | |
| 38 | verify_can_resolve() { |
| 39 | num_nodes="$1" |
| 40 | bound=$(expr "$num_nodes" - 1) |
| 41 | name="$2" |
| 42 | expected="$3" |
| 43 | msg="$4" |
| 44 | |
| 45 | for node in $(test_seq 0 "$bound") |
| 46 | do |
| 47 | test_expect_success "$msg: node $node can resolve entry" ' |
| 48 | ipfsi "$node" name resolve "$name" > resolve |
| 49 | ' |
| 50 | |
| 51 | test_expect_success "$msg: output for node $node looks right" ' |
| 52 | printf "/ipfs/$expected\n" > expected && |
| 53 | test_cmp expected resolve |
| 54 | ' |
| 55 | done |
| 56 | } |
| 57 | |
| 58 | verify_cannot_resolve() { |
| 59 | num_nodes="$1" |
| 60 | bound=$(expr "$num_nodes" - 1) |
| 61 | name="$2" |
| 62 | msg="$3" |
| 63 | |
| 64 | for node in $(test_seq 0 "$bound") |
| 65 | do |
| 66 | test_expect_success "$msg: resolution fails on node $node" ' |
| 67 | test_expect_code 1 ipfsi "$node" name resolve "$name" |
| 68 | ' |
| 69 | done |
| 70 | } |
| 71 | |
| 72 | num_test_nodes=4 |
| 73 | |
| 74 | setup_iptb "$num_test_nodes" |
| 75 | |
| 76 | test_expect_success "publish succeeds" ' |
| 77 | HASH=$(date +"%FT%T.%N%z" | ipfsi 1 add -q) && |
| 78 | ipfsi 1 name publish -t 10s $HASH |
| 79 | ' |
| 80 | |
| 81 | test_expect_success "get id succeeds" ' |
| 82 | id=$(ipfsi 1 id -f "<id>") |
| 83 | ' |
| 84 | |
| 85 | verify_can_resolve "$num_test_nodes" "$id" "$HASH" "just after publishing" |
| 86 | |
| 87 | go-sleep 10s |
| 88 | |
| 89 | verify_cannot_resolve "$num_test_nodes" "$id" "after 10 seconds, records are invalid" |
| 90 | |
| 91 | go-sleep 30s |
| 92 | |
| 93 | verify_can_resolve "$num_test_nodes" "$id" "$HASH" "republisher fires after 30 seconds" |
| 94 | |
| 95 | # |
| 96 | |
| 97 | test_expect_success "generate new key" ' |
| 98 | KEY2=`ipfsi 1 key gen beepboop --type ed25519` |
| 99 | ' |
| 100 | |
| 101 | test_expect_success "publish with new key succeeds" ' |
| 102 | HASH=$(date +"%FT%T.%N%z" | ipfsi 1 add -q) && |
| 103 | ipfsi 1 name publish -t 10s -k "$KEY2" $HASH |
| 104 | ' |
| 105 | |
| 106 | verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "new key just after publishing" |
| 107 | |
| 108 | go-sleep 10s |
| 109 | |
| 110 | verify_cannot_resolve "$num_test_nodes" "$KEY2" "new key cannot resolve after 10 seconds" |
| 111 | |
| 112 | go-sleep 30s |
| 113 | |
| 114 | verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "new key can resolve again after republish" |
| 115 | |
| 116 | # |
| 117 | |
| 118 | teardown_iptb |
| 119 | |
| 120 | test_done |