t0165-keystore.sh 3.12 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 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
'

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
'

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
39
  test_expect_success "create a new rsa key" '
40
    rsahash=$(ipfs key gen -f=b58mh foobarsa --type=rsa --size=2048)
Łukasz Magiera's avatar
Łukasz Magiera committed
41
  '
Jeromy's avatar
Jeromy committed
42

Łukasz Magiera's avatar
Łukasz Magiera committed
43
  test_expect_success "create a new ed25519 key" '
44
    edhash=$(ipfs key gen -f=b58mh bazed --type=ed25519)
Łukasz Magiera's avatar
Łukasz Magiera committed
45
  '
Jeromy's avatar
Jeromy committed
46

Łukasz Magiera's avatar
Łukasz Magiera committed
47 48 49 50
  test_expect_success "both keys show up in list output" '
    echo bazed > list_exp &&
    echo foobarsa >> list_exp &&
    echo self >> list_exp
51
    ipfs key list -f=b58mh | sort > list_out &&
Łukasz Magiera's avatar
Łukasz Magiera committed
52 53
    test_cmp list_exp list_out
  '
Jeromy's avatar
Jeromy committed
54

Łukasz Magiera's avatar
Łukasz Magiera committed
55
  test_expect_success "key hashes show up in long list output" '
56 57
    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
58
  '
59

Łukasz Magiera's avatar
Łukasz Magiera committed
60 61
  test_expect_success "key list -l contains self key with peerID" '
    PeerID="$(ipfs config Identity.PeerID)"
62
    ipfs key list -f=b58mh -l | grep "$PeerID\s\+self"
Łukasz Magiera's avatar
Łukasz Magiera committed
63
  '
Michael Muré's avatar
Michael Muré committed
64

Łukasz Magiera's avatar
Łukasz Magiera committed
65 66 67 68
  test_expect_success "key rm remove a key" '
    ipfs key rm foobarsa
    echo bazed > list_exp &&
    echo self >> list_exp
69
    ipfs key list -f=b58mh | sort > list_out &&
Łukasz Magiera's avatar
Łukasz Magiera committed
70 71
    test_cmp list_exp list_out
  '
72

Łukasz Magiera's avatar
Łukasz Magiera committed
73 74 75 76
  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
  '
77

Łukasz Magiera's avatar
Łukasz Magiera committed
78 79 80 81
  test_expect_success "key rename rename a key" '
    ipfs key rename bazed fooed
    echo fooed > list_exp &&
    echo self >> list_exp
82
    ipfs key list -f=b58mh | sort > list_out &&
Łukasz Magiera's avatar
Łukasz Magiera committed
83 84
    test_cmp list_exp list_out
  '
85

86
  test_expect_success "key rename rename key output succeeds" '
87
    key_content=$(ipfs key gen -f=b58mh key1 --type=rsa --size=2048) &&
88 89 90 91 92
    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
93 94 95 96
  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
  '
97

Łukasz Magiera's avatar
Łukasz Magiera committed
98 99 100 101
  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
102 103 104 105 106
}

test_key_cmd

test_done