Files
mesh-host/internal/identity/serving_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

78 lines
2.5 KiB
Go

package identity
import (
"crypto/ed25519"
"crypto/x509"
"encoding/pem"
"os"
"path/filepath"
"strings"
"testing"
)
// The whole reason the file exists is that something else reads it.
//
// A key in this host's own encoding is intact, unusable, and indistinguishable from a working one
// until the moment a client connects — the mesh delivers the certificate, the file is there with
// the right permissions, and the server will not start.
func testTheServingKeyIsWrittenInTheFormatAServerReads(t *testing.T) {
made, err := GenerateServingKey()
if err != nil {
t.Fatal(err)
}
path := filepath.Join(t.TempDir(), "serving.key")
if err := WriteServingKey(path, made); err != nil {
t.Fatal(err)
}
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
block, _ := pem.Decode(raw)
if block == nil {
t.Fatalf("the serving key is not PEM, so nothing serving TLS can read it:\n%s", raw)
}
parsed, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
t.Fatalf("the serving key is PEM and not a key: %v", err)
}
// And it is the key that was written, not merely a key — a file that round-trips through the
// wrong half would certify a public key the machine cannot prove it holds.
if _, isEd25519 := parsed.(ed25519.PrivateKey); !isEd25519 {
t.Fatalf("the serving key is a %T", parsed)
}
read, err := LoadServingKey(path)
if err != nil {
t.Fatal(err)
}
if read.Public != made.Public {
t.Fatal("the key read back is not the key written, so the mesh would certify the wrong one")
}
}
// The old encoding is refused by name, because the remedy is different from a corrupt file and
// the difference is invisible from the outside.
func testAServingKeyInTheOldEncodingIsNamedRatherThanCalledCorrupt(t *testing.T) {
made, err := GenerateServingKey()
if err != nil {
t.Fatal(err)
}
path := filepath.Join(t.TempDir(), "serving.key")
if err := os.WriteFile(path, []byte(made.Private+"\n"), 0o600); err != nil {
t.Fatal(err)
}
_, err = LoadServingKey(path)
if err == nil {
t.Fatal("a key nothing can serve with was accepted")
}
if !strings.Contains(err.Error(), "enrolling again") {
t.Fatalf("refused without naming the remedy: %v", err)
}
}
// TestServing runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestServing(t *testing.T) {
t.Run("TheServingKeyIsWrittenInTheFormatAServerReads", testTheServingKeyIsWrittenInTheFormatAServerReads)
t.Run("AServingKeyInTheOldEncodingIsNamedRatherThanCalledCorrupt", testAServingKeyInTheOldEncodingIsNamedRatherThanCalledCorrupt)
}