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.

106 lines
2.5 KiB

  1. package filestorage
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. crypto "github.com/tendermint/go-crypto"
  10. keys "github.com/tendermint/go-crypto/keys"
  11. )
  12. func TestBasicCRUD(t *testing.T) {
  13. assert, require := assert.New(t), require.New(t)
  14. dir, err := ioutil.TempDir("", "filestorage-test")
  15. assert.Nil(err)
  16. defer os.RemoveAll(dir)
  17. store := New(dir)
  18. name := "bar"
  19. key := []byte("secret-key-here")
  20. pubkey := crypto.GenPrivKeyEd25519().PubKey()
  21. info := keys.Info{
  22. Name: name,
  23. PubKey: pubkey.Wrap(),
  24. }
  25. // No data: Get and Delete return nothing
  26. _, _, err = store.Get(name)
  27. assert.NotNil(err)
  28. err = store.Delete(name)
  29. assert.NotNil(err)
  30. // List returns empty list
  31. l, err := store.List()
  32. assert.Nil(err)
  33. assert.Empty(l)
  34. // Putting the key in the store must work
  35. err = store.Put(name, key, info)
  36. assert.Nil(err)
  37. // But a second time is a failure
  38. err = store.Put(name, key, info)
  39. assert.NotNil(err)
  40. // Now, we can get and list properly
  41. k, i, err := store.Get(name)
  42. require.Nil(err, "%+v", err)
  43. assert.Equal(key, k)
  44. assert.Equal(info.Name, i.Name)
  45. assert.Equal(info.PubKey, i.PubKey)
  46. assert.NotEmpty(i.Address)
  47. l, err = store.List()
  48. require.Nil(err, "%+v", err)
  49. assert.Equal(1, len(l))
  50. assert.Equal(i, l[0])
  51. // querying a non-existent key fails
  52. _, _, err = store.Get("badname")
  53. assert.NotNil(err)
  54. // We can only delete once
  55. err = store.Delete(name)
  56. assert.Nil(err)
  57. err = store.Delete(name)
  58. assert.NotNil(err)
  59. // and then Get and List don't work
  60. _, _, err = store.Get(name)
  61. assert.NotNil(err)
  62. // List returns empty list
  63. l, err = store.List()
  64. assert.Nil(err)
  65. assert.Empty(l)
  66. }
  67. func TestDirectoryHandling(t *testing.T) {
  68. assert, require := assert.New(t), require.New(t)
  69. // prepare a temp dir and make sure it is not there
  70. newDir := path.Join(os.TempDir(), "file-test-dir")
  71. _, err := os.Open(newDir)
  72. assert.True(os.IsNotExist(err))
  73. defer os.RemoveAll(newDir)
  74. // now, check with two levels deep....
  75. parentDir := path.Join(os.TempDir(), "missing-dir")
  76. nestedDir := path.Join(parentDir, "lots", "of", "levels", "here")
  77. _, err = os.Open(parentDir)
  78. assert.True(os.IsNotExist(err))
  79. defer os.RemoveAll(parentDir)
  80. // create a new storage, and verify it creates the directory with good permissions
  81. for _, dir := range []string{newDir, nestedDir, newDir} {
  82. New(dir)
  83. d, err := os.Open(dir)
  84. require.Nil(err)
  85. defer d.Close()
  86. stat, err := d.Stat()
  87. require.Nil(err)
  88. assert.Equal(dirPerm, stat.Mode()&os.ModePerm)
  89. }
  90. }