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