| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | test_description="Test IPNS pubsub" |
| 4 | |
| 5 | . lib/test-lib.sh |
| 6 | |
| 7 | # start iptb + wait for peering |
| 8 | NUM_NODES=5 |
| 9 | test_expect_success 'init iptb' ' |
| 10 | iptb testbed create -type localipfs -count $NUM_NODES -init |
| 11 | ' |
| 12 | |
| 13 | run_ipnspubsub_tests() { |
| 14 | |
| 15 | test_expect_success 'peer ids' ' |
| 16 | PEERID_0_BASE36=$(ipfsi 0 key list --ipns-base=base36 -l | grep self | head -n 1 | cut -d " " -f1) && |
| 17 | PEERID_0_B58MH=$(ipfsi 0 key list --ipns-base=b58mh -l | grep self | head -n 1 | cut -d " " -f1) |
| 18 | ' |
| 19 | |
| 20 | test_expect_success 'check namesys pubsub state' ' |
| 21 | echo enabled > expected && |
| 22 | ipfsi 0 name pubsub state > state0 && |
| 23 | ipfsi 1 name pubsub state > state1 && |
| 24 | ipfsi 2 name pubsub state > state2 && |
| 25 | test_cmp expected state0 && |
| 26 | test_cmp expected state1 && |
| 27 | test_cmp expected state2 |
| 28 | ' |
| 29 | |
| 30 | # These commands are *expected* to fail. We haven't published anything yet. |
| 31 | test_expect_success 'subscribe nodes to the publisher topic' ' |
| 32 | ipfsi 1 name resolve /ipns/$PEERID_0_BASE36 --timeout=1s; |
| 33 | ipfsi 2 name resolve /ipns/$PEERID_0_BASE36 --timeout=1s; |
| 34 | true |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'check subscriptions' ' |
| 38 | echo /ipns/$PEERID_0_BASE36 > expected_base36 && |
| 39 | echo /ipns/$PEERID_0_B58MH > expected_b58mh && |
| 40 | ipfsi 1 name pubsub subs > subs1 && |
| 41 | ipfsi 2 name pubsub subs > subs2 && |
| 42 | ipfsi 1 name pubsub subs --ipns-base=b58mh > subs1_b58mh && |
| 43 | ipfsi 2 name pubsub subs --ipns-base=b58mh > subs2_b58mh && |
| 44 | test_cmp expected_base36 subs1 && |
| 45 | test_cmp expected_base36 subs2 && |
| 46 | test_cmp expected_b58mh subs1_b58mh && |
| 47 | test_cmp expected_b58mh subs2_b58mh |
| 48 | ' |
| 49 | |
| 50 | test_expect_success 'add an object on publisher node' ' |
| 51 | echo "ipns is super fun" > file && |
| 52 | HASH_FILE=$(ipfsi 0 add -q file) |
| 53 | ' |
| 54 | |
| 55 | test_expect_success 'publish that object as an ipns entry' ' |
| 56 | ipfsi 0 name publish $HASH_FILE |
| 57 | ' |
| 58 | |
| 59 | test_expect_success 'wait for the flood' ' |
| 60 | sleep 1 |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'resolve name in subscriber nodes' ' |
| 64 | echo "/ipfs/$HASH_FILE" > expected && |
| 65 | ipfsi 1 name resolve /ipns/$PEERID_0_BASE36 > name1 && |
| 66 | ipfsi 2 name resolve /ipns/$PEERID_0_BASE36 > name2 && |
| 67 | test_cmp expected name1 && |
| 68 | test_cmp expected name2 |
| 69 | ' |
| 70 | |
| 71 | test_expect_success 'cancel subscriptions to the publisher topic' ' |
| 72 | ipfsi 1 name pubsub cancel /ipns/$PEERID_0_BASE36 && |
| 73 | ipfsi 2 name pubsub cancel /ipns/$PEERID_0_BASE36 |
| 74 | ' |
| 75 | |
| 76 | test_expect_success 'check subscriptions' ' |
| 77 | rm -f expected && touch expected && |
| 78 | ipfsi 1 name pubsub subs > subs1 && |
| 79 | ipfsi 2 name pubsub subs > subs2 && |
| 80 | test_cmp expected subs1 && |
| 81 | test_cmp expected subs2 |
| 82 | ' |
| 83 | |
| 84 | test_expect_success "shut down iptb" ' |
| 85 | iptb stop |
| 86 | ' |
| 87 | |
| 88 | } |
| 89 | |
| 90 | # Test everything with ipns-pubsub enabled via config |
| 91 | test_expect_success 'enable ipns over pubsub' ' |
| 92 | iptb run -- ipfs config --json Ipns.UsePubsub true |
| 93 | ' |
| 94 | |
| 95 | startup_cluster $NUM_NODES |
| 96 | run_ipnspubsub_tests |
| 97 | |
| 98 | # Test again, this time CLI parameter override the config |
| 99 | test_expect_success 'enable ipns over pubsub' ' |
| 100 | iptb run -- ipfs config --json Ipns.UsePubsub false |
| 101 | ' |
| 102 | startup_cluster $NUM_NODES --enable-namesys-pubsub |
| 103 | run_ipnspubsub_tests |
| 104 | |
| 105 | # Confirm negative CLI flag takes precedence over positive config |
| 106 | |
| 107 | test_expect_success 'enable the pubsub-ipns via config' ' |
| 108 | iptb run -- ipfs config --json Ipns.UsePubsub true |
| 109 | ' |
| 110 | startup_cluster $NUM_NODES --enable-namesys-pubsub=false |
| 111 | |
| 112 | test_expect_success 'ipns pubsub cmd fails because it was disabled via cli flag' ' |
| 113 | test_expect_code 1 ipfsi 1 name pubsub subs 2> pubsubipns_cmd_out |
| 114 | ' |
| 115 | |
| 116 | test_expect_success "ipns pubsub cmd produces error" " |
| 117 | echo -e \"Error: IPNS pubsub subsystem is not enabled\nUse 'ipfs name pubsub subs --help' for information about this command\" > expected && |
| 118 | test_cmp expected pubsubipns_cmd_out |
| 119 | " |
| 120 | |
| 121 | test_expect_success 'stop iptb' ' |
| 122 | iptb stop |
| 123 | ' |
| 124 | |
| 125 | test_done |