github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_get_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/holochain/holochain-proto/hash"
     6  	peer "github.com/libp2p/go-libp2p-peer"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"testing"
     9  )
    10  
    11  func TestActionGet(t *testing.T) {
    12  	nodesCount := 3
    13  	mt := setupMultiNodeTesting(nodesCount)
    14  	defer mt.cleanupMultiNodeTesting()
    15  
    16  	h := mt.nodes[0]
    17  
    18  	e := GobEntry{C: "3"}
    19  	hash, _ := e.Sum(h.hashSpec)
    20  
    21  	Convey("receive should return not found if it doesn't exist", t, func() {
    22  		m := h.node.NewMessage(GET_REQUEST, GetReq{H: hash})
    23  		_, err := ActionReceiver(h, m)
    24  		So(err, ShouldEqual, ErrHashNotFound)
    25  
    26  		options := GetOptions{}
    27  		a := ActionGet{GetReq{H: hash}, &options}
    28  		fn := &APIFnGet{action: a}
    29  		response, err := fn.Call(h)
    30  		So(err, ShouldEqual, ErrHashNotFound)
    31  		So(fmt.Sprintf("%v", response), ShouldEqual, "<nil>")
    32  
    33  	})
    34  
    35  	commit(h, "oddNumbers", "3")
    36  	m := h.node.NewMessage(GET_REQUEST, GetReq{H: hash})
    37  	Convey("receive should return value if it exists", t, func() {
    38  		r, err := ActionReceiver(h, m)
    39  		So(err, ShouldBeNil)
    40  		resp := r.(GetResp)
    41  		So(resp.Entry.Content().(string), ShouldEqual, "3")
    42  	})
    43  
    44  	ringConnect(t, mt.ctx, mt.nodes, nodesCount)
    45  	Convey("receive should return closer peers if it can", t, func() {
    46  		h2 := mt.nodes[2]
    47  		r, err := ActionReceiver(h2, m)
    48  		So(err, ShouldBeNil)
    49  		resp := r.(CloserPeersResp)
    50  		So(len(resp.CloserPeers), ShouldEqual, 1)
    51  		So(peer.ID(resp.CloserPeers[0].ID).Pretty(), ShouldEqual, "QmUfY4WeqD3UUfczjdkoFQGEgCAVNf7rgFfjdeTbr7JF1C")
    52  	})
    53  
    54  	Convey("get should return not found if hash doesn't exist and we are connected", t, func() {
    55  		hash, err := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzfrom")
    56  		if err != nil {
    57  			panic(err)
    58  		}
    59  
    60  		options := GetOptions{}
    61  		a := ActionGet{GetReq{H: hash}, &options}
    62  		fn := &APIFnGet{action: a}
    63  		response, err := fn.Call(h)
    64  		So(err, ShouldEqual, ErrHashNotFound)
    65  		So(response, ShouldBeNil)
    66  
    67  	})
    68  
    69  }
    70  
    71  func TestActionGetLocal(t *testing.T) {
    72  	d, _, h := PrepareTestChain("test")
    73  	defer CleanupTestChain(h, d)
    74  
    75  	hash := commit(h, "secret", "31415")
    76  
    77  	Convey("non local get should fail for private entries", t, func() {
    78  		req := GetReq{H: hash, GetMask: GetMaskEntry}
    79  		_, err := callGet(h, req, &GetOptions{GetMask: req.GetMask})
    80  		So(err.Error(), ShouldEqual, "hash not found")
    81  	})
    82  
    83  	Convey("it should fail to get non-existent private local values", t, func() {
    84  		badHash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
    85  		req := GetReq{H: badHash, GetMask: GetMaskEntry}
    86  		_, err := callGet(h, req, &GetOptions{GetMask: req.GetMask, Local: true})
    87  		So(err.Error(), ShouldEqual, "hash not found")
    88  	})
    89  
    90  	Convey("it should get private local values", t, func() {
    91  		req := GetReq{H: hash, GetMask: GetMaskEntry}
    92  		rsp, err := callGet(h, req, &GetOptions{GetMask: req.GetMask, Local: true})
    93  		So(err, ShouldBeNil)
    94  		getResp := rsp.(GetResp)
    95  		So(getResp.Entry.Content().(string), ShouldEqual, "31415")
    96  	})
    97  
    98  	Convey("it should get local bundle values", t, func() {
    99  		_, err := NewStartBundleAction(0, "myBundle").Call(h)
   100  		So(err, ShouldBeNil)
   101  		hash := commit(h, "oddNumbers", "3141")
   102  		req := GetReq{H: hash, GetMask: GetMaskEntry}
   103  		_, err = callGet(h, req, &GetOptions{GetMask: req.GetMask, Local: true})
   104  		So(err, ShouldEqual, ErrHashNotFound)
   105  		rsp, err := callGet(h, req, &GetOptions{GetMask: req.GetMask, Bundle: true})
   106  		So(err, ShouldBeNil)
   107  		getResp := rsp.(GetResp)
   108  		So(getResp.Entry.Content().(string), ShouldEqual, "3141")
   109  	})
   110  }