t0165-keystore.sh 5.69 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1
#!/usr/bin/env bash
Jeromy's avatar
Jeromy committed
2 3 4 5 6 7 8 9 10 11 12 13
#
# Copyright (c) 2017 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test keystore commands"

. lib/test-lib.sh

test_init_ipfs

test_key_cmd() {
14 15 16 17 18 19
# test key output format
test_expect_success "create an RSA key and test B58MH multihash output" '
PEERID=$(ipfs key gen -f=b58mh --type=rsa --size=2048 key_rsa) &&
test_check_rsa2048_b58mh_peerid $PEERID
'

rendaw's avatar
rendaw committed
20 21 22
test_expect_success "test RSA key sk export format" '
SK=$(ipfs key export key_rsa) &&
test_check_rsa2048_sk $SK
rendaw's avatar
rendaw committed
23 24
'

25 26 27 28 29 30 31 32 33 34 35
test_expect_success "test RSA key B36CID multihash format" '
PEERID=$(ipfs key list -f=b36cid -l | grep key_rsa | head -n 1 | cut -d " " -f1) &&
test_check_rsa2048_b36cid_peerid $PEERID &&
ipfs key rm key_rsa
'

test_expect_success "create an ED25519 key and test multihash output" '
PEERID=$(ipfs key gen -f=b36cid --type=ed25519 key_ed25519) &&
test_check_ed25519_b36cid_peerid $PEERID
'

rendaw's avatar
rendaw committed
36 37 38
test_expect_success "test ED25519 key sk export format" '
SK=$(ipfs key export key_ed25519) &&
test_check_ed25519_sk $SK
rendaw's avatar
rendaw committed
39 40
'

41 42 43 44 45 46 47 48
test_expect_success "test ED25519 key B36CID multihash format" '
PEERID=$(ipfs key list -f=b36cid -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) &&
test_check_ed25519_b36cid_peerid $PEERID &&
ipfs key rm key_ed25519
'
# end of format test


Łukasz Magiera's avatar
Łukasz Magiera committed
49
  test_expect_success "create a new rsa key" '
50
    rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048)
Łukasz Magiera's avatar
Łukasz Magiera committed
51
  '
Jeromy's avatar
Jeromy committed
52

Łukasz Magiera's avatar
Łukasz Magiera committed
53
  test_expect_success "create a new ed25519 key" '
54
    edhash=$(ipfs key gen -f=b58mh bazed --type=ed25519)
Łukasz Magiera's avatar
Łukasz Magiera committed
55
  '
rendaw's avatar
rendaw committed
56 57
  
  test_expect_success "import an rsa key" '
58 59
    echo "B9bLmHeKLQU1hX23meSn2kJiNW7AZ31C6PBNSYumejXB13vxSVvViZDkEnchAH4BTs9yVnBNZKZYLwykaCohzxTntesTCVaBhZR2Br2Swav2NXwVBhfrUbaBTrR2248KfbVxSiUdkpFn8kcAmUGwp2KGMGRmq85WreGFDdAvzz8ruN2EFfWSHLc1YeUxHeUgKsQm3N13uF7q5x4qvjWM6yvMWNY7JtZrihT8BZQhgb2ezR4iYPjXZTtPZWsLbrrhUvHhxSn1NsTw6NZ7Jbs84qMXXnH56BmT8J9LugRhQQvgBquSoS1m7aeD2y1L1A7mueVDYGLDzgxRSt5CohY7VVMdheUpPiq44CicuYt5YgbbuE3wMHntn6sd9QSYw7f4SjjKdw7Jhy5fNW29SkHvpLfZKfqzBNhSfHsofXVEASDpfr5mqws1eQTztqvvZhHXQxAoxxP3PK6TfDnATdLdV3Cy5v6nLt7ppsBj54hif5EZneHMLeYP8bYLbELQ2fZdoprpnKVBsMY1nWvgrMKUTpLjoKB6bzAZYuXaYVtmrdhESmKCyE12yEyvCcD8DJQU6JjqaD1DyVSPNkL3ze26Mm3ZyiFEH7M4XirUsPLrsj41Qt1xGaVGNEdkdthihytTZxxnmgyAptZMUNfSviBfH1tVbfoXFtBGU8eYMLdSHFqxSktT1mqeiWatxMQZ8pTeA9VCvAp9RTSRFkTQR9uP6w6qTzRD9cFcH4HyCEc5TJpiZdP7u9RjaEo2S3P9VkHfmqCH4McpLgw7He9nm7rf9JA2Gh7ubTKy5e6dJUWojgYhGS4KGe3yKGFhLaNgRiME63fUEFSnN2ZvCSM9qsrj34q2h8962xBod9hCVEDfk4tfmHu1UHX5AGaW6mk3pzqKKVYTTWXi84JSH7vzKPmQuhwaAR9Ye3Jbdzehp4xhrT5aFCjnz3r5qNv2zz48Fq5bGc1RUh88PUMT3z6kuzv6B1eXTLYpeu9gGdjc5C9DQDTYPfcHWn7dSHr4AGV1sN6SwVy8W5LZdMAZaeXCDn9iXDwbeD2DYd2ozVCEzceygVzpVdnueNx5FmG6zHtGzfuStr4Jj85sbd2jUGh4ES2bMU41jw2gJ6ujjf6CrxZpCWhXz6NJpAS9njcDFXuspf7otbMjCB6TzwokJwEse31nGUZQdhQgXn23vnZtxwCV621uXFbm7xVACRZKeuXgw8VdEVaXGvf2V4DdhjZnjmePBbTeJ7WjABavLcpMqZJH7FgaLxazFqk9RXtnfUEbVAAhuZzxz6L8Z6axHwz3a4EZtALRjfFjn6xjaUtsWXYW8P6F7femM6UHx3qXMo43hKC7oxnd6Tfta972dgyQfSoBwWkWzB8cvaJreNh4bdLNkw6mty86NXGKyijv83LR1HjbnUoTwPbEMX8JyzLfMf3qiWzwf6MHrXprwygmEpNc6w8tNNivmcWyCX3wmPkMKK1bmi5TCHoUtRrxcyXKhmuyo6zzag8KyK6iZaRbMiFiUJBi5VYwidMutkexWo8SRqfSV5yp2kxswknmpeVTnXBhVEy3anMEiD6bV48AnbF6SfKAi2DGBFqxBFfpFEbYtPauHiYYzZX1epqvKxY23xA9J8FosMk4yYN4Ps7Rh" >> importkey
    imphash=$(ipfs key import -f=b58mh quxel $(cat importkey))
rendaw's avatar
rendaw committed
60
  '
Jeromy's avatar
Jeromy committed
61

rendaw's avatar
rendaw committed
62
  test_expect_success "exported key matches imported" '
63 64
    ipfs key export quxel >> exportkey &&
    test_cmp importkey exportkey
rendaw's avatar
rendaw committed
65 66
  '
  
rendaw's avatar
rendaw committed
67
  test_expect_success "key export can't export self" '
rendaw's avatar
rendaw committed
68 69 70 71 72
    test_must_fail ipfs key export self 2>&1 | tee key_exp_out &&
    grep -q "Error: cannot export key with name" key_exp_out
  '

  test_expect_success "key import can't import self" '
73
    test_must_fail ipfs key import self $(cat importkey) 2>&1 | tee key_imp_out &&
rendaw's avatar
rendaw committed
74 75 76 77
    grep -q "Error: cannot import key with name" key_imp_out
  '

  test_expect_success "all keys show up in list output" '
Łukasz Magiera's avatar
Łukasz Magiera committed
78 79
    echo bazed > list_exp &&
    echo foobarsa >> list_exp &&
rendaw's avatar
rendaw committed
80
    echo quxel >> list_exp &&
rendaw's avatar
rendaw committed
81 82 83
    echo self >> list_exp
    ipfs key list -f=b58mh > list_out &&
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
84
  '
Jeromy's avatar
Jeromy committed
85

Łukasz Magiera's avatar
Łukasz Magiera committed
86
  test_expect_success "key hashes show up in long list output" '
87 88
    ipfs key list -f=b58mh -l | grep $edhash > /dev/null &&
    ipfs key list -f=b58mh -l | grep $rsahash > /dev/null
Łukasz Magiera's avatar
Łukasz Magiera committed
89
  '
90

Łukasz Magiera's avatar
Łukasz Magiera committed
91 92
  test_expect_success "key list -l contains self key with peerID" '
    PeerID="$(ipfs config Identity.PeerID)"
93
    ipfs key list -f=b58mh -l | grep "$PeerID\s\+self"
Łukasz Magiera's avatar
Łukasz Magiera committed
94
  '
Michael Muré's avatar
Michael Muré committed
95

Łukasz Magiera's avatar
Łukasz Magiera committed
96 97 98
  test_expect_success "key rm remove a key" '
    ipfs key rm foobarsa
    echo bazed > list_exp &&
rendaw's avatar
rendaw committed
99
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
100
    echo self >> list_exp
rendaw's avatar
rendaw committed
101 102
    ipfs key list -f=b58mh > list_out &&
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
103
  '
104

Łukasz Magiera's avatar
Łukasz Magiera committed
105 106 107 108
  test_expect_success "key rm can't remove self" '
    test_must_fail ipfs key rm self 2>&1 | tee key_rm_out &&
    grep -q "Error: cannot remove key with name" key_rm_out
  '
109

Łukasz Magiera's avatar
Łukasz Magiera committed
110 111 112
  test_expect_success "key rename rename a key" '
    ipfs key rename bazed fooed
    echo fooed > list_exp &&
rendaw's avatar
rendaw committed
113
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
114
    echo self >> list_exp
rendaw's avatar
rendaw committed
115 116
    ipfs key list -f=b58mh > list_out &&
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
117
  '
118

119
  test_expect_success "key rename rename key output succeeds" '
120
    key_content=$(ipfs key gen -f=b58mh key1 --type=rsa --size=2048) &&
121 122 123 124 125
    ipfs key rename key1 key2 >rs &&
    echo "Key $key_content renamed to key2" >expect &&
    test_cmp rs expect
  '

Łukasz Magiera's avatar
Łukasz Magiera committed
126 127 128 129
  test_expect_success "key rename can't rename self" '
    test_must_fail ipfs key rename self bar 2>&1 | tee key_rename_out &&
    grep -q "Error: cannot rename key with name" key_rename_out
  '
130

Łukasz Magiera's avatar
Łukasz Magiera committed
131 132 133 134
  test_expect_success "key rename can't overwrite self, even with force" '
    test_must_fail ipfs key rename -f fooed self 2>&1 | tee key_rename_out &&
    grep -q "Error: cannot overwrite key with name" key_rename_out
  '
Jeromy's avatar
Jeromy committed
135 136 137 138 139
}

test_key_cmd

test_done