2023-07-26 00:42:12 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-07-28 11:59:01 -06:00
|
|
|
"time"
|
2023-07-28 15:07:38 -06:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/ecdsa"
|
2023-07-26 00:42:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeDB(t *testing.T) {
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-26 00:42:12 -06:00
|
|
|
node_type := NodeType("test")
|
2023-07-27 01:30:32 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType})
|
2023-07-26 00:42:12 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 01:30:32 -06:00
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
node := NewNode(ctx, nil, node_type, 10, nil, NewGroupExt(nil))
|
2023-07-26 00:42:12 -06:00
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
ctx.Nodes = NodeMap{}
|
2023-07-28 00:04:18 -06:00
|
|
|
_, err = ctx.GetNode(node.ID)
|
2023-07-26 00:42:12 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
func TestNodeRead(t *testing.T) {
|
2023-07-28 19:32:27 -06:00
|
|
|
ctx := logTestContext(t, []string{})
|
2023-07-28 11:59:01 -06:00
|
|
|
node_type := NodeType("TEST")
|
2023-07-28 19:32:27 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{ACLExtType, GroupExtType, ECDHExtType})
|
2023-07-28 11:59:01 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
n1_key, err := ecdsa.GenerateKey(ctx.ECDSA, rand.Reader)
|
|
|
|
fatalErr(t, err)
|
|
|
|
n2_key, err := ecdsa.GenerateKey(ctx.ECDSA, rand.Reader)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
n1_id := KeyID(&n1_key.PublicKey)
|
|
|
|
n2_id := KeyID(&n2_key.PublicKey)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "N1: %s", n1_id)
|
|
|
|
ctx.Log.Logf("test", "N2: %s", n2_id)
|
|
|
|
|
|
|
|
n2_policy := NewPerNodePolicy(map[NodeID]Actions{
|
|
|
|
n1_id: Actions{MakeAction(ReadResultSignalType, "+")},
|
|
|
|
})
|
|
|
|
n2_listener := NewListenerExt(10)
|
2023-07-28 19:32:27 -06:00
|
|
|
n2 := NewNode(ctx, n2_key, node_type, 10, nil, NewACLExt(n2_policy), NewGroupExt(nil), NewECDHExt(), n2_listener)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
n1_policy := NewPerNodePolicy(map[NodeID]Actions{
|
|
|
|
n2_id: Actions{MakeAction(ReadSignalType, "+")},
|
|
|
|
})
|
2023-07-28 19:32:27 -06:00
|
|
|
n1 := NewNode(ctx, n1_key, node_type, 10, nil, NewACLExt(n1_policy), NewGroupExt(nil), NewECDHExt())
|
2023-07-28 11:59:01 -06:00
|
|
|
|
|
|
|
ctx.Send(n2.ID, n1.ID, NewReadSignal(map[ExtType][]string{
|
|
|
|
GroupExtType: []string{"members"},
|
|
|
|
}))
|
|
|
|
|
2023-07-28 19:32:27 -06:00
|
|
|
res, err := WaitForSignal(ctx, n2_listener, 10*time.Millisecond, ReadResultSignalType, func(sig ReadResultSignal) bool {
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx.Log.Logf("test", "READ_RESULT: %+v", res)
|
|
|
|
}
|
2023-07-28 19:32:27 -06:00
|
|
|
|
|
|
|
func TestECDH(t *testing.T) {
|
|
|
|
ctx := logTestContext(t, []string{"test", "ecdh", "policy"})
|
|
|
|
|
|
|
|
node_type := NodeType("TEST")
|
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{ACLExtType, ECDHExtType})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
n1_listener := NewListenerExt(10)
|
|
|
|
ecdh_policy := NewAllNodesPolicy(Actions{MakeAction(ECDHSignalType, "+")})
|
|
|
|
n1 := NewNode(ctx, nil, node_type, 10, nil, NewACLExt(ecdh_policy), NewECDHExt(), n1_listener)
|
|
|
|
n2 := NewNode(ctx, nil, node_type, 10, nil, NewACLExt(ecdh_policy), NewECDHExt())
|
|
|
|
|
|
|
|
ctx.Log.Logf("test", "N1: %s", n1.ID)
|
|
|
|
ctx.Log.Logf("test", "N2: %s", n2.ID)
|
|
|
|
|
|
|
|
|
|
|
|
ecdh_req, n1_ec, err := NewECDHReqSignal(ctx, n1)
|
|
|
|
ecdh_ext, err := GetExt[*ECDHExt](n1)
|
|
|
|
fatalErr(t, err)
|
|
|
|
ecdh_ext.ECDHStates[n2.ID] = ECDHState{
|
|
|
|
ECKey: n1_ec,
|
|
|
|
SharedSecret: nil,
|
|
|
|
}
|
|
|
|
fatalErr(t, err)
|
|
|
|
ctx.Log.Logf("test", "N1_EC: %+v", n1_ec)
|
|
|
|
err = ctx.Send(n1.ID, n2.ID, ecdh_req)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
_, err = WaitForSignal(ctx, n1_listener, 100*time.Millisecond, ECDHSignalType, func(sig ECDHSignal) bool {
|
|
|
|
return sig.State == "resp"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|