t0165-keystore.sh 6.1 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
# test key output format
15
test_expect_success "create an RSA key and test B58MH/B36CID output formats" '
16
PEERID=$(ipfs key gen -f=b58mh --type=rsa --size=2048 key_rsa) &&
17 18 19 20
test_check_rsa2048_b58mh_peerid $PEERID &&
ipfs key rm key_rsa &&
PEERID=$(ipfs key gen -f=b36cid --type=rsa --size=2048 key_rsa) &&
test_check_rsa2048_b36cid_peerid $PEERID
21 22
'

rendaw's avatar
rendaw committed
23
test_expect_success "test RSA key sk export format" '
24
ipfs key export key_rsa &&
25 26
test_check_rsa2048_sk key_rsa.key &&
rm key_rsa.key
rendaw's avatar
rendaw committed
27 28
'

29 30 31
test_expect_success "test RSA key B58MH/B36CID multihash format" '
PEERID=$(ipfs key list -f=b58mh -l | grep key_rsa | head -n 1 | cut -d " " -f1) &&
test_check_rsa2048_b58mh_peerid $PEERID &&
32 33 34 35 36
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
'

37 38 39 40
test_expect_success "create an ED25519 key and test B58MH/B36CID output formats" '
PEERID=$(ipfs key gen -f=b58mh --type=ed25519 key_ed25519) &&
test_check_ed25519_b58mh_peerid $PEERID &&
ipfs key rm key_ed25519 &&
41 42 43 44
PEERID=$(ipfs key gen -f=b36cid --type=ed25519 key_ed25519) &&
test_check_ed25519_b36cid_peerid $PEERID
'

rendaw's avatar
rendaw committed
45
test_expect_success "test ED25519 key sk export format" '
46
ipfs key export key_ed25519 &&
47 48
test_check_ed25519_sk key_ed25519.key &&
rm key_ed25519.key
rendaw's avatar
rendaw committed
49 50
'

51 52 53
test_expect_success "test ED25519 key B58MH/B36CID multihash format" '
PEERID=$(ipfs key list -f=b58mh -l | grep key_ed25519 | head -n 1 | cut -d " " -f1) &&
test_check_ed25519_b58mh_peerid $PEERID &&
54 55 56 57 58 59 60
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
61
  test_expect_success "create a new rsa key" '
62
    rsahash=$(ipfs key gen generated_rsa_key --type=rsa --size=2048)
63
    echo $rsahash > rsa_key_id
Łukasz Magiera's avatar
Łukasz Magiera committed
64
  '
Jeromy's avatar
Jeromy committed
65

Łukasz Magiera's avatar
Łukasz Magiera committed
66
  test_expect_success "create a new ed25519 key" '
67
    edhash=$(ipfs key gen generated_ed25519_key --type=ed25519)
68
    echo $edhash > ed25519_key_id
Łukasz Magiera's avatar
Łukasz Magiera committed
69
  '
70 71 72 73

  test_expect_success "export and import rsa key" '
    ipfs key export generated_rsa_key &&
    ipfs key rm generated_rsa_key &&
74
    ipfs key import generated_rsa_key generated_rsa_key.key > roundtrip_rsa_key_id &&
75
    test_cmp rsa_key_id roundtrip_rsa_key_id
rendaw's avatar
rendaw committed
76
  '
Jeromy's avatar
Jeromy committed
77

78 79 80
  test_expect_success "export and import ed25519 key" '
    ipfs key export generated_ed25519_key &&
    ipfs key rm generated_ed25519_key &&
81
    ipfs key import generated_ed25519_key generated_ed25519_key.key > roundtrip_ed25519_key_id &&
82 83 84 85 86
    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 &&
87
    test_cmp generated_rsa_key.key named_rsa_export_file &&
88
    ipfs key export generated_ed25519_key -o=named_ed25519_export_file &&
89
    test_cmp generated_ed25519_key.key named_ed25519_export_file
rendaw's avatar
rendaw committed
90 91
  '
  
rendaw's avatar
rendaw committed
92
  test_expect_success "key export can't export self" '
rendaw's avatar
rendaw committed
93
    test_must_fail ipfs key export self 2>&1 | tee key_exp_out &&
94 95
    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
96 97 98 99
    grep -q "Error: cannot export key with name" key_exp_out
  '

  test_expect_success "key import can't import self" '
100 101
    ipfs key gen overwrite_self_import &&
    ipfs key export overwrite_self_import &&
102
    test_must_fail ipfs key import self overwrite_self_import.key 2>&1 | tee key_imp_out &&
103 104
    grep -q "Error: cannot import key with name" key_imp_out &&
    ipfs key rm overwrite_self_import &&
105
    rm overwrite_self_import.key
106 107 108 109
  '

  test_expect_success "add a default key" '
    ipfs key gen quxel
rendaw's avatar
rendaw committed
110 111 112
  '

  test_expect_success "all keys show up in list output" '
113 114
    echo generated_ed25519_key > list_exp &&
    echo generated_rsa_key >> list_exp &&
rendaw's avatar
rendaw committed
115
    echo quxel >> list_exp &&
rendaw's avatar
rendaw committed
116
    echo self >> list_exp
117
    ipfs key list > list_out &&
rendaw's avatar
rendaw committed
118
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
119
  '
Jeromy's avatar
Jeromy committed
120

Łukasz Magiera's avatar
Łukasz Magiera committed
121
  test_expect_success "key hashes show up in long list output" '
122 123
    ipfs key list -l | grep $edhash > /dev/null &&
    ipfs key list -l | grep $rsahash > /dev/null
Łukasz Magiera's avatar
Łukasz Magiera committed
124
  '
125

Łukasz Magiera's avatar
Łukasz Magiera committed
126 127
  test_expect_success "key list -l contains self key with peerID" '
    PeerID="$(ipfs config Identity.PeerID)"
128
    ipfs key list -l | grep "$PeerID\s\+self"
Łukasz Magiera's avatar
Łukasz Magiera committed
129
  '
Michael Muré's avatar
Michael Muré committed
130

Łukasz Magiera's avatar
Łukasz Magiera committed
131
  test_expect_success "key rm remove a key" '
132 133
    ipfs key rm generated_rsa_key
    echo generated_ed25519_key > list_exp &&
rendaw's avatar
rendaw committed
134
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
135
    echo self >> list_exp
136
    ipfs key list > list_out &&
rendaw's avatar
rendaw committed
137
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
138
  '
139

Łukasz Magiera's avatar
Łukasz Magiera committed
140 141 142 143
  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
  '
144

Łukasz Magiera's avatar
Łukasz Magiera committed
145
  test_expect_success "key rename rename a key" '
146
    ipfs key rename generated_ed25519_key fooed
Łukasz Magiera's avatar
Łukasz Magiera committed
147
    echo fooed > list_exp &&
rendaw's avatar
rendaw committed
148
    echo quxel >> list_exp &&
Łukasz Magiera's avatar
Łukasz Magiera committed
149
    echo self >> list_exp
150
    ipfs key list > list_out &&
rendaw's avatar
rendaw committed
151
    test_sort_cmp list_exp list_out
Łukasz Magiera's avatar
Łukasz Magiera committed
152
  '
153

154
  test_expect_success "key rename rename key output succeeds" '
155
    key_content=$(ipfs key gen key1 --type=rsa --size=2048) &&
156 157 158 159 160
    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
161 162 163 164
  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
  '
165

Łukasz Magiera's avatar
Łukasz Magiera committed
166 167 168 169
  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
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
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
188 189 190
test_key_cmd

test_done