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.

193 lines
4.5 KiB

  1. package server_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/gorilla/mux"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. keys "github.com/tendermint/go-crypto/keys"
  12. "github.com/tendermint/go-crypto/keys/cryptostore"
  13. "github.com/tendermint/go-crypto/keys/server"
  14. "github.com/tendermint/go-crypto/keys/server/types"
  15. "github.com/tendermint/go-crypto/keys/storage/memstorage"
  16. )
  17. func TestKeyServer(t *testing.T) {
  18. assert, require := assert.New(t), require.New(t)
  19. r := setupServer()
  20. // let's abstract this out a bit....
  21. keys, code, err := listKeys(r)
  22. require.Nil(err)
  23. require.Equal(http.StatusOK, code)
  24. assert.Equal(0, len(keys))
  25. algo := "ed25519"
  26. n1, n2 := "personal", "business"
  27. p0, p1, p2 := "1234", "over10chars...", "really-secure!@#$"
  28. // this fails for validation
  29. _, code, err = createKey(r, n1, p0, algo)
  30. require.Nil(err, "%+v", err)
  31. require.NotEqual(http.StatusOK, code)
  32. // new password better
  33. key, code, err := createKey(r, n1, p1, algo)
  34. require.Nil(err, "%+v", err)
  35. require.Equal(http.StatusOK, code)
  36. require.Equal(n1, key.Key.Name)
  37. require.NotEmpty(n1, key.Seed)
  38. // the other one works
  39. key2, code, err := createKey(r, n2, p2, algo)
  40. require.Nil(err, "%+v", err)
  41. require.Equal(http.StatusOK, code)
  42. require.Equal(key2.Key.Name, n2)
  43. require.NotEmpty(n2, key.Seed)
  44. // let's abstract this out a bit....
  45. keys, code, err = listKeys(r)
  46. require.Nil(err)
  47. require.Equal(http.StatusOK, code)
  48. if assert.Equal(2, len(keys)) {
  49. // in alphabetical order
  50. assert.Equal(keys[0].Name, n2)
  51. assert.Equal(keys[1].Name, n1)
  52. }
  53. // get works
  54. k, code, err := getKey(r, n1)
  55. require.Nil(err, "%+v", err)
  56. require.Equal(http.StatusOK, code)
  57. assert.Equal(n1, k.Name)
  58. assert.NotNil(k.Address)
  59. assert.Equal(key.Key.Address, k.Address)
  60. // delete with proper key
  61. _, code, err = deleteKey(r, n1, p1)
  62. require.Nil(err, "%+v", err)
  63. require.Equal(http.StatusOK, code)
  64. // after delete, get and list different
  65. _, code, err = getKey(r, n1)
  66. require.Nil(err, "%+v", err)
  67. require.NotEqual(http.StatusOK, code)
  68. keys, code, err = listKeys(r)
  69. require.Nil(err, "%+v", err)
  70. require.Equal(http.StatusOK, code)
  71. if assert.Equal(1, len(keys)) {
  72. assert.Equal(keys[0].Name, n2)
  73. }
  74. }
  75. func setupServer() http.Handler {
  76. // make the storage with reasonable defaults
  77. cstore := cryptostore.New(
  78. cryptostore.SecretBox,
  79. memstorage.New(),
  80. keys.MustLoadCodec("english"),
  81. )
  82. // build your http server
  83. ks := server.New(cstore, "ed25519")
  84. r := mux.NewRouter()
  85. sk := r.PathPrefix("/keys").Subrouter()
  86. ks.Register(sk)
  87. return r
  88. }
  89. // return data, status code, and error
  90. func listKeys(h http.Handler) (keys.Infos, int, error) {
  91. rr := httptest.NewRecorder()
  92. req, err := http.NewRequest("GET", "/keys/", nil)
  93. if err != nil {
  94. return nil, 0, err
  95. }
  96. h.ServeHTTP(rr, req)
  97. if http.StatusOK != rr.Code {
  98. return nil, rr.Code, nil
  99. }
  100. data := keys.Infos{}
  101. err = json.Unmarshal(rr.Body.Bytes(), &data)
  102. return data, rr.Code, err
  103. }
  104. func getKey(h http.Handler, name string) (*keys.Info, int, error) {
  105. rr := httptest.NewRecorder()
  106. req, err := http.NewRequest("GET", "/keys/"+name, nil)
  107. if err != nil {
  108. return nil, 0, err
  109. }
  110. h.ServeHTTP(rr, req)
  111. if http.StatusOK != rr.Code {
  112. return nil, rr.Code, nil
  113. }
  114. data := keys.Info{}
  115. err = json.Unmarshal(rr.Body.Bytes(), &data)
  116. return &data, rr.Code, err
  117. }
  118. func createKey(h http.Handler, name, passphrase, algo string) (*types.CreateKeyResponse, int, error) {
  119. rr := httptest.NewRecorder()
  120. post := types.CreateKeyRequest{
  121. Name: name,
  122. Passphrase: passphrase,
  123. Algo: algo,
  124. }
  125. var b bytes.Buffer
  126. err := json.NewEncoder(&b).Encode(&post)
  127. if err != nil {
  128. return nil, 0, err
  129. }
  130. req, err := http.NewRequest("POST", "/keys/", &b)
  131. if err != nil {
  132. return nil, 0, err
  133. }
  134. h.ServeHTTP(rr, req)
  135. if http.StatusOK != rr.Code {
  136. return nil, rr.Code, nil
  137. }
  138. data := new(types.CreateKeyResponse)
  139. err = json.Unmarshal(rr.Body.Bytes(), data)
  140. return data, rr.Code, err
  141. }
  142. func deleteKey(h http.Handler, name, passphrase string) (*types.ErrorResponse, int, error) {
  143. rr := httptest.NewRecorder()
  144. post := types.DeleteKeyRequest{
  145. Name: name,
  146. Passphrase: passphrase,
  147. }
  148. var b bytes.Buffer
  149. err := json.NewEncoder(&b).Encode(&post)
  150. if err != nil {
  151. return nil, 0, err
  152. }
  153. req, err := http.NewRequest("DELETE", "/keys/"+name, &b)
  154. if err != nil {
  155. return nil, 0, err
  156. }
  157. h.ServeHTTP(rr, req)
  158. if http.StatusOK != rr.Code {
  159. return nil, rr.Code, nil
  160. }
  161. data := types.ErrorResponse{}
  162. err = json.Unmarshal(rr.Body.Bytes(), &data)
  163. return &data, rr.Code, err
  164. }