| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | test_description="Test config command" |
| 4 | |
| 5 | . lib/test-lib.sh |
| 6 | |
| 7 | # we use a function so that we can run it both offline + online |
| 8 | test_config_cmd_set() { |
| 9 | |
| 10 | # flags (like --bool in "ipfs config --bool") |
| 11 | cfg_flags="" # unset in case. |
| 12 | test "$#" = 3 && { cfg_flags=$1; shift; } |
| 13 | |
| 14 | cfg_key=$1 |
| 15 | cfg_val=$2 |
| 16 | test_expect_success "ipfs config succeeds" " |
| 17 | ipfs config $cfg_flags \"$cfg_key\" \"$cfg_val\" |
| 18 | " |
| 19 | |
| 20 | test_expect_success "ipfs config output looks good" " |
| 21 | echo \"$cfg_val\" >expected && |
| 22 | if [$cfg_flags != \"--json\"]; then |
| 23 | ipfs config \"$cfg_key\" >actual && |
| 24 | test_cmp expected actual |
| 25 | else |
| 26 | ipfs config \"$cfg_key\" | tr -d \"\\n\\t \" >actual && |
| 27 | echo >>actual && |
| 28 | test_cmp expected actual |
| 29 | fi |
| 30 | " |
| 31 | } |
| 32 | |
| 33 | test_profile_apply_revert() { |
| 34 | profile=$1 |
| 35 | inverse_profile=$2 |
| 36 | |
| 37 | test_expect_success "save expected config" ' |
| 38 | ipfs config show >expected |
| 39 | ' |
| 40 | |
| 41 | test_expect_success "'ipfs config profile apply ${profile}' works" ' |
| 42 | ipfs config profile apply '${profile}' |
| 43 | ' |
| 44 | |
| 45 | test_expect_success "profile ${profile} changed something" ' |
| 46 | ipfs config show >actual && |
| 47 | test_must_fail test_cmp expected actual |
| 48 | ' |
| 49 | |
| 50 | test_expect_success "'ipfs config profile apply ${inverse_profile}' works" ' |
| 51 | ipfs config profile apply '${inverse_profile}' |
| 52 | ' |
| 53 | |
| 54 | test_expect_success "config is back to previous state after ${inverse_profile} was applied" ' |
| 55 | ipfs config show >actual && |
| 56 | test_cmp expected actual |
| 57 | ' |
| 58 | } |
| 59 | |
| 60 | test_profile_apply_dry_run_not_alter() { |
| 61 | profile=$1 |
| 62 | |
| 63 | test_expect_success "'ipfs config profile apply ${profile} --dry-run' doesn't alter config" ' |
| 64 | cat "$IPFS_PATH/config" >expected && |
| 65 | ipfs config profile apply '${profile}' --dry-run && |
| 66 | cat "$IPFS_PATH/config" >actual && |
| 67 | test_cmp expected actual |
| 68 | ' |
| 69 | } |
| 70 | |
| 71 | test_config_cmd() { |
| 72 | test_config_cmd_set "Addresses.API" "foo" |
| 73 | test_config_cmd_set "Addresses.Gateway" "bar" |
| 74 | test_config_cmd_set "Datastore.GCPeriod" "baz" |
| 75 | test_config_cmd_set "AutoNAT.ServiceMode" "enabled" |
| 76 | test_config_cmd_set "--bool" "Discovery.MDNS.Enabled" "true" |
| 77 | test_config_cmd_set "--bool" "Discovery.MDNS.Enabled" "false" |
| 78 | test_config_cmd_set "--json" "Datastore.HashOnRead" "true" |
| 79 | test_config_cmd_set "--json" "Datastore.HashOnRead" "false" |
| 80 | test_config_cmd_set "--json" "Experimental.FilestoreEnabled" "true" |
| 81 | test_config_cmd_set "--json" "Import.BatchMaxSize" "null" |
| 82 | test_config_cmd_set "--json" "Import.UnixFSRawLeaves" "true" |
| 83 | test_config_cmd_set "--json" "Routing.Routers.Test" "{\\\"Parameters\\\":\\\"Test\\\",\\\"Type\\\":\\\"Test\\\"}" |
| 84 | test_config_cmd_set "--json" "Experimental.OptimisticProvideJobsPoolSize" "1337" |
| 85 | test_config_cmd_set "--json" "Addresses.Swarm" "[\\\"test\\\",\\\"test\\\",\\\"test\\\"]" |
| 86 | test_config_cmd_set "--json" "Gateway.PublicGateways.Foo" "{\\\"DeserializedResponses\\\":true,\\\"InlineDNSLink\\\":false,\\\"NoDNSLink\\\":false,\\\"Paths\\\":[\\\"Bar\\\",\\\"Baz\\\"],\\\"UseSubdomains\\\":true}" |
| 87 | test_config_cmd_set "--bool" "Gateway.PublicGateways.Foo.UseSubdomains" "false" |
| 88 | |
| 89 | test_expect_success "'ipfs config show' works" ' |
| 90 | ipfs config show >actual |
| 91 | ' |
| 92 | |
| 93 | test_expect_success "'ipfs config show' output looks good" ' |
| 94 | grep "\"API\": \"foo\"," actual && |
| 95 | grep "\"Gateway\": \"bar\"" actual && |
| 96 | grep "\"Enabled\": false" actual && |
| 97 | grep "\"HashOnRead\": false" actual |
| 98 | ' |
| 99 | |
| 100 | test_expect_success "'ipfs config show --config-file' works" ' |
| 101 | mv "$IPFS_PATH/config" "$IPFS_PATH/config-moved" && |
| 102 | ipfs config --config-file "$IPFS_PATH/config-moved" show >moved && |
| 103 | test_cmp moved actual && |
| 104 | mv "$IPFS_PATH/config-moved" "$IPFS_PATH/config" |
| 105 | ' |
| 106 | |
| 107 | test_expect_success "setup for config replace test" ' |
| 108 | cp "$IPFS_PATH/config" newconfig.json && |
| 109 | sed -i"~" -e /PrivKey/d -e s/10GB/11GB/ newconfig.json && |
| 110 | sed -i"~" -e '"'"'/PeerID/ {'"'"' -e '"'"' s/,$// '"'"' -e '"'"' } '"'"' newconfig.json |
| 111 | ' |
| 112 | |
| 113 | test_expect_success "run 'ipfs config replace'" ' |
| 114 | ipfs config replace - < newconfig.json |
| 115 | ' |
| 116 | |
| 117 | test_expect_success "check resulting config after 'ipfs config replace'" ' |
| 118 | sed -e /PrivKey/d "$IPFS_PATH/config" > replconfig.json && |
| 119 | sed -i"~" -e '"'"'/PeerID/ {'"'"' -e '"'"' s/,$// '"'"' -e '"'"' } '"'"' replconfig.json && |
| 120 | test_cmp replconfig.json newconfig.json |
| 121 | ' |
| 122 | |
| 123 | # SECURITY |
| 124 | # Those tests are here to prevent exposing the PrivKey on the network |
| 125 | |
| 126 | test_expect_success "'ipfs config Identity' fails" ' |
| 127 | test_expect_code 1 ipfs config Identity 2> ident_out |
| 128 | ' |
| 129 | |
| 130 | test_expect_success "output looks good" ' |
| 131 | echo "Error: cannot show or change private key through API" > ident_exp && |
| 132 | test_cmp ident_exp ident_out |
| 133 | ' |
| 134 | |
| 135 | test_expect_success "'ipfs config Identity.PrivKey' fails" ' |
| 136 | test_expect_code 1 ipfs config Identity.PrivKey 2> ident_out |
| 137 | ' |
| 138 | |
| 139 | test_expect_success "output looks good" ' |
| 140 | test_cmp ident_exp ident_out |
| 141 | ' |
| 142 | |
| 143 | test_expect_success "lower cased PrivKey" ' |
| 144 | sed -i"~" -e '\''s/PrivKey/privkey/'\'' "$IPFS_PATH/config" && |
| 145 | test_expect_code 1 ipfs config Identity.privkey 2> ident_out |
| 146 | ' |
| 147 | |
| 148 | test_expect_success "output looks good" ' |
| 149 | test_cmp ident_exp ident_out |
| 150 | ' |
| 151 | |
| 152 | test_expect_success "fix it back" ' |
| 153 | sed -i"~" -e '\''s/privkey/PrivKey/'\'' "$IPFS_PATH/config" |
| 154 | ' |
| 155 | |
| 156 | test_expect_success "'ipfs config show' doesn't include privkey" ' |
| 157 | ipfs config show > show_config && |
| 158 | test_expect_code 1 grep PrivKey show_config |
| 159 | ' |
| 160 | |
| 161 | test_expect_success "'ipfs config replace' injects privkey back" ' |
| 162 | ipfs config replace show_config && |
| 163 | grep "\"PrivKey\":" "$IPFS_PATH/config" | grep -e ": \".\+\"" >/dev/null |
| 164 | ' |
| 165 | |
| 166 | test_expect_success "'ipfs config replace' with privkey errors out" ' |
| 167 | cp "$IPFS_PATH/config" real_config && |
| 168 | test_expect_code 1 ipfs config replace - < real_config 2> replace_out |
| 169 | ' |
| 170 | |
| 171 | test_expect_success "output looks good" ' |
| 172 | echo "Error: setting private key with API is not supported" > replace_expected |
| 173 | test_cmp replace_out replace_expected |
| 174 | ' |
| 175 | |
| 176 | test_expect_success "'ipfs config replace' with lower case privkey errors out" ' |
| 177 | cp "$IPFS_PATH/config" real_config && |
| 178 | sed -i -e '\''s/PrivKey/privkey/'\'' real_config && |
| 179 | test_expect_code 1 ipfs config replace - < real_config 2> replace_out |
| 180 | ' |
| 181 | |
| 182 | test_expect_success "output looks good" ' |
| 183 | echo "Error: setting private key with API is not supported" > replace_expected |
| 184 | test_cmp replace_out replace_expected |
| 185 | ' |
| 186 | |
| 187 | test_expect_success "'ipfs config Swarm.AddrFilters' looks good" ' |
| 188 | ipfs config Swarm.AddrFilters > actual_config && |
| 189 | test $(cat actual_config | wc -l) = 1 |
| 190 | ' |
| 191 | |
| 192 | test_expect_success "copy ipfs config" ' |
| 193 | cp "$IPFS_PATH/config" before_patch |
| 194 | ' |
| 195 | |
| 196 | test_expect_success "'ipfs config profile apply server' works" ' |
| 197 | ipfs config profile apply server |
| 198 | ' |
| 199 | |
| 200 | test_expect_success "backup was created and looks good" ' |
| 201 | test_cmp "$(find "$IPFS_PATH" -name "config-*")" before_patch |
| 202 | ' |
| 203 | |
| 204 | test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" ' |
| 205 | ipfs config Swarm.AddrFilters > actual_config && |
| 206 | test $(cat actual_config | wc -l) = 21 |
| 207 | ' |
| 208 | |
| 209 | test_expect_success "'ipfs config profile apply local-discovery' works" ' |
| 210 | ipfs config profile apply local-discovery |
| 211 | ' |
| 212 | |
| 213 | test_expect_success "'ipfs config Swarm.AddrFilters' looks good with applied local-discovery profile" ' |
| 214 | ipfs config Swarm.AddrFilters > actual_config && |
| 215 | test $(cat actual_config | wc -l) = 1 |
| 216 | ' |
| 217 | |
| 218 | test_profile_apply_revert server local-discovery |
| 219 | |
| 220 | # tests above mess with values this profile changes, need to do that before testing test profile |
| 221 | test_expect_success "ensure test profile is applied fully" ' |
| 222 | ipfs config profile apply test |
| 223 | ' |
| 224 | |
| 225 | # need to do this in reverse as the test profile is already applied in sharness |
| 226 | test_profile_apply_revert default-networking test |
| 227 | |
| 228 | test_profile_apply_dry_run_not_alter server |
| 229 | |
| 230 | test_profile_apply_dry_run_not_alter local-discovery |
| 231 | |
| 232 | test_profile_apply_dry_run_not_alter test |
| 233 | |
| 234 | test_expect_success "'ipfs config profile apply local-discovery --dry-run' looks good with different profile info" ' |
| 235 | ipfs config profile apply local-discovery --dry-run > diff_info && |
| 236 | test `grep "DisableNatPortMap" diff_info | wc -l` = 2 |
| 237 | ' |
| 238 | |
| 239 | test_expect_success "'ipfs config profile apply server --dry-run' looks good with same profile info" ' |
| 240 | ipfs config profile apply server --dry-run > diff_info && |
| 241 | test `grep "DisableNatPortMap" diff_info | wc -l` = 1 |
| 242 | ' |
| 243 | |
| 244 | test_expect_success "'ipfs config profile apply server' looks good with same profile info" ' |
| 245 | ipfs config profile apply server > diff_info && |
| 246 | test `grep "DisableNatPortMap" diff_info | wc -l` = 1 |
| 247 | ' |
| 248 | |
| 249 | test_expect_success "'ipfs config profile apply local-discovery' looks good with different profile info" ' |
| 250 | ipfs config profile apply local-discovery > diff_info && |
| 251 | test `grep "DisableNatPortMap" diff_info | wc -l` = 2 |
| 252 | ' |
| 253 | |
| 254 | test_expect_success "'ipfs config profile apply test' looks good with different profile info" ' |
| 255 | ipfs config profile apply test > diff_info && |
| 256 | test `grep "DisableNatPortMap" diff_info | wc -l` = 2 |
| 257 | ' |
| 258 | |
| 259 | test_expect_success "'ipfs config profile apply test --dry-run' doesn't include privkey" ' |
| 260 | ipfs config profile apply test --dry-run > show_config && |
| 261 | test_expect_code 1 grep PrivKey show_config |
| 262 | ' |
| 263 | |
| 264 | test_expect_success "'ipfs config profile apply test' doesn't include privkey" ' |
| 265 | ipfs config profile apply test > show_config && |
| 266 | test_expect_code 1 grep PrivKey show_config |
| 267 | ' |
| 268 | |
| 269 | # won't work as it changes datastore definition, which makes ipfs not launch |
| 270 | # without converting first |
| 271 | # test_profile_apply_revert pebbleds |
| 272 | |
| 273 | test_expect_success "cleanup config backups" ' |
| 274 | find "$IPFS_PATH" -name "config-*" -exec rm {} \; |
| 275 | ' |
| 276 | } |
| 277 | |
| 278 | test_init_ipfs |
| 279 | |
| 280 | # should work offline |
| 281 | test_config_cmd |
| 282 | |
| 283 | # should work online |
| 284 | test_launch_ipfs_daemon |
| 285 | test_config_cmd |
| 286 | test_kill_ipfs_daemon |
| 287 | |
| 288 | |
| 289 | test_done |