- 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
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package tce_test
|
|
|
|
import (
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
// scanPackage finds every non-test .go source file in dir.
|
|
func scanPackage(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() {
|
|
continue
|
|
}
|
|
name := e.Name()
|
|
if filepath.Ext(name) != ".go" {
|
|
continue
|
|
}
|
|
if !isTestFile(name) {
|
|
files = append(files, filepath.Join(dir, name))
|
|
}
|
|
}
|
|
return files
|
|
}
|
|
|
|
func isTestFile(name string) bool {
|
|
return len(name) > 8 && name[len(name)-8:] == "_test.go"
|
|
}
|
|
|
|
// importsOf returns the import paths declared in a source file.
|
|
func importsOf(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
|
|
}
|
|
|
|
// sourceOf returns the raw text of a source file.
|
|
func sourceOf(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 scanPackage(t, ".") {
|
|
for _, imp := range importsOf(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 scanPackage(t, ".") {
|
|
if re.MatchString(sourceOf(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{
|
|
"crypto/sha256": true,
|
|
"crypto/subtle": true,
|
|
"encoding/hex": true,
|
|
"errors": true,
|
|
"fmt": true,
|
|
"math/big": true,
|
|
"sort": true,
|
|
"unicode/utf8": true,
|
|
"strconv": true,
|
|
}
|
|
for _, f := range scanPackage(t, ".") {
|
|
for _, imp := range importsOf(t, f) {
|
|
if imp == "go/token" || imp == "go/ast" || imp == "os" || imp == "path/filepath" || imp == "regexp" {
|
|
continue // test-only helpers
|
|
}
|
|
if !allowed[imp] {
|
|
t.Errorf("%s imports %s, which is outside the allowed tce dependency set", f, imp)
|
|
}
|
|
}
|
|
}
|
|
}
|