83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
)
|
|
|
|
// Issue 1480: the bundle's keeper is go/forgekey's, kind machine; no copy of it is left in this module.
|
|
|
|
type recordState struct{ puts map[string][]byte }
|
|
|
|
func (r *recordState) Put(key string, value any) (uint64, error) {
|
|
raw, _ := json.Marshal(value)
|
|
if r.puts == nil {
|
|
r.puts = map[string][]byte{}
|
|
}
|
|
r.puts[key] = raw
|
|
return uint64(len(r.puts)), nil
|
|
}
|
|
|
|
// keygenStandIn writes the pair ssh-keygen -f names, as ssh-keygen would.
|
|
func keygenStandIn(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
|
var file, comment string
|
|
for i, a := range args {
|
|
switch a {
|
|
case "-f":
|
|
file = args[i+1]
|
|
case "-C":
|
|
comment = args[i+1]
|
|
}
|
|
}
|
|
if name != "ssh-keygen" || file == "" {
|
|
return forgekey.Ran{Status: 1, Stderr: "unexpected"}
|
|
}
|
|
os.WriteFile(file, []byte("PRIVATE\n"), 0o600)
|
|
os.WriteFile(file+".pub", []byte(ed25519Line(7, comment)+"\n"), 0o644)
|
|
return forgekey.Ran{}
|
|
}
|
|
|
|
// 1480/AC1
|
|
func TestTheBundlesKeeperIsForgekeysOfKindMachineWithItsEntryAtNodeAccount(t *testing.T) {
|
|
home, st := t.TempDir(), &recordState{}
|
|
k := keeperFor(home, "g14", "nox", keygenStandIn, st)
|
|
k.Now = func() time.Time { return time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC) }
|
|
if err := k.Once(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".ssh", "mesh_ed25519")); err != nil {
|
|
t.Errorf("the key file mesh_ed25519 was not made in an empty home: %v", err)
|
|
}
|
|
raw, ok := st.puts["g14.nox"]
|
|
if !ok || len(st.puts) != 1 {
|
|
t.Fatalf("entries = %v, want one at g14.nox", st.puts)
|
|
}
|
|
var e forgekey.KeyEntry
|
|
if err := json.Unmarshal(raw, &e); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e.Kind != "machine" || e.Node != "g14" || e.Account != "nox" || !strings.HasSuffix(e.Public, " nox@g14 (mesh)") {
|
|
t.Errorf("entry = %s", raw)
|
|
}
|
|
}
|
|
|
|
// 1480/AC2
|
|
func TestNoCopyOfTheKeeperIsLeftInThisModule(t *testing.T) {
|
|
copyOf := regexp.MustCompile(`(?m)^(type (KeyKeeper|KeyEntry|Runner|Ran)\b|func ParseKeyLine\b|func ExecRunner\b)`)
|
|
files, _ := filepath.Glob("*.go")
|
|
for _, f := range files {
|
|
raw, _ := os.ReadFile(f)
|
|
if m := copyOf.Find(raw); m != nil {
|
|
t.Errorf("%s declares %q, which go/forgekey holds", f, m)
|
|
}
|
|
}
|
|
}
|