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

     1  package holochain
     2  
     3  import (
     4  	b58 "github.com/jbenet/go-base58"
     5  	ic "github.com/libp2p/go-libp2p-crypto"
     6  	peer "github.com/libp2p/go-libp2p-peer"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  type FakeRevocation struct {
    14  	data string
    15  }
    16  
    17  func (r *FakeRevocation) Verify() (err error) {
    18  	return
    19  }
    20  func (r *FakeRevocation) Marshal() (string, error) {
    21  	return r.data, nil
    22  }
    23  
    24  func (r *FakeRevocation) Unmarshal(data string) (err error) {
    25  	r.data = data
    26  	return
    27  }
    28  
    29  func TestLibP2PAgent(t *testing.T) {
    30  	d := SetupTestDir()
    31  	defer CleanupTestDir(d)
    32  	a := AgentIdentity("zippy@someemail.com")
    33  
    34  	Convey("it should fail to create an agent with an unknown key type", t, func() {
    35  		_, err := NewAgent(99, a, MakeTestSeed(""))
    36  		So(err.Error(), ShouldEqual, "unknown key type: 99")
    37  	})
    38  	Convey("it should be create a new agent that is saved to a file and then loadable", t, func() {
    39  		a1, err := NewAgent(LibP2P, a, MakeTestSeed(""))
    40  		So(err, ShouldBeNil)
    41  		err = SaveAgent(d, a1)
    42  		So(err, ShouldBeNil)
    43  		a2, err := LoadAgent(d)
    44  		So(err, ShouldBeNil)
    45  		So(a2.Identity(), ShouldEqual, a1.Identity())
    46  		So(ic.KeyEqual(a1.PrivKey(), a2.PrivKey()), ShouldBeTrue)
    47  		So(a1.AgentType(), ShouldEqual, LibP2P)
    48  
    49  		nodeID, nodeIDStr, err := a1.NodeID()
    50  		So(nodeIDStr, ShouldEqual, peer.IDB58Encode(nodeID))
    51  		So(nodeID.MatchesPublicKey(a1.PubKey()), ShouldBeTrue)
    52  
    53  	})
    54  
    55  	Convey("it should be able to b58 encode agent's public key", t, func() {
    56  		a1, err := NewAgent(LibP2P, a, MakeTestSeed(""))
    57  
    58  		b58pk, err := a1.EncodePubKey()
    59  		So(err, ShouldBeNil)
    60  		pk, _ := ic.MarshalPublicKey(a1.PubKey())
    61  		So(b58pk, ShouldEqual, b58.Encode(pk))
    62  	})
    63  
    64  	Convey("it should be able to create an AgentEntry for a chain", t, func() {
    65  		a1, err := NewAgent(LibP2P, a, MakeTestSeed(""))
    66  		So(err, ShouldBeNil)
    67  		revocation := FakeRevocation{data: "fake revocation"}
    68  		entry, err := a1.AgentEntry(&revocation)
    69  		So(err, ShouldBeNil)
    70  		So(entry.Identity, ShouldEqual, a)
    71  		So(string(entry.Revocation), ShouldEqual, "fake revocation")
    72  		pk, _ := a1.EncodePubKey()
    73  		So(string(entry.PublicKey), ShouldEqual, string(pk))
    74  	})
    75  	Convey("it should fail to load an agent file that has bad permissions", t, func() {
    76  		os.Chmod(filepath.Join(d, PrivKeyFileName), OS_USER_RW)
    77  		_, err := LoadAgent(d)
    78  		So(err.Error(), ShouldEqual, filepath.Join(d, PrivKeyFileName)+" file not read-only")
    79  	})
    80  	Convey("genkeys with with nil reader should use random seed", t, func() {
    81  		agent, _ := NewAgent(LibP2P, a, MakeTestSeed(""))
    82  		_, n1, _ := agent.NodeID()
    83  		agent.GenKeys(nil)
    84  		_, n2, _ := agent.NodeID()
    85  		So(n1, ShouldNotEqual, n2)
    86  	})
    87  	Convey("genkeys with fixed seed should generate the same key", t, func() {
    88  		agent, err := NewAgent(LibP2P, a, MakeTestSeed(""))
    89  		So(err, ShouldBeNil)
    90  		_, n1, err := agent.NodeID()
    91  		So(err, ShouldBeNil)
    92  		err = agent.GenKeys(MakeTestSeed("seed1"))
    93  		So(err, ShouldBeNil)
    94  		_, n2, err := agent.NodeID()
    95  		So(err, ShouldBeNil)
    96  		So(n1, ShouldNotEqual, n2)
    97  		err = agent.GenKeys(MakeTestSeed("seed1"))
    98  		So(err, ShouldBeNil)
    99  		_, n1, _ = agent.NodeID()
   100  		So(n1, ShouldEqual, n2)
   101  		err = agent.GenKeys(MakeTestSeed("different seed"))
   102  		So(err, ShouldBeNil)
   103  		_, n2, _ = agent.NodeID()
   104  		So(n1, ShouldNotEqual, n2)
   105  	})
   106  }