niko_trust/internal/protocol/invariants_test.go
Niko Marmeladkov 9d66003689
Initial commit: signed-object trust relay, verifier, and docs
- server: relay storing signed objects (PUT/GET), per-IP rate limiting,
  per-subject quota (1000), one-response-per-request, pagination,
  /v1/healthz /v1/readyz /v1/metrics
- verify: signature-verifying trust evaluator; every object is checked via
  env.Verify(), approvals via VerifyApprovalResponse, revocations via
  VerifyRevocationOf; k-of-n approval quorum
- docs: TRUST-MODEL.md and API.md describing issuer-anchored signatures and
  the endpoint/status-code contract
- tests: server, verify, and ratelimit packages
2026-08-12 22:36:49 +03:00

98 lines
2.4 KiB
Go

package protocol_test
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"strconv"
"testing"
)
func scanPackageProtocol(t *testing.T, dir string) []string {
t.Helper()
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("read dir %s: %v", dir, err)
}
var files []string
for _, e := range entries {
if e.IsDir() || filepath.Ext(e.Name()) != ".go" {
continue
}
if len(e.Name()) <= 8 || e.Name()[len(e.Name())-8:] != "_test.go" {
files = append(files, filepath.Join(dir, e.Name()))
}
}
return files
}
func importsOfProtocol(t *testing.T, path string) []string {
t.Helper()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
if err != nil {
t.Fatalf("parse %s: %v", path, err)
}
var out []string
for _, imp := range f.Imports {
p, err := strconv.Unquote(imp.Path.Value)
if err != nil {
t.Fatalf("bad import path in %s: %v", path, err)
}
out = append(out, p)
}
return out
}
func sourceOfProtocol(t *testing.T, path string) string {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(b)
}
func TestNoJSONImport(t *testing.T) {
for _, f := range scanPackageProtocol(t, ".") {
for _, imp := range importsOfProtocol(t, f) {
if imp == "encoding/json" {
t.Errorf("%s imports encoding/json; PROTOCOL.md forbids JSON in the wire codec", f)
}
}
}
}
func TestNoSigningInCodec(t *testing.T) {
re := regexp.MustCompile(`ed25519\.(Sign|NewKeyFromSeed|GenerateKey)`)
for _, f := range scanPackageProtocol(t, ".") {
if re.MatchString(sourceOfProtocol(t, f)) {
t.Errorf("%s performs ed25519 signing; signing must live only in internal/identity/signer", f)
}
}
}
func TestAllowedImports(t *testing.T) {
allowed := map[string]bool{
"bytes": true,
"crypto/ed25519": true, // Verify only; signing lives in internal/identity/signer
"crypto/subtle": true,
"errors": true,
"fmt": true,
"git.n1ko.dev/Niko/niko_trust/internal/address": true,
"git.n1ko.dev/Niko/niko_trust/internal/tce": true,
}
for _, f := range scanPackageProtocol(t, ".") {
for _, imp := range importsOfProtocol(t, f) {
switch imp {
case "go/token", "go/parser", "os", "path/filepath", "regexp", "strconv":
continue // test-only helpers
}
if !allowed[imp] {
t.Errorf("%s imports %s, which is outside the allowed protocol dependency set", f, imp)
}
}
}
}