graphvent/gql_test.go

258 lines
7.0 KiB
Go

2023-06-25 20:20:59 -06:00
package graphvent
import (
"testing"
2023-06-25 22:19:05 -06:00
"time"
2023-07-29 11:03:41 -06:00
"fmt"
"encoding/json"
"encoding/base64"
2023-07-29 11:03:41 -06:00
"io"
"net/http"
"net"
"crypto/tls"
"crypto/x509"
2023-08-10 23:43:10 -06:00
"crypto/rand"
"crypto/ed25519"
2023-07-29 11:03:41 -06:00
"bytes"
2023-08-15 18:23:06 -06:00
"golang.org/x/net/websocket"
"github.com/google/uuid"
2023-06-25 20:20:59 -06:00
)
func TestGQLServer(t *testing.T) {
2023-08-15 18:23:06 -06:00
ctx := logTestContext(t, []string{"test", "gql", "policy", "pending"})
2023-07-29 00:40:18 -06:00
TestNodeType := NewNodeType("TEST")
err := ctx.RegisterNodeType(TestNodeType, []ExtType{LockableExtType})
2023-07-29 00:40:18 -06:00
fatalErr(t, err)
2023-08-10 23:43:10 -06:00
pub, gql_key, err := ed25519.GenerateKey(rand.Reader)
fatalErr(t, err)
gql_id := KeyID(pub)
group_policy_1 := NewAllNodesPolicy(Tree{
SerializedType(ReadSignalType): Tree{
SerializedType(GroupExtType): Tree{
Hash(FieldNameBase, "members"): Tree{},
2023-08-10 23:43:10 -06:00
},
},
SerializedType(ReadResultSignalType): nil,
SerializedType(ErrorSignalType): nil,
2023-08-10 23:43:10 -06:00
})
2023-08-11 16:00:36 -06:00
group_policy_2 := NewMemberOfPolicy(map[NodeID]Tree{
2023-08-10 23:43:10 -06:00
gql_id: Tree{
SerializedType(LinkSignalType): nil,
SerializedType(LockSignalType): nil,
SerializedType(StatusSignalType): nil,
SerializedType(ReadSignalType): nil,
2023-08-10 23:43:10 -06:00
},
})
user_policy_1 := NewAllNodesPolicy(Tree{
SerializedType(ReadResultSignalType): nil,
SerializedType(ErrorSignalType): nil,
2023-08-10 23:43:10 -06:00
})
2023-08-11 16:00:36 -06:00
user_policy_2 := NewMemberOfPolicy(map[NodeID]Tree{
2023-08-10 23:43:10 -06:00
gql_id: Tree{
SerializedType(LinkSignalType): nil,
SerializedType(ReadSignalType): nil,
2023-08-10 23:43:10 -06:00
},
})
2023-08-15 18:23:06 -06:00
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
2023-07-29 00:40:18 -06:00
fatalErr(t, err)
2023-08-10 23:43:10 -06:00
2023-07-29 00:40:18 -06:00
listener_ext := NewListenerExt(10)
2023-08-31 22:31:29 -06:00
n1, err := NewNode(ctx, nil, TestNodeType, 10, map[PolicyType]Policy{
2023-08-10 23:43:10 -06:00
MemberOfPolicyType: &user_policy_2,
AllNodesPolicyType: &user_policy_1,
}, NewLockableExt(nil))
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
2023-08-31 22:31:29 -06:00
gql, err := NewNode(ctx, gql_key, GQLNodeType, 10, map[PolicyType]Policy{
2023-08-10 23:43:10 -06:00
MemberOfPolicyType: &group_policy_2,
AllNodesPolicyType: &group_policy_1,
}, NewLockableExt([]NodeID{n1.ID}), gql_ext, NewGroupExt(map[NodeID]string{
2023-08-10 23:43:10 -06:00
n1.ID: "user",
gql_id: "self",
}), listener_ext)
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
2023-08-10 23:43:10 -06:00
ctx.Log.Logf("test", "GQL: %s", gql.ID)
ctx.Log.Logf("test", "NODE: %s", n1.ID)
_, err = WaitForSignal(listener_ext.Chan, 100*time.Millisecond, func(sig *StatusSignal) bool {
return sig.Status == "server_started"
2023-07-29 00:40:18 -06:00
})
fatalErr(t, err)
2023-07-29 11:03:41 -06:00
skipVerifyTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: skipVerifyTransport}
port := gql_ext.tcp_listener.Addr().(*net.TCPAddr).Port
url := fmt.Sprintf("https://localhost:%d/gql", port)
2023-08-15 18:23:06 -06:00
ws_url := fmt.Sprintf("wss://127.0.0.1:%d/gqlws", port)
2023-07-29 11:03:41 -06:00
2023-07-29 16:34:21 -06:00
req_1 := GQLPayload{
Query: "query Node($id:String) { Node(id:$id) { ID, TypeHash } }",
Variables: map[string]interface{}{
"id": n1.ID.String(),
},
2023-07-29 16:34:21 -06:00
}
req_2 := GQLPayload{
Query: "query Node($id:String) { Node(id:$id) { ID, TypeHash, ... on GQLServer { Members { ID } , Listen, Requirements { ID, TypeHash Owner { ID } } } } }",
2023-07-29 16:34:21 -06:00
Variables: map[string]interface{}{
"id": gql.ID.String(),
},
}
2023-07-29 11:03:41 -06:00
n1_id_bytes, err := n1.ID.MarshalBinary()
fatalErr(t, err)
auth_username := base64.StdEncoding.EncodeToString(n1_id_bytes)
2023-08-15 18:23:06 -06:00
key_bytes, err := x509.MarshalPKCS8PrivateKey(n1.Key)
fatalErr(t, err)
auth_password := base64.StdEncoding.EncodeToString(key_bytes)
auth_b64 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", auth_username, auth_password)))
2023-07-29 16:34:21 -06:00
SendGQL := func(payload GQLPayload) []byte {
ser, err := json.MarshalIndent(&payload, "", " ")
fatalErr(t, err)
2023-07-29 11:03:41 -06:00
2023-07-29 16:34:21 -06:00
req_data := bytes.NewBuffer(ser)
req, err := http.NewRequest("GET", url, req_data)
fatalErr(t, err)
2023-07-29 11:03:41 -06:00
2023-08-15 18:23:06 -06:00
req.SetBasicAuth(auth_username, auth_password)
2023-07-29 16:34:21 -06:00
resp, err := client.Do(req)
fatalErr(t, err)
2023-07-29 11:03:41 -06:00
2023-07-29 16:34:21 -06:00
body, err := io.ReadAll(resp.Body)
fatalErr(t, err)
2023-07-29 11:03:41 -06:00
2023-07-29 16:34:21 -06:00
resp.Body.Close()
return body
}
2023-07-29 11:03:41 -06:00
2023-07-29 16:34:21 -06:00
resp_1 := SendGQL(req_1)
ctx.Log.Logf("test", "RESP_1: %s", resp_1)
resp_2 := SendGQL(req_2)
ctx.Log.Logf("test", "RESP_2: %s", resp_2)
2023-07-29 11:03:41 -06:00
2023-08-15 18:23:06 -06:00
sub_1 := GQLPayload{
Query: "subscription { Self }",
}
SubGQL := func(payload GQLPayload) {
config, err := websocket.NewConfig(ws_url, url)
fatalErr(t, err)
config.Protocol = append(config.Protocol, "graphql-ws")
config.TlsConfig = &tls.Config{InsecureSkipVerify: true}
config.Header.Add("Authorization", fmt.Sprintf("Basic %s", auth_b64))
ws, err := websocket.DialConfig(config)
fatalErr(t, err)
init := GQLWSMsg{
ID: uuid.New().String(),
Type: "connection_init",
}
ser, err := json.Marshal(&init)
fatalErr(t, err)
_, err = ws.Write(ser)
fatalErr(t, err)
resp := make([]byte, 1024)
n, err := ws.Read(resp)
var init_resp GQLWSMsg
err = json.Unmarshal(resp[:n], &init_resp)
fatalErr(t, err)
if init_resp.Type != "connection_ack" {
t.Fatal("Didn't receive connection_ack")
}
sub := GQLWSMsg{
ID: uuid.New().String(),
Type: "subscribe",
Payload: sub_1,
}
ser, err = json.Marshal(&sub)
fatalErr(t, err)
_, err = ws.Write(ser)
fatalErr(t, err)
for i := 0; i < 10; i++ {
n, err = ws.Read(resp)
fatalErr(t, err)
ctx.Log.Logf("test", "SUB_%d: %s", i, resp[:n])
}
}
SubGQL(sub_1)
msgs := Messages{}
msgs = msgs.Add(ctx, gql.ID, gql.Key, NewStopSignal(), gql.ID)
err = ctx.Send(msgs)
fatalErr(t, err)
_, err = WaitForSignal(listener_ext.Chan, 100*time.Millisecond, func(sig *StatusSignal) bool {
return sig.Status == "stopped"
2023-07-29 11:03:41 -06:00
})
fatalErr(t, err)
2023-07-29 00:40:18 -06:00
}
func TestGQLDB(t *testing.T) {
ctx := logTestContext(t, []string{"test", "serialize", "node"})
2023-07-26 13:28:03 -06:00
TestUserNodeType := NewNodeType("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-08-31 22:31:29 -06:00
u1, err := NewNode(ctx, nil, TestUserNodeType, 10, nil)
fatalErr(t, err)
2023-07-26 13:28:03 -06:00
ctx.Log.Logf("test", "U1_ID: %s", u1.ID)
2023-08-15 18:23:06 -06:00
gql_ext, err := NewGQLExt(ctx, ":0", nil, nil)
2023-07-29 00:40:18 -06:00
fatalErr(t, err)
listener_ext := NewListenerExt(10)
2023-08-31 22:31:29 -06:00
gql, err := NewNode(ctx, nil, GQLNodeType, 10, nil,
2023-07-27 11:33:11 -06:00
gql_ext,
listener_ext,
NewGroupExt(nil))
2023-08-31 22:31:29 -06:00
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID)
msgs := Messages{}
msgs = msgs.Add(ctx, gql.ID, gql.Key, NewStopSignal(), gql.ID)
err = ctx.Send(msgs)
fatalErr(t, err)
_, err = WaitForSignal(listener_ext.Chan, 100*time.Millisecond, func(sig *StatusSignal) bool {
return sig.Status == "stopped" && sig.Source == gql.ID
})
fatalErr(t, err)
2023-07-26 11:56:10 -06:00
// Clear all loaded nodes from the context so it loads them from the database
ctx.nodeMap = map[NodeID]*Node{}
2023-07-26 11:56:10 -06:00
gql_loaded, err := LoadNode(ctx, gql.ID)
fatalErr(t, err)
listener_ext, err = GetExt[*ListenerExt](gql_loaded, GQLExtType)
2023-07-27 12:20:49 -06:00
fatalErr(t, err)
msgs = Messages{}
msgs = msgs.Add(ctx, gql_loaded.ID, gql_loaded.Key, NewStopSignal(), gql_loaded.ID)
err = ctx.Send(msgs)
fatalErr(t, err)
_, err = WaitForSignal(listener_ext.Chan, 100*time.Millisecond, func(sig *StatusSignal) bool {
return sig.Status == "stopped" && sig.Source == gql_loaded.ID
})
fatalErr(t, err)
}