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.

69 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-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. pubkey := crypto.GenPrivKeyEd25519().PubKey()
  14. info := keys.Info{
  15. Name: name,
  16. PubKey: crypto.PubKeyS{pubkey},
  17. }
  18. // No data: Get and Delete return nothing
  19. _, _, err := store.Get(name)
  20. assert.NotNil(err)
  21. err = store.Delete(name)
  22. assert.NotNil(err)
  23. // List returns empty list
  24. l, err := store.List()
  25. assert.Nil(err)
  26. assert.Empty(l)
  27. // Putting the key in the store must work
  28. err = store.Put(name, key, info)
  29. assert.Nil(err)
  30. // But a second time is a failure
  31. err = store.Put(name, key, info)
  32. assert.NotNil(err)
  33. // Now, we can get and list properly
  34. k, i, err := store.Get(name)
  35. assert.Nil(err)
  36. assert.Equal(key, k)
  37. assert.Equal(info.Name, i.Name)
  38. assert.Equal(info.PubKey, i.PubKey)
  39. assert.NotEmpty(i.Address)
  40. l, err = store.List()
  41. assert.Nil(err)
  42. assert.Equal(1, len(l))
  43. assert.Equal(i, l[0])
  44. // querying a non-existent key fails
  45. _, _, err = store.Get("badname")
  46. assert.NotNil(err)
  47. // We can only delete once
  48. err = store.Delete(name)
  49. assert.Nil(err)
  50. err = store.Delete(name)
  51. assert.NotNil(err)
  52. // and then Get and List don't work
  53. _, _, err = store.Get(name)
  54. assert.NotNil(err)
  55. // List returns empty list
  56. l, err = store.List()
  57. assert.Nil(err)
  58. assert.Empty(l)
  59. }