t0165-keystore.sh 5.6 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
test_expect_success "test RSA key sk export format" '
21 22 23
ipfs key export key_rsa &&
test_check_rsa2048_sk key_rsa &&
rm key_rsa
rendaw's avatar
rendaw committed
24 25
'

26 27 28 29 30 31 32 33 34 35 36
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
37
test_expect_success "test ED25519 key sk export format" '
38 39 40
ipfs key export key_ed25519 &&
test_check_ed25519_sk key_ed25519 &&
rm key_ed25519
rendaw's avatar
rendaw committed
41 42
'

43 44 45 46 47 48 49 50
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
51
  test_expect_success "create a new rsa key" '
52 53
    rsahash=$(ipfs key gen -f=b58mh generated_rsa_key --type=rsa --size=2048)
    echo $rsahash > rsa_key_id
Łukasz Magiera's avatar
Łukasz Magiera committed
54
  '
Jeromy's avatar
Jeromy committed
55

Łukasz Magiera's avatar
Łukasz Magiera committed
56
  test_expect_success "create a new ed25519 key" '
57 58
    edhash=$(ipfs key gen -f=b58mh generated_ed25519_key --type=ed25519)
    echo $edhash > ed25519_key_id
Łukasz Magiera's avatar
Łukasz Magiera committed
59
  '
60 61 62 63 64 65

  test_expect_success "export and import rsa key" '
    ipfs key export generated_rsa_key &&
    ipfs key rm generated_rsa_key &&
    ipfs key import generated_rsa_key generated_rsa_key > roundtrip_rsa_key_id &&
    test_cmp rsa_key_id roundtrip_rsa_key_id
rendaw's avatar
rendaw committed
66
  '
Jeromy's avatar
Jeromy committed
67

68 69 70 71 72 73 74 75 76 77 78 79
  test_expect_success "export and import ed25519 key" '
    ipfs key export generated_ed25519_key &&
    ipfs key rm generated_ed25519_key &&
    ipfs key import generated_ed25519_key generated_ed25519_key > roundtrip_ed25519_key_id &&
    test_cmp ed25519_key_id roundtrip_ed25519_key_id
  '

  test_expect_success "test export file option" '
    ipfs key export generated_rsa_key -o=named_rsa_export_file &&
    test_cmp generated_rsa_key named_rsa_export_file &&
    ipfs key export generated_ed25519_key -o=named_ed25519_export_file &&
    test_cmp generated_ed25519_key named_ed25519_export_file
rendaw's avatar
rendaw committed
80 81
  '
  
rendaw's avatar
rendaw committed
82
  test_expect_success "key export can't export self" '
rendaw's avatar
rendaw committed
83
    test_must_fail ipfs key export self 2>&1 | tee key_exp_out &&
84 85
    grep -q "Error: cannot export key with name" key_exp_out &&
    test_must_fail ipfs key export self -o=selfexport 2>&1 | tee key_exp_out &&
rendaw's avatar
rendaw committed
86 87 88 89
    grep -q "Error: cannot export key with name" key_exp_out
  '

  test_expect_success "key import can't import self" '
90 91 92 93 94 95 96 97 98 99
    ipfs key gen overwrite_self_import &&
    ipfs key export overwrite_self_import &&
    test_must_fail ipfs key import self overwrite_self_import 2>&1 | tee key_imp_out &&
    grep -q "Error: cannot import key with name" key_imp_out &&
    ipfs key rm overwrite_self_import &&
    rm overwrite_self_import
  '

  test_expect_success "add a default key" '
    ipfs key gen quxel
rendaw's avatar
rendaw committed
100 101 102
  '

  test_expect_success "all keys show up in list output" '
103 104
    echo generated_ed25519_key > list_exp &&
    echo generated_rsa_key >> list_exp &&
rendaw's avatar
rendaw committed
105
    echo quxel >> list_exp &&
rendaw's avatar
rendaw committed
106 107 108
    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
109
  '
Jeromy's avatar
Jeromy committed
110

Łukasz Magiera's avatar
Łukasz Magiera committed
111
  test_expect_success "key hashes show up in long list output" '
112 113
    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
114
  '
115

Łukasz Magiera's avatar
Łukasz Magiera committed
116 117
  test_expect_success "key list -l contains self key with peerID" '
    PeerID="$(ipfs config Identity.PeerID)"
118
    ipfs key list -f=b58mh -l | grep "$PeerID\s\+self"
Łukasz Magiera's avatar
Łukasz Magiera committed
119
  '
Michael Muré's avatar
Michael Muré committed
120

Łukasz Magiera's avatar
Łukasz Magiera committed
121
  test_expect_success "key rm remove a key" '
122 123
    ipfs key rm generated_rsa_key
    echo generated_ed25519_key > list_exp &&
rendaw's avatar
rendaw committed
124
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
125
    echo self >> list_exp
rendaw's avatar
rendaw committed
126 127
    ipfs key list -f=b58mh > list_out &&
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
128
  '
129

Łukasz Magiera's avatar
Łukasz Magiera committed
130 131 132 133
  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
  '
134

Łukasz Magiera's avatar
Łukasz Magiera committed
135
  test_expect_success "key rename rename a key" '
136
    ipfs key rename generated_ed25519_key fooed
Łukasz Magiera's avatar
Łukasz Magiera committed
137
    echo fooed > list_exp &&
rendaw's avatar
rendaw committed
138
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
139
    echo self >> list_exp
rendaw's avatar
rendaw committed
140 141
    ipfs key list -f=b58mh > list_out &&
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
142
  '
143

144
  test_expect_success "key rename rename key output succeeds" '
145
    key_content=$(ipfs key gen -f=b58mh key1 --type=rsa --size=2048) &&
146 147 148 149 150
    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
151 152 153 154
  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
  '
155

Łukasz Magiera's avatar
Łukasz Magiera committed
156 157 158 159
  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
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
test_check_rsa2048_sk() {
  sklen=$(ls -l $1 | awk '{print $5}') &&
  test "$sklen" -lt "1600" && test "$sklen" -gt "1000" || {
    echo "Bad RSA2048 sk '$1' with len '$sklen'"
    return 1
  }
}

test_check_ed25519_sk() {
  sklen=$(ls -l $1 | awk '{print $5}') &&
  test "$sklen" -lt "100" && test "$sklen" -gt "30" || {
    echo "Bad ED25519 sk '$1' with len '$sklen'"
    return 1
  }
}

Jeromy's avatar
Jeromy committed
178 179 180
test_key_cmd

test_done