Skip to content

Commit 9600620

Browse files
committed
test: add unit tests for extractNonKeymasterContent and removeSelectiveKeymasterContent functions
1 parent 208f8db commit 9600620

File tree

2 files changed

+495
-0
lines changed

2 files changed

+495
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package core
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
8+
"github.com/toeirei/keymaster/internal/model"
9+
)
10+
11+
func TestExtractNonKeymasterContent_Simple(t *testing.T) {
12+
content := "before\n# Keymaster Managed Keys (Serial: 1)\nssh-ed25519 AAA foo@a\n# comment\nafter\n"
13+
got := extractNonKeymasterContent(content)
14+
if !strings.Contains(got, "before") || !strings.Contains(got, "after") {
15+
t.Fatalf("unexpected output: %q", got)
16+
}
17+
}
18+
19+
// fakes local to this file to avoid name collisions with other tests
20+
type fd3 struct {
21+
content []byte
22+
deployed string
23+
getErr, deployErr error
24+
}
25+
26+
func (f *fd3) DeployAuthorizedKeys(content string) error { f.deployed = content; return f.deployErr }
27+
func (f *fd3) GetAuthorizedKeys() ([]byte, error) { return f.content, f.getErr }
28+
func (f *fd3) Close() {}
29+
30+
type kr3 struct {
31+
active *model.SystemKey
32+
by map[int]*model.SystemKey
33+
}
34+
35+
func (k *kr3) GetAllPublicKeys() ([]model.PublicKey, error) { return nil, nil }
36+
func (k *kr3) GetActiveSystemKey() (*model.SystemKey, error) { return k.active, nil }
37+
func (k *kr3) GetSystemKeyBySerial(serial int) (*model.SystemKey, error) {
38+
if v, ok := k.by[serial]; ok {
39+
return v, nil
40+
}
41+
return nil, nil
42+
}
43+
44+
type kl3 struct {
45+
globals []model.PublicKey
46+
acc map[int][]model.PublicKey
47+
}
48+
49+
func (k *kl3) GetGlobalPublicKeys() ([]model.PublicKey, error) { return k.globals, nil }
50+
func (k *kl3) GetKeysForAccount(accountID int) ([]model.PublicKey, error) {
51+
return k.acc[accountID], nil
52+
}
53+
func (k *kl3) GetAllPublicKeys() ([]model.PublicKey, error) {
54+
var out []model.PublicKey
55+
out = append(out, k.globals...)
56+
for _, v := range k.acc {
57+
out = append(out, v...)
58+
}
59+
return out, nil
60+
}
61+
62+
func TestRemoveSelectiveKeymasterContent_EndToEnd(t *testing.T) {
63+
auth := "pre\n# Keymaster Managed Keys (Serial: 1)\nssh-ed25519 AAA key1\n# end\npost\n"
64+
deployer := &fd3{content: []byte(auth)}
65+
66+
sk := &model.SystemKey{Serial: 1, PublicKey: "ssh-ed25519 AAA key1"}
67+
kr := &kr3{active: sk, by: map[int]*model.SystemKey{1: sk}}
68+
kl := &kl3{globals: []model.PublicKey{{ID: 1, Algorithm: "ssh-ed25519", KeyData: "AAA", Comment: "g"}}, acc: map[int][]model.PublicKey{42: {{ID: 2, Algorithm: "ssh-ed25519", KeyData: "BBB", Comment: "a"}}}}
69+
70+
SetDefaultKeyReader(kr)
71+
SetDefaultKeyLister(kl)
72+
defer func() { SetDefaultKeyReader(nil); SetDefaultKeyLister(nil) }()
73+
74+
res := &DecommissionResult{}
75+
if err := removeSelectiveKeymasterContent(deployer, res, 42, nil, true); err != nil {
76+
t.Fatalf("remove returned err: %v", err)
77+
}
78+
if deployer.deployed == "" {
79+
t.Fatalf("expected deploy to be called")
80+
}
81+
if !res.RemoteCleanupDone {
82+
t.Fatalf("expected RemoteCleanupDone true")
83+
}
84+
85+
// test no such file path
86+
d2 := &fd3{getErr: errors.New("no such file")}
87+
res2 := &DecommissionResult{}
88+
if err := removeSelectiveKeymasterContent(d2, res2, 42, nil, true); err != nil {
89+
t.Fatalf("expected nil on no such file, got %v", err)
90+
}
91+
}
92+
93+
func TestGenerateSelectiveKeysContent_Basic(t *testing.T) {
94+
sk := &model.SystemKey{Serial: 5, PublicKey: "ssh-ed25519 AAA pub"}
95+
kr := &kr3{active: sk, by: map[int]*model.SystemKey{5: sk}}
96+
kl := &kl3{globals: []model.PublicKey{{ID: 10, Algorithm: "ssh-ed25519", KeyData: "G", Comment: "g"}}, acc: map[int][]model.PublicKey{7: {{ID: 11, Algorithm: "ssh-ed25519", KeyData: "A", Comment: "a"}}}}
97+
SetDefaultKeyReader(kr)
98+
SetDefaultKeyLister(kl)
99+
defer func() { SetDefaultKeyReader(nil); SetDefaultKeyLister(nil) }()
100+
101+
s, err := GenerateSelectiveKeysContent(7, 0, nil, false)
102+
if err != nil {
103+
t.Fatalf("generate selective failed: %v", err)
104+
}
105+
if !strings.Contains(s, "Keymaster Managed Keys") || !strings.Contains(s, "ssh-ed25519") {
106+
t.Fatalf("unexpected generated content: %q", s)
107+
}
108+
}

0 commit comments

Comments
 (0)