This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_test.go
113 lines (100 loc) · 3.38 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
package stashcp
import (
"net"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// 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) {
// 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()
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()
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()
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()
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()
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()
assert.NoError(t, err)
assert.Equal(t, token_contents, token)
err = os.Chdir(currentDir)
assert.NoError(t, err)
}