-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin_test.go
More file actions
229 lines (211 loc) · 5.96 KB
/
Copy pathplugin_test.go
File metadata and controls
229 lines (211 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"testing"
)
func TestComputeHash(t *testing.T) {
input := []byte("hello world")
expected := sha256.Sum256(input)
want := hex.EncodeToString(expected[:])
got := computeHash(input)
if got != want {
t.Errorf("computeHash(%q) = %q, want %q", input, got, want)
}
}
func TestComputeHash_Empty(t *testing.T) {
got := computeHash([]byte{})
want := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
if got != want {
t.Errorf("computeHash(empty) = %q, want %q", got, want)
}
}
func TestComputeHash_Deterministic(t *testing.T) {
input := []byte("deterministic test input")
h1 := computeHash(input)
h2 := computeHash(input)
if h1 != h2 {
t.Errorf("computeHash not deterministic: %q != %q", h1, h2)
}
}
func TestIsPluginApproved_Approved(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA", "pluginB"},
},
}
if !isPluginApproved(approvals, "/work/dir", "pluginA") {
t.Error("expected pluginA to be approved")
}
}
func TestIsPluginApproved_NotApproved(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA"},
},
}
if isPluginApproved(approvals, "/work/dir", "pluginX") {
t.Error("expected pluginX to not be approved")
}
}
func TestIsPluginApproved_DifferentDir(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir1": {"pluginA"},
},
}
if isPluginApproved(approvals, "/work/dir2", "pluginA") {
t.Error("expected pluginA to not be approved in different directory")
}
}
func TestAddPluginApproval_New(t *testing.T) {
approvals := &approvalRecord{
Directories: make(map[string][]string),
}
addPluginApproval(approvals, "/work/dir", "pluginA")
plugins := approvals.Directories["/work/dir"]
if len(plugins) != 1 || plugins[0] != "pluginA" {
t.Errorf("expected [pluginA], got %v", plugins)
}
}
func TestAddPluginApproval_Duplicate(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA"},
},
}
addPluginApproval(approvals, "/work/dir", "pluginA")
plugins := approvals.Directories["/work/dir"]
if len(plugins) != 1 {
t.Errorf("expected 1 plugin after duplicate add, got %d: %v", len(plugins), plugins)
}
}
func TestLoadSaveApprovalRecords(t *testing.T) {
tmpDir := t.TempDir()
record := &approvalRecord{
Directories: map[string][]string{
"/proj/a": {"plugin1", "plugin2"},
"/proj/b": {"plugin3"},
},
}
if err := saveApprovalRecords(tmpDir, record); err != nil {
t.Fatalf("saveApprovalRecords: %v", err)
}
if _, err := os.Stat(filepath.Join(tmpDir, "approved_plugins.json")); err != nil {
t.Fatalf("expected file to exist: %v", err)
}
loaded, err := loadApprovalRecords(tmpDir)
if err != nil {
t.Fatalf("loadApprovalRecords: %v", err)
}
for dir, plugins := range record.Directories {
got := loaded.Directories[dir]
if len(got) != len(plugins) {
t.Errorf("dir %q: expected %v, got %v", dir, plugins, got)
continue
}
for i := range plugins {
if got[i] != plugins[i] {
t.Errorf("dir %q[%d]: expected %q, got %q", dir, i, plugins[i], got[i])
}
}
}
}
func TestRemovePluginApproval_Exists(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA", "pluginB", "pluginC"},
},
}
if !removePluginApproval(approvals, "/work/dir", "pluginB") {
t.Error("expected removePluginApproval to return true")
}
plugins := approvals.Directories["/work/dir"]
if len(plugins) != 2 {
t.Errorf("expected 2 plugins, got %d: %v", len(plugins), plugins)
}
if isPluginApproved(approvals, "/work/dir", "pluginB") {
t.Error("pluginB should no longer be approved")
}
}
func TestRemovePluginApproval_NotExists(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA"},
},
}
if removePluginApproval(approvals, "/work/dir", "pluginX") {
t.Error("expected removePluginApproval to return false")
}
}
func TestRemovePluginApproval_LastPlugin(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA"},
},
}
if !removePluginApproval(approvals, "/work/dir", "pluginA") {
t.Error("expected removePluginApproval to return true")
}
if _, exists := approvals.Directories["/work/dir"]; exists {
t.Error("expected directory entry to be removed when last plugin is revoked")
}
}
func TestRemoveAllPluginApprovals(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA", "pluginB"},
},
}
count := removeAllPluginApprovals(approvals, "/work/dir")
if count != 2 {
t.Errorf("expected 2, got %d", count)
}
if _, exists := approvals.Directories["/work/dir"]; exists {
t.Error("expected directory entry to be removed")
}
}
func TestRemoveAllPluginApprovals_Empty(t *testing.T) {
approvals := &approvalRecord{
Directories: make(map[string][]string),
}
count := removeAllPluginApprovals(approvals, "/work/dir")
if count != 0 {
t.Errorf("expected 0, got %d", count)
}
}
func TestListApprovedPlugins(t *testing.T) {
approvals := &approvalRecord{
Directories: map[string][]string{
"/work/dir": {"pluginA", "pluginB"},
},
}
plugins := listApprovedPlugins(approvals, "/work/dir")
if len(plugins) != 2 {
t.Errorf("expected 2 plugins, got %d", len(plugins))
}
}
func TestListApprovedPlugins_Empty(t *testing.T) {
approvals := &approvalRecord{
Directories: make(map[string][]string),
}
plugins := listApprovedPlugins(approvals, "/work/dir")
if plugins != nil {
t.Errorf("expected nil, got %v", plugins)
}
}
func TestLoadApprovalRecords_NonExistent(t *testing.T) {
tmpDir := t.TempDir()
record, err := loadApprovalRecords(tmpDir)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if record == nil {
t.Fatal("expected non-nil record")
}
if len(record.Directories) != 0 {
t.Errorf("expected empty directories, got %v", record.Directories)
}
}