forked from opensciencegrid/stashcp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_test.go
304 lines (276 loc) · 10 KB
/
main_test.go
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package stashcp
import (
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
namespaces "github.com/htcondor/osdf-client/v6/namespaces"
)
// TestGetIps calls main.get_ips with a hostname, checking
// for a valid return value.
func TestGetIps(t *testing.T) {
t.Parallel()
ips := get_ips("wlcg-wpad.fnal.gov")
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
if parsedIP.To4() != nil {
// Make sure that the ip doesn't start with a "[", breaks downloads
if strings.HasPrefix(ip, "[") {
t.Fatal("IPv4 address has brackets, will break downloads")
}
} else if parsedIP.To16() != nil {
if !strings.HasPrefix(ip, "[") {
t.Fatal("IPv6 address doesn't have brackets, downloads will parse it as invalid ports")
}
}
}
}
// TestGetToken tests getToken
func TestGetToken(t *testing.T) {
// Need a namespace for token acquisition
namespace, err := namespaces.MatchNamespace("/user/foo")
assert.NoError(t, err)
url, err := url.Parse("osdf:///user/foo")
assert.NoError(t, err)
// ENVs to test: BEARER_TOKEN, BEARER_TOKEN_FILE, XDG_RUNTIME_DIR/bt_u<uid>, TOKEN, _CONDOR_CREDS/scitoken.use, .condor_creds/scitokens.use
os.Setenv("BEARER_TOKEN", "bearer_token_contents")
token, err := getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, "bearer_token_contents", token)
os.Unsetenv("BEARER_TOKEN")
// BEARER_TOKEN_FILE
tmpDir := t.TempDir()
token_contents := "bearer_token_file_contents"
tmpFile := []byte(token_contents)
bearer_token_file := filepath.Join(tmpDir, "bearer_token_file")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("BEARER_TOKEN_FILE", bearer_token_file)
token, err = getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("BEARER_TOKEN_FILE")
// XDG_RUNTIME_DIR/bt_u<uid>
token_contents = "bearer_token_file_contents xdg"
tmpFile = []byte(token_contents)
bearer_token_file = filepath.Join(tmpDir, "bt_u"+strconv.Itoa(os.Getuid()))
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("XDG_RUNTIME_DIR", tmpDir)
token, err = getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("XDG_RUNTIME_DIR")
// TOKEN
token_contents = "bearer_token_file_contents token"
tmpFile = []byte(token_contents)
bearer_token_file = filepath.Join(tmpDir, "token_file")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("TOKEN", bearer_token_file)
token, err = getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("TOKEN")
// _CONDOR_CREDS/scitokens.use
token_contents = "bearer_token_file_contents scitokens.use"
tmpFile = []byte(token_contents)
bearer_token_file = filepath.Join(tmpDir, "scitokens.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
token, err = getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// _CONDOR_CREDS/renamed.use
token_contents = "bearer_token_file_contents renamed.use"
tmpFile = []byte(token_contents)
tmpDir = t.TempDir()
bearer_token_file = filepath.Join(tmpDir, "renamed.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
renamedUrl, err := url.Parse("renamed+osdf:///user/ligo/frames")
assert.NoError(t, err)
renamedNamespace, err := namespaces.MatchNamespace("/user/ligo/frames")
assert.NoError(t, err)
token, err = getToken(renamedUrl, renamedNamespace, false, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// _CONDOR_CREDS/renamed_handle1.use via renamed_handle1+osdf:///user/ligo/frames
token_contents = "bearer_token_file_contents renamed_handle1.use"
tmpFile = []byte(token_contents)
tmpDir = t.TempDir()
bearer_token_file = filepath.Join(tmpDir, "renamed_handle1.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
// Use a valid URL, then replace the scheme
renamedUrl, err = url.Parse("renamed.handle1+osdf:///user/ligo/frames")
renamedUrl.Scheme = "renamed_handle1+osdf"
assert.NoError(t, err)
renamedNamespace, err = namespaces.MatchNamespace("/user/ligo/frames")
assert.NoError(t, err)
token, err = getToken(renamedUrl, renamedNamespace, false, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// _CONDOR_CREDS/renamed_handle2.use via renamed.handle2+osdf:///user/ligo/frames
token_contents = "bearer_token_file_contents renamed.handle2.use"
tmpFile = []byte(token_contents)
tmpDir = t.TempDir()
bearer_token_file = filepath.Join(tmpDir, "renamed_handle2.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
renamedUrl, err = url.Parse("renamed.handle2+osdf:///user/ligo/frames")
assert.NoError(t, err)
renamedNamespace, err = namespaces.MatchNamespace("/user/ligo/frames")
assert.NoError(t, err)
token, err = getToken(renamedUrl, renamedNamespace, false, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// _CONDOR_CREDS/renamed.handle3.use via renamed.handle3+osdf:///user/ligo/frames
token_contents = "bearer_token_file_contents renamed.handle3.use"
tmpFile = []byte(token_contents)
tmpDir = t.TempDir()
bearer_token_file = filepath.Join(tmpDir, "renamed.handle3.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
renamedUrl, err = url.Parse("renamed.handle3+osdf:///user/ligo/frames")
assert.NoError(t, err)
renamedNamespace, err = namespaces.MatchNamespace("/user/ligo/frames")
assert.NoError(t, err)
token, err = getToken(renamedUrl, renamedNamespace, false, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// _CONDOR_CREDS/renamed.use
token_contents = "bearer_token_file_contents renamed.use"
tmpFile = []byte(token_contents)
tmpDir = t.TempDir()
bearer_token_file = filepath.Join(tmpDir, "renamed.use")
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
os.Setenv("_CONDOR_CREDS", tmpDir)
renamedUrl, err = url.Parse("/user/ligo/frames")
assert.NoError(t, err)
renamedNamespace, err = namespaces.MatchNamespace("/user/ligo/frames")
assert.NoError(t, err)
token, err = getToken(renamedUrl, renamedNamespace, false, "renamed")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
os.Unsetenv("_CONDOR_CREDS")
// Current directory .condor_creds/scitokens.use
token_contents = "bearer_token_file_contents .condor_creds/scitokens.use"
tmpFile = []byte(token_contents)
bearer_token_file = filepath.Join(tmpDir, ".condor_creds", "scitokens.use")
err = os.Mkdir(filepath.Join(tmpDir, ".condor_creds"), 0755)
assert.NoError(t, err)
err = os.WriteFile(bearer_token_file, tmpFile, 0644)
assert.NoError(t, err)
currentDir, err := os.Getwd()
assert.NoError(t, err)
err = os.Chdir(tmpDir)
assert.NoError(t, err)
token, err = getToken(url, namespace, true, "")
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
err = os.Chdir(currentDir)
assert.NoError(t, err)
}
// TestGetTokenName tests getTokenName
func TestGetTokenName(t *testing.T) {
cases := []struct {
url string
name string
scheme string
}{
{"osdf://blah+asdf", "", "osdf"},
{"stash://blah+asdf", "", "stash"},
{"file://blah+asdf", "", "file"},
{"tokename+osdf://blah+asdf", "tokename", "osdf"},
{"tokename+stash://blah+asdf", "tokename", "stash"},
{"tokename+file://blah+asdf", "tokename", "file"},
{"tokename+tokename2+osdf://blah+asdf", "tokename+tokename2", "osdf"},
{"token+tokename2+stash://blah+asdf", "token+tokename2", "stash"},
{"token.use+stash://blah+asdf", "token.use", "stash"},
{"token.blah.asdf+stash://blah+asdf", "token.blah.asdf", "stash"},
}
for _, c := range cases {
url, err := url.Parse(c.url)
assert.NoError(t, err)
scheme, tokenName := getTokenName(url)
assert.Equal(t, c.name, tokenName)
assert.Equal(t, c.scheme, scheme)
}
}
func FuzzGetTokenName(f *testing.F) {
testcases := []string{"", "tokename", "tokename+tokename2"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
// Make sure it's a valid URL
urlString := orig + "+osdf://blah+asdf"
url, err := url.Parse(urlString)
// If it's not a valid URL, then it's not a valid token name
if err != nil || url.Scheme == "" {
return
}
assert.NoError(t, err)
_, tokenName := getTokenName(url)
assert.Equal(t, strings.ToLower(orig), tokenName, "URL: "+urlString+"URL String: "+url.String()+" Scheme: "+url.Scheme)
})
}
func TestCorrectURLWithUnderscore(t *testing.T) {
tests := []struct {
name string
url string
expectedURL string
expectedScheme string
}{
{
name: "LIGO URL with underscore",
url: "ligo_data://ligo.org/data/1",
expectedURL: "ligo.data://ligo.org/data/1",
expectedScheme: "ligo_data",
},
{
name: "URL without underscore",
url: "http://example.com",
expectedURL: "http://example.com",
expectedScheme: "http",
},
{
name: "URL with no scheme",
url: "example.com",
expectedURL: "example.com",
expectedScheme: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualURL, actualScheme := correctURLWithUnderscore(tt.url)
if actualURL != tt.expectedURL || actualScheme != tt.expectedScheme {
t.Errorf("correctURLWithUnderscore(%v) = %v, %v; want %v, %v", tt.url, actualURL, actualScheme, tt.expectedURL, tt.expectedScheme)
}
})
}
}
func TestParseNoJobAd(t *testing.T) {
// Job ad file does not exist
tempDir := t.TempDir()
path := filepath.Join(tempDir, ".job.ad")
os.Setenv("_CONDOR_JOB_AD", path)
payload := payloadStruct{}
parse_job_ad(payload)
}