2023-06-25 20:20:59 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-06-25 22:19:05 -06:00
|
|
|
"time"
|
2023-07-23 23:01:45 -06:00
|
|
|
"errors"
|
2023-07-19 14:45:05 -06:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/ecdh"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2023-06-25 20:20:59 -06:00
|
|
|
)
|
|
|
|
|
2023-07-27 00:30:24 -06:00
|
|
|
func TestGQL(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGQLDB(t * testing.T) {
|
2023-07-27 11:33:11 -06:00
|
|
|
ctx := logTestContext(t, []string{"test", "signal", "policy", "db"})
|
2023-07-26 13:28:03 -06:00
|
|
|
|
|
|
|
TestUserNodeType := NodeType("TEST_USER")
|
2023-07-27 11:33:11 -06:00
|
|
|
err := ctx.RegisterNodeType(TestUserNodeType, []ExtType{})
|
2023-07-26 13:28:03 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-26 15:08:14 -06:00
|
|
|
u1 := NewNode(ctx, RandID(), TestUserNodeType)
|
2023-07-26 13:28:03 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("test", "U1_ID: %s", u1.ID)
|
2023-07-01 13:06:39 -06:00
|
|
|
|
2023-07-27 00:30:24 -06:00
|
|
|
l1, listener_ext := NewSimpleListener(ctx, 10)
|
2023-07-26 11:56:10 -06:00
|
|
|
ctx.Log.Logf("test", "L1_ID: %s", l1.ID)
|
2023-07-23 19:04:04 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
TestThreadNodeType := NodeType("TEST_THREAD")
|
2023-07-27 09:32:33 -06:00
|
|
|
err = ctx.RegisterNodeType(TestThreadNodeType, []ExtType{ACLExtType, ThreadExtType, LockableExtType})
|
2023-07-26 11:56:10 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-01 13:06:39 -06:00
|
|
|
|
2023-07-27 10:04:39 -06:00
|
|
|
t1_policy_1 := NewParentOfPolicy(Actions{"signal.abort", "state.write"})
|
|
|
|
t1_policy_2 := NewPerNodePolicy(NodeActions{
|
|
|
|
u1.ID: Actions{"parent.write"},
|
|
|
|
})
|
2023-07-27 11:33:11 -06:00
|
|
|
t1_thread, err := NewThreadExt(ctx, BaseThreadType, nil,nil, "init", nil)
|
2023-07-23 19:04:04 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-27 11:33:11 -06:00
|
|
|
t1 := NewNode(ctx,
|
|
|
|
RandID(),
|
|
|
|
TestThreadNodeType,
|
|
|
|
NewACLExt(&t1_policy_1, &t1_policy_2),
|
|
|
|
t1_thread,
|
|
|
|
NewLockableExt(nil, nil, nil, nil))
|
2023-07-26 11:56:10 -06:00
|
|
|
ctx.Log.Logf("test", "T1_ID: %s", t1.ID)
|
2023-07-23 19:04:04 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2023-07-23 19:04:04 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-26 11:56:10 -06:00
|
|
|
|
2023-07-27 11:33:11 -06:00
|
|
|
gql_p1 := NewChildOfPolicy(Actions{"signal.status"})
|
|
|
|
gql_p2 := NewPerNodePolicy(NodeActions{
|
2023-07-27 10:04:39 -06:00
|
|
|
u1.ID: Actions{"parent.write", "children.write", "dependencies.write"},
|
|
|
|
})
|
2023-07-23 19:04:04 -06:00
|
|
|
|
2023-07-27 11:33:11 -06:00
|
|
|
gql_thread, err := NewThreadExt(ctx, GQLThreadType, nil, nil, "init", nil)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
gql_ext := NewGQLExt(":0", ecdh.P256(), key, nil, nil)
|
|
|
|
gql := NewNode(ctx, RandID(), GQLNodeType,
|
|
|
|
gql_thread,
|
|
|
|
gql_ext,
|
|
|
|
NewACLExt(&gql_p1, &gql_p2),
|
|
|
|
NewGroupExt(nil),
|
|
|
|
NewLockableExt(nil, nil, nil, nil))
|
2023-07-26 11:56:10 -06:00
|
|
|
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
|
2023-07-27 00:57:18 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
info := ParentInfo{true, "start", "restore"}
|
|
|
|
context := NewWriteContext(ctx)
|
2023-07-27 00:57:18 -06:00
|
|
|
err = UpdateStates(context, u1, ACLMap{}, func(context *StateContext) error {
|
2023-07-26 15:08:14 -06:00
|
|
|
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 15:08:14 -06:00
|
|
|
return LinkLockables(context, u1, l1, []*Node{gql})
|
2023-07-04 18:45:23 -06:00
|
|
|
})
|
2023-07-03 13:14:48 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-09 20:30:19 -06:00
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
context = NewReadContext(ctx)
|
2023-07-27 01:30:32 -06:00
|
|
|
err = gql.Process(context, gql.ID, NewStatusSignal("child_linked", t1.ID))
|
2023-07-23 21:14:15 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
context = NewReadContext(ctx)
|
2023-07-27 01:30:32 -06:00
|
|
|
err = gql.Process(context, gql.ID, AbortSignal)
|
2023-07-09 20:30:19 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
err = ThreadLoop(ctx, gql, "start")
|
2023-07-23 23:01:45 -06:00
|
|
|
if errors.Is(err, ThreadAbortedError) == false {
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
2023-07-03 13:14:48 -06:00
|
|
|
|
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")
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
context = NewReadContext(ctx)
|
2023-07-26 15:08:14 -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()
|
2023-07-21 00:02:53 -06:00
|
|
|
ser2, err := u1.Serialize()
|
2023-07-19 20:03:13 -06:00
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser1)
|
|
|
|
ctx.Log.Logf("test", "\n%s\n\n", ser2)
|
2023-07-01 13:06:39 -06:00
|
|
|
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)
|
2023-07-01 13:06:39 -06:00
|
|
|
fatalErr(t, err)
|
2023-07-23 17:57:47 -06:00
|
|
|
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
|
|
|
|
}
|
2023-07-19 14:45:05 -06:00
|
|
|
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 {
|
2023-07-03 13:14:48 -06:00
|
|
|
return err
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
2023-07-27 01:30:32 -06:00
|
|
|
gql_loaded.Process(context, gql_loaded.ID, StopSignal)
|
2023-07-01 13:06:39 -06:00
|
|
|
return err
|
|
|
|
})
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
err = ThreadLoop(ctx, gql_loaded, "start")
|
2023-07-23 19:04:04 -06:00
|
|
|
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")
|
2023-07-03 13:14:48 -06:00
|
|
|
|
2023-07-01 13:06:39 -06:00
|
|
|
}
|
2023-07-19 14:45:05 -06:00
|
|
|
|