feeds_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common/hexutil"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/swarm/api"
  28. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  29. swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
  30. "github.com/ethereum/go-ethereum/swarm/storage/feed"
  31. "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
  32. "github.com/ethereum/go-ethereum/swarm/testutil"
  33. )
  34. func TestCLIFeedUpdate(t *testing.T) {
  35. srv := swarmhttp.NewTestSwarmServer(t, func(api *api.API) swarmhttp.TestServer {
  36. return swarmhttp.NewServer(api, "")
  37. }, nil)
  38. log.Info("starting a test swarm server")
  39. defer srv.Close()
  40. // create a private key file for signing
  41. privkeyHex := "0000000000000000000000000000000000000000000000000000000000001979"
  42. privKey, _ := crypto.HexToECDSA(privkeyHex)
  43. address := crypto.PubkeyToAddress(privKey.PublicKey)
  44. pkFileName := testutil.TempFileWithContent(t, privkeyHex)
  45. defer os.Remove(pkFileName)
  46. // compose a topic. We'll be doing quotes about Miguel de Cervantes
  47. var topic feed.Topic
  48. subject := []byte("Miguel de Cervantes")
  49. copy(topic[:], subject[:])
  50. name := "quotes"
  51. // prepare some data for the update
  52. data := []byte("En boca cerrada no entran moscas")
  53. hexData := hexutil.Encode(data)
  54. flags := []string{
  55. "--bzzapi", srv.URL,
  56. "--bzzaccount", pkFileName,
  57. "feed", "update",
  58. "--topic", topic.Hex(),
  59. "--name", name,
  60. hexData}
  61. // create an update and expect an exit without errors
  62. log.Info(fmt.Sprintf("updating a feed with 'swarm feed update'"))
  63. cmd := runSwarm(t, flags...)
  64. cmd.ExpectExit()
  65. // now try to get the update using the client
  66. client := swarm.NewClient(srv.URL)
  67. // build the same topic as before, this time
  68. // we use NewTopic to create a topic automatically.
  69. topic, err := feed.NewTopic(name, subject)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. // Feed configures whose updates we will be looking up.
  74. fd := feed.Feed{
  75. Topic: topic,
  76. User: address,
  77. }
  78. // Build a query to get the latest update
  79. query := feed.NewQueryLatest(&fd, lookup.NoClue)
  80. // retrieve content!
  81. reader, err := client.QueryFeed(query, "")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. retrieved, err := ioutil.ReadAll(reader)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. // check we retrieved the sent information
  90. if !bytes.Equal(data, retrieved) {
  91. t.Fatalf("Received %s, expected %s", retrieved, data)
  92. }
  93. // Now retrieve info for the next update
  94. flags = []string{
  95. "--bzzapi", srv.URL,
  96. "feed", "info",
  97. "--topic", topic.Hex(),
  98. "--user", address.Hex(),
  99. }
  100. log.Info(fmt.Sprintf("getting feed info with 'swarm feed info'"))
  101. cmd = runSwarm(t, flags...)
  102. _, matches := cmd.ExpectRegexp(`.*`) // regex hack to extract stdout
  103. cmd.ExpectExit()
  104. // verify we can deserialize the result as a valid JSON
  105. var request feed.Request
  106. err = json.Unmarshal([]byte(matches[0]), &request)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. // make sure the retrieved feed is the same
  111. if request.Feed != fd {
  112. t.Fatalf("Expected feed to be: %s, got %s", fd, request.Feed)
  113. }
  114. // test publishing a manifest
  115. flags = []string{
  116. "--bzzapi", srv.URL,
  117. "--bzzaccount", pkFileName,
  118. "feed", "create",
  119. "--topic", topic.Hex(),
  120. }
  121. log.Info(fmt.Sprintf("Publishing manifest with 'swarm feed create'"))
  122. cmd = runSwarm(t, flags...)
  123. _, matches = cmd.ExpectRegexp(`[a-f\d]{64}`) // regex hack to extract stdout
  124. cmd.ExpectExit()
  125. manifestAddress := matches[0] // read the received feed manifest
  126. // now attempt to lookup the latest update using a manifest instead
  127. reader, err = client.QueryFeed(nil, manifestAddress)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. retrieved, err = ioutil.ReadAll(reader)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. if !bytes.Equal(data, retrieved) {
  136. t.Fatalf("Received %s, expected %s", retrieved, data)
  137. }
  138. }