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