graphvent/gql_test.go

147 lines
5.0 KiB
Go

2023-06-25 20:20:59 -06:00
package graphvent
import (
"testing"
2023-06-25 22:19:05 -06:00
"time"
"errors"
"crypto/rand"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
2023-06-25 20:20:59 -06:00
)
func TestGQLDBLoad(t * testing.T) {
2023-07-26 13:28:03 -06:00
ctx := logTestContext(t, []string{"test", "db", "policy", "signal"})
TestUserNodeType := NodeType("TEST_USER")
err := ctx.RegisterNodeType(TestUserNodeType, []ExtType{ACLExtType, ACLPolicyExtType})
fatalErr(t, err)
u1 := NewNode(RandID(), TestUserNodeType)
ctx.Nodes[u1.ID] = &u1
u1.Extensions[ACLExtType] = NewACLExt(nil)
u1.Extensions[ACLPolicyExtType] = NewACLPolicyExt(map[PolicyType]Policy{
PerNodePolicyType: NewPerNodePolicy(map[NodeID][]string{
u1.ID: []string{"users.write", "children.write", "parent.write", "dependencies.write", "requirements.write"},
}, nil),
})
ctx.Log.Logf("test", "U1_ID: %s", u1.ID)
2023-07-26 11:56:10 -06:00
ListenerNodeType := NodeType("LISTENER")
2023-07-26 13:28:03 -06:00
err = ctx.RegisterNodeType(ListenerNodeType, []ExtType{ACLExtType, ListenerExtType, LockableExtType})
2023-07-20 00:24:22 -06:00
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
l1 := NewNode(RandID(), ListenerNodeType)
2023-07-26 13:28:03 -06:00
ctx.Nodes[l1.ID] = &l1
l1.Extensions[ACLExtType] = NewACLExt(NodeList(&u1))
2023-07-26 11:56:10 -06:00
listener_ext := NewListenerExt(10)
l1.Extensions[ListenerExtType] = listener_ext
l1.Extensions[LockableExtType] = NewLockableExt(nil, nil, nil, nil)
2023-07-20 23:19:10 -06:00
2023-07-26 11:56:10 -06:00
ctx.Log.Logf("test", "L1_ID: %s", l1.ID)
2023-07-26 11:56:10 -06:00
TestThreadNodeType := NodeType("TEST_THREAD")
err = ctx.RegisterNodeType(TestThreadNodeType, []ExtType{ACLExtType, ThreadExtType, LockableExtType})
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
t1 := NewNode(RandID(), TestThreadNodeType)
2023-07-26 13:28:03 -06:00
ctx.Nodes[t1.ID] = &t1
t1.Extensions[ACLExtType] = NewACLExt(NodeList(&u1))
t1.Extensions[ACLPolicyExtType] = NewACLPolicyExt(map[PolicyType]Policy{
ParentOfPolicyType: NewParentOfPolicy(map[NodeID][]string{
t1.ID: []string{"signal.abort", "state.write"},
}),
})
2023-07-26 11:56:10 -06:00
t1.Extensions[ThreadExtType], err = NewThreadExt(ctx, BaseThreadType, nil, nil, "init", nil)
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
t1.Extensions[LockableExtType] = NewLockableExt(nil, nil, nil, nil)
ctx.Log.Logf("test", "T1_ID: %s", t1.ID)
2023-07-26 11:56:10 -06:00
TestGQLNodeType := NodeType("TEST_GQL")
err = ctx.RegisterNodeType(TestGQLNodeType, []ExtType{ACLExtType, GroupExtType, GQLExtType, ThreadExtType, LockableExtType})
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
gql := NewNode(RandID(), TestGQLNodeType)
2023-07-26 13:28:03 -06:00
ctx.Nodes[gql.ID] = &gql
gql.Extensions[ACLExtType] = NewACLExt(NodeList(&u1))
gql.Extensions[ACLPolicyExtType] = NewACLPolicyExt(map[PolicyType]Policy{
ChildOfPolicyType: NewChildOfPolicy(map[NodeID][]string{
gql.ID: []string{"signal.status"},
}),
})
2023-07-26 11:56:10 -06:00
gql.Extensions[GroupExtType] = NewGroupExt(nil)
gql.Extensions[GQLExtType] = NewGQLExt(":0", ecdh.P256(), key, nil, nil)
gql.Extensions[ThreadExtType], err = NewThreadExt(ctx, GQLThreadType, nil, nil, "ini", nil)
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
gql.Extensions[LockableExtType] = NewLockableExt(nil, nil, nil, nil)
2023-07-26 11:56:10 -06:00
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
info := ParentInfo{true, "start", "restore"}
context := NewWriteContext(ctx)
2023-07-26 13:28:03 -06:00
err = UpdateStates(context, &u1, NewACLInfo(&gql, []string{"users"}), func(context *StateContext) error {
err := LinkThreads(context, &u1, &gql, ChildInfo{&t1, map[InfoType]Info{
2023-07-26 11:56:10 -06:00
ParentInfoType: &info,
2023-07-24 16:04:56 -06:00
}})
2023-07-22 21:24:54 -06:00
if err != nil {
return err
}
2023-07-26 13:28:03 -06:00
return LinkLockables(context, &u1, &l1, []*Node{&gql})
})
fatalErr(t, err)
2023-07-09 20:30:19 -06:00
context = NewReadContext(ctx)
2023-07-26 11:56:10 -06:00
err = Signal(context, &gql, &gql, NewStatusSignal("child_linked", t1.ID))
fatalErr(t, err)
context = NewReadContext(ctx)
2023-07-24 16:04:56 -06:00
err = Signal(context, &gql, &gql, AbortSignal)
2023-07-09 20:30:19 -06:00
fatalErr(t, err)
2023-07-24 16:04:56 -06:00
err = ThreadLoop(ctx, &gql, "start")
if errors.Is(err, ThreadAbortedError) == false {
fatalErr(t, err)
}
2023-07-26 11:56:10 -06:00
(*GraphTester)(t).WaitForStatus(ctx, listener_ext.Chan, "aborted", 100*time.Millisecond, "Didn't receive aborted on listener")
context = NewReadContext(ctx)
2023-07-26 11:56:10 -06:00
err = UseStates(context, &gql, ACLList([]*Node{&gql, &u1}, nil), func(context *StateContext) error {
2023-07-09 20:30:19 -06:00
ser1, err := gql.Serialize()
ser2, err := u1.Serialize()
ctx.Log.Logf("test", "\n%s\n\n", ser1)
ctx.Log.Logf("test", "\n%s\n\n", ser2)
return err
})
2023-07-26 11:56:10 -06:00
// Clear all loaded nodes from the context so it loads them from the database
ctx.Nodes = NodeMap{}
gql_loaded, err := LoadNode(ctx, gql.ID)
fatalErr(t, err)
context = NewReadContext(ctx)
2023-07-26 11:56:10 -06:00
err = UseStates(context, gql_loaded, NewACLInfo(gql_loaded, []string{"users", "children", "requirements"}), func(context *StateContext) error {
2023-07-09 20:30:19 -06:00
ser, err := gql_loaded.Serialize()
2023-07-26 11:56:10 -06:00
lockable_ext, err := GetExt[*LockableExt](gql_loaded)
if err != nil {
return err
}
ctx.Log.Logf("test", "\n%s\n\n", ser)
2023-07-26 11:56:10 -06:00
dependency := lockable_ext.Dependencies[l1.ID]
listener_ext, err = GetExt[*ListenerExt](dependency)
if err != nil {
return err
2023-07-26 11:56:10 -06:00
}
2023-07-24 01:41:47 -06:00
Signal(context, gql_loaded, gql_loaded, StopSignal)
return err
})
2023-07-26 11:56:10 -06:00
err = ThreadLoop(ctx, gql_loaded, "start")
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
(*GraphTester)(t).WaitForStatus(ctx, listener_ext.Chan, "stopped", 100*time.Millisecond, "Didn't receive stopped on update_channel_2")
}