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"
|
2023-08-06 12:47:47 -06:00
|
|
|
"crypto/ed25519"
|
2023-07-26 00:42:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeDB(t *testing.T) {
|
2023-08-06 12:47:47 -06:00
|
|
|
ctx := logTestContext(t, []string{"signal", "node"})
|
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-08-07 20:26:02 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType, ECDHExtType})
|
2023-07-28 11:59:01 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-06 12:47:47 -06:00
|
|
|
n1_pub, n1_key, err := ed25519.GenerateKey(rand.Reader)
|
2023-07-28 15:07:38 -06:00
|
|
|
fatalErr(t, err)
|
2023-08-06 12:47:47 -06:00
|
|
|
n2_pub, n2_key, err := ed25519.GenerateKey(rand.Reader)
|
2023-07-28 15:07:38 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-06 12:47:47 -06:00
|
|
|
n1_id := KeyID(n1_pub)
|
|
|
|
n2_id := KeyID(n2_pub)
|
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_listener := NewListenerExt(10)
|
2023-08-07 20:26:02 -06:00
|
|
|
n2 := NewNode(ctx, n2_key, node_type, 10, nil, NewGroupExt(nil), NewECDHExt(), n2_listener)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
n1 := NewNode(ctx, n1_key, node_type, 10, nil, NewGroupExt(nil), NewECDHExt())
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
read_sig := NewReadSignal(map[ExtType][]string{
|
2023-07-28 11:59:01 -06:00
|
|
|
GroupExtType: []string{"members"},
|
2023-08-01 20:55:15 -06:00
|
|
|
})
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
|
|
|
msgs = msgs.Add(ctx.Log, n2.ID, n2.Key, read_sig, n1.ID)
|
|
|
|
err = ctx.Send(msgs)
|
|
|
|
fatalErr(t, err)
|
2023-07-28 11:59:01 -06:00
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
res, err := WaitForSignal(ctx, n2_listener.Chan, 10*time.Millisecond, ReadResultSignalType, func(sig *ReadResultSignal) bool {
|
2023-07-28 19:32:27 -06:00
|
|
|
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")
|
2023-08-07 20:26:02 -06:00
|
|
|
err := ctx.RegisterNodeType(node_type, []ExtType{ECDHExtType})
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
n1_listener := NewListenerExt(10)
|
2023-08-07 20:26:02 -06:00
|
|
|
n1 := NewNode(ctx, nil, node_type, 10, nil, NewECDHExt(), n1_listener)
|
|
|
|
n2 := NewNode(ctx, nil, node_type, 10, nil, NewECDHExt())
|
2023-07-29 00:28:44 -06:00
|
|
|
n3_listener := NewListenerExt(10)
|
2023-08-07 20:26:02 -06:00
|
|
|
n3 := NewNode(ctx, nil, node_type, 10, nil, NewECDHExt(), n3_listener)
|
2023-07-28 19:32:27 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "N1: %s", n1.ID)
|
|
|
|
ctx.Log.Logf("test", "N2: %s", n2.ID)
|
|
|
|
|
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
ecdh_req, n1_ec, err := NewECDHReqSignal(n1)
|
2023-07-28 19:32:27 -06:00
|
|
|
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)
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
|
|
|
msgs = msgs.Add(ctx.Log, n1.ID, n1.Key, ecdh_req, n2.ID)
|
|
|
|
err = ctx.Send(msgs)
|
2023-07-28 19:32:27 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
_, err = WaitForSignal(ctx, n1_listener.Chan, 100*time.Millisecond, ECDHSignalType, func(sig *ECDHSignal) bool {
|
2023-07-30 23:42:47 -06:00
|
|
|
return sig.Str == "resp"
|
2023-07-28 19:32:27 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
2023-07-29 00:28:44 -06:00
|
|
|
time.Sleep(10*time.Millisecond)
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
ecdh_sig, err := NewECDHProxySignal(n1.ID, n3.ID, &StopSignal, ecdh_ext.ECDHStates[n2.ID].SharedSecret)
|
2023-07-29 00:28:44 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
msgs = Messages{}
|
|
|
|
msgs = msgs.Add(ctx.Log, n1.ID, n1.Key, ecdh_sig, n2.ID)
|
|
|
|
err = ctx.Send(msgs)
|
2023-07-29 00:28:44 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-28 19:32:27 -06:00
|
|
|
}
|