| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2015 Jeromy Johnson |
| 4 | # MIT Licensed; see the LICENSE file in this repository. |
| 5 | # |
| 6 | |
| 7 | test_description="Test private network feature" |
| 8 | |
| 9 | . lib/test-lib.sh |
| 10 | |
| 11 | test_init_ipfs |
| 12 | |
| 13 | test_expect_success "disable AutoConf for private network tests" ' |
| 14 | ipfs config --json AutoConf.Enabled false |
| 15 | ' |
| 16 | |
| 17 | export LIBP2P_FORCE_PNET=1 |
| 18 | |
| 19 | test_expect_success "daemon won't start with force pnet env but with no key" ' |
| 20 | test_must_fail go-timeout 5 ipfs daemon > stdout 2>&1 |
| 21 | ' |
| 22 | |
| 23 | unset LIBP2P_FORCE_PNET |
| 24 | |
| 25 | test_expect_success "daemon output includes info about the reason" ' |
| 26 | grep "private network was not configured but is enforced by the environment" stdout || |
| 27 | test_fsh cat stdout |
| 28 | ' |
| 29 | |
| 30 | pnet_key() { |
| 31 | echo '/key/swarm/psk/1.0.0/' |
| 32 | echo '/bin/' |
| 33 | random-data -size=32 |
| 34 | } |
| 35 | |
| 36 | pnet_key > "${IPFS_PATH}/swarm.key" |
| 37 | |
| 38 | LIBP2P_FORCE_PNET=1 test_launch_ipfs_daemon |
| 39 | |
| 40 | test_expect_success "set up iptb testbed" ' |
| 41 | iptb testbed create -type localipfs -count 5 -force -init && |
| 42 | iptb run -- ipfs config --json "Routing.LoopbackAddressesOnLanDHT" true && |
| 43 | iptb run -- ipfs config --json "Swarm.Transports.Network.Websocket" false && |
| 44 | iptb run -- ipfs config --json Addresses.Swarm '"'"'["/ip4/127.0.0.1/tcp/0"]'"'"' && |
| 45 | iptb run -- ipfs config --json AutoConf.Enabled false |
| 46 | ' |
| 47 | |
| 48 | set_key() { |
| 49 | node="$1" |
| 50 | keyfile="$2" |
| 51 | |
| 52 | cp "$keyfile" "${IPTB_ROOT}/testbeds/default/${node}/swarm.key" |
| 53 | } |
| 54 | |
| 55 | pnet_key > key1 |
| 56 | pnet_key > key2 |
| 57 | |
| 58 | set_key 1 key1 |
| 59 | set_key 2 key1 |
| 60 | |
| 61 | set_key 3 key2 |
| 62 | set_key 4 key2 |
| 63 | |
| 64 | unset LIBP2P_FORCE_PNET |
| 65 | |
| 66 | test_expect_success "start nodes" ' |
| 67 | iptb start -wait [0-4] |
| 68 | ' |
| 69 | |
| 70 | test_expect_success "try connecting node in public network with priv networks" ' |
| 71 | test_must_fail iptb connect --timeout=2s [1-4] 0 |
| 72 | ' |
| 73 | |
| 74 | test_expect_success "node 0 (public network) swarm is empty" ' |
| 75 | ipfsi 0 swarm peers && |
| 76 | [ $(ipfsi 0 swarm peers | wc -l) -eq 0 ] |
| 77 | ' |
| 78 | |
| 79 | test_expect_success "try connecting nodes in different private networks" ' |
| 80 | test_must_fail iptb connect 2 3 |
| 81 | ' |
| 82 | |
| 83 | test_expect_success "node 3 (pnet 2) swarm is empty" ' |
| 84 | ipfsi 3 swarm peers && |
| 85 | [ $(ipfsi 3 swarm peers | wc -l) -eq 0 ] |
| 86 | ' |
| 87 | |
| 88 | test_expect_success "connect nodes in the same pnet" ' |
| 89 | iptb connect 1 2 && |
| 90 | iptb connect 3 4 |
| 91 | ' |
| 92 | |
| 93 | test_expect_success "nodes 1 and 2 have connected" ' |
| 94 | ipfsi 2 swarm peers && |
| 95 | [ $(ipfsi 2 swarm peers | wc -l) -eq 1 ] |
| 96 | ' |
| 97 | |
| 98 | test_expect_success "nodes 3 and 4 have connected" ' |
| 99 | ipfsi 4 swarm peers && |
| 100 | [ $(ipfsi 4 swarm peers | wc -l) -eq 1 ] |
| 101 | ' |
| 102 | |
| 103 | |
| 104 | run_single_file_test() { |
| 105 | node1=$1 |
| 106 | node2=$2 |
| 107 | |
| 108 | test_expect_success "add a file on node$node1" ' |
| 109 | random-data -size=1000000 > filea && |
| 110 | FILEA_HASH=$(ipfsi $node1 add -q filea) |
| 111 | ' |
| 112 | |
| 113 | check_file_fetch $node1 $FILEA_HASH filea |
| 114 | check_file_fetch $node2 $FILEA_HASH filea |
| 115 | } |
| 116 | |
| 117 | check_file_fetch() { |
| 118 | node="$1" |
| 119 | fhash="$2" |
| 120 | fname="$3" |
| 121 | |
| 122 | test_expect_success "can fetch file" ' |
| 123 | ipfsi $node cat $fhash > fetch_out |
| 124 | ' |
| 125 | |
| 126 | test_expect_success "file looks good" ' |
| 127 | test_cmp $fname fetch_out |
| 128 | ' |
| 129 | } |
| 130 | |
| 131 | run_single_file_test 1 2 |
| 132 | run_single_file_test 2 1 |
| 133 | |
| 134 | run_single_file_test 3 4 |
| 135 | run_single_file_test 4 3 |
| 136 | |
| 137 | |
| 138 | test_expect_success "stop testbed" ' |
| 139 | iptb stop |
| 140 | ' |
| 141 | |
| 142 | test_kill_ipfs_daemon |
| 143 | |
| 144 | # Test that AutoConf with default mainnet URL fails on private networks |
| 145 | test_expect_success "setup test repo with AutoConf enabled and private network" ' |
| 146 | export IPFS_PATH="$(pwd)/.ipfs-autoconf-test" && |
| 147 | ipfs init --profile=test > /dev/null && |
| 148 | ipfs config --json AutoConf.Enabled true && |
| 149 | pnet_key > "${IPFS_PATH}/swarm.key" |
| 150 | ' |
| 151 | |
| 152 | test_expect_success "daemon fails with AutoConf + private network error" ' |
| 153 | export IPFS_PATH="$(pwd)/.ipfs-autoconf-test" && |
| 154 | test_expect_code 1 ipfs daemon > autoconf_stdout 2> autoconf_stderr |
| 155 | ' |
| 156 | |
| 157 | test_expect_success "error message mentions AutoConf and private network conflict" ' |
| 158 | grep "AutoConf cannot use the default mainnet URL" autoconf_stderr > /dev/null && |
| 159 | grep "private network.*swarm.key" autoconf_stderr > /dev/null && |
| 160 | grep "AutoConf.Enabled=false" autoconf_stderr > /dev/null |
| 161 | ' |
| 162 | |
| 163 | test_done |