You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

  1. #!/bin/bash
  2. EXE=keys
  3. oneTimeSetUp() {
  4. PASS=qwertyuiop
  5. export TM_HOME=$HOME/.keys_test
  6. rm -rf $TM_HOME
  7. assertTrue $?
  8. }
  9. newKey(){
  10. assertNotNull "keyname required" "$1"
  11. KEYPASS=${2:-qwertyuiop}
  12. KEY=$(echo $KEYPASS | ${EXE} new $1 -o json)
  13. if ! assertTrue "created $1" $?; then return 1; fi
  14. assertEquals "$1" $(echo $KEY | jq .key.name | tr -d \")
  15. return $?
  16. }
  17. # updateKey <name> <oldkey> <newkey>
  18. updateKey() {
  19. (echo $2; echo $3) | keys update $1 > /dev/null
  20. return $?
  21. }
  22. test00MakeKeys() {
  23. USER=demouser
  24. assertFalse "already user $USER" "${EXE} get $USER"
  25. newKey $USER
  26. assertTrue "no user $USER" "${EXE} get $USER"
  27. # make sure bad password not accepted
  28. assertFalse "accepts short password" "echo 123 | keys new badpass"
  29. }
  30. test01ListKeys() {
  31. # one line plus the number of keys
  32. assertEquals "2" $(keys list | wc -l)
  33. newKey foobar
  34. assertEquals "3" $(keys list | wc -l)
  35. # we got the proper name here...
  36. assertEquals "foobar" $(keys list -o json | jq .[1].name | tr -d \" )
  37. # we get all names in normal output
  38. EXPECTEDNAMES=$(echo demouser; echo foobar)
  39. TEXTNAMES=$(keys list | tail -n +2 | cut -f1)
  40. assertEquals "$EXPECTEDNAMES" "$TEXTNAMES"
  41. # let's make sure the addresses match!
  42. assertEquals "text and json addresses don't match" $(keys list | tail -1 | cut -f3) $(keys list -o json | jq .[1].address | tr -d \")
  43. }
  44. test02updateKeys() {
  45. USER=changer
  46. PASS1=awsedrftgyhu
  47. PASS2=S4H.9j.D9S7hso
  48. PASS3=h8ybO7GY6d2
  49. newKey $USER $PASS1
  50. assertFalse "accepts invalid pass" "updateKey $USER $PASS2 $PASS2"
  51. assertTrue "doesn't update" "updateKey $USER $PASS1 $PASS2"
  52. assertTrue "takes new key after update" "updateKey $USER $PASS2 $PASS3"
  53. }
  54. test03recoverKeys() {
  55. USER=sleepy
  56. PASS1=S4H.9j.D9S7hso
  57. USER2=easy
  58. PASS2=1234567890
  59. # make a user and check they exist
  60. echo "create..."
  61. KEY=$(echo $PASS1 | ${EXE} new $USER -o json)
  62. if ! assertTrue "created $USER" $?; then return 1; fi
  63. if [ -n "$DEBUG" ]; then echo $KEY; echo; fi
  64. SEED=$(echo $KEY | jq .seed | tr -d \")
  65. ADDR=$(echo $KEY | jq .key.address | tr -d \")
  66. PUBKEY=$(echo $KEY | jq .key.pubkey | tr -d \")
  67. assertTrue "${EXE} get $USER > /dev/null"
  68. # let's delete this key
  69. echo "delete..."
  70. assertFalse "echo foo | ${EXE} delete $USER > /dev/null"
  71. assertTrue "echo $PASS1 | ${EXE} delete $USER > /dev/null"
  72. assertFalse "${EXE} get $USER > /dev/null"
  73. # fails on short password
  74. echo "recover..."
  75. assertFalse "echo foo; echo $SEED | ${EXE} recover $USER2 -o json > /dev/null"
  76. # fails on bad seed
  77. assertFalse "echo $PASS2; echo \"silly white whale tower bongo\" | ${EXE} recover $USER2 -o json > /dev/null"
  78. # now we got it
  79. KEY2=$((echo $PASS2; echo $SEED) | ${EXE} recover $USER2 -o json)
  80. if ! assertTrue "recovery failed: $KEY2" $?; then return 1; fi
  81. if [ -n "$DEBUG" ]; then echo $KEY2; echo; fi
  82. # make sure it looks the same
  83. NAME2=$(echo $KEY2 | jq .name | tr -d \")
  84. ADDR2=$(echo $KEY2 | jq .address | tr -d \")
  85. PUBKEY2=$(echo $KEY2 | jq .pubkey | tr -d \")
  86. assertEquals "wrong username" "$USER2" "$NAME2"
  87. assertEquals "address doesn't match" "$ADDR" "$ADDR2"
  88. assertEquals "pubkey doesn't match" "$PUBKEY" "$PUBKEY2"
  89. # and we can find the info
  90. assertTrue "${EXE} get $USER2 > /dev/null"
  91. }
  92. # load and run these tests with shunit2!
  93. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #get this files directory
  94. . $DIR/shunit2