github.com/Tri-stone/burrow@v0.25.0/tests/keys_server/test.sh (about)

     1  #! /bin/bash
     2  
     3  # tests
     4  # run the suite with and without the daemon
     5  
     6  # TODO: run the suite with/without encryption!
     7  
     8  set -e
     9  
    10  burrow_bin=${burrow_bin:-burrow}
    11  
    12  test_dir="./tests/keys_server/test_scratch"
    13  keys_dir="$test_dir/.keys"
    14  tmp_dir="$test_dir/tmp"
    15  mkdir -p "$tmp_dir"
    16  
    17  echo "-----------------------------"
    18  echo "checking for dependent utilities"
    19  for UTILITY in jq xxd openssl; do
    20      echo -n "... "
    21      if ! command -v $UTILITY; then
    22          echo "$UTILITY (missing)"
    23          missing_utility=$UTILITY
    24      fi
    25  done
    26  if [[ ! -z ${missing_utility} ]]; then
    27      echo "FAILED dependency check: the '$missing_utility' utility is missing"
    28      exit 1
    29  fi
    30  
    31  echo "starting the server"
    32  $burrow_bin keys server --dir $keys_dir &
    33  keys_pid=$!
    34  function kill_burrow_keys {
    35      kill -TERM $keys_pid
    36  }
    37  
    38  trap kill_burrow_keys EXIT
    39  sleep 1
    40  echo "-----------------------------"
    41  echo "testing the cli"
    42  
    43  # we test keys, hashes, names, and import
    44  # this file should be run with and without the daemon running
    45  
    46  echo "testing keys"
    47  
    48  CURVETYPES=( "ed25519" "secp256k1" )
    49  for CURVETYPE in ${CURVETYPES[*]}
    50  do
    51      # test key gen, sign verify:
    52      # for each step, ensure it works using --addr or --name
    53      echo "... $CURVETYPE"
    54  
    55      HASH=`$burrow_bin keys hash --type sha256 ok`
    56      #echo "HASH: $HASH"
    57      NAME=testkey1
    58      ADDR=`$burrow_bin keys gen --curvetype $CURVETYPE --name $NAME --no-password`
    59      #echo "my addr: $ADDR"
    60      PUB1=`$burrow_bin keys pub --name $NAME`
    61      PUB2=`$burrow_bin keys pub --addr $ADDR`
    62      if [ "$PUB1" != "$PUB2" ]; then
    63          echo "FAILED pub: got $PUB2, expected $PUB1"
    64          exit 1
    65      fi
    66      echo "...... passed pub"
    67  
    68      SIG1=`$burrow_bin keys sign --name $NAME $HASH`
    69      VERIFY1=`$burrow_bin keys verify --curvetype $CURVETYPE $HASH $SIG1 $PUB1`
    70      if [ $VERIFY1 != "true" ]; then
    71          echo "FAILED verify: got $VERIFY1 expected true"
    72          exit 1
    73      fi
    74  
    75      SIG2=`$burrow_bin keys sign --addr $ADDR $HASH`
    76      VERIFY1=`$burrow_bin keys verify --curvetype $CURVETYPE $HASH $SIG2 $PUB1`
    77      if [ $VERIFY1 != "true" ]; then
    78          echo "FAILED verify: got $VERIFY1 expected true"
    79          exit 1
    80      fi
    81  
    82      echo "...... passed sig/verify"
    83  
    84  done
    85  
    86  echo "testing hashes"
    87  # test hashing (we need openssl)
    88  TOHASH=okeydokey
    89  HASHTYPES=( sha256 ripemd160 )
    90  for HASHTYPE in ${HASHTYPES[*]}
    91  do
    92      echo "... $HASHTYPE"
    93      # XXX: OpenSSL's `openssl dgst -<hash>` command might produce both
    94      # a one-field (LibreSSL 2.2.7)
    95      #
    96      #   $ echo -n okeydokey |openssl dgst -sha256
    97      #   0fd2479fa22057f562698c4e6bb5b6c7430a10ba0fe6cd41fa9908e2c0a684a4
    98      #
    99      # and a two-field result (OpenSSL 1.1.0f):
   100      #
   101      #   $ echo -n okeydokey |openssl dgst -sha256
   102      #   (stdin)= 0fd2479fa22057f562698c4e6bb5b6c7430a10ba0fe6cd41fa9908e2c0a684a4
   103      #
   104      # Generalize to adjust for the inconsistency:
   105      HASH0=`echo -n $TOHASH | openssl dgst -$HASHTYPE | sed 's/^.* //' | tr '[:lower:]' '[:upper:]'`
   106      HASH1=`$burrow_bin keys hash --type $HASHTYPE $TOHASH`
   107      if [ "$HASH0" != "$HASH1" ]; then
   108          echo "FAILED hash $HASHTYPE: got $HASH1 expected $HASH0"
   109      fi
   110      echo "...... passed"
   111  done
   112  
   113  echo "testing imports"
   114  
   115  # TODO: IMPORTS
   116  # for each key type, import a priv key, ensure it returns
   117  # the right address. do again with both plain and encrypted jsons
   118  
   119  for CURVETYPE in ${CURVETYPES[*]}
   120  do
   121      echo "... $CURVETYPE"
   122      # create a key, get its address and priv, backup the json, delete the key
   123      ADDR=`$burrow_bin keys gen --curvetype $CURVETYPE --no-password`
   124      DIR=$keys_dir/data
   125      FILE=$DIR/$ADDR.json
   126      HEXPRIV=`cat $FILE | jq -r .PrivateKey.Plain`
   127      EXPORTJSON=`$burrow_bin keys export --addr $ADDR`
   128  
   129  
   130      cp $FILE "$tmp_dir/$ADDR"
   131      rm -rf $DIR
   132  
   133      # import the key via priv
   134      ADDR2=`$burrow_bin keys import --no-password --curvetype $CURVETYPE $HEXPRIV`
   135      if [ "$ADDR" != "$ADDR2" ]; then
   136          echo "FAILED import $CURVETYPE: got $ADDR2 expected $ADDR"
   137          exit
   138      fi
   139      rm -rf $DIR
   140  
   141      # import the key via json
   142      JSON=`cat "$tmp_dir/$ADDR"`
   143      ADDR2=`$burrow_bin keys import --no-password --curvetype $CURVETYPE $JSON`
   144      if [ "$ADDR" != "$ADDR2" ]; then
   145          echo "FAILED import (json) $CURVETYPE: got $ADDR2 expected $ADDR"
   146          exit
   147      fi
   148      rm -rf $DIR
   149  
   150      # import the key via path
   151      ADDR2=`$burrow_bin keys import --no-password --curvetype $CURVETYPE "$tmp_dir/$ADDR"`
   152      if [ "$ADDR" != "$ADDR2" ]; then
   153          echo "FAILED import $CURVETYPE: got $ADDR2 expected $ADDR"
   154          exit
   155      fi
   156      rm -rf $DIR
   157  
   158      # import the key via export json
   159      ADDR2=`$burrow_bin keys import --no-password "$EXPORTJSON"`
   160      if [ "$ADDR" != "$ADDR2" ]; then
   161          echo "FAILED import from export $CURVETYPE: got $ADDR2 expected $ADDR"
   162          exit
   163      fi
   164      rm -rf $DIR
   165  
   166      echo "...... passed raw hex and json"
   167  done
   168  
   169  
   170  echo "testing names"
   171  
   172  NAME=mykey
   173  ADDR=`$burrow_bin keys gen --name $NAME --no-password`
   174  ADDR2=`$burrow_bin keys list --name $NAME | jq -r '.[0].Address'`
   175  if [ "$ADDR" != "$ADDR2" ]; then
   176      echo "FAILED name: got $ADDR2 expected $ADDR"
   177      exit
   178  fi
   179  
   180  NAME2=mykey2
   181  $burrow_bin keys name $NAME2 $ADDR
   182  ADDR2=`$burrow_bin keys list --name $NAME2 | jq -r '.[0].Address'`
   183  if [ "$ADDR" != "$ADDR2" ]; then
   184      echo "FAILED rename: got $ADDR2 expected $ADDR"
   185      exit
   186  fi
   187  
   188  echo "... passed"
   189  
   190  rm -rf "$test_dir"
   191  
   192  # TODO a little more on names...
   193