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.

70 lines
1.5 KiB

  1. package memstorage
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. crypto "github.com/tendermint/go-crypto"
  6. keys "github.com/tendermint/go-crypto/keys"
  7. )
  8. func TestBasicCRUD(t *testing.T) {
  9. assert := assert.New(t)
  10. store := New()
  11. name := "foo"
  12. key := []byte("secret-key-here")
  13. salt := []byte("salt-here")
  14. pubkey := crypto.GenPrivKeyEd25519().PubKey()
  15. info := keys.Info{
  16. Name: name,
  17. PubKey: pubkey,
  18. }
  19. // No data: Get and Delete return nothing
  20. _, _, _, err := store.Get(name)
  21. assert.NotNil(err)
  22. err = store.Delete(name)
  23. assert.NotNil(err)
  24. // List returns empty list
  25. l, err := store.List()
  26. assert.Nil(err)
  27. assert.Empty(l)
  28. // Putting the key in the store must work
  29. err = store.Put(name, salt, key, info)
  30. assert.Nil(err)
  31. // But a second time is a failure
  32. err = store.Put(name, salt, key, info)
  33. assert.NotNil(err)
  34. // Now, we can get and list properly
  35. _, k, i, err := store.Get(name)
  36. assert.Nil(err)
  37. assert.Equal(key, k)
  38. assert.Equal(info.Name, i.Name)
  39. assert.Equal(info.PubKey, i.PubKey)
  40. assert.NotEmpty(i.Address)
  41. l, err = store.List()
  42. assert.Nil(err)
  43. assert.Equal(1, len(l))
  44. assert.Equal(i, l[0])
  45. // querying a non-existent key fails
  46. _, _, _, err = store.Get("badname")
  47. assert.NotNil(err)
  48. // We can only delete once
  49. err = store.Delete(name)
  50. assert.Nil(err)
  51. err = store.Delete(name)
  52. assert.NotNil(err)
  53. // and then Get and List don't work
  54. _, _, _, err = store.Get(name)
  55. assert.NotNil(err)
  56. // List returns empty list
  57. l, err = store.List()
  58. assert.Nil(err)
  59. assert.Empty(l)
  60. }