feeds_test.go 5.5 KB

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