-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
213 lines (202 loc) · 4.59 KB
/
validator_test.go
File metadata and controls
213 lines (202 loc) · 4.59 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestValidate_TargetExistenceAndType(t *testing.T) {
tempDir := t.TempDir()
fileTarget := filepath.Join(tempDir, "testfile.txt")
err := os.WriteFile(fileTarget, []byte("test"), 0644)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
dirTarget := filepath.Join(tempDir, "testdir")
err = os.Mkdir(dirTarget, 0755)
if err != nil {
t.Fatalf("failed to create test dir: %v", err)
}
missingTarget := filepath.Join(tempDir, "missing.txt")
tests := []struct {
name string
cmd *ParsedCommand
wantErr bool
errText string
}{
{
name: "Valid file",
cmd: &ParsedCommand{
Target: fileTarget,
},
wantErr: false,
},
{
name: "Valid directory",
cmd: &ParsedCommand{
Target: dirTarget,
},
wantErr: false,
},
{
name: "Missing target",
cmd: &ParsedCommand{
Target: missingTarget,
},
wantErr: true,
errText: "does not exist",
},
{
name: "Valid recursive on directory",
cmd: &ParsedCommand{
Target: dirTarget,
Recursively: true,
},
wantErr: false,
},
{
name: "Invalid recursive on file",
cmd: &ParsedCommand{
Target: fileTarget,
Recursively: true,
},
wantErr: true,
errText: "is not a directory",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Validate(tt.cmd)
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
if !strings.Contains(err.Error(), tt.errText) {
t.Errorf("Validate() error = %v, expected error to contain %q", err, tt.errText)
}
}
if !tt.wantErr && tt.cmd.FileInfo == nil {
t.Errorf("Validate() expected FileInfo to be populated, got nil")
}
})
}
}
func TestValidate_ActorExistence(t *testing.T) {
cleanup := setMocks()
defer cleanup()
tempDir := t.TempDir()
fileTarget := filepath.Join(tempDir, "testfile.txt")
os.WriteFile(fileTarget, []byte("test"), 0644)
tests := []struct {
name string
cmd *ParsedCommand
wantErr bool
errText string
}{
{
name: "Valid user implicit",
cmd: &ParsedCommand{
Target: fileTarget,
Actor: "alice",
},
wantErr: false,
},
{
name: "Valid user explicit",
cmd: &ParsedCommand{
Target: fileTarget,
ActorType: "user",
Actor: "bob",
},
wantErr: false,
},
{
name: "Valid group implicit",
cmd: &ParsedCommand{
Target: fileTarget,
Actor: "dev",
},
wantErr: false,
},
{
name: "Valid group explicit",
cmd: &ParsedCommand{
Target: fileTarget,
ActorType: "group",
Actor: "admins",
},
wantErr: false,
},
{
name: "Invalid user explicit",
cmd: &ParsedCommand{
Target: fileTarget,
ActorType: "user",
Actor: "dev", // dev is a group, not a user
},
wantErr: true,
errText: "user 'dev' not found on system",
},
{
name: "Invalid group explicit",
cmd: &ParsedCommand{
Target: fileTarget,
ActorType: "group",
Actor: "alice", // alice is a user, not a group
},
wantErr: true,
errText: "group 'alice' not found on system",
},
{
name: "Invalid actor implicit",
cmd: &ParsedCommand{
Target: fileTarget,
Actor: "nobody",
},
wantErr: true,
errText: "actor 'nobody' not found as a user or group on system",
},
{
name: "Reserved keyword implicit (everyone)",
cmd: &ParsedCommand{
Target: fileTarget,
Actor: "everyone",
},
wantErr: false,
},
{
name: "Reserved keyword explicit user (everyone)",
cmd: &ParsedCommand{
Target: fileTarget,
ActorType: "user",
Actor: "everyone", // System likely doesn't have local user 'everyone', should fail
},
wantErr: true,
errText: "user 'everyone' not found on system",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Validate(tt.cmd)
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err != nil {
if !strings.Contains(err.Error(), tt.errText) {
t.Errorf("Validate() error = %v, expected error to contain %q", err, tt.errText)
}
}
if !tt.wantErr {
if tt.cmd.Actor == "everyone" && tt.cmd.ActorType != "user" && tt.cmd.ActorType != "group" {
if tt.cmd.ActorType != "other" {
t.Errorf("Validate() expected ActorType 'other', got %q", tt.cmd.ActorType)
}
} else if tt.cmd.ActorType == "" {
t.Errorf("Validate() expected ActorType to be resolved, got empty string")
}
}
})
}
}