Python router + Go sm-leaf reject missing to, empty ct, plaintext body. Summary logs never include ciphertext. MODULE.md.
133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package leaf
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestStartInProcess(t *testing.T) {
|
|
n, err := Start("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer n.Shutdown()
|
|
if n.Addr() == "" {
|
|
t.Fatal("empty addr")
|
|
}
|
|
}
|
|
|
|
func TestParseHubLeafScheme(t *testing.T) {
|
|
u, err := ParseHub("nats-leaf://10.10.10.21:7422")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.Scheme != "nats" {
|
|
t.Fatalf("scheme %s", u.Scheme)
|
|
}
|
|
if u.Host != "10.10.10.21:7422" {
|
|
t.Fatalf("host %s", u.Host)
|
|
}
|
|
}
|
|
|
|
func TestHealthHandler(t *testing.T) {
|
|
srv := httptest.NewServer(HealthHandler())
|
|
defer srv.Close()
|
|
resp, err := srv.Client().Get(srv.URL + "/health")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
var m map[string]any
|
|
if err := json.Unmarshal(body, &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m["ok"] != true || m["service"] != "sm-leaf" {
|
|
t.Fatalf("health %s", body)
|
|
}
|
|
}
|
|
|
|
func TestSMSubjectsAck(t *testing.T) {
|
|
n, err := Start("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer n.Shutdown()
|
|
good := []byte(`{"to":"npe.inbox.x","alg":"lab-xor","ct":"abcd","from_lookup_id":"lid-good"}`)
|
|
msg, err := n.nc.Request("verae.sm.send", good, time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ack map[string]any
|
|
if err := json.Unmarshal(msg.Data, &ack); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ack["accepted"] != true || ack["lookup_id"] != "lid-good" {
|
|
t.Fatalf("good send %s", msg.Data)
|
|
}
|
|
for subj, want := range map[string]string{
|
|
"verae.sm.dead": `{"queued":true}`,
|
|
"verae.sm.error": `{"emitted":true}`,
|
|
"verae.sm.log.summary": `{"logged":true}`,
|
|
} {
|
|
got, err := n.nc.Request(subj, []byte(`{"lookup_id":"t","error_code":"x","dest_class":"mailbox"}`), time.Second)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", subj, err)
|
|
}
|
|
if string(got.Data) != want {
|
|
t.Fatalf("%s got %s want %s", subj, got.Data, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSMSendRejectsMissingTo(t *testing.T) {
|
|
n, err := Start("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer n.Shutdown()
|
|
msg, err := n.nc.Request("verae.sm.send", []byte(`{"alg":"lab-xor","ct":"ab"}`), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ack map[string]any
|
|
_ = json.Unmarshal(msg.Data, &ack)
|
|
if ack["accepted"] != false || ack["error_code"] != "SM-MISSING-TO" {
|
|
t.Fatalf("missing to %s", msg.Data)
|
|
}
|
|
}
|
|
|
|
func TestSMSendRejectsEmptyCiphertext(t *testing.T) {
|
|
n, err := Start("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer n.Shutdown()
|
|
msg, err := n.nc.Request("verae.sm.send", []byte(`{"to":"npe.inbox.x","alg":"lab-xor","ct":""}`), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ack map[string]any
|
|
_ = json.Unmarshal(msg.Data, &ack)
|
|
if ack["accepted"] != false || ack["error_code"] != "SM-EMPTY-CT" {
|
|
t.Fatalf("empty ct %s", msg.Data)
|
|
}
|
|
}
|
|
|
|
func TestSMSummaryRejectsCiphertextField(t *testing.T) {
|
|
n, err := Start("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer n.Shutdown()
|
|
msg, err := n.nc.Request("verae.sm.log.summary", []byte(`{"error_code":"x","ct":"secret"}`), time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(msg.Data) == `{"logged":true}` {
|
|
t.Fatal("must not log body-like fields")
|
|
}
|
|
}
|