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