graphvent/gql_test.go

257 lines
7.8 KiB
Go

2023-06-25 20:20:59 -06:00
package graphvent
import (
"testing"
2023-06-25 22:19:05 -06:00
"time"
"net/http"
2023-07-24 17:17:12 -06:00
"net"
"errors"
"io"
"fmt"
"encoding/json"
"bytes"
"crypto/rand"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
2023-07-21 01:05:24 -06:00
"crypto/tls"
2023-07-20 00:24:22 -06:00
"encoding/base64"
2023-06-25 20:20:59 -06:00
)
func TestGQLDBLoad(t * testing.T) {
2023-07-24 17:17:12 -06:00
ctx := logTestContext(t, []string{"test"})
2023-07-24 17:07:27 -06:00
l1 := NewListener(RandID(), "Test Listener 1")
ctx.Log.Logf("test", "L1_ID: %s", l1.ID().String())
2023-07-24 16:04:56 -06:00
t1 := NewThread(RandID(), "Test Thread 1", "init", nil, BaseThreadActions, BaseThreadHandlers)
ctx.Log.Logf("test", "T1_ID: %s", t1.ID().String())
listen_id := RandID()
ctx.Log.Logf("test", "LISTENER_ID: %s", listen_id.String())
2023-07-20 00:24:22 -06:00
u1_key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
fatalErr(t, err)
2023-07-24 22:52:15 -06:00
u1 := NewUser("Test User", time.Now(), &u1_key.PublicKey, []byte{})
ctx.Log.Logf("test", "U1_ID: %s", u1.ID().String())
2023-07-20 23:19:10 -06:00
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
fatalErr(t, err)
2023-07-24 16:04:56 -06:00
gql := NewGQLThread(RandID(), "GQL Thread", "init", ":0", ecdh.P256(), key, nil, nil)
ctx.Log.Logf("test", "GQL_ID: %s", gql.ID().String())
// Policy to allow gql to perform all action on all resources
2023-07-24 16:04:56 -06:00
p1 := NewPerNodePolicy(RandID(), map[NodeID]NodeActions{
gql.ID(): NewNodeActions(nil, []string{"*"}),
})
2023-07-24 16:04:56 -06:00
p2 := NewSimplePolicy(RandID(), NewNodeActions(NodeActions{
"signal": []string{"status"},
}, nil))
context := NewWriteContext(ctx)
2023-07-24 16:04:56 -06:00
err = UpdateStates(context, &gql, LockMap{
p1.ID(): LockInfo{&p1, nil},
p2.ID(): LockInfo{&p2, nil},
}, func(context *StateContext) error {
return nil
})
fatalErr(t, err)
ctx.Log.Logf("test", "P1_ID: %s", p1.ID().String())
ctx.Log.Logf("test", "P2_ID: %s", p2.ID().String())
2023-07-24 17:07:27 -06:00
err = AttachPolicies(ctx, &gql, &p1, &p2)
fatalErr(t, err)
2023-07-24 17:07:27 -06:00
err = AttachPolicies(ctx, &l1, &p1, &p2)
fatalErr(t, err)
2023-07-24 17:07:27 -06:00
err = AttachPolicies(ctx, &t1, &p1, &p2)
fatalErr(t, err)
2023-07-24 17:07:27 -06:00
err = AttachPolicies(ctx, &u1, &p1, &p2)
fatalErr(t, err)
info := NewParentThreadInfo(true, "start", "restore")
context = NewWriteContext(ctx)
2023-07-24 16:04:56 -06:00
err = UpdateStates(context, &gql, NewLockMap(
NewLockInfo(&gql, []string{"users"}),
), func(context *StateContext) error {
gql.UserMap[u1.ID()] = &u1
2023-07-22 21:24:54 -06:00
2023-07-24 16:04:56 -06:00
err := LinkThreads(context, &gql, &gql, ChildInfo{&t1, map[InfoType]interface{}{
"parent": &info,
}})
2023-07-22 21:24:54 -06:00
if err != nil {
return err
}
2023-07-24 17:07:27 -06:00
return LinkLockables(context, &gql, &l1, []LockableNode{&gql})
})
fatalErr(t, err)
2023-07-09 20:30:19 -06:00
context = NewReadContext(ctx)
2023-07-24 16:04:56 -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-24 16:04:56 -06:00
(*GraphTester)(t).WaitForStatus(ctx, l1.Chan, "aborted", 100*time.Millisecond, "Didn't receive aborted on listener")
context = NewReadContext(ctx)
2023-07-24 16:04:56 -06:00
err = UseStates(context, &gql, LockList([]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
})
gql_loaded, err := LoadNode(ctx, gql.ID())
fatalErr(t, err)
2023-07-24 16:04:56 -06:00
var l1_loaded *Listener = nil
context = NewReadContext(ctx)
2023-07-24 16:04:56 -06:00
err = UseStates(context, gql_loaded, NewLockInfo(gql_loaded, []string{"users", "children", "requirements"}), func(context *StateContext) error {
2023-07-09 20:30:19 -06:00
ser, err := gql_loaded.Serialize()
ctx.Log.Logf("test", "\n%s\n\n", ser)
2023-07-24 16:04:56 -06:00
dependency := gql_loaded.(*GQLThread).Thread.Dependencies[l1.ID()].(*Listener)
l1_loaded = dependency
u_loaded := gql_loaded.(*GQLThread).UserMap[u1.ID()]
2023-07-24 16:04:56 -06:00
err = UseStates(context, gql_loaded, NewLockInfo(u_loaded, nil), func(context *StateContext) error {
ser, err := u_loaded.Serialize()
ctx.Log.Logf("test", "\n%s\n\n", ser)
return err
})
2023-07-24 01:41:47 -06:00
Signal(context, gql_loaded, gql_loaded, StopSignal)
return err
})
2023-07-24 16:04:56 -06:00
err = ThreadLoop(ctx, gql_loaded.(ThreadNode), "start")
fatalErr(t, err)
2023-07-24 16:04:56 -06:00
(*GraphTester)(t).WaitForStatus(ctx, l1_loaded.Chan, "stopped", 100*time.Millisecond, "Didn't receive stopped on update_channel_2")
}
func TestGQLAuth(t * testing.T) {
ctx := logTestContext(t, []string{"test", "gql", "policy"})
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
fatalErr(t, err)
2023-07-21 01:05:24 -06:00
2023-07-24 17:17:12 -06:00
p2 := NewSimplePolicy(RandID(), NewNodeActions(NodeActions{
"signal": []string{"status"},
}, nil))
2023-07-24 16:04:56 -06:00
l1 := NewListener(RandID(), "GQL Thread")
2023-07-24 22:52:15 -06:00
err = AttachPolicies(ctx, &l1, &p2)
2023-07-24 16:04:56 -06:00
fatalErr(t, err)
2023-07-21 12:09:29 -06:00
2023-07-24 17:17:12 -06:00
p3 := NewPerNodePolicy(RandID(), map[NodeID]NodeActions{
l1.ID(): NewNodeActions(nil, []string{"*"}),
})
gql := NewGQLThread(RandID(), "GQL Thread", "init", ":0", ecdh.P256(), key, nil, nil)
2023-07-24 22:52:15 -06:00
err = AttachPolicies(ctx, &gql, &p2, &p3)
2023-07-24 16:04:56 -06:00
context := NewWriteContext(ctx)
2023-07-24 17:17:12 -06:00
err = LinkLockables(context, &l1, &l1, []LockableNode{&gql})
fatalErr(t, err)
2023-07-24 17:17:12 -06:00
done := make(chan error, 1)
2023-07-24 16:04:56 -06:00
go func(done chan error, thread ThreadNode) {
timeout := time.After(2*time.Second)
select {
case <-timeout:
ctx.Log.Logf("test", "TIMEOUT")
case <-done:
ctx.Log.Logf("test", "DONE")
}
context := NewReadContext(ctx)
2023-07-24 01:41:47 -06:00
err := Signal(context, thread, thread, StopSignal)
fatalErr(t, err)
2023-07-24 17:17:12 -06:00
}(done, &gql)
2023-07-24 16:04:56 -06:00
go func(thread ThreadNode){
(*GraphTester)(t).WaitForStatus(ctx, l1.Chan, "server_started", 100*time.Millisecond, "Server didn't start")
2023-07-24 17:17:12 -06:00
port := gql.tcp_listener.Addr().(*net.TCPAddr).Port
ctx.Log.Logf("test", "GQL_PORT: %d", port)
2023-07-21 01:05:24 -06:00
customTransport := &http.Transport{
Proxy: http.DefaultTransport.(*http.Transport).Proxy,
DialContext: http.DefaultTransport.(*http.Transport).DialContext,
MaxIdleConns: http.DefaultTransport.(*http.Transport).MaxIdleConns,
IdleConnTimeout: http.DefaultTransport.(*http.Transport).IdleConnTimeout,
ExpectContinueTimeout: http.DefaultTransport.(*http.Transport).ExpectContinueTimeout,
TLSHandshakeTimeout: http.DefaultTransport.(*http.Transport).TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: customTransport}
url := fmt.Sprintf("https://localhost:%d/auth", port)
id, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
fatalErr(t, err)
auth_req, ec_key, err := NewAuthReqJSON(ecdh.P256(), id)
fatalErr(t, err)
str, err := json.Marshal(auth_req)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
b := bytes.NewBuffer(str)
req, err := http.NewRequest("PUT", url, b)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
resp, err := client.Do(req)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
body, err := io.ReadAll(resp.Body)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
resp.Body.Close()
var j AuthRespJSON
err = json.Unmarshal(body, &j)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
shared, err := ParseAuthRespJSON(j, elliptic.P256(), ecdh.P256(), ec_key)
fatalErr(t, err)
2023-07-21 01:05:24 -06:00
url = fmt.Sprintf("https://localhost:%d/gql", port)
2023-07-20 00:24:22 -06:00
ser, err := json.MarshalIndent(&GQLPayload{
Query: "query { Self { Users { ID, Name } } }",
2023-07-20 00:24:22 -06:00
}, "", " ")
fatalErr(t, err)
b = bytes.NewBuffer(ser)
req, err = http.NewRequest("GET", url, b)
fatalErr(t, err)
2023-07-20 00:24:22 -06:00
req.SetBasicAuth(KeyID(&id.PublicKey).String(), base64.StdEncoding.EncodeToString(shared))
resp, err = client.Do(req)
fatalErr(t, err)
body, err = io.ReadAll(resp.Body)
fatalErr(t, err)
resp.Body.Close()
ctx.Log.Logf("test", "TEST_RESP: %s", body)
req.SetBasicAuth(KeyID(&id.PublicKey).String(), "BAD_PASSWORD")
resp, err = client.Do(req)
fatalErr(t, err)
body, err = io.ReadAll(resp.Body)
fatalErr(t, err)
resp.Body.Close()
ctx.Log.Logf("test", "TEST_RESP: %s", body)
done <- nil
2023-07-24 17:17:12 -06:00
}(&gql)
2023-07-24 17:17:12 -06:00
err = ThreadLoop(ctx, &gql, "start")
fatalErr(t, err)
}