feeds_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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"
  22. "io/ioutil"
  23. "os"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/swarm/api"
  26. "github.com/ethereum/go-ethereum/swarm/storage/feeds/lookup"
  27. "github.com/ethereum/go-ethereum/swarm/testutil"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/swarm/storage/feeds"
  30. "github.com/ethereum/go-ethereum/common/hexutil"
  31. "github.com/ethereum/go-ethereum/log"
  32. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  33. swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
  34. )
  35. func TestCLIFeedUpdate(t *testing.T) {
  36. srv := testutil.NewTestSwarmServer(t, func(api *api.API) testutil.TestServer {
  37. return swarmhttp.NewServer(api, "")
  38. }, nil)
  39. log.Info("starting a test swarm server")
  40. defer srv.Close()
  41. // create a private key file for signing
  42. pkfile, err := ioutil.TempFile("", "swarm-test")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer pkfile.Close()
  47. defer os.Remove(pkfile.Name())
  48. privkeyHex := "0000000000000000000000000000000000000000000000000000000000001979"
  49. privKey, _ := crypto.HexToECDSA(privkeyHex)
  50. address := crypto.PubkeyToAddress(privKey.PublicKey)
  51. // save the private key to a file
  52. _, err = io.WriteString(pkfile, privkeyHex)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. // compose a topic. We'll be doing quotes about Miguel de Cervantes
  57. var topic feeds.Topic
  58. subject := []byte("Miguel de Cervantes")
  59. copy(topic[:], subject[:])
  60. name := "quotes"
  61. // prepare some data for the update
  62. data := []byte("En boca cerrada no entran moscas")
  63. hexData := hexutil.Encode(data)
  64. flags := []string{
  65. "--bzzapi", srv.URL,
  66. "--bzzaccount", pkfile.Name(),
  67. "feed", "update",
  68. "--topic", topic.Hex(),
  69. "--name", name,
  70. hexData}
  71. // create an update and expect an exit without errors
  72. log.Info(fmt.Sprintf("updating a feed with 'swarm feed update'"))
  73. cmd := runSwarm(t, flags...)
  74. cmd.ExpectExit()
  75. // now try to get the update using the client
  76. client := swarm.NewClient(srv.URL)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. // build the same topic as before, this time
  81. // we use NewTopic to create a topic automatically.
  82. topic, err = feeds.NewTopic(name, subject)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. // Feed configures whose updates we will be looking up.
  87. feed := feeds.Feed{
  88. Topic: topic,
  89. User: address,
  90. }
  91. // Build a query to get the latest update
  92. query := feeds.NewQueryLatest(&feed, lookup.NoClue)
  93. // retrieve content!
  94. reader, err := client.QueryFeed(query, "")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. retrieved, err := ioutil.ReadAll(reader)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. // check we retrieved the sent information
  103. if !bytes.Equal(data, retrieved) {
  104. t.Fatalf("Received %s, expected %s", retrieved, data)
  105. }
  106. // Now retrieve info for the next update
  107. flags = []string{
  108. "--bzzapi", srv.URL,
  109. "feed", "info",
  110. "--topic", topic.Hex(),
  111. "--user", address.Hex(),
  112. }
  113. log.Info(fmt.Sprintf("getting feed info with 'swarm feed info'"))
  114. cmd = runSwarm(t, flags...)
  115. _, matches := cmd.ExpectRegexp(`.*`) // regex hack to extract stdout
  116. cmd.ExpectExit()
  117. // verify we can deserialize the result as a valid JSON
  118. var request feeds.Request
  119. err = json.Unmarshal([]byte(matches[0]), &request)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. // make sure the retrieved Feed is the same
  124. if request.Feed != feed {
  125. t.Fatalf("Expected feed to be: %s, got %s", feed, request.Feed)
  126. }
  127. // test publishing a manifest
  128. flags = []string{
  129. "--bzzapi", srv.URL,
  130. "--bzzaccount", pkfile.Name(),
  131. "feed", "create",
  132. "--topic", topic.Hex(),
  133. }
  134. log.Info(fmt.Sprintf("Publishing manifest with 'swarm feed create'"))
  135. cmd = runSwarm(t, flags...)
  136. _, matches = cmd.ExpectRegexp(`[a-f\d]{64}`) // regex hack to extract stdout
  137. cmd.ExpectExit()
  138. manifestAddress := matches[0] // read the received feed manifest
  139. // now attempt to lookup the latest update using a manifest instead
  140. reader, err = client.QueryFeed(nil, manifestAddress)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. retrieved, err = ioutil.ReadAll(reader)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. if !bytes.Equal(data, retrieved) {
  149. t.Fatalf("Received %s, expected %s", retrieved, data)
  150. }
  151. }