| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # MIT Licensed; see the LICENSE file in this repository. |
| 4 | # |
| 5 | |
| 6 | test_description="Test daemon command" |
| 7 | |
| 8 | . lib/test-lib.sh |
| 9 | |
| 10 | test_init_ipfs |
| 11 | |
| 12 | differentport=$((API_PORT + 1)) |
| 13 | api_other="/ip4/127.0.0.1/tcp/$differentport" |
| 14 | api_unreachable="/ip4/127.0.0.1/tcp/1" |
| 15 | |
| 16 | test_expect_success "config setup" ' |
| 17 | peerid=$(ipfs config Identity.PeerID) && |
| 18 | test_check_peerid "$peerid" |
| 19 | ' |
| 20 | |
| 21 | test_client() { |
| 22 | opts="$@" |
| 23 | echo "OPTS = " $opts |
| 24 | test_expect_success "client must work properly $state" ' |
| 25 | printf "$peerid" >expected && |
| 26 | ipfs id -f="<id>" $opts >actual && |
| 27 | test_cmp expected actual |
| 28 | ' |
| 29 | } |
| 30 | |
| 31 | test_client_must_fail() { |
| 32 | opts="$@" |
| 33 | echo "OPTS = " $opts |
| 34 | test_expect_success "client should fail $state" ' |
| 35 | echo "Error: cannot connect to the api. Is the daemon running? To run as a standalone CLI command remove the api file in \`\$IPFS_PATH/api\`" >expected_err && |
| 36 | test_must_fail ipfs id -f="<id>" $opts >actual 2>actual_err && |
| 37 | test_cmp expected_err actual_err |
| 38 | ' |
| 39 | } |
| 40 | |
| 41 | test_client_suite() { |
| 42 | state="$1" |
| 43 | cfg_success="$2" |
| 44 | diff_success="$3" |
| 45 | api_fromcfg="$4" |
| 46 | api_different="$5" |
| 47 | |
| 48 | # must always work |
| 49 | test_client |
| 50 | |
| 51 | # must always err |
| 52 | test_client_must_fail --api "$api_unreachable" |
| 53 | |
| 54 | if [ "$cfg_success" = true ]; then |
| 55 | test_client --api "$api_fromcfg" |
| 56 | else |
| 57 | test_client_must_fail --api "$api_fromcfg" |
| 58 | fi |
| 59 | |
| 60 | if [ "$diff_success" = true ]; then |
| 61 | test_client --api "$api_different" |
| 62 | else |
| 63 | test_client_must_fail --api "$api_different" |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # first, test things without daemon, without /api file |
| 68 | # with no daemon, everything should fail |
| 69 | # (using unreachable because API_MADDR doesn't get set until daemon start) |
| 70 | test_client_suite "(daemon off, no --api, no /api file)" false false "$api_unreachable" "$api_other" |
| 71 | |
| 72 | |
| 73 | # then, test things with daemon, with /api file |
| 74 | |
| 75 | test_launch_ipfs_daemon |
| 76 | |
| 77 | test_expect_success "'ipfs daemon' creates api file" ' |
| 78 | test -f ".ipfs/api" |
| 79 | ' |
| 80 | |
| 81 | test_client_suite "(daemon on, no --api, /api file from cfg)" true false "$API_MADDR" "$api_other" |
| 82 | |
| 83 | # then, test things without daemon, with /api file |
| 84 | |
| 85 | test_kill_ipfs_daemon |
| 86 | |
| 87 | # again, both should fail |
| 88 | test_client_suite "(daemon off, no --api, /api file from cfg)" false false "$API_MADDR" "$api_other" |
| 89 | |
| 90 | test_done |