github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/warrant_test.go (about) 1 package holochain 2 3 import ( 4 "fmt" 5 peer "github.com/libp2p/go-libp2p-peer" 6 . "github.com/smartystreets/goconvey/convey" 7 8 "testing" 9 ) 10 11 func TestSelfRevocationWarrant(t *testing.T) { 12 oldH, oldPrivKey := makePeer("peer1") 13 newH, newPrivKey := makePeer("peer2") 14 15 revocation, _ := NewSelfRevocation(oldPrivKey, newPrivKey, []byte("extra data")) 16 17 w, err := NewSelfRevocationWarrant(revocation) 18 19 Convey("NewSelfRevocation should create one", t, func() { 20 So(err, ShouldBeNil) 21 So(fmt.Sprintf("%v", w.Revocation), ShouldEqual, fmt.Sprintf("%v", *revocation)) 22 }) 23 24 Convey("it should have a type", t, func() { 25 So(w.Type(), ShouldEqual, SelfRevocationType) 26 }) 27 28 Convey("it should have the two revocation parties", t, func() { 29 parties, err := w.Parties() 30 So(err, ShouldBeNil) 31 So(len(parties), ShouldEqual, 2) 32 So(peer.IDB58Encode(oldH), ShouldEqual, parties[0].String()) 33 So(peer.IDB58Encode(newH), ShouldEqual, parties[1].String()) 34 }) 35 36 Convey("it should have a payload property", t, func() { 37 payload, err := w.Property("payload") 38 So(err, ShouldBeNil) 39 So(string(payload.([]byte)), ShouldEqual, "extra data") 40 41 _, err = w.Property("foo") 42 So(err, ShouldEqual, WarrantPropertyNotFoundErr) 43 }) 44 45 d, _, h := PrepareTestChain("test") 46 defer CleanupTestChain(h, d) 47 48 Convey("verification should fail if not true in context", t, func() { 49 err = w.Verify(h) 50 So(err.Error(), ShouldEqual, "expected old key to be modified on DHT") 51 }) 52 53 Convey("verification should succeed if true in context", t, func() { 54 oldNodeIDStr := h.nodeIDStr 55 _, err := NewJSRibosome(h, &Zome{RibosomeType: JSRibosomeType, 56 Code: fmt.Sprintf(`updateAgent({Revocation:"some revocation data"})`)}) 57 So(err, ShouldBeNil) 58 header := h.chain.Top() 59 entry, _, _ := h.chain.GetEntry(header.EntryLink) 60 revocation := &SelfRevocation{} 61 a, _ := AgentEntryFromJSON(entry.Content().(string)) 62 revocation.Unmarshal(a.Revocation) 63 w, err := NewSelfRevocationWarrant(revocation) 64 65 parties, err := w.Parties() 66 So(oldNodeIDStr, ShouldEqual, parties[0].String()) 67 So(h.nodeIDStr, ShouldEqual, parties[1].String()) 68 69 err = w.Verify(h) 70 So(err, ShouldBeNil) 71 }) 72 73 Convey("it should encode and decode warrants", t, func() { 74 encoded, err := w.Encode() 75 So(err, ShouldBeNil) 76 w1 := &SelfRevocationWarrant{} 77 err = w1.Decode(encoded) 78 So(err, ShouldBeNil) 79 So(fmt.Sprintf("%v", w1), ShouldEqual, fmt.Sprintf("%v", w)) 80 81 w2, err := DecodeWarrant(SelfRevocationType, encoded) 82 So(err, ShouldBeNil) 83 So(fmt.Sprintf("%v", w2), ShouldEqual, fmt.Sprintf("%v", w)) 84 85 w2, err = DecodeWarrant(99, encoded) 86 So(w2, ShouldBeNil) 87 So(err, ShouldEqual, UnknownWarrantTypeErr) 88 89 }) 90 }