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